Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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