You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2783 lines
115 KiB

  1. # Copyright 2015-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import itertools
  15. import logging
  16. from typing import (
  17. TYPE_CHECKING,
  18. AbstractSet,
  19. Any,
  20. Dict,
  21. FrozenSet,
  22. List,
  23. Mapping,
  24. Optional,
  25. Sequence,
  26. Set,
  27. Tuple,
  28. )
  29. import attr
  30. from prometheus_client import Counter
  31. from synapse.api.constants import (
  32. AccountDataTypes,
  33. EventContentFields,
  34. EventTypes,
  35. Membership,
  36. )
  37. from synapse.api.filtering import FilterCollection
  38. from synapse.api.presence import UserPresenceState
  39. from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
  40. from synapse.events import EventBase
  41. from synapse.handlers.relations import BundledAggregations
  42. from synapse.logging import issue9533_logger
  43. from synapse.logging.context import current_context
  44. from synapse.logging.opentracing import (
  45. SynapseTags,
  46. log_kv,
  47. set_tag,
  48. start_active_span,
  49. trace,
  50. )
  51. from synapse.storage.databases.main.event_push_actions import RoomNotifCounts
  52. from synapse.storage.databases.main.roommember import extract_heroes_from_room_summary
  53. from synapse.storage.roommember import MemberSummary
  54. from synapse.types import (
  55. DeviceListUpdates,
  56. JsonDict,
  57. JsonMapping,
  58. MultiWriterStreamToken,
  59. MutableStateMap,
  60. Requester,
  61. RoomStreamToken,
  62. StateMap,
  63. StrCollection,
  64. StreamKeyType,
  65. StreamToken,
  66. UserID,
  67. )
  68. from synapse.types.state import StateFilter
  69. from synapse.util.async_helpers import concurrently_execute
  70. from synapse.util.caches.expiringcache import ExpiringCache
  71. from synapse.util.caches.lrucache import LruCache
  72. from synapse.util.caches.response_cache import ResponseCache, ResponseCacheContext
  73. from synapse.util.metrics import Measure, measure_func
  74. from synapse.visibility import filter_events_for_client
  75. if TYPE_CHECKING:
  76. from synapse.server import HomeServer
  77. logger = logging.getLogger(__name__)
  78. # Counts the number of times we returned a non-empty sync. `type` is one of
  79. # "initial_sync", "full_state_sync" or "incremental_sync", `lazy_loaded` is
  80. # "true" or "false" depending on if the request asked for lazy loaded members or
  81. # not.
  82. non_empty_sync_counter = Counter(
  83. "synapse_handlers_sync_nonempty_total",
  84. "Count of non empty sync responses. type is initial_sync/full_state_sync"
  85. "/incremental_sync. lazy_loaded indicates if lazy loaded members were "
  86. "enabled for that request.",
  87. ["type", "lazy_loaded"],
  88. )
  89. # Store the cache that tracks which lazy-loaded members have been sent to a given
  90. # client for no more than 30 minutes.
  91. LAZY_LOADED_MEMBERS_CACHE_MAX_AGE = 30 * 60 * 1000
  92. # Remember the last 100 members we sent to a client for the purposes of
  93. # avoiding redundantly sending the same lazy-loaded members to the client
  94. LAZY_LOADED_MEMBERS_CACHE_MAX_SIZE = 100
  95. SyncRequestKey = Tuple[Any, ...]
  96. @attr.s(slots=True, frozen=True, auto_attribs=True)
  97. class SyncConfig:
  98. user: UserID
  99. filter_collection: FilterCollection
  100. is_guest: bool
  101. request_key: SyncRequestKey
  102. device_id: Optional[str]
  103. @attr.s(slots=True, frozen=True, auto_attribs=True)
  104. class TimelineBatch:
  105. prev_batch: StreamToken
  106. events: Sequence[EventBase]
  107. limited: bool
  108. # A mapping of event ID to the bundled aggregations for the above events.
  109. # This is only calculated if limited is true.
  110. bundled_aggregations: Optional[Dict[str, BundledAggregations]] = None
  111. def __bool__(self) -> bool:
  112. """Make the result appear empty if there are no updates. This is used
  113. to tell if room needs to be part of the sync result.
  114. """
  115. return bool(self.events)
  116. # We can't freeze this class, because we need to update it after it's instantiated to
  117. # update its unread count. This is because we calculate the unread count for a room only
  118. # if there are updates for it, which we check after the instance has been created.
  119. # This should not be a big deal because we update the notification counts afterwards as
  120. # well anyway.
  121. @attr.s(slots=True, auto_attribs=True)
  122. class JoinedSyncResult:
  123. room_id: str
  124. timeline: TimelineBatch
  125. state: StateMap[EventBase]
  126. ephemeral: List[JsonDict]
  127. account_data: List[JsonDict]
  128. unread_notifications: JsonDict
  129. unread_thread_notifications: JsonDict
  130. summary: Optional[JsonDict]
  131. unread_count: int
  132. def __bool__(self) -> bool:
  133. """Make the result appear empty if there are no updates. This is used
  134. to tell if room needs to be part of the sync result.
  135. """
  136. return bool(
  137. self.timeline
  138. or self.state
  139. or self.ephemeral
  140. or self.account_data
  141. # nb the notification count does not, er, count: if there's nothing
  142. # else in the result, we don't need to send it.
  143. )
  144. @attr.s(slots=True, frozen=True, auto_attribs=True)
  145. class ArchivedSyncResult:
  146. room_id: str
  147. timeline: TimelineBatch
  148. state: StateMap[EventBase]
  149. account_data: List[JsonDict]
  150. def __bool__(self) -> bool:
  151. """Make the result appear empty if there are no updates. This is used
  152. to tell if room needs to be part of the sync result.
  153. """
  154. return bool(self.timeline or self.state or self.account_data)
  155. @attr.s(slots=True, frozen=True, auto_attribs=True)
  156. class InvitedSyncResult:
  157. room_id: str
  158. invite: EventBase
  159. def __bool__(self) -> bool:
  160. """Invited rooms should always be reported to the client"""
  161. return True
  162. @attr.s(slots=True, frozen=True, auto_attribs=True)
  163. class KnockedSyncResult:
  164. room_id: str
  165. knock: EventBase
  166. def __bool__(self) -> bool:
  167. """Knocked rooms should always be reported to the client"""
  168. return True
  169. @attr.s(slots=True, auto_attribs=True)
  170. class _RoomChanges:
  171. """The set of room entries to include in the sync, plus the set of joined
  172. and left room IDs since last sync.
  173. """
  174. room_entries: List["RoomSyncResultBuilder"]
  175. invited: List[InvitedSyncResult]
  176. knocked: List[KnockedSyncResult]
  177. newly_joined_rooms: List[str]
  178. newly_left_rooms: List[str]
  179. @attr.s(slots=True, frozen=True, auto_attribs=True)
  180. class SyncResult:
  181. """
  182. Attributes:
  183. next_batch: Token for the next sync
  184. presence: List of presence events for the user.
  185. account_data: List of account_data events for the user.
  186. joined: JoinedSyncResult for each joined room.
  187. invited: InvitedSyncResult for each invited room.
  188. knocked: KnockedSyncResult for each knocked on room.
  189. archived: ArchivedSyncResult for each archived room.
  190. to_device: List of direct messages for the device.
  191. device_lists: List of user_ids whose devices have changed
  192. device_one_time_keys_count: Dict of algorithm to count for one time keys
  193. for this device
  194. device_unused_fallback_key_types: List of key types that have an unused fallback
  195. key
  196. """
  197. next_batch: StreamToken
  198. presence: List[UserPresenceState]
  199. account_data: List[JsonDict]
  200. joined: List[JoinedSyncResult]
  201. invited: List[InvitedSyncResult]
  202. knocked: List[KnockedSyncResult]
  203. archived: List[ArchivedSyncResult]
  204. to_device: List[JsonDict]
  205. device_lists: DeviceListUpdates
  206. device_one_time_keys_count: JsonMapping
  207. device_unused_fallback_key_types: List[str]
  208. def __bool__(self) -> bool:
  209. """Make the result appear empty if there are no updates. This is used
  210. to tell if the notifier needs to wait for more events when polling for
  211. events.
  212. """
  213. return bool(
  214. self.presence
  215. or self.joined
  216. or self.invited
  217. or self.knocked
  218. or self.archived
  219. or self.account_data
  220. or self.to_device
  221. or self.device_lists
  222. )
  223. class SyncHandler:
  224. def __init__(self, hs: "HomeServer"):
  225. self.hs_config = hs.config
  226. self.store = hs.get_datastores().main
  227. self.notifier = hs.get_notifier()
  228. self.presence_handler = hs.get_presence_handler()
  229. self._relations_handler = hs.get_relations_handler()
  230. self._push_rules_handler = hs.get_push_rules_handler()
  231. self.event_sources = hs.get_event_sources()
  232. self.clock = hs.get_clock()
  233. self.state = hs.get_state_handler()
  234. self.auth_blocking = hs.get_auth_blocking()
  235. self._storage_controllers = hs.get_storage_controllers()
  236. self._state_storage_controller = self._storage_controllers.state
  237. self._device_handler = hs.get_device_handler()
  238. self._task_scheduler = hs.get_task_scheduler()
  239. self.should_calculate_push_rules = hs.config.push.enable_push
  240. # TODO: flush cache entries on subsequent sync request.
  241. # Once we get the next /sync request (ie, one with the same access token
  242. # that sets 'since' to 'next_batch'), we know that device won't need a
  243. # cached result any more, and we could flush the entry from the cache to save
  244. # memory.
  245. self.response_cache: ResponseCache[SyncRequestKey] = ResponseCache(
  246. hs.get_clock(),
  247. "sync",
  248. timeout_ms=hs.config.caches.sync_response_cache_duration,
  249. )
  250. # ExpiringCache((User, Device)) -> LruCache(user_id => event_id)
  251. self.lazy_loaded_members_cache: ExpiringCache[
  252. Tuple[str, Optional[str]], LruCache[str, str]
  253. ] = ExpiringCache(
  254. "lazy_loaded_members_cache",
  255. self.clock,
  256. max_len=0,
  257. expiry_ms=LAZY_LOADED_MEMBERS_CACHE_MAX_AGE,
  258. )
  259. self.rooms_to_exclude_globally = hs.config.server.rooms_to_exclude_from_sync
  260. async def wait_for_sync_for_user(
  261. self,
  262. requester: Requester,
  263. sync_config: SyncConfig,
  264. since_token: Optional[StreamToken] = None,
  265. timeout: int = 0,
  266. full_state: bool = False,
  267. ) -> SyncResult:
  268. """Get the sync for a client if we have new data for it now. Otherwise
  269. wait for new data to arrive on the server. If the timeout expires, then
  270. return an empty sync result.
  271. """
  272. # If the user is not part of the mau group, then check that limits have
  273. # not been exceeded (if not part of the group by this point, almost certain
  274. # auth_blocking will occur)
  275. user_id = sync_config.user.to_string()
  276. await self.auth_blocking.check_auth_blocking(requester=requester)
  277. res = await self.response_cache.wrap(
  278. sync_config.request_key,
  279. self._wait_for_sync_for_user,
  280. sync_config,
  281. since_token,
  282. timeout,
  283. full_state,
  284. cache_context=True,
  285. )
  286. logger.debug("Returning sync response for %s", user_id)
  287. return res
  288. async def _wait_for_sync_for_user(
  289. self,
  290. sync_config: SyncConfig,
  291. since_token: Optional[StreamToken],
  292. timeout: int,
  293. full_state: bool,
  294. cache_context: ResponseCacheContext[SyncRequestKey],
  295. ) -> SyncResult:
  296. """The start of the machinery that produces a /sync response.
  297. See https://spec.matrix.org/v1.1/client-server-api/#syncing for full details.
  298. This method does high-level bookkeeping:
  299. - tracking the kind of sync in the logging context
  300. - deleting any to_device messages whose delivery has been acknowledged.
  301. - deciding if we should dispatch an instant or delayed response
  302. - marking the sync as being lazily loaded, if appropriate
  303. Computing the body of the response begins in the next method,
  304. `current_sync_for_user`.
  305. """
  306. if since_token is None:
  307. sync_type = "initial_sync"
  308. elif full_state:
  309. sync_type = "full_state_sync"
  310. else:
  311. sync_type = "incremental_sync"
  312. context = current_context()
  313. if context:
  314. context.tag = sync_type
  315. # if we have a since token, delete any to-device messages before that token
  316. # (since we now know that the device has received them)
  317. if since_token is not None:
  318. since_stream_id = since_token.to_device_key
  319. deleted = await self.store.delete_messages_for_device(
  320. sync_config.user.to_string(),
  321. sync_config.device_id,
  322. since_stream_id,
  323. )
  324. logger.debug(
  325. "Deleted %d to-device messages up to %d", deleted, since_stream_id
  326. )
  327. if timeout == 0 or since_token is None or full_state:
  328. # we are going to return immediately, so don't bother calling
  329. # notifier.wait_for_events.
  330. result: SyncResult = await self.current_sync_for_user(
  331. sync_config, since_token, full_state=full_state
  332. )
  333. else:
  334. # Otherwise, we wait for something to happen and report it to the user.
  335. async def current_sync_callback(
  336. before_token: StreamToken, after_token: StreamToken
  337. ) -> SyncResult:
  338. return await self.current_sync_for_user(sync_config, since_token)
  339. result = await self.notifier.wait_for_events(
  340. sync_config.user.to_string(),
  341. timeout,
  342. current_sync_callback,
  343. from_token=since_token,
  344. )
  345. # if nothing has happened in any of the users' rooms since /sync was called,
  346. # the resultant next_batch will be the same as since_token (since the result
  347. # is generated when wait_for_events is first called, and not regenerated
  348. # when wait_for_events times out).
  349. #
  350. # If that happens, we mustn't cache it, so that when the client comes back
  351. # with the same cache token, we don't immediately return the same empty
  352. # result, causing a tightloop. (https://github.com/matrix-org/synapse/issues/8518)
  353. if result.next_batch == since_token:
  354. cache_context.should_cache = False
  355. if result:
  356. if sync_config.filter_collection.lazy_load_members():
  357. lazy_loaded = "true"
  358. else:
  359. lazy_loaded = "false"
  360. non_empty_sync_counter.labels(sync_type, lazy_loaded).inc()
  361. return result
  362. async def current_sync_for_user(
  363. self,
  364. sync_config: SyncConfig,
  365. since_token: Optional[StreamToken] = None,
  366. full_state: bool = False,
  367. ) -> SyncResult:
  368. """Generates the response body of a sync result, represented as a SyncResult.
  369. This is a wrapper around `generate_sync_result` which starts an open tracing
  370. span to track the sync. See `generate_sync_result` for the next part of your
  371. indoctrination.
  372. """
  373. with start_active_span("sync.current_sync_for_user"):
  374. log_kv({"since_token": since_token})
  375. sync_result = await self.generate_sync_result(
  376. sync_config, since_token, full_state
  377. )
  378. set_tag(SynapseTags.SYNC_RESULT, bool(sync_result))
  379. return sync_result
  380. async def ephemeral_by_room(
  381. self,
  382. sync_result_builder: "SyncResultBuilder",
  383. now_token: StreamToken,
  384. since_token: Optional[StreamToken] = None,
  385. ) -> Tuple[StreamToken, Dict[str, List[JsonDict]]]:
  386. """Get the ephemeral events for each room the user is in
  387. Args:
  388. sync_result_builder
  389. now_token: Where the server is currently up to.
  390. since_token: Where the server was when the client
  391. last synced.
  392. Returns:
  393. A tuple of the now StreamToken, updated to reflect the which typing
  394. events are included, and a dict mapping from room_id to a list of
  395. typing events for that room.
  396. """
  397. sync_config = sync_result_builder.sync_config
  398. with Measure(self.clock, "ephemeral_by_room"):
  399. typing_key = since_token.typing_key if since_token else 0
  400. room_ids = sync_result_builder.joined_room_ids
  401. typing_source = self.event_sources.sources.typing
  402. typing, typing_key = await typing_source.get_new_events(
  403. user=sync_config.user,
  404. from_key=typing_key,
  405. limit=sync_config.filter_collection.ephemeral_limit(),
  406. room_ids=room_ids,
  407. is_guest=sync_config.is_guest,
  408. )
  409. now_token = now_token.copy_and_replace(StreamKeyType.TYPING, typing_key)
  410. ephemeral_by_room: JsonDict = {}
  411. for event in typing:
  412. # we want to exclude the room_id from the event, but modifying the
  413. # result returned by the event source is poor form (it might cache
  414. # the object)
  415. room_id = event["room_id"]
  416. event_copy = {k: v for (k, v) in event.items() if k != "room_id"}
  417. ephemeral_by_room.setdefault(room_id, []).append(event_copy)
  418. receipt_key = (
  419. since_token.receipt_key
  420. if since_token
  421. else MultiWriterStreamToken(stream=0)
  422. )
  423. receipt_source = self.event_sources.sources.receipt
  424. receipts, receipt_key = await receipt_source.get_new_events(
  425. user=sync_config.user,
  426. from_key=receipt_key,
  427. limit=sync_config.filter_collection.ephemeral_limit(),
  428. room_ids=room_ids,
  429. is_guest=sync_config.is_guest,
  430. )
  431. now_token = now_token.copy_and_replace(StreamKeyType.RECEIPT, receipt_key)
  432. for event in receipts:
  433. room_id = event["room_id"]
  434. # exclude room id, as above
  435. event_copy = {k: v for (k, v) in event.items() if k != "room_id"}
  436. ephemeral_by_room.setdefault(room_id, []).append(event_copy)
  437. return now_token, ephemeral_by_room
  438. async def _load_filtered_recents(
  439. self,
  440. room_id: str,
  441. sync_result_builder: "SyncResultBuilder",
  442. sync_config: SyncConfig,
  443. upto_token: StreamToken,
  444. since_token: Optional[StreamToken] = None,
  445. potential_recents: Optional[List[EventBase]] = None,
  446. newly_joined_room: bool = False,
  447. ) -> TimelineBatch:
  448. """Create a timeline batch for the room
  449. Args:
  450. room_id
  451. sync_result_builder
  452. sync_config
  453. upto_token: The token up to which we should fetch (more) events.
  454. If `potential_results` is non-empty then this is *start* of
  455. the the list.
  456. since_token
  457. potential_recents: If non-empty, the events between the since token
  458. and current token to send down to clients.
  459. newly_joined_room
  460. """
  461. with Measure(self.clock, "load_filtered_recents"):
  462. timeline_limit = sync_config.filter_collection.timeline_limit()
  463. block_all_timeline = (
  464. sync_config.filter_collection.blocks_all_room_timeline()
  465. )
  466. if (
  467. potential_recents is None
  468. or newly_joined_room
  469. or timeline_limit < len(potential_recents)
  470. ):
  471. limited = True
  472. else:
  473. limited = False
  474. # Check if there is a gap, if so we need to mark this as limited and
  475. # recalculate which events to send down.
  476. gap_token = await self.store.get_timeline_gaps(
  477. room_id,
  478. since_token.room_key if since_token else None,
  479. sync_result_builder.now_token.room_key,
  480. )
  481. if gap_token:
  482. # There's a gap, so we need to ignore the passed in
  483. # `potential_recents`, and reset `upto_token` to match.
  484. potential_recents = None
  485. upto_token = sync_result_builder.now_token
  486. limited = True
  487. log_kv({"limited": limited})
  488. if potential_recents:
  489. recents = await sync_config.filter_collection.filter_room_timeline(
  490. potential_recents
  491. )
  492. log_kv({"recents_after_sync_filtering": len(recents)})
  493. # We check if there are any state events, if there are then we pass
  494. # all current state events to the filter_events function. This is to
  495. # ensure that we always include current state in the timeline
  496. current_state_ids: FrozenSet[str] = frozenset()
  497. if any(e.is_state() for e in recents):
  498. # FIXME(faster_joins): We use the partial state here as
  499. # we don't want to block `/sync` on finishing a lazy join.
  500. # Which should be fine once
  501. # https://github.com/matrix-org/synapse/issues/12989 is resolved,
  502. # since we shouldn't reach here anymore?
  503. # Note that we use the current state as a whitelist for filtering
  504. # `recents`, so partial state is only a problem when a membership
  505. # event turns up in `recents` but has not made it into the current
  506. # state.
  507. current_state_ids_map = (
  508. await self.store.get_partial_current_state_ids(room_id)
  509. )
  510. current_state_ids = frozenset(current_state_ids_map.values())
  511. recents = await filter_events_for_client(
  512. self._storage_controllers,
  513. sync_config.user.to_string(),
  514. recents,
  515. always_include_ids=current_state_ids,
  516. )
  517. log_kv({"recents_after_visibility_filtering": len(recents)})
  518. else:
  519. recents = []
  520. if not limited or block_all_timeline:
  521. prev_batch_token = upto_token
  522. if recents:
  523. room_key = recents[0].internal_metadata.before
  524. prev_batch_token = upto_token.copy_and_replace(
  525. StreamKeyType.ROOM, room_key
  526. )
  527. return TimelineBatch(
  528. events=recents, prev_batch=prev_batch_token, limited=False
  529. )
  530. filtering_factor = 2
  531. load_limit = max(timeline_limit * filtering_factor, 10)
  532. max_repeat = 5 # Only try a few times per room, otherwise
  533. room_key = upto_token.room_key
  534. end_key = room_key
  535. since_key = None
  536. if since_token and gap_token:
  537. # If there is a gap then we need to only include events after
  538. # it.
  539. since_key = gap_token
  540. elif since_token and not newly_joined_room:
  541. since_key = since_token.room_key
  542. while limited and len(recents) < timeline_limit and max_repeat:
  543. # If we have a since_key then we are trying to get any events
  544. # that have happened since `since_key` up to `end_key`, so we
  545. # can just use `get_room_events_stream_for_room`.
  546. # Otherwise, we want to return the last N events in the room
  547. # in topological ordering.
  548. if since_key:
  549. events, end_key = await self.store.get_room_events_stream_for_room(
  550. room_id,
  551. limit=load_limit + 1,
  552. from_key=since_key,
  553. to_key=end_key,
  554. )
  555. else:
  556. events, end_key = await self.store.get_recent_events_for_room(
  557. room_id, limit=load_limit + 1, end_token=end_key
  558. )
  559. log_kv({"loaded_recents": len(events)})
  560. loaded_recents = (
  561. await sync_config.filter_collection.filter_room_timeline(events)
  562. )
  563. log_kv({"loaded_recents_after_sync_filtering": len(loaded_recents)})
  564. # We check if there are any state events, if there are then we pass
  565. # all current state events to the filter_events function. This is to
  566. # ensure that we always include current state in the timeline
  567. current_state_ids = frozenset()
  568. if any(e.is_state() for e in loaded_recents):
  569. # FIXME(faster_joins): We use the partial state here as
  570. # we don't want to block `/sync` on finishing a lazy join.
  571. # Which should be fine once
  572. # https://github.com/matrix-org/synapse/issues/12989 is resolved,
  573. # since we shouldn't reach here anymore?
  574. # Note that we use the current state as a whitelist for filtering
  575. # `loaded_recents`, so partial state is only a problem when a
  576. # membership event turns up in `loaded_recents` but has not made it
  577. # into the current state.
  578. current_state_ids_map = (
  579. await self.store.get_partial_current_state_ids(room_id)
  580. )
  581. current_state_ids = frozenset(current_state_ids_map.values())
  582. loaded_recents = await filter_events_for_client(
  583. self._storage_controllers,
  584. sync_config.user.to_string(),
  585. loaded_recents,
  586. always_include_ids=current_state_ids,
  587. )
  588. log_kv({"loaded_recents_after_client_filtering": len(loaded_recents)})
  589. loaded_recents.extend(recents)
  590. recents = loaded_recents
  591. if len(events) <= load_limit:
  592. limited = False
  593. break
  594. max_repeat -= 1
  595. if len(recents) > timeline_limit:
  596. limited = True
  597. recents = recents[-timeline_limit:]
  598. room_key = recents[0].internal_metadata.before
  599. prev_batch_token = upto_token.copy_and_replace(StreamKeyType.ROOM, room_key)
  600. # Don't bother to bundle aggregations if the timeline is unlimited,
  601. # as clients will have all the necessary information.
  602. bundled_aggregations = None
  603. if limited or newly_joined_room:
  604. bundled_aggregations = (
  605. await self._relations_handler.get_bundled_aggregations(
  606. recents, sync_config.user.to_string()
  607. )
  608. )
  609. return TimelineBatch(
  610. events=recents,
  611. prev_batch=prev_batch_token,
  612. # Also mark as limited if this is a new room or there has been a gap
  613. # (to force client to paginate the gap).
  614. limited=limited or newly_joined_room or gap_token is not None,
  615. bundled_aggregations=bundled_aggregations,
  616. )
  617. async def get_state_after_event(
  618. self,
  619. event_id: str,
  620. state_filter: Optional[StateFilter] = None,
  621. await_full_state: bool = True,
  622. ) -> StateMap[str]:
  623. """
  624. Get the room state after the given event
  625. Args:
  626. event_id: event of interest
  627. state_filter: The state filter used to fetch state from the database.
  628. await_full_state: if `True`, will block if we do not yet have complete state
  629. at the event and `state_filter` is not satisfied by partial state.
  630. Defaults to `True`.
  631. """
  632. state_ids = await self._state_storage_controller.get_state_ids_for_event(
  633. event_id,
  634. state_filter=state_filter or StateFilter.all(),
  635. await_full_state=await_full_state,
  636. )
  637. # using get_metadata_for_events here (instead of get_event) sidesteps an issue
  638. # with redactions: if `event_id` is a redaction event, and we don't have the
  639. # original (possibly because it got purged), get_event will refuse to return
  640. # the redaction event, which isn't terribly helpful here.
  641. #
  642. # (To be fair, in that case we could assume it's *not* a state event, and
  643. # therefore we don't need to worry about it. But still, it seems cleaner just
  644. # to pull the metadata.)
  645. m = (await self.store.get_metadata_for_events([event_id]))[event_id]
  646. if m.state_key is not None and m.rejection_reason is None:
  647. state_ids = dict(state_ids)
  648. state_ids[(m.event_type, m.state_key)] = event_id
  649. return state_ids
  650. async def get_state_at(
  651. self,
  652. room_id: str,
  653. stream_position: StreamToken,
  654. state_filter: Optional[StateFilter] = None,
  655. await_full_state: bool = True,
  656. ) -> StateMap[str]:
  657. """Get the room state at a particular stream position
  658. Args:
  659. room_id: room for which to get state
  660. stream_position: point at which to get state
  661. state_filter: The state filter used to fetch state from the database.
  662. await_full_state: if `True`, will block if we do not yet have complete state
  663. at the last event in the room before `stream_position` and
  664. `state_filter` is not satisfied by partial state. Defaults to `True`.
  665. """
  666. # FIXME: This gets the state at the latest event before the stream ordering,
  667. # which might not be the same as the "current state" of the room at the time
  668. # of the stream token if there were multiple forward extremities at the time.
  669. last_event_id = await self.store.get_last_event_in_room_before_stream_ordering(
  670. room_id,
  671. end_token=stream_position.room_key,
  672. )
  673. if last_event_id:
  674. state = await self.get_state_after_event(
  675. last_event_id,
  676. state_filter=state_filter or StateFilter.all(),
  677. await_full_state=await_full_state,
  678. )
  679. else:
  680. # no events in this room - so presumably no state
  681. state = {}
  682. # (erikj) This should be rarely hit, but we've had some reports that
  683. # we get more state down gappy syncs than we should, so let's add
  684. # some logging.
  685. logger.info(
  686. "Failed to find any events in room %s at %s",
  687. room_id,
  688. stream_position.room_key,
  689. )
  690. return state
  691. async def compute_summary(
  692. self,
  693. room_id: str,
  694. sync_config: SyncConfig,
  695. batch: TimelineBatch,
  696. state: MutableStateMap[EventBase],
  697. now_token: StreamToken,
  698. ) -> Optional[JsonDict]:
  699. """Works out a room summary block for this room, summarising the number
  700. of joined members in the room, and providing the 'hero' members if the
  701. room has no name so clients can consistently name rooms. Also adds
  702. state events to 'state' if needed to describe the heroes.
  703. Args
  704. room_id
  705. sync_config
  706. batch: The timeline batch for the room that will be sent to the user.
  707. state: State as returned by compute_state_delta
  708. now_token: Token of the end of the current batch.
  709. """
  710. # FIXME: we could/should get this from room_stats when matthew/stats lands
  711. # FIXME: this promulgates https://github.com/matrix-org/synapse/issues/3305
  712. last_events, _ = await self.store.get_recent_event_ids_for_room(
  713. room_id, end_token=now_token.room_key, limit=1
  714. )
  715. if not last_events:
  716. return None
  717. last_event = last_events[-1]
  718. state_ids = await self._state_storage_controller.get_state_ids_for_event(
  719. last_event.event_id,
  720. state_filter=StateFilter.from_types(
  721. [(EventTypes.Name, ""), (EventTypes.CanonicalAlias, "")]
  722. ),
  723. )
  724. # this is heavily cached, thus: fast.
  725. details = await self.store.get_room_summary(room_id)
  726. name_id = state_ids.get((EventTypes.Name, ""))
  727. canonical_alias_id = state_ids.get((EventTypes.CanonicalAlias, ""))
  728. summary: JsonDict = {}
  729. empty_ms = MemberSummary([], 0)
  730. # TODO: only send these when they change.
  731. summary["m.joined_member_count"] = details.get(Membership.JOIN, empty_ms).count
  732. summary["m.invited_member_count"] = details.get(
  733. Membership.INVITE, empty_ms
  734. ).count
  735. # if the room has a name or canonical_alias set, we can skip
  736. # calculating heroes. Empty strings are falsey, so we check
  737. # for the "name" value and default to an empty string.
  738. if name_id:
  739. name = await self.store.get_event(name_id, allow_none=True)
  740. if name and name.content.get("name"):
  741. return summary
  742. if canonical_alias_id:
  743. canonical_alias = await self.store.get_event(
  744. canonical_alias_id, allow_none=True
  745. )
  746. if canonical_alias and canonical_alias.content.get("alias"):
  747. return summary
  748. # FIXME: only build up a member_ids list for our heroes
  749. member_ids = {}
  750. for membership in (
  751. Membership.JOIN,
  752. Membership.INVITE,
  753. Membership.LEAVE,
  754. Membership.BAN,
  755. ):
  756. for user_id, event_id in details.get(membership, empty_ms).members:
  757. member_ids[user_id] = event_id
  758. me = sync_config.user.to_string()
  759. summary["m.heroes"] = extract_heroes_from_room_summary(details, me)
  760. if not sync_config.filter_collection.lazy_load_members():
  761. return summary
  762. # ensure we send membership events for heroes if needed
  763. cache_key = (sync_config.user.to_string(), sync_config.device_id)
  764. cache = self.get_lazy_loaded_members_cache(cache_key)
  765. # track which members the client should already know about via LL:
  766. # Ones which are already in state...
  767. existing_members = {
  768. user_id for (typ, user_id) in state.keys() if typ == EventTypes.Member
  769. }
  770. # ...or ones which are in the timeline...
  771. for ev in batch.events:
  772. if ev.type == EventTypes.Member:
  773. existing_members.add(ev.state_key)
  774. # ...and then ensure any missing ones get included in state.
  775. missing_hero_event_ids = [
  776. member_ids[hero_id]
  777. for hero_id in summary["m.heroes"]
  778. if (
  779. cache.get(hero_id) != member_ids[hero_id]
  780. and hero_id not in existing_members
  781. )
  782. ]
  783. missing_hero_state = await self.store.get_events(missing_hero_event_ids)
  784. for s in missing_hero_state.values():
  785. cache.set(s.state_key, s.event_id)
  786. state[(EventTypes.Member, s.state_key)] = s
  787. return summary
  788. def get_lazy_loaded_members_cache(
  789. self, cache_key: Tuple[str, Optional[str]]
  790. ) -> LruCache[str, str]:
  791. cache: Optional[LruCache[str, str]] = self.lazy_loaded_members_cache.get(
  792. cache_key
  793. )
  794. if cache is None:
  795. logger.debug("creating LruCache for %r", cache_key)
  796. cache = LruCache(LAZY_LOADED_MEMBERS_CACHE_MAX_SIZE)
  797. self.lazy_loaded_members_cache[cache_key] = cache
  798. else:
  799. logger.debug("found LruCache for %r", cache_key)
  800. return cache
  801. async def compute_state_delta(
  802. self,
  803. room_id: str,
  804. batch: TimelineBatch,
  805. sync_config: SyncConfig,
  806. since_token: Optional[StreamToken],
  807. now_token: StreamToken,
  808. full_state: bool,
  809. ) -> MutableStateMap[EventBase]:
  810. """Works out the difference in state between the end of the previous sync and
  811. the start of the timeline.
  812. Args:
  813. room_id:
  814. batch: The timeline batch for the room that will be sent to the user.
  815. sync_config:
  816. since_token: Token of the end of the previous batch. May be `None`.
  817. now_token: Token of the end of the current batch.
  818. full_state: Whether to force returning the full state.
  819. `lazy_load_members` still applies when `full_state` is `True`.
  820. Returns:
  821. The state to return in the sync response for the room.
  822. Clients will overlay this onto the state at the end of the previous sync to
  823. arrive at the state at the start of the timeline.
  824. Clients will then overlay state events in the timeline to arrive at the
  825. state at the end of the timeline, in preparation for the next sync.
  826. """
  827. # TODO(mjark) Check if the state events were received by the server
  828. # after the previous sync, since we need to include those state
  829. # updates even if they occurred logically before the previous event.
  830. # TODO(mjark) Check for new redactions in the state events.
  831. with Measure(self.clock, "compute_state_delta"):
  832. # The memberships needed for events in the timeline.
  833. # Only calculated when `lazy_load_members` is on.
  834. members_to_fetch: Optional[Set[str]] = None
  835. # A dictionary mapping user IDs to the first event in the timeline sent by
  836. # them. Only calculated when `lazy_load_members` is on.
  837. first_event_by_sender_map: Optional[Dict[str, EventBase]] = None
  838. # The contribution to the room state from state events in the timeline.
  839. # Only contains the last event for any given state key.
  840. timeline_state: StateMap[str]
  841. lazy_load_members = sync_config.filter_collection.lazy_load_members()
  842. include_redundant_members = (
  843. sync_config.filter_collection.include_redundant_members()
  844. )
  845. if lazy_load_members:
  846. # We only request state for the members needed to display the
  847. # timeline:
  848. timeline_state = {}
  849. # Membership events to fetch that can be found in the room state, or in
  850. # the case of partial state rooms, the auth events of timeline events.
  851. members_to_fetch = set()
  852. first_event_by_sender_map = {}
  853. for event in batch.events:
  854. # Build the map from user IDs to the first timeline event they sent.
  855. if event.sender not in first_event_by_sender_map:
  856. first_event_by_sender_map[event.sender] = event
  857. # We need the event's sender, unless their membership was in a
  858. # previous timeline event.
  859. if (EventTypes.Member, event.sender) not in timeline_state:
  860. members_to_fetch.add(event.sender)
  861. # FIXME: we also care about invite targets etc.
  862. if event.is_state():
  863. timeline_state[(event.type, event.state_key)] = event.event_id
  864. if full_state:
  865. # always make sure we LL ourselves so we know we're in the room
  866. # (if we are) to fix https://github.com/vector-im/riot-web/issues/7209
  867. # We only need apply this on full state syncs given we disabled
  868. # LL for incr syncs in https://github.com/matrix-org/synapse/pull/3840.
  869. # We don't insert ourselves into `members_to_fetch`, because in some
  870. # rare cases (an empty event batch with a now_token after the user's
  871. # leave in a partial state room which another local user has
  872. # joined), the room state will be missing our membership and there
  873. # is no guarantee that our membership will be in the auth events of
  874. # timeline events when the room is partial stated.
  875. state_filter = StateFilter.from_lazy_load_member_list(
  876. members_to_fetch.union((sync_config.user.to_string(),))
  877. )
  878. else:
  879. state_filter = StateFilter.from_lazy_load_member_list(
  880. members_to_fetch
  881. )
  882. # We are happy to use partial state to compute the `/sync` response.
  883. # Since partial state may not include the lazy-loaded memberships we
  884. # require, we fix up the state response afterwards with memberships from
  885. # auth events.
  886. await_full_state = False
  887. else:
  888. timeline_state = {
  889. (event.type, event.state_key): event.event_id
  890. for event in batch.events
  891. if event.is_state()
  892. }
  893. state_filter = StateFilter.all()
  894. await_full_state = True
  895. # Now calculate the state to return in the sync response for the room.
  896. # This is more or less the change in state between the end of the previous
  897. # sync's timeline and the start of the current sync's timeline.
  898. # See the docstring above for details.
  899. state_ids: StateMap[str]
  900. # We need to know whether the state we fetch may be partial, so check
  901. # whether the room is partial stated *before* fetching it.
  902. is_partial_state_room = await self.store.is_partial_state_room(room_id)
  903. if full_state:
  904. if batch:
  905. state_at_timeline_end = (
  906. await self._state_storage_controller.get_state_ids_for_event(
  907. batch.events[-1].event_id,
  908. state_filter=state_filter,
  909. await_full_state=await_full_state,
  910. )
  911. )
  912. state_at_timeline_start = (
  913. await self._state_storage_controller.get_state_ids_for_event(
  914. batch.events[0].event_id,
  915. state_filter=state_filter,
  916. await_full_state=await_full_state,
  917. )
  918. )
  919. else:
  920. state_at_timeline_end = await self.get_state_at(
  921. room_id,
  922. stream_position=now_token,
  923. state_filter=state_filter,
  924. await_full_state=await_full_state,
  925. )
  926. state_at_timeline_start = state_at_timeline_end
  927. state_ids = _calculate_state(
  928. timeline_contains=timeline_state,
  929. timeline_start=state_at_timeline_start,
  930. timeline_end=state_at_timeline_end,
  931. previous_timeline_end={},
  932. lazy_load_members=lazy_load_members,
  933. )
  934. elif batch.limited:
  935. if batch:
  936. state_at_timeline_start = (
  937. await self._state_storage_controller.get_state_ids_for_event(
  938. batch.events[0].event_id,
  939. state_filter=state_filter,
  940. await_full_state=await_full_state,
  941. )
  942. )
  943. else:
  944. # We can get here if the user has ignored the senders of all
  945. # the recent events.
  946. state_at_timeline_start = await self.get_state_at(
  947. room_id,
  948. stream_position=now_token,
  949. state_filter=state_filter,
  950. await_full_state=await_full_state,
  951. )
  952. # for now, we disable LL for gappy syncs - see
  953. # https://github.com/vector-im/riot-web/issues/7211#issuecomment-419976346
  954. # N.B. this slows down incr syncs as we are now processing way
  955. # more state in the server than if we were LLing.
  956. #
  957. # We still have to filter timeline_start to LL entries (above) in order
  958. # for _calculate_state's LL logic to work, as we have to include LL
  959. # members for timeline senders in case they weren't loaded in the initial
  960. # sync. We do this by (counterintuitively) by filtering timeline_start
  961. # members to just be ones which were timeline senders, which then ensures
  962. # all of the rest get included in the state block (if we need to know
  963. # about them).
  964. state_filter = StateFilter.all()
  965. # If this is an initial sync then full_state should be set, and
  966. # that case is handled above. We assert here to ensure that this
  967. # is indeed the case.
  968. assert since_token is not None
  969. state_at_previous_sync = await self.get_state_at(
  970. room_id,
  971. stream_position=since_token,
  972. state_filter=state_filter,
  973. await_full_state=await_full_state,
  974. )
  975. if batch:
  976. state_at_timeline_end = (
  977. await self._state_storage_controller.get_state_ids_for_event(
  978. batch.events[-1].event_id,
  979. state_filter=state_filter,
  980. await_full_state=await_full_state,
  981. )
  982. )
  983. else:
  984. # We can get here if the user has ignored the senders of all
  985. # the recent events.
  986. state_at_timeline_end = await self.get_state_at(
  987. room_id,
  988. stream_position=now_token,
  989. state_filter=state_filter,
  990. await_full_state=await_full_state,
  991. )
  992. state_ids = _calculate_state(
  993. timeline_contains=timeline_state,
  994. timeline_start=state_at_timeline_start,
  995. timeline_end=state_at_timeline_end,
  996. previous_timeline_end=state_at_previous_sync,
  997. # we have to include LL members in case LL initial sync missed them
  998. lazy_load_members=lazy_load_members,
  999. )
  1000. else:
  1001. state_ids = {}
  1002. if lazy_load_members:
  1003. if members_to_fetch and batch.events:
  1004. # We're returning an incremental sync, with no
  1005. # "gap" since the previous sync, so normally there would be
  1006. # no state to return.
  1007. # But we're lazy-loading, so the client might need some more
  1008. # member events to understand the events in this timeline.
  1009. # So we fish out all the member events corresponding to the
  1010. # timeline here, and then dedupe any redundant ones below.
  1011. state_ids = await self._state_storage_controller.get_state_ids_for_event(
  1012. batch.events[0].event_id,
  1013. # we only want members!
  1014. state_filter=StateFilter.from_types(
  1015. (EventTypes.Member, member)
  1016. for member in members_to_fetch
  1017. ),
  1018. await_full_state=False,
  1019. )
  1020. # If we only have partial state for the room, `state_ids` may be missing the
  1021. # memberships we wanted. We attempt to find some by digging through the auth
  1022. # events of timeline events.
  1023. if lazy_load_members and is_partial_state_room:
  1024. assert members_to_fetch is not None
  1025. assert first_event_by_sender_map is not None
  1026. additional_state_ids = (
  1027. await self._find_missing_partial_state_memberships(
  1028. room_id, members_to_fetch, first_event_by_sender_map, state_ids
  1029. )
  1030. )
  1031. state_ids = {**state_ids, **additional_state_ids}
  1032. # At this point, if `lazy_load_members` is enabled, `state_ids` includes
  1033. # the memberships of all event senders in the timeline. This is because we
  1034. # may not have sent the memberships in a previous sync.
  1035. # When `include_redundant_members` is on, we send all the lazy-loaded
  1036. # memberships of event senders. Otherwise we make an effort to limit the set
  1037. # of memberships we send to those that we have not already sent to this client.
  1038. if lazy_load_members and not include_redundant_members:
  1039. cache_key = (sync_config.user.to_string(), sync_config.device_id)
  1040. cache = self.get_lazy_loaded_members_cache(cache_key)
  1041. # if it's a new sync sequence, then assume the client has had
  1042. # amnesia and doesn't want any recent lazy-loaded members
  1043. # de-duplicated.
  1044. if since_token is None:
  1045. logger.debug("clearing LruCache for %r", cache_key)
  1046. cache.clear()
  1047. else:
  1048. # only send members which aren't in our LruCache (either
  1049. # because they're new to this client or have been pushed out
  1050. # of the cache)
  1051. logger.debug("filtering state from %r...", state_ids)
  1052. state_ids = {
  1053. t: event_id
  1054. for t, event_id in state_ids.items()
  1055. if cache.get(t[1]) != event_id
  1056. }
  1057. logger.debug("...to %r", state_ids)
  1058. # add any member IDs we are about to send into our LruCache
  1059. for t, event_id in itertools.chain(
  1060. state_ids.items(), timeline_state.items()
  1061. ):
  1062. if t[0] == EventTypes.Member:
  1063. cache.set(t[1], event_id)
  1064. state: Dict[str, EventBase] = {}
  1065. if state_ids:
  1066. state = await self.store.get_events(list(state_ids.values()))
  1067. return {
  1068. (e.type, e.state_key): e
  1069. for e in await sync_config.filter_collection.filter_room_state(
  1070. list(state.values())
  1071. )
  1072. if e.type != EventTypes.Aliases # until MSC2261 or alternative solution
  1073. }
  1074. async def _find_missing_partial_state_memberships(
  1075. self,
  1076. room_id: str,
  1077. members_to_fetch: StrCollection,
  1078. events_with_membership_auth: Mapping[str, EventBase],
  1079. found_state_ids: StateMap[str],
  1080. ) -> StateMap[str]:
  1081. """Finds missing memberships from a set of auth events and returns them as a
  1082. state map.
  1083. Args:
  1084. room_id: The partial state room to find the remaining memberships for.
  1085. members_to_fetch: The memberships to find.
  1086. events_with_membership_auth: A mapping from user IDs to events whose auth
  1087. events would contain their prior membership, if one exists.
  1088. Note that join events will not cite a prior membership if a user has
  1089. never been in a room before.
  1090. found_state_ids: A dict from (type, state_key) -> state_event_id, containing
  1091. memberships that have been previously found. Entries in
  1092. `members_to_fetch` that have a membership in `found_state_ids` are
  1093. ignored.
  1094. Returns:
  1095. A dict from ("m.room.member", state_key) -> state_event_id, containing the
  1096. memberships missing from `found_state_ids`.
  1097. When `events_with_membership_auth` contains a join event for a given user
  1098. which does not cite a prior membership, no membership is returned for that
  1099. user.
  1100. Raises:
  1101. KeyError: if `events_with_membership_auth` does not have an entry for a
  1102. missing membership. Memberships in `found_state_ids` do not need an
  1103. entry in `events_with_membership_auth`.
  1104. """
  1105. additional_state_ids: MutableStateMap[str] = {}
  1106. # Tracks the missing members for logging purposes.
  1107. missing_members = set()
  1108. # Identify memberships missing from `found_state_ids` and pick out the auth
  1109. # events in which to look for them.
  1110. auth_event_ids: Set[str] = set()
  1111. for member in members_to_fetch:
  1112. if (EventTypes.Member, member) in found_state_ids:
  1113. continue
  1114. event_with_membership_auth = events_with_membership_auth[member]
  1115. is_create = (
  1116. event_with_membership_auth.is_state()
  1117. and event_with_membership_auth.type == EventTypes.Create
  1118. )
  1119. is_join = (
  1120. event_with_membership_auth.is_state()
  1121. and event_with_membership_auth.type == EventTypes.Member
  1122. and event_with_membership_auth.state_key == member
  1123. and event_with_membership_auth.content.get("membership")
  1124. == Membership.JOIN
  1125. )
  1126. if not is_create and not is_join:
  1127. # The event must include the desired membership as an auth event, unless
  1128. # it's the `m.room.create` event for a room or the first join event for
  1129. # a given user.
  1130. missing_members.add(member)
  1131. auth_event_ids.update(event_with_membership_auth.auth_event_ids())
  1132. auth_events = await self.store.get_events(auth_event_ids)
  1133. # Run through the missing memberships once more, picking out the memberships
  1134. # from the pile of auth events we have just fetched.
  1135. for member in members_to_fetch:
  1136. if (EventTypes.Member, member) in found_state_ids:
  1137. continue
  1138. event_with_membership_auth = events_with_membership_auth[member]
  1139. # Dig through the auth events to find the desired membership.
  1140. for auth_event_id in event_with_membership_auth.auth_event_ids():
  1141. # We only store events once we have all their auth events,
  1142. # so the auth event must be in the pile we have just
  1143. # fetched.
  1144. auth_event = auth_events[auth_event_id]
  1145. if (
  1146. auth_event.type == EventTypes.Member
  1147. and auth_event.state_key == member
  1148. ):
  1149. missing_members.discard(member)
  1150. additional_state_ids[
  1151. (EventTypes.Member, member)
  1152. ] = auth_event.event_id
  1153. break
  1154. if missing_members:
  1155. # There really shouldn't be any missing memberships now. Either:
  1156. # * we couldn't find an auth event, which shouldn't happen because we do
  1157. # not persist events with persisting their auth events first, or
  1158. # * the set of auth events did not contain a membership we wanted, which
  1159. # means our caller didn't compute the events in `members_to_fetch`
  1160. # correctly, or we somehow accepted an event whose auth events were
  1161. # dodgy.
  1162. logger.error(
  1163. "Failed to find memberships for %s in partial state room "
  1164. "%s in the auth events of %s.",
  1165. missing_members,
  1166. room_id,
  1167. [
  1168. events_with_membership_auth[member].event_id
  1169. for member in missing_members
  1170. ],
  1171. )
  1172. return additional_state_ids
  1173. async def unread_notifs_for_room_id(
  1174. self, room_id: str, sync_config: SyncConfig
  1175. ) -> RoomNotifCounts:
  1176. if not self.should_calculate_push_rules:
  1177. # If push rules have been universally disabled then we know we won't
  1178. # have any unread counts in the DB, so we may as well skip asking
  1179. # the DB.
  1180. return RoomNotifCounts.empty()
  1181. with Measure(self.clock, "unread_notifs_for_room_id"):
  1182. return await self.store.get_unread_event_push_actions_by_room_for_user(
  1183. room_id,
  1184. sync_config.user.to_string(),
  1185. )
  1186. async def generate_sync_result(
  1187. self,
  1188. sync_config: SyncConfig,
  1189. since_token: Optional[StreamToken] = None,
  1190. full_state: bool = False,
  1191. ) -> SyncResult:
  1192. """Generates the response body of a sync result.
  1193. This is represented by a `SyncResult` struct, which is built from small pieces
  1194. using a `SyncResultBuilder`. See also
  1195. https://spec.matrix.org/v1.1/client-server-api/#get_matrixclientv3sync
  1196. the `sync_result_builder` is passed as a mutable ("inout") parameter to various
  1197. helper functions. These retrieve and process the data which forms the sync body,
  1198. often writing to the `sync_result_builder` to store their output.
  1199. At the end, we transfer data from the `sync_result_builder` to a new `SyncResult`
  1200. instance to signify that the sync calculation is complete.
  1201. """
  1202. user_id = sync_config.user.to_string()
  1203. app_service = self.store.get_app_service_by_user_id(user_id)
  1204. if app_service:
  1205. # We no longer support AS users using /sync directly.
  1206. # See https://github.com/matrix-org/matrix-doc/issues/1144
  1207. raise NotImplementedError()
  1208. # Note: we get the users room list *before* we get the current token, this
  1209. # avoids checking back in history if rooms are joined after the token is fetched.
  1210. token_before_rooms = self.event_sources.get_current_token()
  1211. mutable_joined_room_ids = set(await self.store.get_rooms_for_user(user_id))
  1212. # NB: The now_token gets changed by some of the generate_sync_* methods,
  1213. # this is due to some of the underlying streams not supporting the ability
  1214. # to query up to a given point.
  1215. # Always use the `now_token` in `SyncResultBuilder`
  1216. now_token = self.event_sources.get_current_token()
  1217. log_kv({"now_token": now_token})
  1218. # Since we fetched the users room list before the token, there's a small window
  1219. # during which membership events may have been persisted, so we fetch these now
  1220. # and modify the joined room list for any changes between the get_rooms_for_user
  1221. # call and the get_current_token call.
  1222. membership_change_events = []
  1223. if since_token:
  1224. membership_change_events = await self.store.get_membership_changes_for_user(
  1225. user_id,
  1226. since_token.room_key,
  1227. now_token.room_key,
  1228. self.rooms_to_exclude_globally,
  1229. )
  1230. mem_last_change_by_room_id: Dict[str, EventBase] = {}
  1231. for event in membership_change_events:
  1232. mem_last_change_by_room_id[event.room_id] = event
  1233. # For the latest membership event in each room found, add/remove the room ID
  1234. # from the joined room list accordingly. In this case we only care if the
  1235. # latest change is JOIN.
  1236. for room_id, event in mem_last_change_by_room_id.items():
  1237. assert event.internal_metadata.stream_ordering
  1238. if (
  1239. event.internal_metadata.stream_ordering
  1240. < token_before_rooms.room_key.stream
  1241. ):
  1242. continue
  1243. logger.info(
  1244. "User membership change between getting rooms and current token: %s %s %s",
  1245. user_id,
  1246. event.membership,
  1247. room_id,
  1248. )
  1249. # User joined a room - we have to then check the room state to ensure we
  1250. # respect any bans if there's a race between the join and ban events.
  1251. if event.membership == Membership.JOIN:
  1252. user_ids_in_room = await self.store.get_users_in_room(room_id)
  1253. if user_id in user_ids_in_room:
  1254. mutable_joined_room_ids.add(room_id)
  1255. # The user left the room, or left and was re-invited but not joined yet
  1256. else:
  1257. mutable_joined_room_ids.discard(room_id)
  1258. # Tweak the set of rooms to return to the client for eager (non-lazy) syncs.
  1259. mutable_rooms_to_exclude = set(self.rooms_to_exclude_globally)
  1260. if not sync_config.filter_collection.lazy_load_members():
  1261. # Non-lazy syncs should never include partially stated rooms.
  1262. # Exclude all partially stated rooms from this sync.
  1263. results = await self.store.is_partial_state_room_batched(
  1264. mutable_joined_room_ids
  1265. )
  1266. mutable_rooms_to_exclude.update(
  1267. room_id
  1268. for room_id, is_partial_state in results.items()
  1269. if is_partial_state
  1270. )
  1271. membership_change_events = [
  1272. event
  1273. for event in membership_change_events
  1274. if not results.get(event.room_id, False)
  1275. ]
  1276. # Incremental eager syncs should additionally include rooms that
  1277. # - we are joined to
  1278. # - are full-stated
  1279. # - became fully-stated at some point during the sync period
  1280. # (These rooms will have been omitted during a previous eager sync.)
  1281. forced_newly_joined_room_ids: Set[str] = set()
  1282. if since_token and not sync_config.filter_collection.lazy_load_members():
  1283. un_partial_stated_rooms = (
  1284. await self.store.get_un_partial_stated_rooms_between(
  1285. since_token.un_partial_stated_rooms_key,
  1286. now_token.un_partial_stated_rooms_key,
  1287. mutable_joined_room_ids,
  1288. )
  1289. )
  1290. results = await self.store.is_partial_state_room_batched(
  1291. un_partial_stated_rooms
  1292. )
  1293. forced_newly_joined_room_ids.update(
  1294. room_id
  1295. for room_id, is_partial_state in results.items()
  1296. if not is_partial_state
  1297. )
  1298. # Now we have our list of joined room IDs, exclude as configured and freeze
  1299. joined_room_ids = frozenset(
  1300. room_id
  1301. for room_id in mutable_joined_room_ids
  1302. if room_id not in mutable_rooms_to_exclude
  1303. )
  1304. logger.debug(
  1305. "Calculating sync response for %r between %s and %s",
  1306. sync_config.user,
  1307. since_token,
  1308. now_token,
  1309. )
  1310. sync_result_builder = SyncResultBuilder(
  1311. sync_config,
  1312. full_state,
  1313. since_token=since_token,
  1314. now_token=now_token,
  1315. joined_room_ids=joined_room_ids,
  1316. excluded_room_ids=frozenset(mutable_rooms_to_exclude),
  1317. forced_newly_joined_room_ids=frozenset(forced_newly_joined_room_ids),
  1318. membership_change_events=membership_change_events,
  1319. )
  1320. logger.debug("Fetching account data")
  1321. # Global account data is included if it is not filtered out.
  1322. if not sync_config.filter_collection.blocks_all_global_account_data():
  1323. await self._generate_sync_entry_for_account_data(sync_result_builder)
  1324. # Presence data is included if the server has it enabled and not filtered out.
  1325. include_presence_data = bool(
  1326. self.hs_config.server.presence_enabled
  1327. and not sync_config.filter_collection.blocks_all_presence()
  1328. )
  1329. # Device list updates are sent if a since token is provided.
  1330. include_device_list_updates = bool(since_token and since_token.device_list_key)
  1331. # If we do not care about the rooms or things which depend on the room
  1332. # data (namely presence and device list updates), then we can skip
  1333. # this process completely.
  1334. device_lists = DeviceListUpdates()
  1335. if (
  1336. not sync_result_builder.sync_config.filter_collection.blocks_all_rooms()
  1337. or include_presence_data
  1338. or include_device_list_updates
  1339. ):
  1340. logger.debug("Fetching room data")
  1341. # Note that _generate_sync_entry_for_rooms sets sync_result_builder.joined, which
  1342. # is used in calculate_user_changes below.
  1343. (
  1344. newly_joined_rooms,
  1345. newly_left_rooms,
  1346. ) = await self._generate_sync_entry_for_rooms(sync_result_builder)
  1347. # Work out which users have joined or left rooms we're in. We use this
  1348. # to build the presence and device_list parts of the sync response in
  1349. # `_generate_sync_entry_for_presence` and
  1350. # `_generate_sync_entry_for_device_list` respectively.
  1351. if include_presence_data or include_device_list_updates:
  1352. # This uses the sync_result_builder.joined which is set in
  1353. # `_generate_sync_entry_for_rooms`, if that didn't find any joined
  1354. # rooms for some reason it is a no-op.
  1355. (
  1356. newly_joined_or_invited_or_knocked_users,
  1357. newly_left_users,
  1358. ) = sync_result_builder.calculate_user_changes()
  1359. if include_presence_data:
  1360. logger.debug("Fetching presence data")
  1361. await self._generate_sync_entry_for_presence(
  1362. sync_result_builder,
  1363. newly_joined_rooms,
  1364. newly_joined_or_invited_or_knocked_users,
  1365. )
  1366. if include_device_list_updates:
  1367. device_lists = await self._generate_sync_entry_for_device_list(
  1368. sync_result_builder,
  1369. newly_joined_rooms=newly_joined_rooms,
  1370. newly_joined_or_invited_or_knocked_users=newly_joined_or_invited_or_knocked_users,
  1371. newly_left_rooms=newly_left_rooms,
  1372. newly_left_users=newly_left_users,
  1373. )
  1374. logger.debug("Fetching to-device data")
  1375. await self._generate_sync_entry_for_to_device(sync_result_builder)
  1376. logger.debug("Fetching OTK data")
  1377. device_id = sync_config.device_id
  1378. one_time_keys_count: JsonMapping = {}
  1379. unused_fallback_key_types: List[str] = []
  1380. if device_id:
  1381. # TODO: We should have a way to let clients differentiate between the states of:
  1382. # * no change in OTK count since the provided since token
  1383. # * the server has zero OTKs left for this device
  1384. # Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
  1385. one_time_keys_count = await self.store.count_e2e_one_time_keys(
  1386. user_id, device_id
  1387. )
  1388. unused_fallback_key_types = list(
  1389. await self.store.get_e2e_unused_fallback_key_types(user_id, device_id)
  1390. )
  1391. num_events = 0
  1392. # debug for https://github.com/matrix-org/synapse/issues/9424
  1393. for joined_room in sync_result_builder.joined:
  1394. num_events += len(joined_room.timeline.events)
  1395. log_kv(
  1396. {
  1397. "joined_rooms_in_result": len(sync_result_builder.joined),
  1398. "events_in_result": num_events,
  1399. }
  1400. )
  1401. logger.debug("Sync response calculation complete")
  1402. return SyncResult(
  1403. presence=sync_result_builder.presence,
  1404. account_data=sync_result_builder.account_data,
  1405. joined=sync_result_builder.joined,
  1406. invited=sync_result_builder.invited,
  1407. knocked=sync_result_builder.knocked,
  1408. archived=sync_result_builder.archived,
  1409. to_device=sync_result_builder.to_device,
  1410. device_lists=device_lists,
  1411. device_one_time_keys_count=one_time_keys_count,
  1412. device_unused_fallback_key_types=unused_fallback_key_types,
  1413. next_batch=sync_result_builder.now_token,
  1414. )
  1415. @measure_func("_generate_sync_entry_for_device_list")
  1416. async def _generate_sync_entry_for_device_list(
  1417. self,
  1418. sync_result_builder: "SyncResultBuilder",
  1419. newly_joined_rooms: AbstractSet[str],
  1420. newly_joined_or_invited_or_knocked_users: AbstractSet[str],
  1421. newly_left_rooms: AbstractSet[str],
  1422. newly_left_users: AbstractSet[str],
  1423. ) -> DeviceListUpdates:
  1424. """Generate the DeviceListUpdates section of sync
  1425. Args:
  1426. sync_result_builder
  1427. newly_joined_rooms: Set of rooms user has joined since previous sync
  1428. newly_joined_or_invited_or_knocked_users: Set of users that have joined,
  1429. been invited to a room or are knocking on a room since
  1430. previous sync.
  1431. newly_left_rooms: Set of rooms user has left since previous sync
  1432. newly_left_users: Set of users that have left a room we're in since
  1433. previous sync
  1434. """
  1435. user_id = sync_result_builder.sync_config.user.to_string()
  1436. since_token = sync_result_builder.since_token
  1437. assert since_token is not None
  1438. # Take a copy since these fields will be mutated later.
  1439. newly_joined_or_invited_or_knocked_users = set(
  1440. newly_joined_or_invited_or_knocked_users
  1441. )
  1442. newly_left_users = set(newly_left_users)
  1443. # We want to figure out what user IDs the client should refetch
  1444. # device keys for, and which users we aren't going to track changes
  1445. # for anymore.
  1446. #
  1447. # For the first step we check:
  1448. # a. if any users we share a room with have updated their devices,
  1449. # and
  1450. # b. we also check if we've joined any new rooms, or if a user has
  1451. # joined a room we're in.
  1452. #
  1453. # For the second step we just find any users we no longer share a
  1454. # room with by looking at all users that have left a room plus users
  1455. # that were in a room we've left.
  1456. users_that_have_changed = set()
  1457. joined_rooms = sync_result_builder.joined_room_ids
  1458. # Step 1a, check for changes in devices of users we share a room
  1459. # with
  1460. #
  1461. # We do this in two different ways depending on what we have cached.
  1462. # If we already have a list of all the user that have changed since
  1463. # the last sync then it's likely more efficient to compare the rooms
  1464. # they're in with the rooms the syncing user is in.
  1465. #
  1466. # If we don't have that info cached then we get all the users that
  1467. # share a room with our user and check if those users have changed.
  1468. cache_result = self.store.get_cached_device_list_changes(
  1469. since_token.device_list_key
  1470. )
  1471. if cache_result.hit:
  1472. changed_users = cache_result.entities
  1473. result = await self.store.get_rooms_for_users(changed_users)
  1474. for changed_user_id, entries in result.items():
  1475. # Check if the changed user shares any rooms with the user,
  1476. # or if the changed user is the syncing user (as we always
  1477. # want to include device list updates of their own devices).
  1478. if user_id == changed_user_id or any(
  1479. rid in joined_rooms for rid in entries
  1480. ):
  1481. users_that_have_changed.add(changed_user_id)
  1482. else:
  1483. users_that_have_changed = (
  1484. await self._device_handler.get_device_changes_in_shared_rooms(
  1485. user_id,
  1486. sync_result_builder.joined_room_ids,
  1487. from_token=since_token,
  1488. )
  1489. )
  1490. # Step 1b, check for newly joined rooms
  1491. for room_id in newly_joined_rooms:
  1492. joined_users = await self.store.get_users_in_room(room_id)
  1493. newly_joined_or_invited_or_knocked_users.update(joined_users)
  1494. # TODO: Check that these users are actually new, i.e. either they
  1495. # weren't in the previous sync *or* they left and rejoined.
  1496. users_that_have_changed.update(newly_joined_or_invited_or_knocked_users)
  1497. user_signatures_changed = await self.store.get_users_whose_signatures_changed(
  1498. user_id, since_token.device_list_key
  1499. )
  1500. users_that_have_changed.update(user_signatures_changed)
  1501. # Now find users that we no longer track
  1502. for room_id in newly_left_rooms:
  1503. left_users = await self.store.get_users_in_room(room_id)
  1504. newly_left_users.update(left_users)
  1505. # Remove any users that we still share a room with.
  1506. left_users_rooms = await self.store.get_rooms_for_users(newly_left_users)
  1507. for user_id, entries in left_users_rooms.items():
  1508. if any(rid in joined_rooms for rid in entries):
  1509. newly_left_users.discard(user_id)
  1510. return DeviceListUpdates(changed=users_that_have_changed, left=newly_left_users)
  1511. @trace
  1512. async def _generate_sync_entry_for_to_device(
  1513. self, sync_result_builder: "SyncResultBuilder"
  1514. ) -> None:
  1515. """Generates the portion of the sync response. Populates
  1516. `sync_result_builder` with the result.
  1517. """
  1518. user_id = sync_result_builder.sync_config.user.to_string()
  1519. device_id = sync_result_builder.sync_config.device_id
  1520. now_token = sync_result_builder.now_token
  1521. since_stream_id = 0
  1522. if sync_result_builder.since_token is not None:
  1523. since_stream_id = int(sync_result_builder.since_token.to_device_key)
  1524. if device_id is not None and since_stream_id != int(now_token.to_device_key):
  1525. messages, stream_id = await self.store.get_messages_for_device(
  1526. user_id, device_id, since_stream_id, now_token.to_device_key
  1527. )
  1528. for message in messages:
  1529. log_kv(
  1530. {
  1531. "event": "to_device_message",
  1532. "sender": message["sender"],
  1533. "type": message["type"],
  1534. EventContentFields.TO_DEVICE_MSGID: message["content"].get(
  1535. EventContentFields.TO_DEVICE_MSGID
  1536. ),
  1537. }
  1538. )
  1539. if messages and issue9533_logger.isEnabledFor(logging.DEBUG):
  1540. issue9533_logger.debug(
  1541. "Returning to-device messages with stream_ids (%d, %d]; now: %d;"
  1542. " msgids: %s",
  1543. since_stream_id,
  1544. stream_id,
  1545. now_token.to_device_key,
  1546. [
  1547. message["content"].get(EventContentFields.TO_DEVICE_MSGID)
  1548. for message in messages
  1549. ],
  1550. )
  1551. sync_result_builder.now_token = now_token.copy_and_replace(
  1552. StreamKeyType.TO_DEVICE, stream_id
  1553. )
  1554. sync_result_builder.to_device = messages
  1555. else:
  1556. sync_result_builder.to_device = []
  1557. async def _generate_sync_entry_for_account_data(
  1558. self, sync_result_builder: "SyncResultBuilder"
  1559. ) -> None:
  1560. """Generates the global account data portion of the sync response.
  1561. Account data (called "Client Config" in the spec) can be set either globally
  1562. or for a specific room. Account data consists of a list of events which
  1563. accumulate state, much like a room.
  1564. This function retrieves global account data and writes it to the given
  1565. `sync_result_builder`. See `_generate_sync_entry_for_rooms` for handling
  1566. of per-room account data.
  1567. Args:
  1568. sync_result_builder
  1569. """
  1570. sync_config = sync_result_builder.sync_config
  1571. user_id = sync_result_builder.sync_config.user.to_string()
  1572. since_token = sync_result_builder.since_token
  1573. if since_token and not sync_result_builder.full_state:
  1574. global_account_data = (
  1575. await self.store.get_updated_global_account_data_for_user(
  1576. user_id, since_token.account_data_key
  1577. )
  1578. )
  1579. push_rules_changed = await self.store.have_push_rules_changed_for_user(
  1580. user_id, int(since_token.push_rules_key)
  1581. )
  1582. if push_rules_changed:
  1583. global_account_data = {
  1584. AccountDataTypes.PUSH_RULES: await self._push_rules_handler.push_rules_for_user(
  1585. sync_config.user
  1586. ),
  1587. **global_account_data,
  1588. }
  1589. else:
  1590. all_global_account_data = await self.store.get_global_account_data_for_user(
  1591. user_id
  1592. )
  1593. global_account_data = {
  1594. AccountDataTypes.PUSH_RULES: await self._push_rules_handler.push_rules_for_user(
  1595. sync_config.user
  1596. ),
  1597. **all_global_account_data,
  1598. }
  1599. account_data_for_user = (
  1600. await sync_config.filter_collection.filter_global_account_data(
  1601. [
  1602. {"type": account_data_type, "content": content}
  1603. for account_data_type, content in global_account_data.items()
  1604. ]
  1605. )
  1606. )
  1607. sync_result_builder.account_data = account_data_for_user
  1608. async def _generate_sync_entry_for_presence(
  1609. self,
  1610. sync_result_builder: "SyncResultBuilder",
  1611. newly_joined_rooms: AbstractSet[str],
  1612. newly_joined_or_invited_users: AbstractSet[str],
  1613. ) -> None:
  1614. """Generates the presence portion of the sync response. Populates the
  1615. `sync_result_builder` with the result.
  1616. Args:
  1617. sync_result_builder
  1618. newly_joined_rooms: Set of rooms that the user has joined since
  1619. the last sync (or empty if an initial sync)
  1620. newly_joined_or_invited_users: Set of users that have joined or
  1621. been invited to rooms since the last sync (or empty if an
  1622. initial sync)
  1623. """
  1624. now_token = sync_result_builder.now_token
  1625. sync_config = sync_result_builder.sync_config
  1626. user = sync_result_builder.sync_config.user
  1627. presence_source = self.event_sources.sources.presence
  1628. since_token = sync_result_builder.since_token
  1629. presence_key = None
  1630. include_offline = False
  1631. if since_token and not sync_result_builder.full_state:
  1632. presence_key = since_token.presence_key
  1633. include_offline = True
  1634. presence, presence_key = await presence_source.get_new_events(
  1635. user=user,
  1636. from_key=presence_key,
  1637. is_guest=sync_config.is_guest,
  1638. include_offline=include_offline,
  1639. )
  1640. assert presence_key
  1641. sync_result_builder.now_token = now_token.copy_and_replace(
  1642. StreamKeyType.PRESENCE, presence_key
  1643. )
  1644. extra_users_ids = set(newly_joined_or_invited_users)
  1645. for room_id in newly_joined_rooms:
  1646. users = await self.store.get_users_in_room(room_id)
  1647. extra_users_ids.update(users)
  1648. extra_users_ids.discard(user.to_string())
  1649. if extra_users_ids:
  1650. states = await self.presence_handler.get_states(extra_users_ids)
  1651. presence.extend(states)
  1652. # Deduplicate the presence entries so that there's at most one per user
  1653. presence = list({p.user_id: p for p in presence}.values())
  1654. presence = await sync_config.filter_collection.filter_presence(presence)
  1655. sync_result_builder.presence = presence
  1656. async def _generate_sync_entry_for_rooms(
  1657. self, sync_result_builder: "SyncResultBuilder"
  1658. ) -> Tuple[AbstractSet[str], AbstractSet[str]]:
  1659. """Generates the rooms portion of the sync response. Populates the
  1660. `sync_result_builder` with the result.
  1661. In the response that reaches the client, rooms are divided into four categories:
  1662. `invite`, `join`, `knock`, `leave`. These aren't the same as the four sets of
  1663. room ids returned by this function.
  1664. Args:
  1665. sync_result_builder
  1666. Returns:
  1667. Returns a 2-tuple describing rooms the user has joined or left.
  1668. Its entries are:
  1669. - newly_joined_rooms
  1670. - newly_left_rooms
  1671. """
  1672. since_token = sync_result_builder.since_token
  1673. user_id = sync_result_builder.sync_config.user.to_string()
  1674. blocks_all_rooms = (
  1675. sync_result_builder.sync_config.filter_collection.blocks_all_rooms()
  1676. )
  1677. # 0. Start by fetching room account data (if required).
  1678. if (
  1679. blocks_all_rooms
  1680. or sync_result_builder.sync_config.filter_collection.blocks_all_room_account_data()
  1681. ):
  1682. account_data_by_room: Mapping[str, Mapping[str, JsonMapping]] = {}
  1683. elif since_token and not sync_result_builder.full_state:
  1684. account_data_by_room = (
  1685. await self.store.get_updated_room_account_data_for_user(
  1686. user_id, since_token.account_data_key
  1687. )
  1688. )
  1689. else:
  1690. account_data_by_room = await self.store.get_room_account_data_for_user(
  1691. user_id
  1692. )
  1693. # 1. Start by fetching all ephemeral events in rooms we've joined (if required).
  1694. block_all_room_ephemeral = (
  1695. blocks_all_rooms
  1696. or sync_result_builder.sync_config.filter_collection.blocks_all_room_ephemeral()
  1697. )
  1698. if block_all_room_ephemeral:
  1699. ephemeral_by_room: Dict[str, List[JsonDict]] = {}
  1700. else:
  1701. now_token, ephemeral_by_room = await self.ephemeral_by_room(
  1702. sync_result_builder,
  1703. now_token=sync_result_builder.now_token,
  1704. since_token=sync_result_builder.since_token,
  1705. )
  1706. sync_result_builder.now_token = now_token
  1707. # 2. We check up front if anything has changed, if it hasn't then there is
  1708. # no point in going further.
  1709. if not sync_result_builder.full_state:
  1710. if since_token and not ephemeral_by_room and not account_data_by_room:
  1711. have_changed = await self._have_rooms_changed(sync_result_builder)
  1712. log_kv({"rooms_have_changed": have_changed})
  1713. if not have_changed:
  1714. tags_by_room = await self.store.get_updated_tags(
  1715. user_id, since_token.account_data_key
  1716. )
  1717. if not tags_by_room:
  1718. logger.debug("no-oping sync")
  1719. return set(), set()
  1720. # 3. Work out which rooms need reporting in the sync response.
  1721. ignored_users = await self.store.ignored_users(user_id)
  1722. if since_token:
  1723. room_changes = await self._get_room_changes_for_incremental_sync(
  1724. sync_result_builder, ignored_users
  1725. )
  1726. tags_by_room = await self.store.get_updated_tags(
  1727. user_id, since_token.account_data_key
  1728. )
  1729. else:
  1730. room_changes = await self._get_room_changes_for_initial_sync(
  1731. sync_result_builder, ignored_users
  1732. )
  1733. tags_by_room = await self.store.get_tags_for_user(user_id)
  1734. log_kv({"rooms_changed": len(room_changes.room_entries)})
  1735. room_entries = room_changes.room_entries
  1736. invited = room_changes.invited
  1737. knocked = room_changes.knocked
  1738. newly_joined_rooms = room_changes.newly_joined_rooms
  1739. newly_left_rooms = room_changes.newly_left_rooms
  1740. # 4. We need to apply further processing to `room_entries` (rooms considered
  1741. # joined or archived).
  1742. async def handle_room_entries(room_entry: "RoomSyncResultBuilder") -> None:
  1743. logger.debug("Generating room entry for %s", room_entry.room_id)
  1744. # Note that this mutates sync_result_builder.{joined,archived}.
  1745. await self._generate_room_entry(
  1746. sync_result_builder,
  1747. room_entry,
  1748. ephemeral=ephemeral_by_room.get(room_entry.room_id, []),
  1749. tags=tags_by_room.get(room_entry.room_id),
  1750. account_data=account_data_by_room.get(room_entry.room_id, {}),
  1751. always_include=sync_result_builder.full_state,
  1752. )
  1753. logger.debug("Generated room entry for %s", room_entry.room_id)
  1754. with start_active_span("sync.generate_room_entries"):
  1755. await concurrently_execute(handle_room_entries, room_entries, 10)
  1756. sync_result_builder.invited.extend(invited)
  1757. sync_result_builder.knocked.extend(knocked)
  1758. return set(newly_joined_rooms), set(newly_left_rooms)
  1759. async def _have_rooms_changed(
  1760. self, sync_result_builder: "SyncResultBuilder"
  1761. ) -> bool:
  1762. """Returns whether there may be any new events that should be sent down
  1763. the sync. Returns True if there are.
  1764. Does not modify the `sync_result_builder`.
  1765. """
  1766. since_token = sync_result_builder.since_token
  1767. membership_change_events = sync_result_builder.membership_change_events
  1768. assert since_token
  1769. if membership_change_events or sync_result_builder.forced_newly_joined_room_ids:
  1770. return True
  1771. stream_id = since_token.room_key.stream
  1772. for room_id in sync_result_builder.joined_room_ids:
  1773. if self.store.has_room_changed_since(room_id, stream_id):
  1774. return True
  1775. return False
  1776. async def _get_room_changes_for_incremental_sync(
  1777. self,
  1778. sync_result_builder: "SyncResultBuilder",
  1779. ignored_users: FrozenSet[str],
  1780. ) -> _RoomChanges:
  1781. """Determine the changes in rooms to report to the user.
  1782. This function is a first pass at generating the rooms part of the sync response.
  1783. It determines which rooms have changed during the sync period, and categorises
  1784. them into four buckets: "knock", "invite", "join" and "leave". It also excludes
  1785. from that list any room that appears in the list of rooms to exclude from sync
  1786. results in the server configuration.
  1787. 1. Finds all membership changes for the user in the sync period (from
  1788. `since_token` up to `now_token`).
  1789. 2. Uses those to place the room in one of the four categories above.
  1790. 3. Builds a `_RoomChanges` struct to record this, and return that struct.
  1791. For rooms classified as "knock", "invite" or "leave", we just need to report
  1792. a single membership event in the eventual /sync response. For "join" we need
  1793. to fetch additional non-membership events, e.g. messages in the room. That is
  1794. more complicated, so instead we report an intermediary `RoomSyncResultBuilder`
  1795. struct, and leave the additional work to `_generate_room_entry`.
  1796. The sync_result_builder is not modified by this function.
  1797. """
  1798. user_id = sync_result_builder.sync_config.user.to_string()
  1799. since_token = sync_result_builder.since_token
  1800. now_token = sync_result_builder.now_token
  1801. sync_config = sync_result_builder.sync_config
  1802. membership_change_events = sync_result_builder.membership_change_events
  1803. assert since_token
  1804. mem_change_events_by_room_id: Dict[str, List[EventBase]] = {}
  1805. for event in membership_change_events:
  1806. mem_change_events_by_room_id.setdefault(event.room_id, []).append(event)
  1807. newly_joined_rooms: List[str] = list(
  1808. sync_result_builder.forced_newly_joined_room_ids
  1809. )
  1810. newly_left_rooms: List[str] = []
  1811. room_entries: List[RoomSyncResultBuilder] = []
  1812. invited: List[InvitedSyncResult] = []
  1813. knocked: List[KnockedSyncResult] = []
  1814. for room_id, events in mem_change_events_by_room_id.items():
  1815. # The body of this loop will add this room to at least one of the five lists
  1816. # above. Things get messy if you've e.g. joined, left, joined then left the
  1817. # room all in the same sync period.
  1818. logger.debug(
  1819. "Membership changes in %s: [%s]",
  1820. room_id,
  1821. ", ".join("%s (%s)" % (e.event_id, e.membership) for e in events),
  1822. )
  1823. non_joins = [e for e in events if e.membership != Membership.JOIN]
  1824. has_join = len(non_joins) != len(events)
  1825. # We want to figure out if we joined the room at some point since
  1826. # the last sync (even if we have since left). This is to make sure
  1827. # we do send down the room, and with full state, where necessary
  1828. old_state_ids = None
  1829. if room_id in sync_result_builder.joined_room_ids and non_joins:
  1830. # Always include if the user (re)joined the room, especially
  1831. # important so that device list changes are calculated correctly.
  1832. # If there are non-join member events, but we are still in the room,
  1833. # then the user must have left and joined
  1834. newly_joined_rooms.append(room_id)
  1835. # User is in the room so we don't need to do the invite/leave checks
  1836. continue
  1837. if room_id in sync_result_builder.joined_room_ids or has_join:
  1838. old_state_ids = await self.get_state_at(
  1839. room_id,
  1840. since_token,
  1841. state_filter=StateFilter.from_types([(EventTypes.Member, user_id)]),
  1842. )
  1843. old_mem_ev_id = old_state_ids.get((EventTypes.Member, user_id), None)
  1844. old_mem_ev = None
  1845. if old_mem_ev_id:
  1846. old_mem_ev = await self.store.get_event(
  1847. old_mem_ev_id, allow_none=True
  1848. )
  1849. if not old_mem_ev or old_mem_ev.membership != Membership.JOIN:
  1850. newly_joined_rooms.append(room_id)
  1851. # If user is in the room then we don't need to do the invite/leave checks
  1852. if room_id in sync_result_builder.joined_room_ids:
  1853. continue
  1854. if not non_joins:
  1855. continue
  1856. last_non_join = non_joins[-1]
  1857. # Check if we have left the room. This can either be because we were
  1858. # joined before *or* that we since joined and then left.
  1859. if events[-1].membership != Membership.JOIN:
  1860. if has_join:
  1861. newly_left_rooms.append(room_id)
  1862. else:
  1863. if not old_state_ids:
  1864. old_state_ids = await self.get_state_at(
  1865. room_id,
  1866. since_token,
  1867. state_filter=StateFilter.from_types(
  1868. [(EventTypes.Member, user_id)]
  1869. ),
  1870. )
  1871. old_mem_ev_id = old_state_ids.get(
  1872. (EventTypes.Member, user_id), None
  1873. )
  1874. old_mem_ev = None
  1875. if old_mem_ev_id:
  1876. old_mem_ev = await self.store.get_event(
  1877. old_mem_ev_id, allow_none=True
  1878. )
  1879. if old_mem_ev and old_mem_ev.membership == Membership.JOIN:
  1880. newly_left_rooms.append(room_id)
  1881. # Only bother if we're still currently invited
  1882. should_invite = last_non_join.membership == Membership.INVITE
  1883. if should_invite:
  1884. if last_non_join.sender not in ignored_users:
  1885. invite_room_sync = InvitedSyncResult(room_id, invite=last_non_join)
  1886. if invite_room_sync:
  1887. invited.append(invite_room_sync)
  1888. # Only bother if our latest membership in the room is knock (and we haven't
  1889. # been accepted/rejected in the meantime).
  1890. should_knock = last_non_join.membership == Membership.KNOCK
  1891. if should_knock:
  1892. knock_room_sync = KnockedSyncResult(room_id, knock=last_non_join)
  1893. if knock_room_sync:
  1894. knocked.append(knock_room_sync)
  1895. # Always include leave/ban events. Just take the last one.
  1896. # TODO: How do we handle ban -> leave in same batch?
  1897. leave_events = [
  1898. e
  1899. for e in non_joins
  1900. if e.membership in (Membership.LEAVE, Membership.BAN)
  1901. ]
  1902. if leave_events:
  1903. leave_event = leave_events[-1]
  1904. leave_position = await self.store.get_position_for_event(
  1905. leave_event.event_id
  1906. )
  1907. # If the leave event happened before the since token then we
  1908. # bail.
  1909. if since_token and not leave_position.persisted_after(
  1910. since_token.room_key
  1911. ):
  1912. continue
  1913. # We can safely convert the position of the leave event into a
  1914. # stream token as it'll only be used in the context of this
  1915. # room. (c.f. the docstring of `to_room_stream_token`).
  1916. leave_token = since_token.copy_and_replace(
  1917. StreamKeyType.ROOM, leave_position.to_room_stream_token()
  1918. )
  1919. # If this is an out of band message, like a remote invite
  1920. # rejection, we include it in the recents batch. Otherwise, we
  1921. # let _load_filtered_recents handle fetching the correct
  1922. # batches.
  1923. #
  1924. # This is all screaming out for a refactor, as the logic here is
  1925. # subtle and the moving parts numerous.
  1926. if leave_event.internal_metadata.is_out_of_band_membership():
  1927. batch_events: Optional[List[EventBase]] = [leave_event]
  1928. else:
  1929. batch_events = None
  1930. room_entries.append(
  1931. RoomSyncResultBuilder(
  1932. room_id=room_id,
  1933. rtype="archived",
  1934. events=batch_events,
  1935. newly_joined=room_id in newly_joined_rooms,
  1936. full_state=False,
  1937. since_token=since_token,
  1938. upto_token=leave_token,
  1939. out_of_band=leave_event.internal_metadata.is_out_of_band_membership(),
  1940. )
  1941. )
  1942. timeline_limit = sync_config.filter_collection.timeline_limit()
  1943. # Get all events since the `from_key` in rooms we're currently joined to.
  1944. # If there are too many, we get the most recent events only. This leaves
  1945. # a "gap" in the timeline, as described by the spec for /sync.
  1946. room_to_events = await self.store.get_room_events_stream_for_rooms(
  1947. room_ids=sync_result_builder.joined_room_ids,
  1948. from_key=since_token.room_key,
  1949. to_key=now_token.room_key,
  1950. limit=timeline_limit + 1,
  1951. )
  1952. # We loop through all room ids, even if there are no new events, in case
  1953. # there are non room events that we need to notify about.
  1954. for room_id in sync_result_builder.joined_room_ids:
  1955. room_entry = room_to_events.get(room_id, None)
  1956. newly_joined = room_id in newly_joined_rooms
  1957. if room_entry:
  1958. events, start_key = room_entry
  1959. prev_batch_token = now_token.copy_and_replace(
  1960. StreamKeyType.ROOM, start_key
  1961. )
  1962. entry = RoomSyncResultBuilder(
  1963. room_id=room_id,
  1964. rtype="joined",
  1965. events=events,
  1966. newly_joined=newly_joined,
  1967. full_state=False,
  1968. since_token=None if newly_joined else since_token,
  1969. upto_token=prev_batch_token,
  1970. )
  1971. else:
  1972. entry = RoomSyncResultBuilder(
  1973. room_id=room_id,
  1974. rtype="joined",
  1975. events=[],
  1976. newly_joined=newly_joined,
  1977. full_state=False,
  1978. since_token=since_token,
  1979. upto_token=since_token,
  1980. )
  1981. room_entries.append(entry)
  1982. return _RoomChanges(
  1983. room_entries,
  1984. invited,
  1985. knocked,
  1986. newly_joined_rooms,
  1987. newly_left_rooms,
  1988. )
  1989. async def _get_room_changes_for_initial_sync(
  1990. self,
  1991. sync_result_builder: "SyncResultBuilder",
  1992. ignored_users: FrozenSet[str],
  1993. ) -> _RoomChanges:
  1994. """Returns entries for all rooms for the user.
  1995. Like `_get_rooms_changed`, but assumes the `since_token` is `None`.
  1996. This function does not modify the sync_result_builder.
  1997. Args:
  1998. sync_result_builder
  1999. ignored_users: Set of users ignored by user.
  2000. ignored_rooms: List of rooms to ignore.
  2001. """
  2002. user_id = sync_result_builder.sync_config.user.to_string()
  2003. since_token = sync_result_builder.since_token
  2004. now_token = sync_result_builder.now_token
  2005. sync_config = sync_result_builder.sync_config
  2006. room_list = await self.store.get_rooms_for_local_user_where_membership_is(
  2007. user_id=user_id,
  2008. membership_list=Membership.LIST,
  2009. excluded_rooms=sync_result_builder.excluded_room_ids,
  2010. )
  2011. room_entries = []
  2012. invited = []
  2013. knocked = []
  2014. for event in room_list:
  2015. if event.room_version_id not in KNOWN_ROOM_VERSIONS:
  2016. continue
  2017. if event.membership == Membership.JOIN:
  2018. room_entries.append(
  2019. RoomSyncResultBuilder(
  2020. room_id=event.room_id,
  2021. rtype="joined",
  2022. events=None,
  2023. newly_joined=False,
  2024. full_state=True,
  2025. since_token=since_token,
  2026. upto_token=now_token,
  2027. )
  2028. )
  2029. elif event.membership == Membership.INVITE:
  2030. if event.sender in ignored_users:
  2031. continue
  2032. invite = await self.store.get_event(event.event_id)
  2033. invited.append(InvitedSyncResult(room_id=event.room_id, invite=invite))
  2034. elif event.membership == Membership.KNOCK:
  2035. knock = await self.store.get_event(event.event_id)
  2036. knocked.append(KnockedSyncResult(room_id=event.room_id, knock=knock))
  2037. elif event.membership in (Membership.LEAVE, Membership.BAN):
  2038. # Always send down rooms we were banned from or kicked from.
  2039. if not sync_config.filter_collection.include_leave:
  2040. if event.membership == Membership.LEAVE:
  2041. if user_id == event.sender:
  2042. continue
  2043. leave_token = now_token.copy_and_replace(
  2044. StreamKeyType.ROOM, RoomStreamToken(stream=event.stream_ordering)
  2045. )
  2046. room_entries.append(
  2047. RoomSyncResultBuilder(
  2048. room_id=event.room_id,
  2049. rtype="archived",
  2050. events=None,
  2051. newly_joined=False,
  2052. full_state=True,
  2053. since_token=since_token,
  2054. upto_token=leave_token,
  2055. )
  2056. )
  2057. return _RoomChanges(room_entries, invited, knocked, [], [])
  2058. async def _generate_room_entry(
  2059. self,
  2060. sync_result_builder: "SyncResultBuilder",
  2061. room_builder: "RoomSyncResultBuilder",
  2062. ephemeral: List[JsonDict],
  2063. tags: Optional[Mapping[str, JsonMapping]],
  2064. account_data: Mapping[str, JsonMapping],
  2065. always_include: bool = False,
  2066. ) -> None:
  2067. """Populates the `joined` and `archived` section of `sync_result_builder`
  2068. based on the `room_builder`.
  2069. Ideally, we want to report all events whose stream ordering `s` lies in the
  2070. range `since_token < s <= now_token`, where the two tokens are read from the
  2071. sync_result_builder.
  2072. If there are too many events in that range to report, things get complicated.
  2073. In this situation we return a truncated list of the most recent events, and
  2074. indicate in the response that there is a "gap" of omitted events. Lots of this
  2075. is handled in `_load_filtered_recents`, but some of is handled in this method.
  2076. Additionally:
  2077. - we include a "state_delta", to describe the changes in state over the gap,
  2078. - we include all membership events applying to the user making the request,
  2079. even those in the gap.
  2080. See the spec for the rationale:
  2081. https://spec.matrix.org/v1.1/client-server-api/#syncing
  2082. Args:
  2083. sync_result_builder
  2084. room_builder
  2085. ephemeral: List of new ephemeral events for room
  2086. tags: List of *all* tags for room, or None if there has been
  2087. no change.
  2088. account_data: List of new account data for room
  2089. always_include: Always include this room in the sync response,
  2090. even if empty.
  2091. """
  2092. newly_joined = room_builder.newly_joined
  2093. full_state = (
  2094. room_builder.full_state or newly_joined or sync_result_builder.full_state
  2095. )
  2096. events = room_builder.events
  2097. # We want to shortcut out as early as possible.
  2098. if not (always_include or account_data or ephemeral or full_state):
  2099. if events == [] and tags is None:
  2100. return
  2101. now_token = sync_result_builder.now_token
  2102. sync_config = sync_result_builder.sync_config
  2103. room_id = room_builder.room_id
  2104. since_token = room_builder.since_token
  2105. upto_token = room_builder.upto_token
  2106. with start_active_span("sync.generate_room_entry"):
  2107. set_tag("room_id", room_id)
  2108. log_kv({"events": len(events or ())})
  2109. log_kv(
  2110. {
  2111. "since_token": since_token,
  2112. "upto_token": upto_token,
  2113. }
  2114. )
  2115. batch = await self._load_filtered_recents(
  2116. room_id,
  2117. sync_result_builder,
  2118. sync_config,
  2119. upto_token=upto_token,
  2120. since_token=since_token,
  2121. potential_recents=events,
  2122. newly_joined_room=newly_joined,
  2123. )
  2124. log_kv(
  2125. {
  2126. "batch_events": len(batch.events),
  2127. "prev_batch": batch.prev_batch,
  2128. "batch_limited": batch.limited,
  2129. }
  2130. )
  2131. # Note: `batch` can be both empty and limited here in the case where
  2132. # `_load_filtered_recents` can't find any events the user should see
  2133. # (e.g. due to having ignored the sender of the last 50 events).
  2134. # When we join the room (or the client requests full_state), we should
  2135. # send down any existing tags. Usually the user won't have tags in a
  2136. # newly joined room, unless either a) they've joined before or b) the
  2137. # tag was added by synapse e.g. for server notice rooms.
  2138. if full_state:
  2139. user_id = sync_result_builder.sync_config.user.to_string()
  2140. tags = await self.store.get_tags_for_room(user_id, room_id)
  2141. # If there aren't any tags, don't send the empty tags list down
  2142. # sync
  2143. if not tags:
  2144. tags = None
  2145. account_data_events = []
  2146. if tags is not None:
  2147. account_data_events.append(
  2148. {"type": AccountDataTypes.TAG, "content": {"tags": tags}}
  2149. )
  2150. for account_data_type, content in account_data.items():
  2151. account_data_events.append(
  2152. {"type": account_data_type, "content": content}
  2153. )
  2154. account_data_events = (
  2155. await sync_config.filter_collection.filter_room_account_data(
  2156. account_data_events
  2157. )
  2158. )
  2159. ephemeral = await sync_config.filter_collection.filter_room_ephemeral(
  2160. ephemeral
  2161. )
  2162. if not (
  2163. always_include
  2164. or batch
  2165. or account_data_events
  2166. or ephemeral
  2167. or full_state
  2168. ):
  2169. return
  2170. if not room_builder.out_of_band:
  2171. state = await self.compute_state_delta(
  2172. room_id,
  2173. batch,
  2174. sync_config,
  2175. since_token,
  2176. now_token,
  2177. full_state=full_state,
  2178. )
  2179. else:
  2180. # An out of band room won't have any state changes.
  2181. state = {}
  2182. summary: Optional[JsonDict] = {}
  2183. # we include a summary in room responses when we're lazy loading
  2184. # members (as the client otherwise doesn't have enough info to form
  2185. # the name itself).
  2186. if (
  2187. not room_builder.out_of_band
  2188. and sync_config.filter_collection.lazy_load_members()
  2189. and (
  2190. # we recalculate the summary:
  2191. # if there are membership changes in the timeline, or
  2192. # if membership has changed during a gappy sync, or
  2193. # if this is an initial sync.
  2194. any(ev.type == EventTypes.Member for ev in batch.events)
  2195. or (
  2196. # XXX: this may include false positives in the form of LL
  2197. # members which have snuck into state
  2198. batch.limited
  2199. and any(t == EventTypes.Member for (t, k) in state)
  2200. )
  2201. or since_token is None
  2202. )
  2203. ):
  2204. summary = await self.compute_summary(
  2205. room_id, sync_config, batch, state, now_token
  2206. )
  2207. if room_builder.rtype == "joined":
  2208. unread_notifications: Dict[str, int] = {}
  2209. room_sync = JoinedSyncResult(
  2210. room_id=room_id,
  2211. timeline=batch,
  2212. state=state,
  2213. ephemeral=ephemeral,
  2214. account_data=account_data_events,
  2215. unread_notifications=unread_notifications,
  2216. unread_thread_notifications={},
  2217. summary=summary,
  2218. unread_count=0,
  2219. )
  2220. if room_sync or always_include:
  2221. notifs = await self.unread_notifs_for_room_id(room_id, sync_config)
  2222. # Notifications for the main timeline.
  2223. notify_count = notifs.main_timeline.notify_count
  2224. highlight_count = notifs.main_timeline.highlight_count
  2225. unread_count = notifs.main_timeline.unread_count
  2226. # Check the sync configuration.
  2227. if sync_config.filter_collection.unread_thread_notifications():
  2228. # And add info for each thread.
  2229. room_sync.unread_thread_notifications = {
  2230. thread_id: {
  2231. "notification_count": thread_notifs.notify_count,
  2232. "highlight_count": thread_notifs.highlight_count,
  2233. }
  2234. for thread_id, thread_notifs in notifs.threads.items()
  2235. if thread_id is not None
  2236. }
  2237. else:
  2238. # Combine the unread counts for all threads and main timeline.
  2239. for thread_notifs in notifs.threads.values():
  2240. notify_count += thread_notifs.notify_count
  2241. highlight_count += thread_notifs.highlight_count
  2242. unread_count += thread_notifs.unread_count
  2243. unread_notifications["notification_count"] = notify_count
  2244. unread_notifications["highlight_count"] = highlight_count
  2245. room_sync.unread_count = unread_count
  2246. sync_result_builder.joined.append(room_sync)
  2247. if batch.limited and since_token:
  2248. user_id = sync_result_builder.sync_config.user.to_string()
  2249. logger.debug(
  2250. "Incremental gappy sync of %s for user %s with %d state events"
  2251. % (room_id, user_id, len(state))
  2252. )
  2253. elif room_builder.rtype == "archived":
  2254. archived_room_sync = ArchivedSyncResult(
  2255. room_id=room_id,
  2256. timeline=batch,
  2257. state=state,
  2258. account_data=account_data_events,
  2259. )
  2260. if archived_room_sync or always_include:
  2261. sync_result_builder.archived.append(archived_room_sync)
  2262. else:
  2263. raise Exception("Unrecognized rtype: %r", room_builder.rtype)
  2264. def _action_has_highlight(actions: List[JsonDict]) -> bool:
  2265. for action in actions:
  2266. try:
  2267. if action.get("set_tweak", None) == "highlight":
  2268. return action.get("value", True)
  2269. except AttributeError:
  2270. pass
  2271. return False
  2272. def _calculate_state(
  2273. timeline_contains: StateMap[str],
  2274. timeline_start: StateMap[str],
  2275. timeline_end: StateMap[str],
  2276. previous_timeline_end: StateMap[str],
  2277. lazy_load_members: bool,
  2278. ) -> StateMap[str]:
  2279. """Works out what state to include in a sync response.
  2280. Args:
  2281. timeline_contains: state in the timeline
  2282. timeline_start: state at the start of the timeline
  2283. timeline_end: state at the end of the timeline
  2284. previous_timeline_end: state at the end of the previous sync (or empty dict
  2285. if this is an initial sync)
  2286. lazy_load_members: whether to return members from timeline_start
  2287. or not. assumes that timeline_start has already been filtered to
  2288. include only the members the client needs to know about.
  2289. """
  2290. event_id_to_state_key = {
  2291. event_id: state_key
  2292. for state_key, event_id in itertools.chain(
  2293. timeline_contains.items(),
  2294. timeline_start.items(),
  2295. timeline_end.items(),
  2296. previous_timeline_end.items(),
  2297. )
  2298. }
  2299. timeline_end_ids = set(timeline_end.values())
  2300. timeline_start_ids = set(timeline_start.values())
  2301. previous_timeline_end_ids = set(previous_timeline_end.values())
  2302. timeline_contains_ids = set(timeline_contains.values())
  2303. # If we are lazyloading room members, we explicitly add the membership events
  2304. # for the senders in the timeline into the state block returned by /sync,
  2305. # as we may not have sent them to the client before. We find these membership
  2306. # events by filtering them out of timeline_start, which has already been filtered
  2307. # to only include membership events for the senders in the timeline.
  2308. # In practice, we can do this by removing them from the previous_timeline_end_ids
  2309. # list, which is the list of relevant state we know we have already sent to the
  2310. # client.
  2311. # see https://github.com/matrix-org/synapse/pull/2970/files/efcdacad7d1b7f52f879179701c7e0d9b763511f#r204732809
  2312. if lazy_load_members:
  2313. previous_timeline_end_ids.difference_update(
  2314. e for t, e in timeline_start.items() if t[0] == EventTypes.Member
  2315. )
  2316. state_ids = (
  2317. (timeline_end_ids | timeline_start_ids)
  2318. - previous_timeline_end_ids
  2319. - timeline_contains_ids
  2320. )
  2321. return {event_id_to_state_key[e]: e for e in state_ids}
  2322. @attr.s(slots=True, auto_attribs=True)
  2323. class SyncResultBuilder:
  2324. """Used to help build up a new SyncResult for a user
  2325. Attributes:
  2326. sync_config
  2327. full_state: The full_state flag as specified by user
  2328. since_token: The token supplied by user, or None.
  2329. now_token: The token to sync up to.
  2330. joined_room_ids: List of rooms the user is joined to
  2331. excluded_room_ids: Set of room ids we should omit from the /sync response.
  2332. forced_newly_joined_room_ids:
  2333. Rooms that should be presented in the /sync response as if they were
  2334. newly joined during the sync period, even if that's not the case.
  2335. (This is useful if the room was previously excluded from a /sync response,
  2336. and now the client should be made aware of it.)
  2337. Only used by incremental syncs.
  2338. # The following mirror the fields in a sync response
  2339. presence
  2340. account_data
  2341. joined
  2342. invited
  2343. knocked
  2344. archived
  2345. to_device
  2346. """
  2347. sync_config: SyncConfig
  2348. full_state: bool
  2349. since_token: Optional[StreamToken]
  2350. now_token: StreamToken
  2351. joined_room_ids: FrozenSet[str]
  2352. excluded_room_ids: FrozenSet[str]
  2353. forced_newly_joined_room_ids: FrozenSet[str]
  2354. membership_change_events: List[EventBase]
  2355. presence: List[UserPresenceState] = attr.Factory(list)
  2356. account_data: List[JsonDict] = attr.Factory(list)
  2357. joined: List[JoinedSyncResult] = attr.Factory(list)
  2358. invited: List[InvitedSyncResult] = attr.Factory(list)
  2359. knocked: List[KnockedSyncResult] = attr.Factory(list)
  2360. archived: List[ArchivedSyncResult] = attr.Factory(list)
  2361. to_device: List[JsonDict] = attr.Factory(list)
  2362. def calculate_user_changes(self) -> Tuple[AbstractSet[str], AbstractSet[str]]:
  2363. """Work out which other users have joined or left rooms we are joined to.
  2364. This data only is only useful for an incremental sync.
  2365. The SyncResultBuilder is not modified by this function.
  2366. """
  2367. newly_joined_or_invited_or_knocked_users = set()
  2368. newly_left_users = set()
  2369. if self.since_token:
  2370. for joined_sync in self.joined:
  2371. it = itertools.chain(
  2372. joined_sync.timeline.events, joined_sync.state.values()
  2373. )
  2374. for event in it:
  2375. if event.type == EventTypes.Member:
  2376. if (
  2377. event.membership == Membership.JOIN
  2378. or event.membership == Membership.INVITE
  2379. or event.membership == Membership.KNOCK
  2380. ):
  2381. newly_joined_or_invited_or_knocked_users.add(
  2382. event.state_key
  2383. )
  2384. else:
  2385. prev_content = event.unsigned.get("prev_content", {})
  2386. prev_membership = prev_content.get("membership", None)
  2387. if prev_membership == Membership.JOIN:
  2388. newly_left_users.add(event.state_key)
  2389. newly_left_users -= newly_joined_or_invited_or_knocked_users
  2390. return newly_joined_or_invited_or_knocked_users, newly_left_users
  2391. @attr.s(slots=True, auto_attribs=True)
  2392. class RoomSyncResultBuilder:
  2393. """Stores information needed to create either a `JoinedSyncResult` or
  2394. `ArchivedSyncResult`.
  2395. Attributes:
  2396. room_id
  2397. rtype: One of `"joined"` or `"archived"`
  2398. events: List of events to include in the room (more events may be added
  2399. when generating result).
  2400. newly_joined: If the user has newly joined the room
  2401. full_state: Whether the full state should be sent in result
  2402. since_token: Earliest point to return events from, or None
  2403. upto_token: Latest point to return events from.
  2404. out_of_band: whether the events in the room are "out of band" events
  2405. and the server isn't in the room.
  2406. """
  2407. room_id: str
  2408. rtype: str
  2409. events: Optional[List[EventBase]]
  2410. newly_joined: bool
  2411. full_state: bool
  2412. since_token: Optional[StreamToken]
  2413. upto_token: StreamToken
  2414. out_of_band: bool = False