Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
d26ac74
Start thinking about schemas
MadLittleMods Jul 31, 2024
e7e9cb2
Add changelog
MadLittleMods Jul 31, 2024
8392d6a
Use foreign keys
MadLittleMods Aug 1, 2024
ad1c887
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 5, 2024
2b5f07d
Start of updating `sliding_sync_joined_rooms`
MadLittleMods Aug 6, 2024
1a251d5
Fill in `sliding_sync_non_join_memberships` when current state changes
MadLittleMods Aug 6, 2024
f96d0c3
Special treatment for boolean columns
MadLittleMods Aug 6, 2024
2f3bd27
Test is running
MadLittleMods Aug 6, 2024
cb33580
Server left room test
MadLittleMods Aug 7, 2024
87d9561
Change to updating the latest membership in the room
MadLittleMods Aug 7, 2024
61cea4e
Closer to right
MadLittleMods Aug 7, 2024
68a3daf
Fix comparison and insert
MadLittleMods Aug 7, 2024
5b1053f
Better test assertions
MadLittleMods Aug 8, 2024
c590474
Test non-joins
MadLittleMods Aug 8, 2024
a1aaa47
Add more tests
MadLittleMods Aug 8, 2024
bf78692
Handle to_delete
MadLittleMods Aug 8, 2024
5cf3ad3
Handle server left room
MadLittleMods Aug 8, 2024
bc3796d
Fix some lints
MadLittleMods Aug 8, 2024
cc2d2b6
Fill in `stream_ordering`/`bump_stamp` when we add current state to t…
MadLittleMods Aug 8, 2024
ca90901
Fill in `stream_ordering`/`bump_stamp` for any event being persisted
MadLittleMods Aug 8, 2024
3367422
Need to fix upsert
MadLittleMods Aug 8, 2024
ed47a7e
Fix bumping when events are persisted out of order
MadLittleMods Aug 12, 2024
0af3b48
Refactor to `sliding_sync_membership_snapshots`
MadLittleMods Aug 12, 2024
552f8f4
Update descriptions
MadLittleMods Aug 12, 2024
f069659
Fix lints
MadLittleMods Aug 12, 2024
53232e6
Fill in for remote invites (out of band, outlier membership)
MadLittleMods Aug 12, 2024
ab074f5
Fix events from rooms we're not joined to affecting the joined room s…
MadLittleMods Aug 13, 2024
3e1f24e
User ID is not unique because user is joined to many rooms
MadLittleMods Aug 13, 2024
83a5858
Add tests for remote invites
MadLittleMods Aug 13, 2024
5589ae4
Add test for remote invite rejected/retracted
MadLittleMods Aug 13, 2024
3423eb7
Add test to make sure snapshot evolves with membership
MadLittleMods Aug 13, 2024
f600eac
Adjust test description
MadLittleMods Aug 13, 2024
517946d
Fix lints
MadLittleMods Aug 13, 2024
eb3a185
Fix federating backfill test
MadLittleMods Aug 13, 2024
a90f3d4
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 13, 2024
32ae162
Fix rejecting invite when no_longer_in_room (and other non-join trans…
MadLittleMods Aug 13, 2024
dc447a6
Clarify when/why we upsert
MadLittleMods Aug 13, 2024
96a4614
Update fixme comment
MadLittleMods Aug 13, 2024
3566abd
Fix boolean schema for Postgres
MadLittleMods Aug 13, 2024
5df94f4
Fix running into `StopIteration`
MadLittleMods Aug 13, 2024
8b0e169
More realistic remote room forgotten test
MadLittleMods Aug 13, 2024
f49003c
No invites needed
MadLittleMods Aug 13, 2024
c8508f1
Clean up tables when a room is purged/deleted
MadLittleMods Aug 14, 2024
9f551f0
Fix lints
MadLittleMods Aug 14, 2024
1c931cb
Add background update for `sliding_sync_joined_rooms`
MadLittleMods Aug 15, 2024
23e0d34
Add more tests
MadLittleMods Aug 15, 2024
d113e74
Fix lints
MadLittleMods Aug 15, 2024
4b42e44
Work on background update for `sliding_sync_membership_snapshots`
MadLittleMods Aug 15, 2024
cbeff57
Use helper
MadLittleMods Aug 16, 2024
6c2fc1d
Move background updates to `StateBackgroundUpdateStore`
MadLittleMods Aug 16, 2024
8461faf
Add historical case to background update
MadLittleMods Aug 16, 2024
fb5af8f
Add background update test for `sliding_sync_membership_snapshots`
MadLittleMods Aug 16, 2024
ef5f0fc
Add more tests
MadLittleMods Aug 16, 2024
419be7c
Finish off background update tests
MadLittleMods Aug 16, 2024
fa63c02
Fix lints
MadLittleMods Aug 16, 2024
2ec93e3
Move function next to other helpers
MadLittleMods Aug 16, 2024
c89d859
Fill in docstrings
MadLittleMods Aug 16, 2024
d2f5247
Update comment
MadLittleMods Aug 16, 2024
df0c57d
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 19, 2024
98fb56e
Prefer `_update_sliding_sync_tables_with_new_persisted_events_txn(...…
MadLittleMods Aug 20, 2024
8ee2e11
Add test to handle state reset in the meta data
MadLittleMods Aug 20, 2024
574a04a
Test state reset on membership
MadLittleMods Aug 20, 2024
6cc6bdb
Start of moving logic outside of the transaction (pre-process)
MadLittleMods Aug 20, 2024
95d39db
Closer types
MadLittleMods Aug 20, 2024
2964c56
Use dicts
MadLittleMods Aug 20, 2024
ac5b05c
Use `TypedDict`
MadLittleMods Aug 20, 2024
45c89ec
Move pre-processing completely outside transaction
MadLittleMods Aug 20, 2024
3eb77c3
Add sanity checks and fix wrong variable usage
MadLittleMods Aug 20, 2024
cc200ee
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 20, 2024
726a8e9
Attempt getting real events in backgroun update (needs work)
MadLittleMods Aug 21, 2024
357132d
Go back to simpler fetching senders
MadLittleMods Aug 21, 2024
0233e20
Use full event version after solving the circular import issues
MadLittleMods Aug 21, 2024
a5e06c6
Move back to the main store
MadLittleMods Aug 21, 2024
d3f90e4
Get full events for `_sliding_sync_joined_rooms_backfill`
MadLittleMods Aug 21, 2024
cda92af
No need to update `event_stream_ordering`/`bump_stamp` `ON CONFLICT`
MadLittleMods Aug 21, 2024
772c501
Use available `stream_id`
MadLittleMods Aug 21, 2024
f6d7ffd
Move `_calculate_sliding_sync_table_changes(...)` after we assign `st…
MadLittleMods Aug 21, 2024
e7a3328
Pre-populate `membership` and `membership_event_stream_ordering`
MadLittleMods Aug 21, 2024
5b1db39
Add `sender` column so we can tell leaves from kicks
MadLittleMods Aug 21, 2024
c612572
Move away from `stream_id`
MadLittleMods Aug 21, 2024
cda2311
Add `tombstone_successor_room_id` column
MadLittleMods Aug 21, 2024
513ec8e
Update tests
MadLittleMods Aug 21, 2024
8ddf5c7
Add tombstone to tests
MadLittleMods Aug 22, 2024
0271155
Better handle none case
MadLittleMods Aug 22, 2024
9724836
Log which room is strange
MadLittleMods Aug 22, 2024
0a938b1
Add missing boolean column to portdb script
MadLittleMods Aug 22, 2024
ee2ef0b
Add `forgotten` column
MadLittleMods Aug 22, 2024
b45b189
Fill out docstring
MadLittleMods Aug 22, 2024
31300f4
More docstring
MadLittleMods Aug 22, 2024
339500d
Fix sub-query selecting multiple rows
MadLittleMods Aug 22, 2024
9b8d201
Check `events_and_context` for state events
MadLittleMods Aug 22, 2024
fc73b6f
Rename `insert_key`/`insert_value`
MadLittleMods Aug 22, 2024
980ee9a
Prefer `simple_update_txn`
MadLittleMods Aug 22, 2024
6723824
Prefer `simple_delete_many_txn`
MadLittleMods Aug 22, 2024
d61aada
Simplify `_update_sliding_sync_tables_with_new_persisted_events_txn()`
MadLittleMods Aug 22, 2024
4d87fa6
"backfill" -> "bg_update"
MadLittleMods Aug 22, 2024
693c06b
Move away from backfill language
MadLittleMods Aug 22, 2024
bcba8cc
No need for transaction
MadLittleMods Aug 22, 2024
44432e2
Move tests to dedicated file
MadLittleMods Aug 22, 2024
6edc4c7
Allow for no `bump_stamp` (fix `portdb` CI job)
MadLittleMods Aug 22, 2024
0726a6d
Derive best effort `stream_ordering` outside of the transaction
MadLittleMods Aug 22, 2024
088a4c7
Use `simple_upsert_txn` to update `sliding_sync_joined_rooms`
MadLittleMods Aug 22, 2024
4b866c4
Simplify what we need to think about to grab the best effort value
MadLittleMods Aug 22, 2024
fdb8b59
Correct comment
MadLittleMods Aug 22, 2024
21cc97b
Use `simple_upsert_many_txn` for `sliding_sync_membership_snapshots`
MadLittleMods Aug 22, 2024
f8926d0
Fix partial-stated room re-syncing state but nothing has changed
MadLittleMods Aug 23, 2024
b6a7d2b
Use `simple_upsert_txn` for `sliding_sync_joined_rooms` in background…
MadLittleMods Aug 23, 2024
a57d47b
Use `simple_upsert_txn` for `sliding_sync_membership_snapshots` in ba…
MadLittleMods Aug 23, 2024
9795556
Update comment
MadLittleMods Aug 26, 2024
addb914
Split test cases
MadLittleMods Aug 26, 2024
8bddbe2
Clear out-of-date rows
MadLittleMods Aug 26, 2024
a94c1dd
Add more context for why
MadLittleMods Aug 26, 2024
6a44686
Why it matters
MadLittleMods Aug 26, 2024
eb3c84c
Kick-off background update for out-of-date snapshots
MadLittleMods Aug 26, 2024
53473a0
Adapt `sliding_sync_joined_rooms` background update to use `event_str…
MadLittleMods Aug 26, 2024
7fe5d31
Note down caveat about `forgotten`
MadLittleMods Aug 26, 2024
7a0c281
Add placeholder tests
MadLittleMods Aug 27, 2024
9764f62
Fix query in Postgres
MadLittleMods Aug 27, 2024
c51a309
Maybe: always start background update
MadLittleMods Aug 27, 2024
9a7d8c2
Start catch-up if nothing written yet
MadLittleMods Aug 27, 2024
4dc9e26
Add test for catch-up background update
MadLittleMods Aug 27, 2024
c8e17f7
Add test when no rooms
MadLittleMods Aug 27, 2024
e5e7269
Add more tests
MadLittleMods Aug 27, 2024
85a60c3
More tests
MadLittleMods Aug 28, 2024
56a4c0b
Round out tests
MadLittleMods Aug 28, 2024
9d08bc2
Remove debug logs
MadLittleMods Aug 28, 2024
a507f15
Use `stream_id` of some point before we fetch the current state
MadLittleMods Aug 28, 2024
94e1a54
`get_events(...)` will omit events from unknown room versions
MadLittleMods Aug 28, 2024
53b7309
Add `sliding_sync_joined_rooms_to_recalculate` table
MadLittleMods Aug 28, 2024
8468401
Adapt to using `sliding_sync_joined_rooms_to_recalculate` table
MadLittleMods Aug 28, 2024
da463fb
Add unique index right away for `sliding_sync_joined_rooms_to_recalcu…
MadLittleMods Aug 28, 2024
7c9c620
Remove all rooms pulled out from the queue
erikjohnston Aug 28, 2024
bb905cd
Only run the sliding sync background updates on the main database
erikjohnston Aug 28, 2024
6f9932d
Handle old rows with null event_stream_ordering column
erikjohnston Aug 28, 2024
ab414f2
Use event_auth table to get previous membership
erikjohnston Aug 28, 2024
90d0e03
Fix port script tests by handling empty DBs correctly
erikjohnston Aug 28, 2024
6a164eb
Clarify why we return early
MadLittleMods Aug 28, 2024
3f56efb
Note why it's there
MadLittleMods Aug 28, 2024
e12f95f
Update metric with how long it took with the index and upsert
MadLittleMods Aug 28, 2024
116b833
Ignore membership where we don't have a row in `rooms` table
MadLittleMods Aug 28, 2024
de30d19
Fix-up `rooms` table for old out-of-band memberships
MadLittleMods Aug 28, 2024
15ad7a7
Fill in placeholder issue
MadLittleMods Aug 28, 2024
2f6ee08
Fix join condition not working in Postgres
MadLittleMods Aug 28, 2024
6622a1c
Add `instance_name` to `sliding_sync_membership_snapshots`
MadLittleMods Aug 28, 2024
bcc3e50
Fully-insert `sliding_sync_joined_rooms` rows
MadLittleMods Aug 29, 2024
95d5471
Merge branch 'develop' into madlittlemods/sliding-sync-pre-populate-r…
MadLittleMods Aug 29, 2024
b63188c
Explain more in schema
MadLittleMods Aug 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/17512.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pre-populate room data used in experimental [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575) Sliding Sync `/sync` endpoint for quick filtering/sorting.
2 changes: 2 additions & 0 deletions synapse/_scripts/synapse_port_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
"remote_media_cache": ["authenticated"],
"room_stats_state": ["is_federatable"],
"rooms": ["is_public", "has_auth_chain_index"],
"sliding_sync_joined_rooms": ["is_encrypted"],
"sliding_sync_non_join_memberships": ["is_encrypted"],
"users": ["shadow_banned", "approved", "locked", "suspended"],
"un_partial_stated_event_stream": ["rejection_status_changed"],
"users_who_share_rooms": ["share_private"],
Expand Down
290 changes: 283 additions & 7 deletions synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@
Optional,
Set,
Tuple,
Union,
cast,
)

