Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7aee344

Browse files
author
David Robertson
committed
Pull out _classify_rooms_by_membership_changes
1 parent 26b5d23 commit 7aee344

File tree

1 file changed

+72
-55
lines changed

1 file changed

+72
-55
lines changed

synapse/handlers/sync.py

Lines changed: 72 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
# Debug logger for https://github.com/matrix-org/synapse/issues/4422
6363
issue4422_logger = logging.getLogger("synapse.handler.sync.4422_debug")
6464

65-
6665
# Counts the number of times we returned a non-empty sync. `type` is one of
6766
# "initial_sync", "full_state_sync" or "incremental_sync", `lazy_loaded` is
6867
# "true" or "false" depending on if the request asked for lazy loaded members or
@@ -83,7 +82,6 @@
8382
# avoiding redundantly sending the same lazy-loaded members to the client
8483
LAZY_LOADED_MEMBERS_CACHE_MAX_SIZE = 100
8584

86-
8785
SyncRequestKey = Tuple[Any, ...]
8886

8987

@@ -1684,7 +1682,7 @@ async def _get_rooms_changed(
16841682
now_token = sync_result_builder.now_token
16851683
sync_config = sync_result_builder.sync_config
16861684

1687-
assert since_token
1685+
assert since_token is not None
16881686

16891687
# The spec
16901688
# https://spec.matrix.org/v1.1/client-server-api/#get_matrixclientv3sync
@@ -1703,6 +1701,77 @@ async def _get_rooms_changed(
17031701
user_id, since_token.room_key, now_token.room_key
17041702
)
17051703

1704+
room_changes = await self._classify_rooms_by_membership_changes(
1705+
sync_result_builder, membership_change_events, ignored_users
1706+
)
1707+
1708+
timeline_limit = sync_config.filter_collection.timeline_limit()
1709+
1710+
# Get all events since the `from_key` in rooms we're currently joined to.
1711+
# If there are too many, we get the most recent events only. This leaves
1712+
# a "gap" in the timeline, as described by the spec for /sync.
1713+
room_to_events = await self.store.get_room_events_stream_for_rooms(
1714+
room_ids=sync_result_builder.joined_room_ids,
1715+
from_key=since_token.room_key,
1716+
to_key=now_token.room_key,
1717+
limit=timeline_limit + 1,
1718+
)
1719+
1720+
# We loop through all room ids, even if there are no new events, in case
1721+
# there are non room events that we need to notify about.
1722+
for room_id in sync_result_builder.joined_room_ids:
1723+
room_entry = room_to_events.get(room_id, None)
1724+
1725+
newly_joined = room_id in room_changes.newly_joined_rooms
1726+
if room_entry:
1727+
events, start_key = room_entry
1728+
1729+
prev_batch_token = now_token.copy_and_replace("room_key", start_key)
1730+
1731+
entry = RoomSyncResultBuilder(
1732+
room_id=room_id,
1733+
rtype="joined",
1734+
events=events,
1735+
newly_joined=newly_joined,
1736+
full_state=False,
1737+
since_token=None if newly_joined else since_token,
1738+
upto_token=prev_batch_token,
1739+
)
1740+
else:
1741+
entry = RoomSyncResultBuilder(
1742+
room_id=room_id,
1743+
rtype="joined",
1744+
events=[],
1745+
newly_joined=newly_joined,
1746+
full_state=False,
1747+
since_token=since_token,
1748+
upto_token=since_token,
1749+
)
1750+
1751+
if newly_joined:
1752+
# debugging for https://github.com/matrix-org/synapse/issues/4422
1753+
issue4422_logger.debug(
1754+
"RoomSyncResultBuilder events for newly joined room %s: %r",
1755+
room_id,
1756+
entry.events,
1757+
)
1758+
room_changes.room_entries.append(entry)
1759+
1760+
return room_changes
1761+
1762+
async def _classify_rooms_by_membership_changes(
1763+
self,
1764+
sync_result_builder: "SyncResultBuilder",
1765+
membership_change_events: List[EventBase],
1766+
ignored_users: Collection[str],
1767+
) -> _RoomChanges:
1768+
since_token = sync_result_builder.since_token
1769+
# This assetion is also made in the caller, `_get_rooms_changed`. We repeat it
1770+
# here for mypy's benefit.
1771+
assert since_token is not None
1772+
1773+
user_id = sync_result_builder.sync_config.user.to_string()
1774+
17061775
mem_change_events_by_room_id: Dict[str, List[EventBase]] = {}
17071776
for event in membership_change_events:
17081777
mem_change_events_by_room_id.setdefault(event.room_id, []).append(event)
@@ -1859,58 +1928,6 @@ async def _get_rooms_changed(
18591928
)
18601929
)
18611930

1862-
timeline_limit = sync_config.filter_collection.timeline_limit()
1863-
1864-
# Get all events since the `from_key` in rooms we're currently joined to.
1865-
# If there are too many, we get the most recent events only. This leaves
1866-
# a "gap" in the timeline, as described by the spec for /sync.
1867-
room_to_events = await self.store.get_room_events_stream_for_rooms(
1868-
room_ids=sync_result_builder.joined_room_ids,
1869-
from_key=since_token.room_key,
1870-
to_key=now_token.room_key,
1871-
limit=timeline_limit + 1,
1872-
)
1873-
1874-
# We loop through all room ids, even if there are no new events, in case
1875-
# there are non room events that we need to notify about.
1876-
for room_id in sync_result_builder.joined_room_ids:
1877-
room_entry = room_to_events.get(room_id, None)
1878-
1879-
newly_joined = room_id in newly_joined_rooms
1880-
if room_entry:
1881-
events, start_key = room_entry
1882-
1883-
prev_batch_token = now_token.copy_and_replace("room_key", start_key)
1884-
1885-
entry = RoomSyncResultBuilder(
1886-
room_id=room_id,
1887-
rtype="joined",
1888-
events=events,
1889-
newly_joined=newly_joined,
1890-
full_state=False,
1891-
since_token=None if newly_joined else since_token,
1892-
upto_token=prev_batch_token,
1893-
)
1894-
else:
1895-
entry = RoomSyncResultBuilder(
1896-
room_id=room_id,
1897-
rtype="joined",
1898-
events=[],
1899-
newly_joined=newly_joined,
1900-
full_state=False,
1901-
since_token=since_token,
1902-
upto_token=since_token,
1903-
)
1904-
1905-
if newly_joined:
1906-
# debugging for https://github.com/matrix-org/synapse/issues/4422
1907-
issue4422_logger.debug(
1908-
"RoomSyncResultBuilder events for newly joined room %s: %r",
1909-
room_id,
1910-
entry.events,
1911-
)
1912-
room_entries.append(entry)
1913-
19141931
return _RoomChanges(
19151932
room_entries,
19161933
invited,

0 commit comments

Comments
 (0)