sync.py 108 KB

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