def _get_rooms_for_local_user_where_membership_is_txn( self, txn, user_id, membership_list): # Paranoia check. if not self.hs.is_mine_id(user_id): raise Exception( "Cannot call 'get_rooms_for_local_user_where_membership_is' on non-local user %r" % (user_id, ), ) clause, args = make_in_list_sql_clause(self.database_engine, "c.membership", membership_list) sql = """ SELECT room_id, e.sender, c.membership, event_id, e.stream_ordering FROM local_current_membership AS c INNER JOIN events AS e USING (room_id, event_id) WHERE user_id = ? AND %s """ % (clause, ) txn.execute(sql, (user_id, *args)) results = [RoomsForUser(**r) for r in self.db.cursor_to_dict(txn)] return results
def _get_rooms_for_user_where_membership_is_txn( self, txn, user_id, membership_list ): do_invite = Membership.INVITE in membership_list membership_list = [m for m in membership_list if m != Membership.INVITE] results = [] if membership_list: if self._current_state_events_membership_up_to_date: clause, args = make_in_list_sql_clause( self.database_engine, "c.membership", membership_list ) sql = """ SELECT room_id, e.sender, c.membership, event_id, e.stream_ordering FROM current_state_events AS c INNER JOIN events AS e USING (room_id, event_id) WHERE c.type = 'm.room.member' AND state_key = ? AND %s """ % ( clause, ) else: clause, args = make_in_list_sql_clause( self.database_engine, "m.membership", membership_list ) sql = """ SELECT room_id, e.sender, m.membership, event_id, e.stream_ordering FROM current_state_events AS c INNER JOIN room_memberships AS m USING (room_id, event_id) INNER JOIN events AS e USING (room_id, event_id) WHERE c.type = 'm.room.member' AND state_key = ? AND %s """ % ( clause, ) txn.execute(sql, (user_id, *args)) results = [RoomsForUser(**r) for r in self.db.cursor_to_dict(txn)] if do_invite: sql = ( "SELECT i.room_id, inviter, i.event_id, e.stream_ordering" " FROM local_invites as i" " INNER JOIN events as e USING (event_id)" " WHERE invitee = ? AND locally_rejected is NULL" " AND replaced_by is NULL" ) txn.execute(sql, (user_id,)) results.extend( RoomsForUser( room_id=r["room_id"], sender=r["inviter"], event_id=r["event_id"], stream_ordering=r["stream_ordering"], membership=Membership.INVITE, ) for r in self.db.cursor_to_dict(txn) ) return results