def get_legacy_user_info( presence_rows: Sequence[Mapping[str, Any]], mobile_user_ids: Set[int] ) -> Dict[str, Any]: # The format of data here is for legacy users of our API, # including old versions of the mobile app. info_rows = [] for row in presence_rows: client_name = row["client__name"] status = UserPresence.status_to_string(row["status"]) dt = row["timestamp"] timestamp = datetime_to_timestamp(dt) push_enabled = row["user_profile__enable_offline_push_notifications"] has_push_devices = row["user_profile_id"] in mobile_user_ids pushable = push_enabled and has_push_devices info = dict( client=client_name, status=status, timestamp=timestamp, pushable=pushable, ) info_rows.append(info) most_recent_info = info_rows[-1] result = {} # The word "aggregated" here is possibly misleading. # It's really just the most recent client's info. result["aggregated"] = dict( client=most_recent_info["client"], status=most_recent_info["status"], timestamp=most_recent_info["timestamp"], ) # Build a dictionary of client -> info. There should # only be one row per client, but to be on the safe side, # we always overwrite with rows that are later in our list. for info in info_rows: result[info["client"]] = info return result