mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 10:56:06 -04:00
Add function to get all forgotten rooms for user
This will allow us to efficiently filter out rooms that have been forgotten in other queries without having to join against the `room_memberships` table.
This commit is contained in:
parent
18a466b84e
commit
32768e96d4
2 changed files with 68 additions and 0 deletions
|
@ -639,6 +639,39 @@ class RoomMemberWorkerStore(EventsWorkerStore):
|
|||
count = yield self.runInteraction("did_forget_membership", f)
|
||||
defer.returnValue(count == 0)
|
||||
|
||||
@cached()
|
||||
def get_forgotten_rooms_for_user(self, user_id):
|
||||
"""Gets all rooms the user has forgotten.
|
||||
|
||||
Args:
|
||||
user_id (str)
|
||||
|
||||
Returns:
|
||||
Deferred[set[str]]
|
||||
"""
|
||||
|
||||
def _get_forgotten_rooms_for_user_txn(txn):
|
||||
# This is a slightly convoluted query that first looks up all rooms
|
||||
# that the user has forgotten in the past, then rechecks that list
|
||||
# to see if any have subsequently been updated. This is done so that
|
||||
# we can use a partial index on `forgotten = 1` on the assumption
|
||||
# that few users will actually forget many rooms.
|
||||
sql = """
|
||||
SELECT room_id, (
|
||||
SELECT count(*) FROM room_memberships
|
||||
WHERE room_id = m.room_id AND user_id = m.user_id AND forgotten = 0
|
||||
) AS count
|
||||
FROM room_memberships AS m
|
||||
WHERE user_id = ? AND forgotten = 1
|
||||
GROUP BY room_id, user_id;
|
||||
"""
|
||||
txn.execute(sql, (user_id,))
|
||||
return set(row[0] for row in txn if row[1] == 0)
|
||||
|
||||
return self.runInteraction(
|
||||
"get_forgotten_rooms_for_user", _get_forgotten_rooms_for_user_txn
|
||||
)
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def get_rooms_user_has_been_in(self, user_id):
|
||||
"""Get all rooms that the user has ever been in.
|
||||
|
@ -670,6 +703,13 @@ class RoomMemberStore(RoomMemberWorkerStore):
|
|||
_CURRENT_STATE_MEMBERSHIP_UPDATE_NAME,
|
||||
self._background_current_state_membership,
|
||||
)
|
||||
self.register_background_index_update(
|
||||
"room_membership_forgotten_idx",
|
||||
index_name="room_memberships_user_room_forgotten",
|
||||
table="room_memberships",
|
||||
columns=["user_id", "room_id"],
|
||||
where_clause="forgotten = 1",
|
||||
)
|
||||
|
||||
def _store_room_members_txn(self, txn, events, backfilled):
|
||||
"""Store a room member in the database.
|
||||
|
@ -771,6 +811,9 @@ class RoomMemberStore(RoomMemberWorkerStore):
|
|||
txn.execute(sql, (user_id, room_id))
|
||||
|
||||
self._invalidate_cache_and_stream(txn, self.did_forget, (user_id, room_id))
|
||||
self._invalidate_cache_and_stream(
|
||||
txn, self.get_forgotten_rooms_for_user, (user_id,)
|
||||
)
|
||||
|
||||
return self.runInteraction("forget_membership", f)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue