def load_outbound_sessions(self, sessions): """Loads all saved outbound Megolm sessions. Also loads the devices each are shared with. Args: sessions (dict): A map from room_id to a ``MegolmOutboundSession`` object, which will be populated. """ c = self.conn.cursor() rows = c.execute( 'SELECT * FROM megolm_outbound_sessions WHERE device_id=?', (self.device_id, )) for row in rows.fetchall(): device_ids = c.execute( 'SELECT user_device_id FROM megolm_outbound_devices WHERE device_id=? ' 'AND room_id=?', (self.device_id, row['room_id'])) devices = {device_id[0] for device_id in device_ids} max_age_s = row['max_age_s'] max_age = timedelta(seconds=max_age_s) session = MegolmOutboundSession.from_pickle( bytes(row['session']), devices, max_age, row['max_messages'], row['creation_time'], row['message_count'], self.pickle_key) sessions[row['room_id']] = session c.close()
def get_outbound_session(self, room_id, sessions=None): """Gets a saved outbound Megolm session. Also loads the devices it is shared with. Args: room_id (str): The room corresponding to the session. sessions (dict): Optional. A map from room_id to a :class:`.MegolmOutboundSession` object, to which the session will be added. Returns: :class:`.MegolmOutboundSession` object, or ``None`` if the session was not found. """ c = self.conn.cursor() c.execute( 'SELECT * FROM megolm_outbound_sessions WHERE device_id=? AND room_id=?', (self.device_id, room_id)) try: row = c.fetchone() session_data = bytes(row['session']) except TypeError: c.close() return None device_ids = c.execute( 'SELECT user_device_id FROM megolm_outbound_devices WHERE device_id=? ' 'AND room_id=?', (self.device_id, room_id)) devices = {device_id[0] for device_id in device_ids} c.close() max_age_s = row['max_age_s'] max_age = timedelta(seconds=max_age_s) session = MegolmOutboundSession.from_pickle( session_data, devices, max_age, row['max_messages'], row['creation_time'], row['message_count'], self.pickle_key) if sessions is not None: sessions[room_id] = session return session