import attr
from prometheus_client import Counter

import synapse.metrics
from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
from synapse.api.constants import (
EventContentFields,
EventTypes,
Membership,
RelationTypes,
)
from synapse.api.errors import PartialStateConflictError
from synapse.api.room_versions import RoomVersions
from synapse.events import EventBase, relation_from_event
Expand All @@ -52,14 +58,21 @@
DatabasePool,
LoggingDatabaseConnection,
LoggingTransaction,
make_tuple_in_list_sql_clause,
)
from synapse.storage.databases.main.event_federation import EventFederationStore
from synapse.storage.databases.main.events_worker import EventCacheEntry
from synapse.storage.databases.main.search import SearchEntry
from synapse.storage.engines import PostgresEngine
from synapse.storage.util.id_generators import AbstractStreamIdGenerator
from synapse.storage.util.sequence import SequenceGenerator
from synapse.types import JsonDict, StateMap, StrCollection, get_domain_from_id
from synapse.types import (
JsonDict,
MutableStateMap,
StateMap,
StrCollection,
get_domain_from_id,
)
from synapse.util import json_encoder
from synapse.util.iterutils import batch_iter, sorted_topologically
from synapse.util.stringutils import non_null_str_or_none
Expand Down Expand Up @@ -1153,17 +1166,185 @@ def _update_current_state_txn(
to_delete = delta_state.to_delete
to_insert = delta_state.to_insert

logger.info(
"asdf _update_current_state_txn no_longer_in_room=%s to_insert=%s to_delete=%s",
delta_state.no_longer_in_room,
to_insert,
to_delete,
)

# Figure out the changes of membership to invalidate the
# `get_rooms_for_user` cache.
# We find out which membership events we may have deleted
# and which we have added, then we invalidate the caches for all
# those users.
members_changed = {
members_to_cache_bust = {
state_key
for ev_type, state_key in itertools.chain(to_delete, to_insert)
if ev_type == EventTypes.Member
}

# We now update `sliding_sync_non_join_memberships`.
#
# This would only happen if someone was state reset out of the room
if to_delete:
txn.execute_batch(
"DELETE FROM sliding_sync_non_join_memberships"
" WHERE room_id = ? AND user_id = ?",
(
(room_id, state_key)
for event_type, state_key in to_delete
if event_type == EventTypes.Member and self.is_mine_id(state_key)
),
)

# We handle `sliding_sync_non_join_memberships` before `current_state_events` so
# we can gather the current state before it might be deleted if we are
# `no_longer_in_room`.
#
# We do this regardless of whether the server is `no_longer_in_room` or not
# because we still want a row if a local user was just left/kicked or got banned
# from the room.
if to_insert:
membership_event_id_to_user_id_map: Dict[str, str] = {}
for state_key, event_id in to_insert.items():
if state_key[0] == EventTypes.Member and self.is_mine_id(state_key[1]):
membership_event_id_to_user_id_map[event_id] = state_key[1]

if len(membership_event_id_to_user_id_map) > 0:
# Map of values to insert/update in the `sliding_sync_non_join_memberships` table
sliding_sync_non_joined_rooms_insert_map: Dict[
str, Optional[Union[str, bool]]
] = {}

relevant_state_set = {
(EventTypes.Create, ""),
(EventTypes.RoomEncryption, ""),
(EventTypes.Name, ""),
}

# Fetch the current state event IDs from the database
(
event_type_and_state_key_in_list_clause,
event_type_and_state_key_args,
) = make_tuple_in_list_sql_clause(
self.database_engine,
("type", "state_key"),
relevant_state_set,
)
txn.execute(
f"""
SELECT c.event_id, c.type, c.state_key
FROM current_state_events AS c
WHERE
c.room_id = ?
AND {event_type_and_state_key_in_list_clause}
""",
[room_id] + event_type_and_state_key_args,
)
current_state_map: MutableStateMap[str] = {
(event_type, state_key): event_id
for event_id, event_type, state_key in txn
}
# Since we fetched the current state before we took `to_insert`/`to_delete`
# into account, we need to do a couple fixups.
#
# Update the current_state_map with what we have `to_delete`
for state_key in to_delete:
current_state_map.pop(state_key, None)
# Update the current_state_map with what we have `to_insert`
for state_key, event_id in to_insert.items():
if state_key in relevant_state_set:
current_state_map[state_key] = event_id

# Fetch the raw event JSON from the database
(
event_id_in_list_clause,
event_id_args,
) = make_in_list_sql_clause(
self.database_engine,
"event_id",
current_state_map.values(),
)
txn.execute(
f"""
SELECT event_id, type, state_key, json FROM event_json
INNER JOIN events USING (event_id)
WHERE {event_id_in_list_clause}
""",
event_id_args,
)

# Parse the raw event JSON
for row in txn:
event_id, event_type, state_key, json = row
event_json = db_to_json(json)

if event_type == EventTypes.Create:
room_type = event_json.get("content", {}).get(
EventContentFields.ROOM_TYPE
)
sliding_sync_non_joined_rooms_insert_map["room_type"] = (
room_type
)
elif event_type == EventTypes.RoomEncryption:
encryption_algorithm = event_json.get("content", {}).get(
EventContentFields.ENCRYPTION_ALGORITHM
)
is_encrypted = encryption_algorithm is not None
sliding_sync_non_joined_rooms_insert_map["is_encrypted"] = (
is_encrypted
)
elif event_type == EventTypes.Name:
room_name = event_json.get("content", {}).get(
EventContentFields.ROOM_NAME
)
sliding_sync_non_joined_rooms_insert_map["room_name"] = (
room_name
)
else:
raise AssertionError(
f"Unexpected event (we should not be fetching extra events): ({event_type}, {state_key})"
)

# Update the `sliding_sync_non_join_memberships` table
insert_keys = sliding_sync_non_joined_rooms_insert_map.keys()
insert_values = sliding_sync_non_joined_rooms_insert_map.values()
txn.execute_batch(
f"""
WITH data_table (room_id, user_id, membership_event_id, membership, event_stream_ordering, {", ".join(insert_keys)}) AS (
VALUES (
?, ?, ?,
(SELECT membership FROM room_memberships WHERE event_id = ?),
(SELECT stream_ordering FROM events WHERE event_id = ?),
{", ".join("?" for _ in insert_values)}
)
)
INSERT INTO sliding_sync_non_join_memberships
(room_id, user_id, membership_event_id, membership, event_stream_ordering, {", ".join(insert_keys)})
SELECT * FROM data_table
WHERE membership != ?
ON CONFLICT (room_id, user_id)
DO UPDATE SET
membership_event_id = EXCLUDED.membership_event_id,
membership = EXCLUDED.membership,
event_stream_ordering = EXCLUDED.event_stream_ordering,
{", ".join(f"{key} = EXCLUDED.{key}" for key in insert_keys)}
""",
[
[
room_id,
user_id,
membership_event_id,
membership_event_id,
membership_event_id,
]
+ list(insert_values)
+ [Membership.JOIN]
for membership_event_id, user_id in membership_event_id_to_user_id_map.items()
],
)

if delta_state.no_longer_in_room:
# Server is no longer in the room so we delete the room from
# current_state_events, being careful we've already updated the
Expand All @@ -1182,16 +1363,22 @@ def _update_current_state_txn(
"""
txn.execute(sql, (stream_id, self._instance_name, room_id))

# Grab the list of users before we clear out the current state
users_in_room = self.store.get_users_in_room_txn(txn, room_id)
# We also want to invalidate the membership caches for users
# that were in the room.
users_in_room = self.store.get_users_in_room_txn(txn, room_id)
members_changed.update(users_in_room)
members_to_cache_bust.update(users_in_room)

self.db_pool.simple_delete_txn(
txn,
table="current_state_events",
keyvalues={"room_id": room_id},
)
self.db_pool.simple_delete_txn(
txn,
table="sliding_sync_joined_rooms",
keyvalues={"room_id": room_id},
)
else:
# We're still in the room, so we update the current state as normal.

Expand Down Expand Up @@ -1260,6 +1447,93 @@ def _update_current_state_txn(
],
)

# Handle updating the `sliding_sync_joined_rooms` table
event_ids_to_fetch: List[str] = []
create_event_id = None
room_encryption_event_id = None
room_name_event_id = None
for state_key, event_id in to_insert.items():
if state_key[0] == EventTypes.Create and state_key[1] == "":
create_event_id = event_id
event_ids_to_fetch.append(event_id)
elif state_key[0] == EventTypes.RoomEncryption and state_key[1] == "":
room_encryption_event_id = event_id
event_ids_to_fetch.append(event_id)
elif state_key[0] == EventTypes.Name and state_key[1] == "":
room_name_event_id = event_id
event_ids_to_fetch.append(event_id)

# Map of values to insert/update in the `sliding_sync_joined_rooms` table
sliding_sync_joined_rooms_insert_map: Dict[
str, Optional[Union[str, bool]]
] = {}

# If something is being deleted from the state, we need to clear it out
for state_key in to_delete:
if state_key == (EventTypes.Create, ""):
sliding_sync_joined_rooms_insert_map["room_type"] = None
elif state_key == (EventTypes.RoomEncryption, ""):
sliding_sync_joined_rooms_insert_map["is_encrypted"] = False
elif state_key == (EventTypes.Name, ""):
sliding_sync_joined_rooms_insert_map["room_name"] = None

# Fetch the events from the database
event_json_rows = cast(
List[Tuple[str, str]],
self.db_pool.simple_select_many_txn(
txn,
table="event_json",
column="event_id",
iterable=event_ids_to_fetch,
retcols=["event_id", "json"],
keyvalues={},
),
)
# Parse the raw event JSON
for event_id, json in event_json_rows:
event_json = db_to_json(json)

if event_id == create_event_id:
room_type = event_json.get("content", {}).get(
EventContentFields.ROOM_TYPE
)
sliding_sync_joined_rooms_insert_map["room_type"] = room_type
elif event_id == room_encryption_event_id:
encryption_algorithm = event_json.get("content", {}).get(
EventContentFields.ENCRYPTION_ALGORITHM
)
is_encrypted = encryption_algorithm is not None
sliding_sync_joined_rooms_insert_map["is_encrypted"] = is_encrypted
elif event_id == room_name_event_id:
room_name = event_json.get("content", {}).get(
EventContentFields.ROOM_NAME
)
sliding_sync_joined_rooms_insert_map["room_name"] = room_name
else:
raise AssertionError(
f"Unexpected event_id (we should not be fetching extra events): {event_id}"
)

# Update the `sliding_sync_joined_rooms` table
insert_keys = sliding_sync_joined_rooms_insert_map.keys()
insert_values = sliding_sync_joined_rooms_insert_map.values()
if len(insert_keys) > 0:
# TODO: Should we add `event_stream_ordering`, `bump_stamp` on insert?
txn.execute(
f"""
INSERT INTO sliding_sync_joined_rooms
(room_id, {", ".join(insert_keys)})
VALUES (
?,
{", ".join("?" for _ in insert_values)}
)
ON CONFLICT (room_id)
DO UPDATE SET
{", ".join(f"{key} = EXCLUDED.{key}" for key in insert_keys)}
""",
[room_id] + list(insert_values),
)

# We now update `local_current_membership`. We do this regardless
# of whether we're still in the room or not to handle the case where
# e.g. we just got banned (where we need to record that fact here).
Expand Down Expand Up @@ -1303,12 +1577,14 @@ def _update_current_state_txn(
)

# Invalidate the various caches
self.store._invalidate_state_caches_and_stream(txn, room_id, members_changed)
self.store._invalidate_state_caches_and_stream(
txn, room_id, members_to_cache_bust
)

# Check if any of the remote membership changes requires us to
# unsubscribe from their device lists.
self.store.handle_potentially_left_users_txn(
txn, {m for m in members_changed if not self.hs.is_mine_id(m)}
txn, {m for m in members_to_cache_bust if not self.hs.is_mine_id(m)}
)

def _upsert_room_version_txn(self, txn: LoggingTransaction, room_id: str) -> None:
Expand Down
5 changes: 4 additions & 1 deletion synapse/storage/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#
#

SCHEMA_VERSION = 86 # remember to update the list below when updating
SCHEMA_VERSION = 87 # remember to update the list below when updating
"""Represents the expectations made by the codebase about the database schema

This should be incremented whenever the codebase changes its requirements on the
Expand Down Expand Up @@ -142,6 +142,9 @@

Changes in SCHEMA_VERSION = 86
- Add a column `authenticated` to the tables `local_media_repository` and `remote_media_cache`

Changes in SCHEMA_VERSION = 87
- TODO
"""


Expand Down
Loading