-
Notifications
You must be signed in to change notification settings - Fork 564
Add a column participant to room_memberships table
#18068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
afeed5b
754c27b
830e5e6
bc05f58
e76f94e
62e6a04
93e7a0c
115ff00
b0f804e
4e8dbd6
bc8f329
1539eb6
8b95806
3b9745d
72f283f
c8baecd
3db14a7
db6cbaa
48aa8d9
dd0a538
f934de4
096de2f
c7dc36c
af81dd6
5e105f4
109a326
d85541b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add a column `participant` to `room_memberships` table. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,7 @@ | |
|
|
||
| _MEMBERSHIP_PROFILE_UPDATE_NAME = "room_membership_profile_update" | ||
| _CURRENT_STATE_MEMBERSHIP_UPDATE_NAME = "current_state_events_membership" | ||
| _POPULATE_PARTICIPANT_BG_UPDATE_BATCH_SIZE = 1000 | ||
|
|
||
|
|
||
| @attr.s(frozen=True, slots=True, auto_attribs=True) | ||
|
|
@@ -1606,6 +1607,66 @@ def _get_rooms_for_user_by_join_date_txn( | |
| from_ts, | ||
| ) | ||
|
|
||
| async def set_room_participation(self, user_id: str, room_id: str) -> None: | ||
| """ | ||
| Record the provided user as participating in the given room | ||
|
|
||
| Args: | ||
| user_id: the user ID of the user | ||
| room_id: ID of the room to set the participant in | ||
| """ | ||
|
|
||
| def _set_room_participation_txn( | ||
| txn: LoggingTransaction, user_id: str, room_id: str | ||
| ) -> None: | ||
| sql = """ | ||
| UPDATE room_memberships | ||
| SET participant = true | ||
| WHERE (user_id, room_id) IN ( | ||
| SELECT user_id, room_id | ||
| FROM room_memberships | ||
| WHERE user_id = ? | ||
| AND room_id = ? | ||
| ORDER BY event_stream_ordering DESC | ||
| LIMIT 1 | ||
| ) | ||
| """ | ||
| txn.execute(sql, (user_id, room_id)) | ||
|
|
||
| await self.db_pool.runInteraction( | ||
| "_set_room_participation_txn", _set_room_participation_txn, user_id, room_id | ||
| ) | ||
|
|
||
| async def get_room_participation(self, user_id: str, room_id: str) -> bool: | ||
| """ | ||
| Check whether a user is listed as a participant in a room | ||
|
|
||
| Args: | ||
| user_id: user ID of the user | ||
| room_id: ID of the room to check in | ||
| """ | ||
|
|
||
| def _get_room_participation_txn( | ||
| txn: LoggingTransaction, user_id: str, room_id: str | ||
| ) -> bool: | ||
| sql = """ | ||
| SELECT participant | ||
| FROM room_memberships | ||
| WHERE user_id = ? | ||
| AND room_id = ? | ||
| ORDER BY event_stream_ordering DESC | ||
| LIMIT 1 | ||
| """ | ||
| txn.execute(sql, (user_id, room_id)) | ||
| res = txn.fetchone() | ||
| if res: | ||
| return res[0] | ||
| return False | ||
|
|
||
| return await self.db_pool.runInteraction( | ||
| "_get_room_participation_txn", _get_room_participation_txn, user_id, room_id | ||
| ) | ||
|
|
||
|
|
||
| class RoomMemberBackgroundUpdateStore(SQLBaseStore): | ||
| def __init__( | ||
|
|
@@ -1636,6 +1697,93 @@ def __init__( | |
| columns=["user_id", "room_id"], | ||
| ) | ||
|
|
||
| self.db_pool.updates.register_background_update_handler( | ||
| "populate_participant_bg_update", self._populate_participant | ||
| ) | ||
|
|
||
| async def _populate_participant(self, progress: JsonDict, batch_size: int) -> int: | ||
| """ | ||
| Background update to populate column `participant` on `room_memberships` table | ||
|
|
||
| A 'participant' is someone who is currently joined to a room and has sent at least | ||
| one `m.room.message` or `m.room.encrypted` event. | ||
|
|
||
| This background update will set the `participant` column across all rows in | ||
| `room_memberships` based on the user's *current* join status, and if | ||
| they've *ever* sent a message or encrypted event. Therefore one should | ||
| never assume the `participant` column's value is based solely on whether | ||
| the user participated in a previous "session" (where a "session" is defined | ||
| as a period between the user joining and leaving). See | ||
| https://github.com/element-hq/synapse/pull/18068#discussion_r1931070291 | ||
| for further detail. | ||
| """ | ||
| stream_token = progress.get("last_stream_token", None) | ||
|
|
||
| def _get_max_stream_token_txn(txn: LoggingTransaction) -> int: | ||
| sql = """ | ||
| SELECT event_stream_ordering from room_memberships | ||
| ORDER BY event_stream_ordering DESC | ||
| LIMIT 1; | ||
| """ | ||
| txn.execute(sql) | ||
| res = txn.fetchone() | ||
| if not res or not res[0]: | ||
| return 0 | ||
| return res[0] | ||
|
|
||
| def _background_populate_participant_txn( | ||
| txn: LoggingTransaction, stream_token: str | ||
| ) -> None: | ||
| sql = """ | ||
| UPDATE room_memberships | ||
| SET participant = True | ||
| FROM ( | ||
| SELECT DISTINCT c.state_key, e.room_id | ||
| FROM current_state_events AS c | ||
| INNER JOIN events AS e ON c.room_id = e.room_id | ||
| WHERE c.membership = 'join' | ||
| AND c.state_key = e.sender | ||
| AND ( | ||
| e.type = 'm.room.message' | ||
| OR e.type = 'm.room.encrypted' | ||
| ) | ||
| ) AS subquery | ||
| WHERE room_memberships.user_id = subquery.state_key | ||
| AND room_memberships.room_id = subquery.room_id | ||
| AND room_memberships.event_stream_ordering <= ? | ||
| AND room_memberships.event_stream_ordering > ?; | ||
| """ | ||
|
H-Shay marked this conversation as resolved.
|
||
| batch = int(stream_token) - _POPULATE_PARTICIPANT_BG_UPDATE_BATCH_SIZE | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that we're not using the This background update currently does not autoscale down. I wonder if this might be part of the cause of pain for many SQLite users: #18325
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not. I missed that in the review. I'll write a quick PR to use that, good spot! |
||
| txn.execute(sql, (stream_token, batch)) | ||
|
|
||
| if stream_token is None: | ||
| stream_token = await self.db_pool.runInteraction( | ||
| "_get_max_stream_token", _get_max_stream_token_txn | ||
| ) | ||
|
|
||
| if stream_token < 0: | ||
| await self.db_pool.updates._end_background_update( | ||
| "populate_participant_bg_update" | ||
| ) | ||
| return _POPULATE_PARTICIPANT_BG_UPDATE_BATCH_SIZE | ||
|
|
||
| await self.db_pool.runInteraction( | ||
| "_background_populate_participant_txn", | ||
| _background_populate_participant_txn, | ||
| stream_token, | ||
| ) | ||
|
|
||
| progress["last_stream_token"] = ( | ||
| stream_token - _POPULATE_PARTICIPANT_BG_UPDATE_BATCH_SIZE | ||
| ) | ||
| await self.db_pool.runInteraction( | ||
| "populate_participant_bg_update", | ||
| self.db_pool.updates._background_update_progress_txn, | ||
| "populate_participant_bg_update", | ||
| progress, | ||
| ) | ||
| return _POPULATE_PARTICIPANT_BG_UPDATE_BATCH_SIZE | ||
|
|
||
| async def _background_add_membership_profile( | ||
| self, progress: JsonDict, batch_size: int | ||
| ) -> int: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| -- | ||
| -- This file is licensed under the Affero General Public License (AGPL) version 3. | ||
| -- | ||
| -- Copyright (C) 2025 New Vector, Ltd | ||
| -- | ||
| -- This program is free software: you can redistribute it and/or modify | ||
| -- it under the terms of the GNU Affero General Public License as | ||
| -- published by the Free Software Foundation, either version 3 of the | ||
| -- License, or (at your option) any later version. | ||
| -- | ||
| -- See the GNU Affero General Public License for more details: | ||
| -- <https://www.gnu.org/licenses/agpl-3.0.html>. | ||
|
|
||
| -- Add a column `participant` to `room_memberships` table to track whether a room member has sent | ||
| -- a `m.room.message` or `m.room.encrypted` event into a room they are a member of | ||
| ALTER TABLE room_memberships ADD COLUMN participant BOOLEAN DEFAULT FALSE; | ||
|
|
||
| -- Add a background update to populate `participant` column | ||
| INSERT INTO background_updates (ordering, update_name, progress_json) VALUES | ||
| (9001, 'populate_participant_bg_update', '{}'); |
Uh oh!
There was an error while loading. Please reload this page.