matrix-synapse 1.142.0rc3__cp314-abi3-musllinux_1_2_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of matrix-synapse might be problematic. Click here for more details.

Files changed (1057) hide show
  1. matrix_synapse-1.142.0rc3.dist-info/AUTHORS.rst +51 -0
  2. matrix_synapse-1.142.0rc3.dist-info/LICENSE-AGPL-3.0 +661 -0
  3. matrix_synapse-1.142.0rc3.dist-info/LICENSE-COMMERCIAL +6 -0
  4. matrix_synapse-1.142.0rc3.dist-info/METADATA +375 -0
  5. matrix_synapse-1.142.0rc3.dist-info/RECORD +1057 -0
  6. matrix_synapse-1.142.0rc3.dist-info/WHEEL +4 -0
  7. matrix_synapse-1.142.0rc3.dist-info/entry_points.txt +14 -0
  8. matrix_synapse.libs/libgcc_s-2d945d6c.so.1 +0 -0
  9. synapse/__init__.py +97 -0
  10. synapse/_scripts/__init__.py +0 -0
  11. synapse/_scripts/export_signing_key.py +109 -0
  12. synapse/_scripts/generate_config.py +83 -0
  13. synapse/_scripts/generate_log_config.py +56 -0
  14. synapse/_scripts/generate_signing_key.py +55 -0
  15. synapse/_scripts/generate_workers_map.py +318 -0
  16. synapse/_scripts/hash_password.py +95 -0
  17. synapse/_scripts/move_remote_media_to_new_store.py +128 -0
  18. synapse/_scripts/register_new_matrix_user.py +374 -0
  19. synapse/_scripts/review_recent_signups.py +212 -0
  20. synapse/_scripts/synapse_port_db.py +1603 -0
  21. synapse/_scripts/synctl.py +365 -0
  22. synapse/_scripts/update_synapse_database.py +130 -0
  23. synapse/api/__init__.py +20 -0
  24. synapse/api/auth/__init__.py +207 -0
  25. synapse/api/auth/base.py +406 -0
  26. synapse/api/auth/internal.py +299 -0
  27. synapse/api/auth/mas.py +457 -0
  28. synapse/api/auth/msc3861_delegated.py +617 -0
  29. synapse/api/auth_blocking.py +144 -0
  30. synapse/api/constants.py +362 -0
  31. synapse/api/errors.py +907 -0
  32. synapse/api/filtering.py +539 -0
  33. synapse/api/presence.py +104 -0
  34. synapse/api/ratelimiting.py +482 -0
  35. synapse/api/room_versions.py +535 -0
  36. synapse/api/urls.py +119 -0
  37. synapse/app/__init__.py +60 -0
  38. synapse/app/_base.py +866 -0
  39. synapse/app/admin_cmd.py +388 -0
  40. synapse/app/appservice.py +30 -0
  41. synapse/app/client_reader.py +30 -0
  42. synapse/app/complement_fork_starter.py +206 -0
  43. synapse/app/event_creator.py +29 -0
  44. synapse/app/federation_reader.py +30 -0
  45. synapse/app/federation_sender.py +30 -0
  46. synapse/app/frontend_proxy.py +30 -0
  47. synapse/app/generic_worker.py +475 -0
  48. synapse/app/homeserver.py +504 -0
  49. synapse/app/media_repository.py +30 -0
  50. synapse/app/phone_stats_home.py +296 -0
  51. synapse/app/pusher.py +30 -0
  52. synapse/app/synchrotron.py +30 -0
  53. synapse/app/user_dir.py +31 -0
  54. synapse/appservice/__init__.py +461 -0
  55. synapse/appservice/api.py +569 -0
  56. synapse/appservice/scheduler.py +567 -0
  57. synapse/config/__init__.py +27 -0
  58. synapse/config/__main__.py +62 -0
  59. synapse/config/_base.py +1108 -0
  60. synapse/config/_base.pyi +217 -0
  61. synapse/config/_util.py +99 -0
  62. synapse/config/account_validity.py +116 -0
  63. synapse/config/api.py +141 -0
  64. synapse/config/appservice.py +210 -0
  65. synapse/config/auth.py +80 -0
  66. synapse/config/auto_accept_invites.py +43 -0
  67. synapse/config/background_updates.py +44 -0
  68. synapse/config/cache.py +231 -0
  69. synapse/config/captcha.py +90 -0
  70. synapse/config/cas.py +116 -0
  71. synapse/config/consent.py +73 -0
  72. synapse/config/database.py +184 -0
  73. synapse/config/emailconfig.py +367 -0
  74. synapse/config/experimental.py +595 -0
  75. synapse/config/federation.py +114 -0
  76. synapse/config/homeserver.py +141 -0
  77. synapse/config/jwt.py +55 -0
  78. synapse/config/key.py +447 -0
  79. synapse/config/logger.py +390 -0
  80. synapse/config/mas.py +191 -0
  81. synapse/config/matrixrtc.py +66 -0
  82. synapse/config/metrics.py +84 -0
  83. synapse/config/modules.py +40 -0
  84. synapse/config/oembed.py +185 -0
  85. synapse/config/oidc.py +509 -0
  86. synapse/config/password_auth_providers.py +82 -0
  87. synapse/config/push.py +64 -0
  88. synapse/config/ratelimiting.py +254 -0
  89. synapse/config/redis.py +74 -0
  90. synapse/config/registration.py +296 -0
  91. synapse/config/repository.py +311 -0
  92. synapse/config/retention.py +162 -0
  93. synapse/config/room.py +88 -0
  94. synapse/config/room_directory.py +165 -0
  95. synapse/config/saml2.py +251 -0
  96. synapse/config/server.py +1170 -0
  97. synapse/config/server_notices.py +84 -0
  98. synapse/config/spam_checker.py +66 -0
  99. synapse/config/sso.py +121 -0
  100. synapse/config/stats.py +54 -0
  101. synapse/config/third_party_event_rules.py +40 -0
  102. synapse/config/tls.py +192 -0
  103. synapse/config/tracer.py +71 -0
  104. synapse/config/user_directory.py +47 -0
  105. synapse/config/user_types.py +44 -0
  106. synapse/config/voip.py +59 -0
  107. synapse/config/workers.py +642 -0
  108. synapse/crypto/__init__.py +20 -0
  109. synapse/crypto/context_factory.py +278 -0
  110. synapse/crypto/event_signing.py +194 -0
  111. synapse/crypto/keyring.py +931 -0
  112. synapse/event_auth.py +1266 -0
  113. synapse/events/__init__.py +668 -0
  114. synapse/events/auto_accept_invites.py +216 -0
  115. synapse/events/builder.py +387 -0
  116. synapse/events/presence_router.py +245 -0
  117. synapse/events/snapshot.py +559 -0
  118. synapse/events/utils.py +928 -0
  119. synapse/events/validator.py +305 -0
  120. synapse/federation/__init__.py +22 -0
  121. synapse/federation/federation_base.py +383 -0
  122. synapse/federation/federation_client.py +2134 -0
  123. synapse/federation/federation_server.py +1544 -0
  124. synapse/federation/persistence.py +71 -0
  125. synapse/federation/send_queue.py +532 -0
  126. synapse/federation/sender/__init__.py +1165 -0
  127. synapse/federation/sender/per_destination_queue.py +884 -0
  128. synapse/federation/sender/transaction_manager.py +210 -0
  129. synapse/federation/transport/__init__.py +28 -0
  130. synapse/federation/transport/client.py +1201 -0
  131. synapse/federation/transport/server/__init__.py +334 -0
  132. synapse/federation/transport/server/_base.py +429 -0
  133. synapse/federation/transport/server/federation.py +912 -0
  134. synapse/federation/units.py +133 -0
  135. synapse/handlers/__init__.py +20 -0
  136. synapse/handlers/account.py +162 -0
  137. synapse/handlers/account_data.py +362 -0
  138. synapse/handlers/account_validity.py +361 -0
  139. synapse/handlers/admin.py +618 -0
  140. synapse/handlers/appservice.py +991 -0
  141. synapse/handlers/auth.py +2494 -0
  142. synapse/handlers/cas.py +413 -0
  143. synapse/handlers/deactivate_account.py +363 -0
  144. synapse/handlers/delayed_events.py +635 -0
  145. synapse/handlers/device.py +1873 -0
  146. synapse/handlers/devicemessage.py +399 -0
  147. synapse/handlers/directory.py +554 -0
  148. synapse/handlers/e2e_keys.py +1834 -0
  149. synapse/handlers/e2e_room_keys.py +455 -0
  150. synapse/handlers/event_auth.py +390 -0
  151. synapse/handlers/events.py +201 -0
  152. synapse/handlers/federation.py +2043 -0
  153. synapse/handlers/federation_event.py +2420 -0
  154. synapse/handlers/identity.py +812 -0
  155. synapse/handlers/initial_sync.py +528 -0
  156. synapse/handlers/jwt.py +120 -0
  157. synapse/handlers/message.py +2347 -0
  158. synapse/handlers/oidc.py +1803 -0
  159. synapse/handlers/pagination.py +768 -0
  160. synapse/handlers/password_policy.py +102 -0
  161. synapse/handlers/presence.py +2638 -0
  162. synapse/handlers/profile.py +655 -0
  163. synapse/handlers/push_rules.py +164 -0
  164. synapse/handlers/read_marker.py +79 -0
  165. synapse/handlers/receipts.py +351 -0
  166. synapse/handlers/register.py +1060 -0
  167. synapse/handlers/relations.py +624 -0
  168. synapse/handlers/reports.py +98 -0
  169. synapse/handlers/room.py +2447 -0
  170. synapse/handlers/room_list.py +632 -0
  171. synapse/handlers/room_member.py +2365 -0
  172. synapse/handlers/room_member_worker.py +146 -0
  173. synapse/handlers/room_policy.py +186 -0
  174. synapse/handlers/room_summary.py +1057 -0
  175. synapse/handlers/saml.py +524 -0
  176. synapse/handlers/search.py +723 -0
  177. synapse/handlers/send_email.py +209 -0
  178. synapse/handlers/set_password.py +71 -0
  179. synapse/handlers/sliding_sync/__init__.py +1701 -0
  180. synapse/handlers/sliding_sync/extensions.py +970 -0
  181. synapse/handlers/sliding_sync/room_lists.py +2266 -0
  182. synapse/handlers/sliding_sync/store.py +128 -0
  183. synapse/handlers/sso.py +1292 -0
  184. synapse/handlers/state_deltas.py +82 -0
  185. synapse/handlers/stats.py +322 -0
  186. synapse/handlers/sync.py +3109 -0
  187. synapse/handlers/thread_subscriptions.py +190 -0
  188. synapse/handlers/typing.py +606 -0
  189. synapse/handlers/ui_auth/__init__.py +48 -0
  190. synapse/handlers/ui_auth/checkers.py +332 -0
  191. synapse/handlers/user_directory.py +783 -0
  192. synapse/handlers/worker_lock.py +365 -0
  193. synapse/http/__init__.py +106 -0
  194. synapse/http/additional_resource.py +62 -0
  195. synapse/http/client.py +1360 -0
  196. synapse/http/connectproxyclient.py +309 -0
  197. synapse/http/federation/__init__.py +19 -0
  198. synapse/http/federation/matrix_federation_agent.py +490 -0
  199. synapse/http/federation/srv_resolver.py +196 -0
  200. synapse/http/federation/well_known_resolver.py +367 -0
  201. synapse/http/matrixfederationclient.py +1875 -0
  202. synapse/http/proxy.py +290 -0
  203. synapse/http/proxyagent.py +497 -0
  204. synapse/http/replicationagent.py +203 -0
  205. synapse/http/request_metrics.py +309 -0
  206. synapse/http/server.py +1114 -0
  207. synapse/http/servlet.py +1019 -0
  208. synapse/http/site.py +825 -0
  209. synapse/http/types.py +27 -0
  210. synapse/logging/__init__.py +31 -0
  211. synapse/logging/_remote.py +261 -0
  212. synapse/logging/_terse_json.py +95 -0
  213. synapse/logging/context.py +1211 -0
  214. synapse/logging/formatter.py +63 -0
  215. synapse/logging/handlers.py +99 -0
  216. synapse/logging/loggers.py +25 -0
  217. synapse/logging/opentracing.py +1132 -0
  218. synapse/logging/scopecontextmanager.py +161 -0
  219. synapse/media/_base.py +827 -0
  220. synapse/media/filepath.py +417 -0
  221. synapse/media/media_repository.py +1580 -0
  222. synapse/media/media_storage.py +704 -0
  223. synapse/media/oembed.py +277 -0
  224. synapse/media/preview_html.py +559 -0
  225. synapse/media/storage_provider.py +195 -0
  226. synapse/media/thumbnailer.py +833 -0
  227. synapse/media/url_previewer.py +875 -0
  228. synapse/metrics/__init__.py +754 -0
  229. synapse/metrics/_gc.py +219 -0
  230. synapse/metrics/_reactor_metrics.py +171 -0
  231. synapse/metrics/_types.py +38 -0
  232. synapse/metrics/background_process_metrics.py +556 -0
  233. synapse/metrics/common_usage_metrics.py +94 -0
  234. synapse/metrics/jemalloc.py +248 -0
  235. synapse/module_api/__init__.py +2154 -0
  236. synapse/module_api/callbacks/__init__.py +50 -0
  237. synapse/module_api/callbacks/account_validity_callbacks.py +106 -0
  238. synapse/module_api/callbacks/media_repository_callbacks.py +160 -0
  239. synapse/module_api/callbacks/ratelimit_callbacks.py +79 -0
  240. synapse/module_api/callbacks/spamchecker_callbacks.py +1113 -0
  241. synapse/module_api/callbacks/third_party_event_rules_callbacks.py +599 -0
  242. synapse/module_api/errors.py +42 -0
  243. synapse/notifier.py +972 -0
  244. synapse/push/__init__.py +212 -0
  245. synapse/push/bulk_push_rule_evaluator.py +637 -0
  246. synapse/push/clientformat.py +126 -0
  247. synapse/push/emailpusher.py +333 -0
  248. synapse/push/httppusher.py +564 -0
  249. synapse/push/mailer.py +1012 -0
  250. synapse/push/presentable_names.py +216 -0
  251. synapse/push/push_tools.py +114 -0
  252. synapse/push/push_types.py +141 -0
  253. synapse/push/pusher.py +87 -0
  254. synapse/push/pusherpool.py +501 -0
  255. synapse/push/rulekinds.py +33 -0
  256. synapse/py.typed +0 -0
  257. synapse/replication/__init__.py +20 -0
  258. synapse/replication/http/__init__.py +68 -0
  259. synapse/replication/http/_base.py +468 -0
  260. synapse/replication/http/account_data.py +297 -0
  261. synapse/replication/http/deactivate_account.py +81 -0
  262. synapse/replication/http/delayed_events.py +62 -0
  263. synapse/replication/http/devices.py +254 -0
  264. synapse/replication/http/federation.py +334 -0
  265. synapse/replication/http/login.py +106 -0
  266. synapse/replication/http/membership.py +364 -0
  267. synapse/replication/http/presence.py +133 -0
  268. synapse/replication/http/push.py +156 -0
  269. synapse/replication/http/register.py +172 -0
  270. synapse/replication/http/send_events.py +182 -0
  271. synapse/replication/http/state.py +82 -0
  272. synapse/replication/http/streams.py +101 -0
  273. synapse/replication/tcp/__init__.py +56 -0
  274. synapse/replication/tcp/client.py +552 -0
  275. synapse/replication/tcp/commands.py +569 -0
  276. synapse/replication/tcp/context.py +41 -0
  277. synapse/replication/tcp/external_cache.py +156 -0
  278. synapse/replication/tcp/handler.py +942 -0
  279. synapse/replication/tcp/protocol.py +608 -0
  280. synapse/replication/tcp/redis.py +509 -0
  281. synapse/replication/tcp/resource.py +348 -0
  282. synapse/replication/tcp/streams/__init__.py +96 -0
  283. synapse/replication/tcp/streams/_base.py +766 -0
  284. synapse/replication/tcp/streams/events.py +287 -0
  285. synapse/replication/tcp/streams/federation.py +92 -0
  286. synapse/replication/tcp/streams/partial_state.py +80 -0
  287. synapse/res/providers.json +29 -0
  288. synapse/res/templates/_base.html +29 -0
  289. synapse/res/templates/account_previously_renewed.html +6 -0
  290. synapse/res/templates/account_renewed.html +6 -0
  291. synapse/res/templates/add_threepid.html +8 -0
  292. synapse/res/templates/add_threepid.txt +6 -0
  293. synapse/res/templates/add_threepid_failure.html +7 -0
  294. synapse/res/templates/add_threepid_success.html +6 -0
  295. synapse/res/templates/already_in_use.html +12 -0
  296. synapse/res/templates/already_in_use.txt +10 -0
  297. synapse/res/templates/auth_success.html +21 -0
  298. synapse/res/templates/invalid_token.html +6 -0
  299. synapse/res/templates/mail-Element.css +7 -0
  300. synapse/res/templates/mail-Vector.css +7 -0
  301. synapse/res/templates/mail-expiry.css +4 -0
  302. synapse/res/templates/mail.css +156 -0
  303. synapse/res/templates/notice_expiry.html +46 -0
  304. synapse/res/templates/notice_expiry.txt +7 -0
  305. synapse/res/templates/notif.html +51 -0
  306. synapse/res/templates/notif.txt +22 -0
  307. synapse/res/templates/notif_mail.html +59 -0
  308. synapse/res/templates/notif_mail.txt +10 -0
  309. synapse/res/templates/password_reset.html +10 -0
  310. synapse/res/templates/password_reset.txt +7 -0
  311. synapse/res/templates/password_reset_confirmation.html +15 -0
  312. synapse/res/templates/password_reset_failure.html +7 -0
  313. synapse/res/templates/password_reset_success.html +6 -0
  314. synapse/res/templates/recaptcha.html +42 -0
  315. synapse/res/templates/registration.html +12 -0
  316. synapse/res/templates/registration.txt +10 -0
  317. synapse/res/templates/registration_failure.html +6 -0
  318. synapse/res/templates/registration_success.html +6 -0
  319. synapse/res/templates/registration_token.html +18 -0
  320. synapse/res/templates/room.html +33 -0
  321. synapse/res/templates/room.txt +9 -0
  322. synapse/res/templates/sso.css +129 -0
  323. synapse/res/templates/sso_account_deactivated.html +25 -0
  324. synapse/res/templates/sso_auth_account_details.html +186 -0
  325. synapse/res/templates/sso_auth_account_details.js +116 -0
  326. synapse/res/templates/sso_auth_bad_user.html +26 -0
  327. synapse/res/templates/sso_auth_confirm.html +27 -0
  328. synapse/res/templates/sso_auth_success.html +26 -0
  329. synapse/res/templates/sso_error.html +71 -0
  330. synapse/res/templates/sso_footer.html +19 -0
  331. synapse/res/templates/sso_login_idp_picker.html +60 -0
  332. synapse/res/templates/sso_new_user_consent.html +30 -0
  333. synapse/res/templates/sso_partial_profile.html +19 -0
  334. synapse/res/templates/sso_redirect_confirm.html +39 -0
  335. synapse/res/templates/style.css +33 -0
  336. synapse/res/templates/terms.html +27 -0
  337. synapse/rest/__init__.py +197 -0
  338. synapse/rest/admin/__init__.py +390 -0
  339. synapse/rest/admin/_base.py +72 -0
  340. synapse/rest/admin/background_updates.py +171 -0
  341. synapse/rest/admin/devices.py +221 -0
  342. synapse/rest/admin/event_reports.py +173 -0
  343. synapse/rest/admin/events.py +69 -0
  344. synapse/rest/admin/experimental_features.py +137 -0
  345. synapse/rest/admin/federation.py +243 -0
  346. synapse/rest/admin/media.py +540 -0
  347. synapse/rest/admin/registration_tokens.py +358 -0
  348. synapse/rest/admin/rooms.py +1061 -0
  349. synapse/rest/admin/scheduled_tasks.py +70 -0
  350. synapse/rest/admin/server_notice_servlet.py +132 -0
  351. synapse/rest/admin/statistics.py +132 -0
  352. synapse/rest/admin/username_available.py +58 -0
  353. synapse/rest/admin/users.py +1608 -0
  354. synapse/rest/client/__init__.py +20 -0
  355. synapse/rest/client/_base.py +113 -0
  356. synapse/rest/client/account.py +930 -0
  357. synapse/rest/client/account_data.py +319 -0
  358. synapse/rest/client/account_validity.py +103 -0
  359. synapse/rest/client/appservice_ping.py +125 -0
  360. synapse/rest/client/auth.py +218 -0
  361. synapse/rest/client/auth_metadata.py +122 -0
  362. synapse/rest/client/capabilities.py +121 -0
  363. synapse/rest/client/delayed_events.py +111 -0
  364. synapse/rest/client/devices.py +587 -0
  365. synapse/rest/client/directory.py +211 -0
  366. synapse/rest/client/events.py +116 -0
  367. synapse/rest/client/filter.py +112 -0
  368. synapse/rest/client/initial_sync.py +65 -0
  369. synapse/rest/client/keys.py +678 -0
  370. synapse/rest/client/knock.py +104 -0
  371. synapse/rest/client/login.py +754 -0
  372. synapse/rest/client/login_token_request.py +127 -0
  373. synapse/rest/client/logout.py +93 -0
  374. synapse/rest/client/matrixrtc.py +52 -0
  375. synapse/rest/client/media.py +286 -0
  376. synapse/rest/client/mutual_rooms.py +93 -0
  377. synapse/rest/client/notifications.py +137 -0
  378. synapse/rest/client/openid.py +109 -0
  379. synapse/rest/client/password_policy.py +69 -0
  380. synapse/rest/client/presence.py +131 -0
  381. synapse/rest/client/profile.py +291 -0
  382. synapse/rest/client/push_rule.py +331 -0
  383. synapse/rest/client/pusher.py +181 -0
  384. synapse/rest/client/read_marker.py +104 -0
  385. synapse/rest/client/receipts.py +165 -0
  386. synapse/rest/client/register.py +1067 -0
  387. synapse/rest/client/relations.py +138 -0
  388. synapse/rest/client/rendezvous.py +76 -0
  389. synapse/rest/client/reporting.py +207 -0
  390. synapse/rest/client/room.py +1669 -0
  391. synapse/rest/client/room_keys.py +426 -0
  392. synapse/rest/client/room_upgrade_rest_servlet.py +112 -0
  393. synapse/rest/client/sendtodevice.py +85 -0
  394. synapse/rest/client/sync.py +1131 -0
  395. synapse/rest/client/tags.py +129 -0
  396. synapse/rest/client/thirdparty.py +130 -0
  397. synapse/rest/client/thread_subscriptions.py +247 -0
  398. synapse/rest/client/tokenrefresh.py +52 -0
  399. synapse/rest/client/transactions.py +149 -0
  400. synapse/rest/client/user_directory.py +90 -0
  401. synapse/rest/client/versions.py +191 -0
  402. synapse/rest/client/voip.py +88 -0
  403. synapse/rest/consent/__init__.py +0 -0
  404. synapse/rest/consent/consent_resource.py +210 -0
  405. synapse/rest/health.py +38 -0
  406. synapse/rest/key/__init__.py +20 -0
  407. synapse/rest/key/v2/__init__.py +40 -0
  408. synapse/rest/key/v2/local_key_resource.py +125 -0
  409. synapse/rest/key/v2/remote_key_resource.py +302 -0
  410. synapse/rest/media/__init__.py +0 -0
  411. synapse/rest/media/config_resource.py +53 -0
  412. synapse/rest/media/create_resource.py +90 -0
  413. synapse/rest/media/download_resource.py +110 -0
  414. synapse/rest/media/media_repository_resource.py +113 -0
  415. synapse/rest/media/preview_url_resource.py +77 -0
  416. synapse/rest/media/thumbnail_resource.py +142 -0
  417. synapse/rest/media/upload_resource.py +187 -0
  418. synapse/rest/media/v1/__init__.py +39 -0
  419. synapse/rest/media/v1/_base.py +23 -0
  420. synapse/rest/media/v1/media_storage.py +23 -0
  421. synapse/rest/media/v1/storage_provider.py +23 -0
  422. synapse/rest/synapse/__init__.py +20 -0
  423. synapse/rest/synapse/client/__init__.py +93 -0
  424. synapse/rest/synapse/client/federation_whitelist.py +66 -0
  425. synapse/rest/synapse/client/jwks.py +77 -0
  426. synapse/rest/synapse/client/new_user_consent.py +115 -0
  427. synapse/rest/synapse/client/oidc/__init__.py +45 -0
  428. synapse/rest/synapse/client/oidc/backchannel_logout_resource.py +42 -0
  429. synapse/rest/synapse/client/oidc/callback_resource.py +48 -0
  430. synapse/rest/synapse/client/password_reset.py +129 -0
  431. synapse/rest/synapse/client/pick_idp.py +107 -0
  432. synapse/rest/synapse/client/pick_username.py +153 -0
  433. synapse/rest/synapse/client/rendezvous.py +58 -0
  434. synapse/rest/synapse/client/saml2/__init__.py +42 -0
  435. synapse/rest/synapse/client/saml2/metadata_resource.py +46 -0
  436. synapse/rest/synapse/client/saml2/response_resource.py +52 -0
  437. synapse/rest/synapse/client/sso_register.py +56 -0
  438. synapse/rest/synapse/client/unsubscribe.py +88 -0
  439. synapse/rest/synapse/mas/__init__.py +71 -0
  440. synapse/rest/synapse/mas/_base.py +55 -0
  441. synapse/rest/synapse/mas/devices.py +239 -0
  442. synapse/rest/synapse/mas/users.py +469 -0
  443. synapse/rest/well_known.py +148 -0
  444. synapse/server.py +1258 -0
  445. synapse/server_notices/__init__.py +0 -0
  446. synapse/server_notices/consent_server_notices.py +136 -0
  447. synapse/server_notices/resource_limits_server_notices.py +215 -0
  448. synapse/server_notices/server_notices_manager.py +388 -0
  449. synapse/server_notices/server_notices_sender.py +67 -0
  450. synapse/server_notices/worker_server_notices_sender.py +46 -0
  451. synapse/spam_checker_api/__init__.py +31 -0
  452. synapse/state/__init__.py +1022 -0
  453. synapse/state/v1.py +370 -0
  454. synapse/state/v2.py +985 -0
  455. synapse/static/client/login/index.html +47 -0
  456. synapse/static/client/login/js/jquery-3.4.1.min.js +2 -0
  457. synapse/static/client/login/js/login.js +291 -0
  458. synapse/static/client/login/spinner.gif +0 -0
  459. synapse/static/client/login/style.css +79 -0
  460. synapse/static/index.html +63 -0
  461. synapse/storage/__init__.py +43 -0
  462. synapse/storage/_base.py +245 -0
  463. synapse/storage/admin_client_config.py +26 -0
  464. synapse/storage/background_updates.py +1189 -0
  465. synapse/storage/controllers/__init__.py +57 -0
  466. synapse/storage/controllers/persist_events.py +1239 -0
  467. synapse/storage/controllers/purge_events.py +456 -0
  468. synapse/storage/controllers/state.py +954 -0
  469. synapse/storage/controllers/stats.py +119 -0
  470. synapse/storage/database.py +2720 -0
  471. synapse/storage/databases/__init__.py +175 -0
  472. synapse/storage/databases/main/__init__.py +424 -0
  473. synapse/storage/databases/main/account_data.py +1060 -0
  474. synapse/storage/databases/main/appservice.py +473 -0
  475. synapse/storage/databases/main/cache.py +911 -0
  476. synapse/storage/databases/main/censor_events.py +225 -0
  477. synapse/storage/databases/main/client_ips.py +817 -0
  478. synapse/storage/databases/main/delayed_events.py +560 -0
  479. synapse/storage/databases/main/deviceinbox.py +1272 -0
  480. synapse/storage/databases/main/devices.py +2581 -0
  481. synapse/storage/databases/main/directory.py +212 -0
  482. synapse/storage/databases/main/e2e_room_keys.py +690 -0
  483. synapse/storage/databases/main/end_to_end_keys.py +1896 -0
  484. synapse/storage/databases/main/event_federation.py +2509 -0
  485. synapse/storage/databases/main/event_push_actions.py +1937 -0
  486. synapse/storage/databases/main/events.py +3746 -0
  487. synapse/storage/databases/main/events_bg_updates.py +2910 -0
  488. synapse/storage/databases/main/events_forward_extremities.py +126 -0
  489. synapse/storage/databases/main/events_worker.py +2784 -0
  490. synapse/storage/databases/main/experimental_features.py +130 -0
  491. synapse/storage/databases/main/filtering.py +231 -0
  492. synapse/storage/databases/main/keys.py +291 -0
  493. synapse/storage/databases/main/lock.py +553 -0
  494. synapse/storage/databases/main/media_repository.py +1070 -0
  495. synapse/storage/databases/main/metrics.py +460 -0
  496. synapse/storage/databases/main/monthly_active_users.py +443 -0
  497. synapse/storage/databases/main/openid.py +61 -0
  498. synapse/storage/databases/main/presence.py +511 -0
  499. synapse/storage/databases/main/profile.py +541 -0
  500. synapse/storage/databases/main/purge_events.py +511 -0
  501. synapse/storage/databases/main/push_rule.py +972 -0
  502. synapse/storage/databases/main/pusher.py +794 -0
  503. synapse/storage/databases/main/receipts.py +1342 -0
  504. synapse/storage/databases/main/registration.py +3076 -0
  505. synapse/storage/databases/main/rejections.py +38 -0
  506. synapse/storage/databases/main/relations.py +1118 -0
  507. synapse/storage/databases/main/room.py +2781 -0
  508. synapse/storage/databases/main/roommember.py +2112 -0
  509. synapse/storage/databases/main/search.py +941 -0
  510. synapse/storage/databases/main/session.py +151 -0
  511. synapse/storage/databases/main/signatures.py +94 -0
  512. synapse/storage/databases/main/sliding_sync.py +603 -0
  513. synapse/storage/databases/main/state.py +1006 -0
  514. synapse/storage/databases/main/state_deltas.py +329 -0
  515. synapse/storage/databases/main/stats.py +791 -0
  516. synapse/storage/databases/main/stream.py +2580 -0
  517. synapse/storage/databases/main/tags.py +360 -0
  518. synapse/storage/databases/main/task_scheduler.py +225 -0
  519. synapse/storage/databases/main/thread_subscriptions.py +591 -0
  520. synapse/storage/databases/main/transactions.py +681 -0
  521. synapse/storage/databases/main/ui_auth.py +420 -0
  522. synapse/storage/databases/main/user_directory.py +1331 -0
  523. synapse/storage/databases/main/user_erasure_store.py +117 -0
  524. synapse/storage/databases/state/__init__.py +22 -0
  525. synapse/storage/databases/state/bg_updates.py +499 -0
  526. synapse/storage/databases/state/deletion.py +558 -0
  527. synapse/storage/databases/state/store.py +949 -0
  528. synapse/storage/engines/__init__.py +70 -0
  529. synapse/storage/engines/_base.py +154 -0
  530. synapse/storage/engines/postgres.py +261 -0
  531. synapse/storage/engines/sqlite.py +199 -0
  532. synapse/storage/invite_rule.py +112 -0
  533. synapse/storage/keys.py +40 -0
  534. synapse/storage/prepare_database.py +731 -0
  535. synapse/storage/push_rule.py +28 -0
  536. synapse/storage/roommember.py +89 -0
  537. synapse/storage/schema/README.md +4 -0
  538. synapse/storage/schema/__init__.py +182 -0
  539. synapse/storage/schema/common/delta/25/00background_updates.sql +40 -0
  540. synapse/storage/schema/common/delta/35/00background_updates_add_col.sql +36 -0
  541. synapse/storage/schema/common/delta/58/00background_update_ordering.sql +38 -0
  542. synapse/storage/schema/common/full_schemas/72/full.sql.postgres +8 -0
  543. synapse/storage/schema/common/full_schemas/72/full.sql.sqlite +6 -0
  544. synapse/storage/schema/common/schema_version.sql +60 -0
  545. synapse/storage/schema/main/delta/12/v12.sql +82 -0
  546. synapse/storage/schema/main/delta/13/v13.sql +38 -0
  547. synapse/storage/schema/main/delta/14/v14.sql +42 -0
  548. synapse/storage/schema/main/delta/15/appservice_txns.sql +50 -0
  549. synapse/storage/schema/main/delta/15/presence_indices.sql +2 -0
  550. synapse/storage/schema/main/delta/15/v15.sql +24 -0
  551. synapse/storage/schema/main/delta/16/events_order_index.sql +4 -0
  552. synapse/storage/schema/main/delta/16/remote_media_cache_index.sql +2 -0
  553. synapse/storage/schema/main/delta/16/remove_duplicates.sql +9 -0
  554. synapse/storage/schema/main/delta/16/room_alias_index.sql +3 -0
  555. synapse/storage/schema/main/delta/16/unique_constraints.sql +72 -0
  556. synapse/storage/schema/main/delta/16/users.sql +56 -0
  557. synapse/storage/schema/main/delta/17/drop_indexes.sql +37 -0
  558. synapse/storage/schema/main/delta/17/server_keys.sql +43 -0
  559. synapse/storage/schema/main/delta/17/user_threepids.sql +9 -0
  560. synapse/storage/schema/main/delta/18/server_keys_bigger_ints.sql +51 -0
  561. synapse/storage/schema/main/delta/19/event_index.sql +38 -0
  562. synapse/storage/schema/main/delta/20/dummy.sql +1 -0
  563. synapse/storage/schema/main/delta/20/pushers.py +93 -0
  564. synapse/storage/schema/main/delta/21/end_to_end_keys.sql +53 -0
  565. synapse/storage/schema/main/delta/21/receipts.sql +57 -0
  566. synapse/storage/schema/main/delta/22/receipts_index.sql +41 -0
  567. synapse/storage/schema/main/delta/22/user_threepids_unique.sql +19 -0
  568. synapse/storage/schema/main/delta/24/stats_reporting.sql +37 -0
  569. synapse/storage/schema/main/delta/25/fts.py +81 -0
  570. synapse/storage/schema/main/delta/25/guest_access.sql +44 -0
  571. synapse/storage/schema/main/delta/25/history_visibility.sql +44 -0
  572. synapse/storage/schema/main/delta/25/tags.sql +57 -0
  573. synapse/storage/schema/main/delta/26/account_data.sql +36 -0
  574. synapse/storage/schema/main/delta/27/account_data.sql +55 -0
  575. synapse/storage/schema/main/delta/27/forgotten_memberships.sql +45 -0
  576. synapse/storage/schema/main/delta/27/ts.py +61 -0
  577. synapse/storage/schema/main/delta/28/event_push_actions.sql +46 -0
  578. synapse/storage/schema/main/delta/28/events_room_stream.sql +39 -0
  579. synapse/storage/schema/main/delta/28/public_roms_index.sql +39 -0
  580. synapse/storage/schema/main/delta/28/receipts_user_id_index.sql +41 -0
  581. synapse/storage/schema/main/delta/28/upgrade_times.sql +40 -0
  582. synapse/storage/schema/main/delta/28/users_is_guest.sql +41 -0
  583. synapse/storage/schema/main/delta/29/push_actions.sql +54 -0
  584. synapse/storage/schema/main/delta/30/alias_creator.sql +35 -0
  585. synapse/storage/schema/main/delta/30/as_users.py +82 -0
  586. synapse/storage/schema/main/delta/30/deleted_pushers.sql +44 -0
  587. synapse/storage/schema/main/delta/30/presence_stream.sql +49 -0
  588. synapse/storage/schema/main/delta/30/public_rooms.sql +42 -0
  589. synapse/storage/schema/main/delta/30/push_rule_stream.sql +57 -0
  590. synapse/storage/schema/main/delta/30/threepid_guest_access_tokens.sql +43 -0
  591. synapse/storage/schema/main/delta/31/invites.sql +61 -0
  592. synapse/storage/schema/main/delta/31/local_media_repository_url_cache.sql +46 -0
  593. synapse/storage/schema/main/delta/31/pushers_0.py +92 -0
  594. synapse/storage/schema/main/delta/31/pushers_index.sql +41 -0
  595. synapse/storage/schema/main/delta/31/search_update.py +65 -0
  596. synapse/storage/schema/main/delta/32/events.sql +35 -0
  597. synapse/storage/schema/main/delta/32/openid.sql +9 -0
  598. synapse/storage/schema/main/delta/32/pusher_throttle.sql +42 -0
  599. synapse/storage/schema/main/delta/32/remove_indices.sql +52 -0
  600. synapse/storage/schema/main/delta/32/reports.sql +44 -0
  601. synapse/storage/schema/main/delta/33/access_tokens_device_index.sql +36 -0
  602. synapse/storage/schema/main/delta/33/devices.sql +40 -0
  603. synapse/storage/schema/main/delta/33/devices_for_e2e_keys.sql +38 -0
  604. synapse/storage/schema/main/delta/33/devices_for_e2e_keys_clear_unknown_device.sql +39 -0
  605. synapse/storage/schema/main/delta/33/event_fields.py +61 -0
  606. synapse/storage/schema/main/delta/33/remote_media_ts.py +43 -0
  607. synapse/storage/schema/main/delta/33/user_ips_index.sql +36 -0
  608. synapse/storage/schema/main/delta/34/appservice_stream.sql +42 -0
  609. synapse/storage/schema/main/delta/34/cache_stream.py +50 -0
  610. synapse/storage/schema/main/delta/34/device_inbox.sql +43 -0
  611. synapse/storage/schema/main/delta/34/push_display_name_rename.sql +39 -0
  612. synapse/storage/schema/main/delta/34/received_txn_purge.py +36 -0
  613. synapse/storage/schema/main/delta/35/contains_url.sql +36 -0
  614. synapse/storage/schema/main/delta/35/device_outbox.sql +58 -0
  615. synapse/storage/schema/main/delta/35/device_stream_id.sql +40 -0
  616. synapse/storage/schema/main/delta/35/event_push_actions_index.sql +36 -0
  617. synapse/storage/schema/main/delta/35/public_room_list_change_stream.sql +52 -0
  618. synapse/storage/schema/main/delta/35/stream_order_to_extrem.sql +56 -0
  619. synapse/storage/schema/main/delta/36/readd_public_rooms.sql +45 -0
  620. synapse/storage/schema/main/delta/37/remove_auth_idx.py +89 -0
  621. synapse/storage/schema/main/delta/37/user_threepids.sql +71 -0
  622. synapse/storage/schema/main/delta/38/postgres_fts_gist.sql +38 -0
  623. synapse/storage/schema/main/delta/39/appservice_room_list.sql +48 -0
  624. synapse/storage/schema/main/delta/39/device_federation_stream_idx.sql +35 -0
  625. synapse/storage/schema/main/delta/39/event_push_index.sql +36 -0
  626. synapse/storage/schema/main/delta/39/federation_out_position.sql +41 -0
  627. synapse/storage/schema/main/delta/39/membership_profile.sql +39 -0
  628. synapse/storage/schema/main/delta/40/current_state_idx.sql +36 -0
  629. synapse/storage/schema/main/delta/40/device_inbox.sql +40 -0
  630. synapse/storage/schema/main/delta/40/device_list_streams.sql +79 -0
  631. synapse/storage/schema/main/delta/40/event_push_summary.sql +57 -0
  632. synapse/storage/schema/main/delta/40/pushers.sql +58 -0
  633. synapse/storage/schema/main/delta/41/device_list_stream_idx.sql +36 -0
  634. synapse/storage/schema/main/delta/41/device_outbound_index.sql +35 -0
  635. synapse/storage/schema/main/delta/41/event_search_event_id_idx.sql +36 -0
  636. synapse/storage/schema/main/delta/41/ratelimit.sql +41 -0
  637. synapse/storage/schema/main/delta/42/current_state_delta.sql +48 -0
  638. synapse/storage/schema/main/delta/42/device_list_last_id.sql +52 -0
  639. synapse/storage/schema/main/delta/42/event_auth_state_only.sql +36 -0
  640. synapse/storage/schema/main/delta/42/user_dir.py +88 -0
  641. synapse/storage/schema/main/delta/43/blocked_rooms.sql +40 -0
  642. synapse/storage/schema/main/delta/43/quarantine_media.sql +36 -0
  643. synapse/storage/schema/main/delta/43/url_cache.sql +35 -0
  644. synapse/storage/schema/main/delta/43/user_share.sql +52 -0
  645. synapse/storage/schema/main/delta/44/expire_url_cache.sql +60 -0
  646. synapse/storage/schema/main/delta/45/group_server.sql +186 -0
  647. synapse/storage/schema/main/delta/45/profile_cache.sql +47 -0
  648. synapse/storage/schema/main/delta/46/drop_refresh_tokens.sql +36 -0
  649. synapse/storage/schema/main/delta/46/drop_unique_deleted_pushers.sql +54 -0
  650. synapse/storage/schema/main/delta/46/group_server.sql +51 -0
  651. synapse/storage/schema/main/delta/46/local_media_repository_url_idx.sql +43 -0
  652. synapse/storage/schema/main/delta/46/user_dir_null_room_ids.sql +54 -0
  653. synapse/storage/schema/main/delta/46/user_dir_typos.sql +43 -0
  654. synapse/storage/schema/main/delta/47/last_access_media.sql +35 -0
  655. synapse/storage/schema/main/delta/47/postgres_fts_gin.sql +36 -0
  656. synapse/storage/schema/main/delta/47/push_actions_staging.sql +47 -0
  657. synapse/storage/schema/main/delta/48/add_user_consent.sql +37 -0
  658. synapse/storage/schema/main/delta/48/add_user_ips_last_seen_index.sql +36 -0
  659. synapse/storage/schema/main/delta/48/deactivated_users.sql +44 -0
  660. synapse/storage/schema/main/delta/48/group_unique_indexes.py +67 -0
  661. synapse/storage/schema/main/delta/48/groups_joinable.sql +41 -0
  662. synapse/storage/schema/main/delta/49/add_user_consent_server_notice_sent.sql +39 -0
  663. synapse/storage/schema/main/delta/49/add_user_daily_visits.sql +40 -0
  664. synapse/storage/schema/main/delta/49/add_user_ips_last_seen_only_index.sql +36 -0
  665. synapse/storage/schema/main/delta/50/add_creation_ts_users_index.sql +38 -0
  666. synapse/storage/schema/main/delta/50/erasure_store.sql +40 -0
  667. synapse/storage/schema/main/delta/50/make_event_content_nullable.py +102 -0
  668. synapse/storage/schema/main/delta/51/e2e_room_keys.sql +58 -0
  669. synapse/storage/schema/main/delta/51/monthly_active_users.sql +46 -0
  670. synapse/storage/schema/main/delta/52/add_event_to_state_group_index.sql +38 -0
  671. synapse/storage/schema/main/delta/52/device_list_streams_unique_idx.sql +55 -0
  672. synapse/storage/schema/main/delta/52/e2e_room_keys.sql +72 -0
  673. synapse/storage/schema/main/delta/53/add_user_type_to_users.sql +38 -0
  674. synapse/storage/schema/main/delta/53/drop_sent_transactions.sql +35 -0
  675. synapse/storage/schema/main/delta/53/event_format_version.sql +35 -0
  676. synapse/storage/schema/main/delta/53/user_dir_populate.sql +49 -0
  677. synapse/storage/schema/main/delta/53/user_ips_index.sql +49 -0
  678. synapse/storage/schema/main/delta/53/user_share.sql +63 -0
  679. synapse/storage/schema/main/delta/53/user_threepid_id.sql +48 -0
  680. synapse/storage/schema/main/delta/53/users_in_public_rooms.sql +47 -0
  681. synapse/storage/schema/main/delta/54/account_validity_with_renewal.sql +49 -0
  682. synapse/storage/schema/main/delta/54/add_validity_to_server_keys.sql +42 -0
  683. synapse/storage/schema/main/delta/54/delete_forward_extremities.sql +42 -0
  684. synapse/storage/schema/main/delta/54/drop_legacy_tables.sql +49 -0
  685. synapse/storage/schema/main/delta/54/drop_presence_list.sql +35 -0
  686. synapse/storage/schema/main/delta/54/relations.sql +46 -0
  687. synapse/storage/schema/main/delta/54/stats.sql +99 -0
  688. synapse/storage/schema/main/delta/54/stats2.sql +47 -0
  689. synapse/storage/schema/main/delta/55/access_token_expiry.sql +37 -0
  690. synapse/storage/schema/main/delta/55/track_threepid_validations.sql +50 -0
  691. synapse/storage/schema/main/delta/55/users_alter_deactivated.sql +38 -0
  692. synapse/storage/schema/main/delta/56/add_spans_to_device_lists.sql +39 -0
  693. synapse/storage/schema/main/delta/56/current_state_events_membership.sql +41 -0
  694. synapse/storage/schema/main/delta/56/current_state_events_membership_mk2.sql +43 -0
  695. synapse/storage/schema/main/delta/56/delete_keys_from_deleted_backups.sql +44 -0
  696. synapse/storage/schema/main/delta/56/destinations_failure_ts.sql +44 -0
  697. synapse/storage/schema/main/delta/56/destinations_retry_interval_type.sql.postgres +18 -0
  698. synapse/storage/schema/main/delta/56/device_stream_id_insert.sql +39 -0
  699. synapse/storage/schema/main/delta/56/devices_last_seen.sql +43 -0
  700. synapse/storage/schema/main/delta/56/drop_unused_event_tables.sql +39 -0
  701. synapse/storage/schema/main/delta/56/event_expiry.sql +40 -0
  702. synapse/storage/schema/main/delta/56/event_labels.sql +49 -0
  703. synapse/storage/schema/main/delta/56/event_labels_background_update.sql +36 -0
  704. synapse/storage/schema/main/delta/56/fix_room_keys_index.sql +37 -0
  705. synapse/storage/schema/main/delta/56/hidden_devices.sql +37 -0
  706. synapse/storage/schema/main/delta/56/hidden_devices_fix.sql.sqlite +42 -0
  707. synapse/storage/schema/main/delta/56/nuke_empty_communities_from_db.sql +48 -0
  708. synapse/storage/schema/main/delta/56/public_room_list_idx.sql +35 -0
  709. synapse/storage/schema/main/delta/56/redaction_censor.sql +35 -0
  710. synapse/storage/schema/main/delta/56/redaction_censor2.sql +41 -0
  711. synapse/storage/schema/main/delta/56/redaction_censor3_fix_update.sql.postgres +25 -0
  712. synapse/storage/schema/main/delta/56/redaction_censor4.sql +35 -0
  713. synapse/storage/schema/main/delta/56/remove_tombstoned_rooms_from_directory.sql +38 -0
  714. synapse/storage/schema/main/delta/56/room_key_etag.sql +36 -0
  715. synapse/storage/schema/main/delta/56/room_membership_idx.sql +37 -0
  716. synapse/storage/schema/main/delta/56/room_retention.sql +52 -0
  717. synapse/storage/schema/main/delta/56/signing_keys.sql +75 -0
  718. synapse/storage/schema/main/delta/56/signing_keys_nonunique_signatures.sql +41 -0
  719. synapse/storage/schema/main/delta/56/stats_separated.sql +175 -0
  720. synapse/storage/schema/main/delta/56/unique_user_filter_index.py +46 -0
  721. synapse/storage/schema/main/delta/56/user_external_ids.sql +43 -0
  722. synapse/storage/schema/main/delta/56/users_in_public_rooms_idx.sql +36 -0
  723. synapse/storage/schema/main/delta/57/delete_old_current_state_events.sql +41 -0
  724. synapse/storage/schema/main/delta/57/device_list_remote_cache_stale.sql +44 -0
  725. synapse/storage/schema/main/delta/57/local_current_membership.py +111 -0
  726. synapse/storage/schema/main/delta/57/remove_sent_outbound_pokes.sql +40 -0
  727. synapse/storage/schema/main/delta/57/rooms_version_column.sql +43 -0
  728. synapse/storage/schema/main/delta/57/rooms_version_column_2.sql.postgres +35 -0
  729. synapse/storage/schema/main/delta/57/rooms_version_column_2.sql.sqlite +22 -0
  730. synapse/storage/schema/main/delta/57/rooms_version_column_3.sql.postgres +39 -0
  731. synapse/storage/schema/main/delta/57/rooms_version_column_3.sql.sqlite +23 -0
  732. synapse/storage/schema/main/delta/58/02remove_dup_outbound_pokes.sql +41 -0
  733. synapse/storage/schema/main/delta/58/03persist_ui_auth.sql +55 -0
  734. synapse/storage/schema/main/delta/58/05cache_instance.sql.postgres +30 -0
  735. synapse/storage/schema/main/delta/58/06dlols_unique_idx.py +83 -0
  736. synapse/storage/schema/main/delta/58/07add_method_to_thumbnail_constraint.sql.postgres +33 -0
  737. synapse/storage/schema/main/delta/58/07add_method_to_thumbnail_constraint.sql.sqlite +44 -0
  738. synapse/storage/schema/main/delta/58/07persist_ui_auth_ips.sql +44 -0
  739. synapse/storage/schema/main/delta/58/08_media_safe_from_quarantine.sql.postgres +18 -0
  740. synapse/storage/schema/main/delta/58/08_media_safe_from_quarantine.sql.sqlite +18 -0
  741. synapse/storage/schema/main/delta/58/09shadow_ban.sql +37 -0
  742. synapse/storage/schema/main/delta/58/10_pushrules_enabled_delete_obsolete.sql +47 -0
  743. synapse/storage/schema/main/delta/58/10drop_local_rejections_stream.sql +41 -0
  744. synapse/storage/schema/main/delta/58/10federation_pos_instance_name.sql +41 -0
  745. synapse/storage/schema/main/delta/58/11dehydration.sql +39 -0
  746. synapse/storage/schema/main/delta/58/11fallback.sql +43 -0
  747. synapse/storage/schema/main/delta/58/11user_id_seq.py +38 -0
  748. synapse/storage/schema/main/delta/58/12room_stats.sql +51 -0
  749. synapse/storage/schema/main/delta/58/13remove_presence_allow_inbound.sql +36 -0
  750. synapse/storage/schema/main/delta/58/14events_instance_name.sql +35 -0
  751. synapse/storage/schema/main/delta/58/14events_instance_name.sql.postgres +28 -0
  752. synapse/storage/schema/main/delta/58/15_catchup_destination_rooms.sql +61 -0
  753. synapse/storage/schema/main/delta/58/15unread_count.sql +45 -0
  754. synapse/storage/schema/main/delta/58/16populate_stats_process_rooms_fix.sql +41 -0
  755. synapse/storage/schema/main/delta/58/17_catchup_last_successful.sql +40 -0
  756. synapse/storage/schema/main/delta/58/18stream_positions.sql +41 -0
  757. synapse/storage/schema/main/delta/58/19instance_map.sql.postgres +25 -0
  758. synapse/storage/schema/main/delta/58/19txn_id.sql +59 -0
  759. synapse/storage/schema/main/delta/58/20instance_name_event_tables.sql +36 -0
  760. synapse/storage/schema/main/delta/58/20user_daily_visits.sql +37 -0
  761. synapse/storage/schema/main/delta/58/21as_device_stream.sql +36 -0
  762. synapse/storage/schema/main/delta/58/21drop_device_max_stream_id.sql +1 -0
  763. synapse/storage/schema/main/delta/58/22puppet_token.sql +36 -0
  764. synapse/storage/schema/main/delta/58/22users_have_local_media.sql +2 -0
  765. synapse/storage/schema/main/delta/58/23e2e_cross_signing_keys_idx.sql +36 -0
  766. synapse/storage/schema/main/delta/58/24drop_event_json_index.sql +38 -0
  767. synapse/storage/schema/main/delta/58/25user_external_ids_user_id_idx.sql +36 -0
  768. synapse/storage/schema/main/delta/58/26access_token_last_validated.sql +37 -0
  769. synapse/storage/schema/main/delta/58/27local_invites.sql +37 -0
  770. synapse/storage/schema/main/delta/58/28drop_last_used_column.sql.postgres +16 -0
  771. synapse/storage/schema/main/delta/58/28drop_last_used_column.sql.sqlite +62 -0
  772. synapse/storage/schema/main/delta/59/01ignored_user.py +85 -0
  773. synapse/storage/schema/main/delta/59/02shard_send_to_device.sql +37 -0
  774. synapse/storage/schema/main/delta/59/03shard_send_to_device_sequence.sql.postgres +25 -0
  775. synapse/storage/schema/main/delta/59/04_event_auth_chains.sql +71 -0
  776. synapse/storage/schema/main/delta/59/04_event_auth_chains.sql.postgres +16 -0
  777. synapse/storage/schema/main/delta/59/04drop_account_data.sql +36 -0
  778. synapse/storage/schema/main/delta/59/05cache_invalidation.sql +36 -0
  779. synapse/storage/schema/main/delta/59/06chain_cover_index.sql +36 -0
  780. synapse/storage/schema/main/delta/59/06shard_account_data.sql +39 -0
  781. synapse/storage/schema/main/delta/59/06shard_account_data.sql.postgres +32 -0
  782. synapse/storage/schema/main/delta/59/07shard_account_data_fix.sql +37 -0
  783. synapse/storage/schema/main/delta/59/08delete_pushers_for_deactivated_accounts.sql +39 -0
  784. synapse/storage/schema/main/delta/59/08delete_stale_pushers.sql +39 -0
  785. synapse/storage/schema/main/delta/59/09rejected_events_metadata.sql +45 -0
  786. synapse/storage/schema/main/delta/59/10delete_purged_chain_cover.sql +36 -0
  787. synapse/storage/schema/main/delta/59/11add_knock_members_to_stats.sql +39 -0
  788. synapse/storage/schema/main/delta/59/11drop_thumbnail_constraint.sql.postgres +22 -0
  789. synapse/storage/schema/main/delta/59/12account_validity_token_used_ts_ms.sql +37 -0
  790. synapse/storage/schema/main/delta/59/12presence_stream_instance.sql +37 -0
  791. synapse/storage/schema/main/delta/59/12presence_stream_instance_seq.sql.postgres +20 -0
  792. synapse/storage/schema/main/delta/59/13users_to_send_full_presence_to.sql +53 -0
  793. synapse/storage/schema/main/delta/59/14refresh_tokens.sql +53 -0
  794. synapse/storage/schema/main/delta/59/15locks.sql +56 -0
  795. synapse/storage/schema/main/delta/59/16federation_inbound_staging.sql +51 -0
  796. synapse/storage/schema/main/delta/60/01recreate_stream_ordering.sql.postgres +45 -0
  797. synapse/storage/schema/main/delta/60/02change_stream_ordering_columns.sql.postgres +30 -0
  798. synapse/storage/schema/main/delta/61/01change_appservices_txns.sql.postgres +23 -0
  799. synapse/storage/schema/main/delta/61/01insertion_event_lookups.sql +68 -0
  800. synapse/storage/schema/main/delta/61/02drop_redundant_room_depth_index.sql +37 -0
  801. synapse/storage/schema/main/delta/61/03recreate_min_depth.py +74 -0
  802. synapse/storage/schema/main/delta/62/01insertion_event_extremities.sql +43 -0
  803. synapse/storage/schema/main/delta/63/01create_registration_tokens.sql +42 -0
  804. synapse/storage/schema/main/delta/63/02delete_unlinked_email_pushers.sql +39 -0
  805. synapse/storage/schema/main/delta/63/02populate-rooms-creator.sql +36 -0
  806. synapse/storage/schema/main/delta/63/03session_store.sql +42 -0
  807. synapse/storage/schema/main/delta/63/04add_presence_stream_not_offline_index.sql +37 -0
  808. synapse/storage/schema/main/delta/64/01msc2716_chunk_to_batch_rename.sql.postgres +23 -0
  809. synapse/storage/schema/main/delta/64/01msc2716_chunk_to_batch_rename.sql.sqlite +37 -0
  810. synapse/storage/schema/main/delta/65/01msc2716_insertion_event_edges.sql +38 -0
  811. synapse/storage/schema/main/delta/65/03remove_hidden_devices_from_device_inbox.sql +41 -0
  812. synapse/storage/schema/main/delta/65/04_local_group_updates.sql +37 -0
  813. synapse/storage/schema/main/delta/65/05_remove_room_stats_historical_and_user_stats_historical.sql +38 -0
  814. synapse/storage/schema/main/delta/65/06remove_deleted_devices_from_device_inbox.sql +53 -0
  815. synapse/storage/schema/main/delta/65/07_arbitrary_relations.sql +37 -0
  816. synapse/storage/schema/main/delta/65/08_device_inbox_background_updates.sql +37 -0
  817. synapse/storage/schema/main/delta/65/10_expirable_refresh_tokens.sql +47 -0
  818. synapse/storage/schema/main/delta/65/11_devices_auth_provider_session.sql +46 -0
  819. synapse/storage/schema/main/delta/67/01drop_public_room_list_stream.sql +37 -0
  820. synapse/storage/schema/main/delta/68/01event_columns.sql +45 -0
  821. synapse/storage/schema/main/delta/68/02_msc2409_add_device_id_appservice_stream_type.sql +40 -0
  822. synapse/storage/schema/main/delta/68/03_delete_account_data_for_deactivated_accounts.sql +39 -0
  823. synapse/storage/schema/main/delta/68/04_refresh_tokens_index_next_token_id.sql +47 -0
  824. synapse/storage/schema/main/delta/68/04partial_state_rooms.sql +60 -0
  825. synapse/storage/schema/main/delta/68/05_delete_non_strings_from_event_search.sql.sqlite +22 -0
  826. synapse/storage/schema/main/delta/68/05partial_state_rooms_triggers.py +80 -0
  827. synapse/storage/schema/main/delta/68/06_msc3202_add_device_list_appservice_stream_type.sql +42 -0
  828. synapse/storage/schema/main/delta/69/01as_txn_seq.py +54 -0
  829. synapse/storage/schema/main/delta/69/01device_list_oubound_by_room.sql +57 -0
  830. synapse/storage/schema/main/delta/69/02cache_invalidation_index.sql +37 -0
  831. synapse/storage/schema/main/delta/70/01clean_table_purged_rooms.sql +39 -0
  832. synapse/storage/schema/main/delta/71/01rebuild_event_edges.sql.postgres +43 -0
  833. synapse/storage/schema/main/delta/71/01rebuild_event_edges.sql.sqlite +47 -0
  834. synapse/storage/schema/main/delta/71/01remove_noop_background_updates.sql +80 -0
  835. synapse/storage/schema/main/delta/71/02event_push_summary_unique.sql +37 -0
  836. synapse/storage/schema/main/delta/72/01add_room_type_to_state_stats.sql +38 -0
  837. synapse/storage/schema/main/delta/72/01event_push_summary_receipt.sql +54 -0
  838. synapse/storage/schema/main/delta/72/02event_push_actions_index.sql +38 -0
  839. synapse/storage/schema/main/delta/72/03bg_populate_events_columns.py +57 -0
  840. synapse/storage/schema/main/delta/72/03drop_event_reference_hashes.sql +36 -0
  841. synapse/storage/schema/main/delta/72/03remove_groups.sql +50 -0
  842. synapse/storage/schema/main/delta/72/04drop_column_application_services_state_last_txn.sql.postgres +17 -0
  843. synapse/storage/schema/main/delta/72/04drop_column_application_services_state_last_txn.sql.sqlite +40 -0
  844. synapse/storage/schema/main/delta/72/05receipts_event_stream_ordering.sql +38 -0
  845. synapse/storage/schema/main/delta/72/05remove_unstable_private_read_receipts.sql +38 -0
  846. synapse/storage/schema/main/delta/72/06add_consent_ts_to_users.sql +35 -0
  847. synapse/storage/schema/main/delta/72/06thread_notifications.sql +49 -0
  848. synapse/storage/schema/main/delta/72/07force_update_current_state_events_membership.py +67 -0
  849. synapse/storage/schema/main/delta/72/07thread_receipts.sql.postgres +30 -0
  850. synapse/storage/schema/main/delta/72/07thread_receipts.sql.sqlite +70 -0
  851. synapse/storage/schema/main/delta/72/08begin_cache_invalidation_seq_at_2.sql.postgres +23 -0
  852. synapse/storage/schema/main/delta/72/08thread_receipts.sql +39 -0
  853. synapse/storage/schema/main/delta/72/09partial_indices.sql.sqlite +56 -0
  854. synapse/storage/schema/main/delta/73/01event_failed_pull_attempts.sql +48 -0
  855. synapse/storage/schema/main/delta/73/02add_pusher_enabled.sql +35 -0
  856. synapse/storage/schema/main/delta/73/02room_id_indexes_for_purging.sql +41 -0
  857. synapse/storage/schema/main/delta/73/03pusher_device_id.sql +39 -0
  858. synapse/storage/schema/main/delta/73/03users_approved_column.sql +39 -0
  859. synapse/storage/schema/main/delta/73/04partial_join_details.sql +42 -0
  860. synapse/storage/schema/main/delta/73/04pending_device_list_updates.sql +47 -0
  861. synapse/storage/schema/main/delta/73/05old_push_actions.sql.postgres +22 -0
  862. synapse/storage/schema/main/delta/73/05old_push_actions.sql.sqlite +24 -0
  863. synapse/storage/schema/main/delta/73/06thread_notifications_thread_id_idx.sql +42 -0
  864. synapse/storage/schema/main/delta/73/08thread_receipts_non_null.sql.postgres +23 -0
  865. synapse/storage/schema/main/delta/73/08thread_receipts_non_null.sql.sqlite +76 -0
  866. synapse/storage/schema/main/delta/73/09partial_joined_via_destination.sql +37 -0
  867. synapse/storage/schema/main/delta/73/09threads_table.sql +49 -0
  868. synapse/storage/schema/main/delta/73/10_update_sqlite_fts4_tokenizer.py +71 -0
  869. synapse/storage/schema/main/delta/73/10login_tokens.sql +54 -0
  870. synapse/storage/schema/main/delta/73/11event_search_room_id_n_distinct.sql.postgres +33 -0
  871. synapse/storage/schema/main/delta/73/12refactor_device_list_outbound_pokes.sql +72 -0
  872. synapse/storage/schema/main/delta/73/13add_device_lists_index.sql +39 -0
  873. synapse/storage/schema/main/delta/73/20_un_partial_stated_room_stream.sql +51 -0
  874. synapse/storage/schema/main/delta/73/21_un_partial_stated_room_stream_seq.sql.postgres +20 -0
  875. synapse/storage/schema/main/delta/73/22_rebuild_user_dir_stats.sql +48 -0
  876. synapse/storage/schema/main/delta/73/22_un_partial_stated_event_stream.sql +53 -0
  877. synapse/storage/schema/main/delta/73/23_fix_thread_index.sql +52 -0
  878. synapse/storage/schema/main/delta/73/23_un_partial_stated_room_stream_seq.sql.postgres +20 -0
  879. synapse/storage/schema/main/delta/73/24_events_jump_to_date_index.sql +36 -0
  880. synapse/storage/schema/main/delta/73/25drop_presence.sql +36 -0
  881. synapse/storage/schema/main/delta/74/01_user_directory_stale_remote_users.sql +58 -0
  882. synapse/storage/schema/main/delta/74/02_set_device_id_for_pushers_bg_update.sql +38 -0
  883. synapse/storage/schema/main/delta/74/03_membership_tables_event_stream_ordering.sql.postgres +29 -0
  884. synapse/storage/schema/main/delta/74/03_membership_tables_event_stream_ordering.sql.sqlite +23 -0
  885. synapse/storage/schema/main/delta/74/03_room_membership_index.sql +38 -0
  886. synapse/storage/schema/main/delta/74/04_delete_e2e_backup_keys_for_deactivated_users.sql +36 -0
  887. synapse/storage/schema/main/delta/74/04_membership_tables_event_stream_ordering_triggers.py +87 -0
  888. synapse/storage/schema/main/delta/74/05_events_txn_id_device_id.sql +72 -0
  889. synapse/storage/schema/main/delta/74/90COMMENTS_destinations.sql.postgres +52 -0
  890. synapse/storage/schema/main/delta/76/01_add_profiles_full_user_id_column.sql +39 -0
  891. synapse/storage/schema/main/delta/76/02_add_user_filters_full_user_id_column.sql +39 -0
  892. synapse/storage/schema/main/delta/76/03_per_user_experimental_features.sql +46 -0
  893. synapse/storage/schema/main/delta/76/04_add_room_forgetter.sql +43 -0
  894. synapse/storage/schema/main/delta/77/01_add_profiles_not_valid_check.sql.postgres +16 -0
  895. synapse/storage/schema/main/delta/77/02_add_user_filters_not_valid_check.sql.postgres +16 -0
  896. synapse/storage/schema/main/delta/77/03bg_populate_full_user_id_profiles.sql +35 -0
  897. synapse/storage/schema/main/delta/77/04bg_populate_full_user_id_user_filters.sql +35 -0
  898. synapse/storage/schema/main/delta/77/05thread_notifications_backfill.sql +67 -0
  899. synapse/storage/schema/main/delta/77/06thread_notifications_not_null.sql.sqlite +102 -0
  900. synapse/storage/schema/main/delta/77/06thread_notifications_not_null_event_push_actions.sql.postgres +27 -0
  901. synapse/storage/schema/main/delta/77/06thread_notifications_not_null_event_push_actions_staging.sql.postgres +27 -0
  902. synapse/storage/schema/main/delta/77/06thread_notifications_not_null_event_push_summary.sql.postgres +29 -0
  903. synapse/storage/schema/main/delta/77/14bg_indices_event_stream_ordering.sql +39 -0
  904. synapse/storage/schema/main/delta/78/01_validate_and_update_profiles.py +99 -0
  905. synapse/storage/schema/main/delta/78/02_validate_and_update_user_filters.py +100 -0
  906. synapse/storage/schema/main/delta/78/03_remove_unused_indexes_user_filters.py +72 -0
  907. synapse/storage/schema/main/delta/78/03event_extremities_constraints.py +65 -0
  908. synapse/storage/schema/main/delta/78/04_add_full_user_id_index_user_filters.py +32 -0
  909. synapse/storage/schema/main/delta/79/03_read_write_locks_triggers.sql.postgres +102 -0
  910. synapse/storage/schema/main/delta/79/03_read_write_locks_triggers.sql.sqlite +72 -0
  911. synapse/storage/schema/main/delta/79/04_mitigate_stream_ordering_update_race.py +70 -0
  912. synapse/storage/schema/main/delta/79/05_read_write_locks_triggers.sql.postgres +69 -0
  913. synapse/storage/schema/main/delta/79/05_read_write_locks_triggers.sql.sqlite +65 -0
  914. synapse/storage/schema/main/delta/80/01_users_alter_locked.sql +35 -0
  915. synapse/storage/schema/main/delta/80/02_read_write_locks_unlogged.sql.postgres +30 -0
  916. synapse/storage/schema/main/delta/80/02_scheduled_tasks.sql +47 -0
  917. synapse/storage/schema/main/delta/80/03_read_write_locks_triggers.sql.postgres +37 -0
  918. synapse/storage/schema/main/delta/80/04_read_write_locks_deadlock.sql.postgres +71 -0
  919. synapse/storage/schema/main/delta/82/02_scheduled_tasks_index.sql +35 -0
  920. synapse/storage/schema/main/delta/82/04_add_indices_for_purging_rooms.sql +39 -0
  921. synapse/storage/schema/main/delta/82/05gaps.sql +44 -0
  922. synapse/storage/schema/main/delta/83/01_drop_old_tables.sql +43 -0
  923. synapse/storage/schema/main/delta/83/03_instance_name_receipts.sql.sqlite +17 -0
  924. synapse/storage/schema/main/delta/83/05_cross_signing_key_update_grant.sql +34 -0
  925. synapse/storage/schema/main/delta/83/06_event_push_summary_room.sql +36 -0
  926. synapse/storage/schema/main/delta/84/01_auth_links_stats.sql.postgres +20 -0
  927. synapse/storage/schema/main/delta/84/02_auth_links_index.sql +16 -0
  928. synapse/storage/schema/main/delta/84/03_auth_links_analyze.sql.postgres +16 -0
  929. synapse/storage/schema/main/delta/84/04_access_token_index.sql +15 -0
  930. synapse/storage/schema/main/delta/85/01_add_suspended.sql +14 -0
  931. synapse/storage/schema/main/delta/85/02_add_instance_names.sql +27 -0
  932. synapse/storage/schema/main/delta/85/03_new_sequences.sql.postgres +54 -0
  933. synapse/storage/schema/main/delta/85/04_cleanup_device_federation_outbox.sql +15 -0
  934. synapse/storage/schema/main/delta/85/05_add_instance_names_converted_pos.sql +16 -0
  935. synapse/storage/schema/main/delta/85/06_add_room_reports.sql +20 -0
  936. synapse/storage/schema/main/delta/86/01_authenticate_media.sql +15 -0
  937. synapse/storage/schema/main/delta/86/02_receipts_event_id_index.sql +15 -0
  938. synapse/storage/schema/main/delta/87/01_sliding_sync_memberships.sql +169 -0
  939. synapse/storage/schema/main/delta/87/02_per_connection_state.sql +81 -0
  940. synapse/storage/schema/main/delta/87/03_current_state_index.sql +19 -0
  941. synapse/storage/schema/main/delta/88/01_add_delayed_events.sql +43 -0
  942. synapse/storage/schema/main/delta/88/01_custom_profile_fields.sql +15 -0
  943. synapse/storage/schema/main/delta/88/02_fix_sliding_sync_membership_snapshots_forgotten_column.sql +21 -0
  944. synapse/storage/schema/main/delta/88/03_add_otk_ts_added_index.sql +18 -0
  945. synapse/storage/schema/main/delta/88/04_current_state_delta_index.sql +18 -0
  946. synapse/storage/schema/main/delta/88/05_drop_old_otks.sql.postgres +19 -0
  947. synapse/storage/schema/main/delta/88/05_drop_old_otks.sql.sqlite +19 -0
  948. synapse/storage/schema/main/delta/88/05_sliding_sync_room_config_index.sql +20 -0
  949. synapse/storage/schema/main/delta/88/06_events_received_ts_index.sql +17 -0
  950. synapse/storage/schema/main/delta/89/01_sliding_sync_membership_snapshot_index.sql +15 -0
  951. synapse/storage/schema/main/delta/90/01_add_column_participant_room_memberships_table.sql +16 -0
  952. synapse/storage/schema/main/delta/91/01_media_hash.sql +28 -0
  953. synapse/storage/schema/main/delta/92/01_remove_trigger.sql.postgres +16 -0
  954. synapse/storage/schema/main/delta/92/01_remove_trigger.sql.sqlite +16 -0
  955. synapse/storage/schema/main/delta/92/02_remove_populate_participant_bg_update.sql +17 -0
  956. synapse/storage/schema/main/delta/92/04_ss_membership_snapshot_idx.sql +16 -0
  957. synapse/storage/schema/main/delta/92/04_thread_subscriptions.sql +59 -0
  958. synapse/storage/schema/main/delta/92/04_thread_subscriptions_seq.sql.postgres +19 -0
  959. synapse/storage/schema/main/delta/92/05_fixup_max_depth_cap.sql +17 -0
  960. synapse/storage/schema/main/delta/92/05_thread_subscriptions_comments.sql.postgres +18 -0
  961. synapse/storage/schema/main/delta/92/06_device_federation_inbox_index.sql +16 -0
  962. synapse/storage/schema/main/delta/92/06_threads_last_sent_stream_ordering_comments.sql.postgres +24 -0
  963. synapse/storage/schema/main/delta/92/07_add_user_reports.sql +22 -0
  964. synapse/storage/schema/main/delta/92/07_event_txn_id_device_id_txn_id2.sql +15 -0
  965. synapse/storage/schema/main/delta/92/08_room_ban_redactions.sql +21 -0
  966. synapse/storage/schema/main/delta/92/08_thread_subscriptions_seq_fixup.sql.postgres +19 -0
  967. synapse/storage/schema/main/delta/92/09_thread_subscriptions_update.sql +20 -0
  968. synapse/storage/schema/main/delta/92/09_thread_subscriptions_update.sql.postgres +18 -0
  969. synapse/storage/schema/main/full_schemas/72/full.sql.postgres +1344 -0
  970. synapse/storage/schema/main/full_schemas/72/full.sql.sqlite +646 -0
  971. synapse/storage/schema/state/delta/23/drop_state_index.sql +35 -0
  972. synapse/storage/schema/state/delta/32/remove_state_indices.sql +38 -0
  973. synapse/storage/schema/state/delta/35/add_state_index.sql +36 -0
  974. synapse/storage/schema/state/delta/35/state.sql +41 -0
  975. synapse/storage/schema/state/delta/35/state_dedupe.sql +36 -0
  976. synapse/storage/schema/state/delta/47/state_group_seq.py +38 -0
  977. synapse/storage/schema/state/delta/56/state_group_room_idx.sql +36 -0
  978. synapse/storage/schema/state/delta/61/02state_groups_state_n_distinct.sql.postgres +34 -0
  979. synapse/storage/schema/state/delta/70/08_state_group_edges_unique.sql +36 -0
  980. synapse/storage/schema/state/delta/89/01_state_groups_deletion.sql +39 -0
  981. synapse/storage/schema/state/delta/90/02_delete_unreferenced_state_groups.sql +16 -0
  982. synapse/storage/schema/state/delta/90/03_remove_old_deletion_bg_update.sql +15 -0
  983. synapse/storage/schema/state/full_schemas/72/full.sql.postgres +30 -0
  984. synapse/storage/schema/state/full_schemas/72/full.sql.sqlite +20 -0
  985. synapse/storage/types.py +185 -0
  986. synapse/storage/util/__init__.py +20 -0
  987. synapse/storage/util/id_generators.py +909 -0
  988. synapse/storage/util/partial_state_events_tracker.py +194 -0
  989. synapse/storage/util/sequence.py +315 -0
  990. synapse/streams/__init__.py +43 -0
  991. synapse/streams/config.py +92 -0
  992. synapse/streams/events.py +203 -0
  993. synapse/synapse_rust/__init__.pyi +3 -0
  994. synapse/synapse_rust/acl.pyi +20 -0
  995. synapse/synapse_rust/events.pyi +136 -0
  996. synapse/synapse_rust/http_client.pyi +32 -0
  997. synapse/synapse_rust/push.pyi +86 -0
  998. synapse/synapse_rust/rendezvous.pyi +30 -0
  999. synapse/synapse_rust/segmenter.pyi +1 -0
  1000. synapse/synapse_rust.abi3.so +0 -0
  1001. synapse/types/__init__.py +1600 -0
  1002. synapse/types/handlers/__init__.py +93 -0
  1003. synapse/types/handlers/policy_server.py +16 -0
  1004. synapse/types/handlers/sliding_sync.py +909 -0
  1005. synapse/types/rest/__init__.py +25 -0
  1006. synapse/types/rest/client/__init__.py +415 -0
  1007. synapse/types/state.py +635 -0
  1008. synapse/types/storage/__init__.py +66 -0
  1009. synapse/util/__init__.py +170 -0
  1010. synapse/util/async_helpers.py +1067 -0
  1011. synapse/util/batching_queue.py +202 -0
  1012. synapse/util/caches/__init__.py +300 -0
  1013. synapse/util/caches/cached_call.py +143 -0
  1014. synapse/util/caches/deferred_cache.py +530 -0
  1015. synapse/util/caches/descriptors.py +694 -0
  1016. synapse/util/caches/dictionary_cache.py +350 -0
  1017. synapse/util/caches/expiringcache.py +251 -0
  1018. synapse/util/caches/lrucache.py +977 -0
  1019. synapse/util/caches/response_cache.py +323 -0
  1020. synapse/util/caches/stream_change_cache.py +370 -0
  1021. synapse/util/caches/treecache.py +189 -0
  1022. synapse/util/caches/ttlcache.py +197 -0
  1023. synapse/util/cancellation.py +63 -0
  1024. synapse/util/check_dependencies.py +335 -0
  1025. synapse/util/clock.py +500 -0
  1026. synapse/util/constants.py +22 -0
  1027. synapse/util/daemonize.py +165 -0
  1028. synapse/util/distributor.py +159 -0
  1029. synapse/util/events.py +134 -0
  1030. synapse/util/file_consumer.py +164 -0
  1031. synapse/util/frozenutils.py +57 -0
  1032. synapse/util/gai_resolver.py +180 -0
  1033. synapse/util/hash.py +38 -0
  1034. synapse/util/httpresourcetree.py +108 -0
  1035. synapse/util/iterutils.py +189 -0
  1036. synapse/util/json.py +56 -0
  1037. synapse/util/linked_list.py +156 -0
  1038. synapse/util/logcontext.py +46 -0
  1039. synapse/util/logformatter.py +28 -0
  1040. synapse/util/macaroons.py +325 -0
  1041. synapse/util/manhole.py +191 -0
  1042. synapse/util/metrics.py +340 -0
  1043. synapse/util/module_loader.py +116 -0
  1044. synapse/util/msisdn.py +51 -0
  1045. synapse/util/patch_inline_callbacks.py +250 -0
  1046. synapse/util/pydantic_models.py +56 -0
  1047. synapse/util/ratelimitutils.py +420 -0
  1048. synapse/util/retryutils.py +339 -0
  1049. synapse/util/rlimit.py +42 -0
  1050. synapse/util/rust.py +134 -0
  1051. synapse/util/sentinel.py +21 -0
  1052. synapse/util/stringutils.py +293 -0
  1053. synapse/util/task_scheduler.py +493 -0
  1054. synapse/util/templates.py +126 -0
  1055. synapse/util/threepids.py +123 -0
  1056. synapse/util/wheel_timer.py +112 -0
  1057. synapse/visibility.py +836 -0
@@ -0,0 +1,3109 @@
1
+ #
2
+ # This file is licensed under the Affero General Public License (AGPL) version 3.
3
+ #
4
+ # Copyright 2015-2021 The Matrix.org Foundation C.I.C.
5
+ # Copyright (C) 2023 New Vector, Ltd
6
+ #
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Affero General Public License as
9
+ # published by the Free Software Foundation, either version 3 of the
10
+ # License, or (at your option) any later version.
11
+ #
12
+ # See the GNU Affero General Public License for more details:
13
+ # <https://www.gnu.org/licenses/agpl-3.0.html>.
14
+ #
15
+ # Originally licensed under the Apache License, Version 2.0:
16
+ # <http://www.apache.org/licenses/LICENSE-2.0>.
17
+ #
18
+ # [This file includes modifications made by New Vector Limited]
19
+ #
20
+ #
21
+ import itertools
22
+ import logging
23
+ from typing import (
24
+ TYPE_CHECKING,
25
+ AbstractSet,
26
+ Any,
27
+ Mapping,
28
+ Optional,
29
+ Sequence,
30
+ )
31
+
32
+ import attr
33
+ from prometheus_client import Counter
34
+
35
+ from synapse.api.constants import (
36
+ AccountDataTypes,
37
+ Direction,
38
+ EventContentFields,
39
+ EventTypes,
40
+ JoinRules,
41
+ Membership,
42
+ )
43
+ from synapse.api.filtering import FilterCollection
44
+ from synapse.api.presence import UserPresenceState
45
+ from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
46
+ from synapse.events import EventBase
47
+ from synapse.handlers.relations import BundledAggregations
48
+ from synapse.logging import issue9533_logger
49
+ from synapse.logging.context import current_context
50
+ from synapse.logging.opentracing import (
51
+ SynapseTags,
52
+ log_kv,
53
+ set_tag,
54
+ start_active_span,
55
+ trace,
56
+ )
57
+ from synapse.metrics import SERVER_NAME_LABEL
58
+ from synapse.storage.databases.main.event_push_actions import RoomNotifCounts
59
+ from synapse.storage.databases.main.roommember import extract_heroes_from_room_summary
60
+ from synapse.storage.databases.main.stream import PaginateFunction
61
+ from synapse.storage.invite_rule import InviteRule
62
+ from synapse.storage.roommember import MemberSummary
63
+ from synapse.types import (
64
+ DeviceListUpdates,
65
+ JsonDict,
66
+ JsonMapping,
67
+ MultiWriterStreamToken,
68
+ MutableStateMap,
69
+ Requester,
70
+ RoomStreamToken,
71
+ StateMap,
72
+ StrCollection,
73
+ StreamKeyType,
74
+ StreamToken,
75
+ UserID,
76
+ )
77
+ from synapse.types.state import StateFilter
78
+ from synapse.util.async_helpers import concurrently_execute
79
+ from synapse.util.caches.expiringcache import ExpiringCache
80
+ from synapse.util.caches.lrucache import LruCache
81
+ from synapse.util.caches.response_cache import ResponseCache, ResponseCacheContext
82
+ from synapse.util.metrics import Measure
83
+ from synapse.visibility import filter_events_for_client
84
+
85
+ if TYPE_CHECKING:
86
+ from synapse.server import HomeServer
87
+
88
+ logger = logging.getLogger(__name__)
89
+
90
+ # Counts the number of times we returned a non-empty sync. `type` is one of
91
+ # "initial_sync", "full_state_sync" or "incremental_sync", `lazy_loaded` is
92
+ # "true" or "false" depending on if the request asked for lazy loaded members or
93
+ # not.
94
+ non_empty_sync_counter = Counter(
95
+ "synapse_handlers_sync_nonempty_total",
96
+ "Count of non empty sync responses. type is initial_sync/full_state_sync"
97
+ "/incremental_sync. lazy_loaded indicates if lazy loaded members were "
98
+ "enabled for that request.",
99
+ labelnames=["type", "lazy_loaded", SERVER_NAME_LABEL],
100
+ )
101
+
102
+ # Store the cache that tracks which lazy-loaded members have been sent to a given
103
+ # client for no more than 30 minutes.
104
+ LAZY_LOADED_MEMBERS_CACHE_MAX_AGE = 30 * 60 * 1000
105
+
106
+ # Remember the last 100 members we sent to a client for the purposes of
107
+ # avoiding redundantly sending the same lazy-loaded members to the client
108
+ LAZY_LOADED_MEMBERS_CACHE_MAX_SIZE = 100
109
+
110
+
111
+ SyncRequestKey = tuple[Any, ...]
112
+
113
+
114
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
115
+ class SyncConfig:
116
+ user: UserID
117
+ filter_collection: FilterCollection
118
+ is_guest: bool
119
+ device_id: Optional[str]
120
+ use_state_after: bool
121
+
122
+
123
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
124
+ class TimelineBatch:
125
+ prev_batch: StreamToken
126
+ events: Sequence[EventBase]
127
+ limited: bool
128
+ # A mapping of event ID to the bundled aggregations for the above events.
129
+ # This is only calculated if limited is true.
130
+ bundled_aggregations: Optional[dict[str, BundledAggregations]] = None
131
+
132
+ def __bool__(self) -> bool:
133
+ """Make the result appear empty if there are no updates. This is used
134
+ to tell if room needs to be part of the sync result.
135
+ """
136
+ return bool(self.events)
137
+
138
+
139
+ # We can't freeze this class, because we need to update it after it's instantiated to
140
+ # update its unread count. This is because we calculate the unread count for a room only
141
+ # if there are updates for it, which we check after the instance has been created.
142
+ # This should not be a big deal because we update the notification counts afterwards as
143
+ # well anyway.
144
+ @attr.s(slots=True, auto_attribs=True)
145
+ class JoinedSyncResult:
146
+ room_id: str
147
+ timeline: TimelineBatch
148
+ state: StateMap[EventBase]
149
+ ephemeral: list[JsonDict]
150
+ account_data: list[JsonDict]
151
+ unread_notifications: JsonDict
152
+ unread_thread_notifications: JsonDict
153
+ summary: Optional[JsonDict]
154
+ unread_count: int
155
+
156
+ def __bool__(self) -> bool:
157
+ """Make the result appear empty if there are no updates. This is used
158
+ to tell if room needs to be part of the sync result.
159
+ """
160
+ return bool(
161
+ self.timeline or self.state or self.ephemeral or self.account_data
162
+ # nb the notification count does not, er, count: if there's nothing
163
+ # else in the result, we don't need to send it.
164
+ )
165
+
166
+
167
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
168
+ class ArchivedSyncResult:
169
+ room_id: str
170
+ timeline: TimelineBatch
171
+ state: StateMap[EventBase]
172
+ account_data: list[JsonDict]
173
+
174
+ def __bool__(self) -> bool:
175
+ """Make the result appear empty if there are no updates. This is used
176
+ to tell if room needs to be part of the sync result.
177
+ """
178
+ return bool(self.timeline or self.state or self.account_data)
179
+
180
+
181
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
182
+ class InvitedSyncResult:
183
+ room_id: str
184
+ invite: EventBase
185
+
186
+ def __bool__(self) -> bool:
187
+ """Invited rooms should always be reported to the client"""
188
+ return True
189
+
190
+
191
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
192
+ class KnockedSyncResult:
193
+ room_id: str
194
+ knock: EventBase
195
+
196
+ def __bool__(self) -> bool:
197
+ """Knocked rooms should always be reported to the client"""
198
+ return True
199
+
200
+
201
+ @attr.s(slots=True, auto_attribs=True)
202
+ class _RoomChanges:
203
+ """The set of room entries to include in the sync, plus the set of joined
204
+ and left room IDs since last sync.
205
+ """
206
+
207
+ room_entries: list["RoomSyncResultBuilder"]
208
+ invited: list[InvitedSyncResult]
209
+ knocked: list[KnockedSyncResult]
210
+ newly_joined_rooms: list[str]
211
+ newly_left_rooms: list[str]
212
+
213
+
214
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
215
+ class SyncResult:
216
+ """
217
+ Attributes:
218
+ next_batch: Token for the next sync
219
+ presence: List of presence events for the user.
220
+ account_data: List of account_data events for the user.
221
+ joined: JoinedSyncResult for each joined room.
222
+ invited: InvitedSyncResult for each invited room.
223
+ knocked: KnockedSyncResult for each knocked on room.
224
+ archived: ArchivedSyncResult for each archived room.
225
+ to_device: List of direct messages for the device.
226
+ device_lists: List of user_ids whose devices have changed
227
+ device_one_time_keys_count: Dict of algorithm to count for one time keys
228
+ for this device
229
+ device_unused_fallback_key_types: List of key types that have an unused fallback
230
+ key
231
+ """
232
+
233
+ next_batch: StreamToken
234
+ presence: list[UserPresenceState]
235
+ account_data: list[JsonDict]
236
+ joined: list[JoinedSyncResult]
237
+ invited: list[InvitedSyncResult]
238
+ knocked: list[KnockedSyncResult]
239
+ archived: list[ArchivedSyncResult]
240
+ to_device: list[JsonDict]
241
+ device_lists: DeviceListUpdates
242
+ device_one_time_keys_count: JsonMapping
243
+ device_unused_fallback_key_types: list[str]
244
+
245
+ def __bool__(self) -> bool:
246
+ """Make the result appear empty if there are no updates. This is used
247
+ to tell if the notifier needs to wait for more events when polling for
248
+ events.
249
+ """
250
+ return bool(
251
+ self.presence
252
+ or self.joined
253
+ or self.invited
254
+ or self.knocked
255
+ or self.archived
256
+ or self.account_data
257
+ or self.to_device
258
+ or self.device_lists
259
+ )
260
+
261
+ @staticmethod
262
+ def empty(
263
+ next_batch: StreamToken,
264
+ device_one_time_keys_count: JsonMapping,
265
+ device_unused_fallback_key_types: list[str],
266
+ ) -> "SyncResult":
267
+ "Return a new empty result"
268
+ return SyncResult(
269
+ next_batch=next_batch,
270
+ presence=[],
271
+ account_data=[],
272
+ joined=[],
273
+ invited=[],
274
+ knocked=[],
275
+ archived=[],
276
+ to_device=[],
277
+ device_lists=DeviceListUpdates(),
278
+ device_one_time_keys_count=device_one_time_keys_count,
279
+ device_unused_fallback_key_types=device_unused_fallback_key_types,
280
+ )
281
+
282
+
283
+ class SyncHandler:
284
+ def __init__(self, hs: "HomeServer"):
285
+ self.server_name = hs.hostname
286
+ self.hs_config = hs.config
287
+ self.store = hs.get_datastores().main
288
+ self.notifier = hs.get_notifier()
289
+ self.presence_handler = hs.get_presence_handler()
290
+ self._relations_handler = hs.get_relations_handler()
291
+ self._push_rules_handler = hs.get_push_rules_handler()
292
+ self.event_sources = hs.get_event_sources()
293
+ self.clock = hs.get_clock()
294
+ self.state = hs.get_state_handler()
295
+ self.auth_blocking = hs.get_auth_blocking()
296
+ self._storage_controllers = hs.get_storage_controllers()
297
+ self._state_storage_controller = self._storage_controllers.state
298
+ self._device_handler = hs.get_device_handler()
299
+ self._task_scheduler = hs.get_task_scheduler()
300
+
301
+ self.should_calculate_push_rules = hs.config.push.enable_push
302
+
303
+ # TODO: flush cache entries on subsequent sync request.
304
+ # Once we get the next /sync request (ie, one with the same access token
305
+ # that sets 'since' to 'next_batch'), we know that device won't need a
306
+ # cached result any more, and we could flush the entry from the cache to save
307
+ # memory.
308
+ self.response_cache: ResponseCache[SyncRequestKey] = ResponseCache(
309
+ clock=hs.get_clock(),
310
+ name="sync",
311
+ server_name=self.server_name,
312
+ timeout_ms=hs.config.caches.sync_response_cache_duration,
313
+ )
314
+
315
+ # ExpiringCache((User, Device)) -> LruCache(user_id => event_id)
316
+ self.lazy_loaded_members_cache: ExpiringCache[
317
+ tuple[str, Optional[str]], LruCache[str, str]
318
+ ] = ExpiringCache(
319
+ cache_name="lazy_loaded_members_cache",
320
+ server_name=self.server_name,
321
+ hs=hs,
322
+ clock=self.clock,
323
+ max_len=0,
324
+ expiry_ms=LAZY_LOADED_MEMBERS_CACHE_MAX_AGE,
325
+ )
326
+
327
+ self.rooms_to_exclude_globally = hs.config.server.rooms_to_exclude_from_sync
328
+
329
+ async def wait_for_sync_for_user(
330
+ self,
331
+ requester: Requester,
332
+ sync_config: SyncConfig,
333
+ request_key: SyncRequestKey,
334
+ since_token: Optional[StreamToken] = None,
335
+ timeout: int = 0,
336
+ full_state: bool = False,
337
+ ) -> SyncResult:
338
+ """Get the sync for a client if we have new data for it now. Otherwise
339
+ wait for new data to arrive on the server. If the timeout expires, then
340
+ return an empty sync result.
341
+
342
+ Args:
343
+ requester: The user requesting the sync response.
344
+ sync_config: Config/info necessary to process the sync request.
345
+ sync_version: Determines what kind of sync response to generate.
346
+ request_key: The key to use for caching the response.
347
+ since_token: The point in the stream to sync from.
348
+ timeout: How long to wait for new data to arrive before giving up.
349
+ full_state: Whether to return the full state for each room.
350
+
351
+ Returns:
352
+ returns a full `SyncResult`.
353
+ """
354
+ # If the user is not part of the mau group, then check that limits have
355
+ # not been exceeded (if not part of the group by this point, almost certain
356
+ # auth_blocking will occur)
357
+ user_id = sync_config.user.to_string()
358
+ await self.auth_blocking.check_auth_blocking(requester=requester)
359
+
360
+ res = await self.response_cache.wrap(
361
+ request_key,
362
+ self._wait_for_sync_for_user,
363
+ sync_config,
364
+ since_token,
365
+ timeout,
366
+ full_state,
367
+ cache_context=True,
368
+ )
369
+ logger.debug("Returning sync response for %s", user_id)
370
+ return res
371
+
372
+ async def _wait_for_sync_for_user(
373
+ self,
374
+ sync_config: SyncConfig,
375
+ since_token: Optional[StreamToken],
376
+ timeout: int,
377
+ full_state: bool,
378
+ cache_context: ResponseCacheContext[SyncRequestKey],
379
+ ) -> SyncResult:
380
+ """The start of the machinery that produces a /sync response.
381
+
382
+ See https://spec.matrix.org/v1.1/client-server-api/#syncing for full details.
383
+
384
+ This method does high-level bookkeeping:
385
+ - tracking the kind of sync in the logging context
386
+ - deleting any to_device messages whose delivery has been acknowledged.
387
+ - deciding if we should dispatch an instant or delayed response
388
+ - marking the sync as being lazily loaded, if appropriate
389
+
390
+ Computing the body of the response begins in the next method,
391
+ `current_sync_for_user`.
392
+ """
393
+ if since_token is None:
394
+ sync_type = "initial_sync"
395
+ elif full_state:
396
+ sync_type = "full_state_sync"
397
+ else:
398
+ sync_type = "incremental_sync"
399
+
400
+ sync_label = f"sync_v2:{sync_type}"
401
+
402
+ context = current_context()
403
+ if context:
404
+ context.tag = sync_label
405
+
406
+ if since_token is not None:
407
+ # We need to make sure this worker has caught up with the token. If
408
+ # this returns false it means we timed out waiting, and we should
409
+ # just return an empty response.
410
+ start = self.clock.time_msec()
411
+ if not await self.notifier.wait_for_stream_token(since_token):
412
+ logger.warning(
413
+ "Timed out waiting for worker to catch up. Returning empty response"
414
+ )
415
+ device_id = sync_config.device_id
416
+ one_time_keys_count: JsonMapping = {}
417
+ unused_fallback_key_types: list[str] = []
418
+ if device_id:
419
+ user_id = sync_config.user.to_string()
420
+ # TODO: We should have a way to let clients differentiate between the states of:
421
+ # * no change in OTK count since the provided since token
422
+ # * the server has zero OTKs left for this device
423
+ # Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
424
+ one_time_keys_count = await self.store.count_e2e_one_time_keys(
425
+ user_id, device_id
426
+ )
427
+ unused_fallback_key_types = list(
428
+ await self.store.get_e2e_unused_fallback_key_types(
429
+ user_id, device_id
430
+ )
431
+ )
432
+
433
+ cache_context.should_cache = False # Don't cache empty responses
434
+ return SyncResult.empty(
435
+ since_token, one_time_keys_count, unused_fallback_key_types
436
+ )
437
+
438
+ # If we've spent significant time waiting to catch up, take it off
439
+ # the timeout.
440
+ now = self.clock.time_msec()
441
+ if now - start > 1_000:
442
+ timeout -= now - start
443
+ timeout = max(timeout, 0)
444
+
445
+ # if we have a since token, delete any to-device messages before that token
446
+ # (since we now know that the device has received them)
447
+ if since_token is not None:
448
+ since_stream_id = since_token.to_device_key
449
+ deleted = await self.store.delete_messages_for_device(
450
+ sync_config.user.to_string(),
451
+ sync_config.device_id,
452
+ since_stream_id,
453
+ )
454
+ logger.debug(
455
+ "Deleted %d to-device messages up to %d", deleted, since_stream_id
456
+ )
457
+
458
+ if timeout == 0 or since_token is None or full_state:
459
+ # we are going to return immediately, so don't bother calling
460
+ # notifier.wait_for_events.
461
+ result = await self.current_sync_for_user(
462
+ sync_config, since_token, full_state=full_state
463
+ )
464
+ else:
465
+ # Otherwise, we wait for something to happen and report it to the user.
466
+ async def current_sync_callback(
467
+ before_token: StreamToken, after_token: StreamToken
468
+ ) -> SyncResult:
469
+ return await self.current_sync_for_user(sync_config, since_token)
470
+
471
+ result = await self.notifier.wait_for_events(
472
+ sync_config.user.to_string(),
473
+ timeout,
474
+ current_sync_callback,
475
+ from_token=since_token,
476
+ )
477
+
478
+ # if nothing has happened in any of the users' rooms since /sync was called,
479
+ # the resultant next_batch will be the same as since_token (since the result
480
+ # is generated when wait_for_events is first called, and not regenerated
481
+ # when wait_for_events times out).
482
+ #
483
+ # If that happens, we mustn't cache it, so that when the client comes back
484
+ # with the same cache token, we don't immediately return the same empty
485
+ # result, causing a tightloop. (https://github.com/matrix-org/synapse/issues/8518)
486
+ if result.next_batch == since_token:
487
+ cache_context.should_cache = False
488
+
489
+ if result:
490
+ if sync_config.filter_collection.lazy_load_members():
491
+ lazy_loaded = "true"
492
+ else:
493
+ lazy_loaded = "false"
494
+ non_empty_sync_counter.labels(
495
+ type=sync_label,
496
+ lazy_loaded=lazy_loaded,
497
+ **{SERVER_NAME_LABEL: self.server_name},
498
+ ).inc()
499
+
500
+ return result
501
+
502
+ async def current_sync_for_user(
503
+ self,
504
+ sync_config: SyncConfig,
505
+ since_token: Optional[StreamToken] = None,
506
+ full_state: bool = False,
507
+ ) -> SyncResult:
508
+ """
509
+ Generates the response body of a sync result, represented as a
510
+ `SyncResult`.
511
+
512
+ This is a wrapper around `generate_sync_result` which starts an open tracing
513
+ span to track the sync. See `generate_sync_result` for the next part of your
514
+ indoctrination.
515
+
516
+ Args:
517
+ sync_config: Config/info necessary to process the sync request.
518
+ sync_version: Determines what kind of sync response to generate.
519
+ since_token: The point in the stream to sync from.p.
520
+ full_state: Whether to return the full state for each room.
521
+
522
+ Returns:
523
+ returns a full `SyncResult`.
524
+ """
525
+ with start_active_span("sync.current_sync_for_user"):
526
+ log_kv({"since_token": since_token})
527
+
528
+ # Go through the `/sync` v2 path
529
+ sync_result = await self.generate_sync_result(
530
+ sync_config, since_token, full_state
531
+ )
532
+
533
+ set_tag(SynapseTags.SYNC_RESULT, bool(sync_result))
534
+ return sync_result
535
+
536
+ async def ephemeral_by_room(
537
+ self,
538
+ sync_result_builder: "SyncResultBuilder",
539
+ now_token: StreamToken,
540
+ since_token: Optional[StreamToken] = None,
541
+ ) -> tuple[StreamToken, dict[str, list[JsonDict]]]:
542
+ """Get the ephemeral events for each room the user is in
543
+ Args:
544
+ sync_result_builder
545
+ now_token: Where the server is currently up to.
546
+ since_token: Where the server was when the client
547
+ last synced.
548
+ Returns:
549
+ A tuple of the now StreamToken, updated to reflect the which typing
550
+ events are included, and a dict mapping from room_id to a list of
551
+ ephemeral events for that room.
552
+ """
553
+
554
+ sync_config = sync_result_builder.sync_config
555
+
556
+ with Measure(
557
+ self.clock, name="ephemeral_by_room", server_name=self.server_name
558
+ ):
559
+ typing_key = since_token.typing_key if since_token else 0
560
+
561
+ room_ids = sync_result_builder.joined_room_ids
562
+
563
+ typing_source = self.event_sources.sources.typing
564
+ typing, typing_key = await typing_source.get_new_events(
565
+ user=sync_config.user,
566
+ from_key=typing_key,
567
+ limit=sync_config.filter_collection.ephemeral_limit(),
568
+ room_ids=room_ids,
569
+ is_guest=sync_config.is_guest,
570
+ )
571
+ now_token = now_token.copy_and_replace(StreamKeyType.TYPING, typing_key)
572
+
573
+ ephemeral_by_room: JsonDict = {}
574
+
575
+ for event in typing:
576
+ room_id = event["room_id"]
577
+ ephemeral_by_room.setdefault(room_id, []).append(event)
578
+
579
+ receipt_key = (
580
+ since_token.receipt_key
581
+ if since_token
582
+ else MultiWriterStreamToken(stream=0)
583
+ )
584
+
585
+ receipt_source = self.event_sources.sources.receipt
586
+ receipts, receipt_key = await receipt_source.get_new_events(
587
+ user=sync_config.user,
588
+ from_key=receipt_key,
589
+ limit=sync_config.filter_collection.ephemeral_limit(),
590
+ room_ids=room_ids,
591
+ is_guest=sync_config.is_guest,
592
+ )
593
+ now_token = now_token.copy_and_replace(StreamKeyType.RECEIPT, receipt_key)
594
+
595
+ for event in receipts:
596
+ room_id = event["room_id"]
597
+ ephemeral_by_room.setdefault(room_id, []).append(event)
598
+
599
+ return now_token, ephemeral_by_room
600
+
601
+ async def _load_filtered_recents(
602
+ self,
603
+ room_id: str,
604
+ sync_result_builder: "SyncResultBuilder",
605
+ sync_config: SyncConfig,
606
+ upto_token: StreamToken,
607
+ since_token: Optional[StreamToken] = None,
608
+ potential_recents: Optional[list[EventBase]] = None,
609
+ newly_joined_room: bool = False,
610
+ ) -> TimelineBatch:
611
+ """Create a timeline batch for the room
612
+
613
+ Args:
614
+ room_id
615
+ sync_result_builder
616
+ sync_config
617
+ upto_token: The token up to which we should fetch (more) events.
618
+ If `potential_results` is non-empty then this is *start* of
619
+ the the list.
620
+ since_token
621
+ potential_recents: If non-empty, the events between the since token
622
+ and current token to send down to clients.
623
+ newly_joined_room
624
+ """
625
+ with Measure(
626
+ self.clock, name="load_filtered_recents", server_name=self.server_name
627
+ ):
628
+ timeline_limit = sync_config.filter_collection.timeline_limit()
629
+ block_all_timeline = (
630
+ sync_config.filter_collection.blocks_all_room_timeline()
631
+ )
632
+
633
+ if (
634
+ potential_recents is None
635
+ or newly_joined_room
636
+ or timeline_limit < len(potential_recents)
637
+ ):
638
+ limited = True
639
+ else:
640
+ limited = False
641
+
642
+ # Check if there is a gap, if so we need to mark this as limited and
643
+ # recalculate which events to send down.
644
+ gap_token = await self.store.get_timeline_gaps(
645
+ room_id,
646
+ since_token.room_key if since_token else None,
647
+ sync_result_builder.now_token.room_key,
648
+ )
649
+ if gap_token:
650
+ # There's a gap, so we need to ignore the passed in
651
+ # `potential_recents`, and reset `upto_token` to match.
652
+ potential_recents = None
653
+ upto_token = sync_result_builder.now_token
654
+ limited = True
655
+
656
+ log_kv({"limited": limited})
657
+
658
+ if potential_recents:
659
+ recents = await sync_config.filter_collection.filter_room_timeline(
660
+ potential_recents
661
+ )
662
+ log_kv({"recents_after_sync_filtering": len(recents)})
663
+
664
+ # We check if there are any state events, if there are then we pass
665
+ # all current state events to the filter_events function. This is to
666
+ # ensure that we always include current state in the timeline
667
+ current_state_ids: frozenset[str] = frozenset()
668
+ if any(e.is_state() for e in recents):
669
+ # FIXME(faster_joins): We use the partial state here as
670
+ # we don't want to block `/sync` on finishing a lazy join.
671
+ # Which should be fine once
672
+ # https://github.com/matrix-org/synapse/issues/12989 is resolved,
673
+ # since we shouldn't reach here anymore?
674
+ # Note that we use the current state as a whitelist for filtering
675
+ # `recents`, so partial state is only a problem when a membership
676
+ # event turns up in `recents` but has not made it into the current
677
+ # state.
678
+ current_state_ids = (
679
+ await self.store.check_if_events_in_current_state(
680
+ {e.event_id for e in recents if e.is_state()}
681
+ )
682
+ )
683
+
684
+ recents = await filter_events_for_client(
685
+ self._storage_controllers,
686
+ sync_config.user.to_string(),
687
+ recents,
688
+ always_include_ids=current_state_ids,
689
+ )
690
+ log_kv({"recents_after_visibility_filtering": len(recents)})
691
+ else:
692
+ recents = []
693
+
694
+ if not limited or block_all_timeline:
695
+ prev_batch_token = upto_token
696
+ if recents:
697
+ assert recents[0].internal_metadata.stream_ordering
698
+ room_key = RoomStreamToken(
699
+ stream=recents[0].internal_metadata.stream_ordering - 1
700
+ )
701
+ prev_batch_token = upto_token.copy_and_replace(
702
+ StreamKeyType.ROOM, room_key
703
+ )
704
+
705
+ return TimelineBatch(
706
+ events=recents, prev_batch=prev_batch_token, limited=False
707
+ )
708
+
709
+ filtering_factor = 2
710
+ load_limit = max(timeline_limit * filtering_factor, 10)
711
+ max_repeat = 5 # Only try a few times per room, otherwise
712
+ room_key = upto_token.room_key
713
+ end_key = room_key
714
+
715
+ since_key = None
716
+ if since_token and gap_token:
717
+ # If there is a gap then we need to only include events after
718
+ # it.
719
+ since_key = gap_token
720
+ elif since_token and not newly_joined_room:
721
+ since_key = since_token.room_key
722
+
723
+ while limited and len(recents) < timeline_limit and max_repeat:
724
+ # For initial `/sync`, we want to view a historical section of the
725
+ # timeline; to fetch events by `topological_ordering` (best
726
+ # representation of the room DAG as others were seeing it at the time).
727
+ # This also aligns with the order that `/messages` returns events in.
728
+ #
729
+ # For incremental `/sync`, we want to get all updates for rooms since
730
+ # the last `/sync` (regardless if those updates arrived late or happened
731
+ # a while ago in the past); to fetch events by `stream_ordering` (in the
732
+ # order they were received by the server).
733
+ #
734
+ # Relevant spec issue: https://github.com/matrix-org/matrix-spec/issues/1917
735
+ #
736
+ # FIXME: Using workaround for mypy,
737
+ # https://github.com/python/mypy/issues/10740#issuecomment-1997047277 and
738
+ # https://github.com/python/mypy/issues/17479
739
+ paginate_room_events_by_topological_ordering: PaginateFunction = (
740
+ self.store.paginate_room_events_by_topological_ordering
741
+ )
742
+ paginate_room_events_by_stream_ordering: PaginateFunction = (
743
+ self.store.paginate_room_events_by_stream_ordering
744
+ )
745
+ pagination_method: PaginateFunction = (
746
+ # Use `topographical_ordering` for historical events
747
+ paginate_room_events_by_topological_ordering
748
+ if since_key is None
749
+ # Use `stream_ordering` for updates
750
+ else paginate_room_events_by_stream_ordering
751
+ )
752
+ events, end_key, limited = await pagination_method(
753
+ room_id=room_id,
754
+ # The bounds are reversed so we can paginate backwards
755
+ # (from newer to older events) starting at to_bound.
756
+ # This ensures we fill the `limit` with the newest events first,
757
+ from_key=end_key,
758
+ to_key=since_key,
759
+ direction=Direction.BACKWARDS,
760
+ limit=load_limit,
761
+ )
762
+ # We want to return the events in ascending order (the last event is the
763
+ # most recent).
764
+ events.reverse()
765
+
766
+ log_kv({"loaded_recents": len(events)})
767
+
768
+ loaded_recents = (
769
+ await sync_config.filter_collection.filter_room_timeline(events)
770
+ )
771
+
772
+ log_kv({"loaded_recents_after_sync_filtering": len(loaded_recents)})
773
+
774
+ # We check if there are any state events, if there are then we pass
775
+ # all current state events to the filter_events function. This is to
776
+ # ensure that we always include current state in the timeline
777
+ current_state_ids = frozenset()
778
+ if any(e.is_state() for e in loaded_recents):
779
+ # FIXME(faster_joins): We use the partial state here as
780
+ # we don't want to block `/sync` on finishing a lazy join.
781
+ # Which should be fine once
782
+ # https://github.com/matrix-org/synapse/issues/12989 is resolved,
783
+ # since we shouldn't reach here anymore?
784
+ # Note that we use the current state as a whitelist for filtering
785
+ # `loaded_recents`, so partial state is only a problem when a
786
+ # membership event turns up in `loaded_recents` but has not made it
787
+ # into the current state.
788
+ current_state_ids = (
789
+ await self.store.check_if_events_in_current_state(
790
+ {e.event_id for e in loaded_recents if e.is_state()}
791
+ )
792
+ )
793
+
794
+ filtered_recents = await filter_events_for_client(
795
+ self._storage_controllers,
796
+ sync_config.user.to_string(),
797
+ loaded_recents,
798
+ always_include_ids=current_state_ids,
799
+ )
800
+
801
+ loaded_recents = []
802
+ for event in filtered_recents:
803
+ if event.type == EventTypes.CallInvite:
804
+ room_info = await self.store.get_room_with_stats(event.room_id)
805
+ assert room_info is not None
806
+ if room_info.join_rules == JoinRules.PUBLIC:
807
+ continue
808
+ loaded_recents.append(event)
809
+
810
+ log_kv({"loaded_recents_after_client_filtering": len(loaded_recents)})
811
+
812
+ loaded_recents.extend(recents)
813
+ recents = loaded_recents
814
+
815
+ max_repeat -= 1
816
+
817
+ if len(recents) > timeline_limit:
818
+ limited = True
819
+ recents = recents[-timeline_limit:]
820
+ assert recents[0].internal_metadata.stream_ordering
821
+ room_key = RoomStreamToken(
822
+ stream=recents[0].internal_metadata.stream_ordering - 1
823
+ )
824
+
825
+ prev_batch_token = upto_token.copy_and_replace(StreamKeyType.ROOM, room_key)
826
+
827
+ # Don't bother to bundle aggregations if the timeline is unlimited,
828
+ # as clients will have all the necessary information.
829
+ bundled_aggregations = None
830
+ if limited or newly_joined_room:
831
+ bundled_aggregations = (
832
+ await self._relations_handler.get_bundled_aggregations(
833
+ recents, sync_config.user.to_string()
834
+ )
835
+ )
836
+
837
+ return TimelineBatch(
838
+ events=recents,
839
+ prev_batch=prev_batch_token,
840
+ # Also mark as limited if this is a new room or there has been a gap
841
+ # (to force client to paginate the gap).
842
+ limited=limited or newly_joined_room or gap_token is not None,
843
+ bundled_aggregations=bundled_aggregations,
844
+ )
845
+
846
+ async def compute_summary(
847
+ self,
848
+ room_id: str,
849
+ sync_config: SyncConfig,
850
+ batch: TimelineBatch,
851
+ state: MutableStateMap[EventBase],
852
+ now_token: StreamToken,
853
+ ) -> Optional[JsonDict]:
854
+ """Works out a room summary block for this room, summarising the number
855
+ of joined members in the room, and providing the 'hero' members if the
856
+ room has no name so clients can consistently name rooms. Also adds
857
+ state events to 'state' if needed to describe the heroes.
858
+
859
+ Args
860
+ room_id
861
+ sync_config
862
+ batch: The timeline batch for the room that will be sent to the user.
863
+ state: State as returned by compute_state_delta
864
+ now_token: Token of the end of the current batch.
865
+ """
866
+
867
+ # FIXME: we could/should get this from room_stats when matthew/stats lands
868
+
869
+ # FIXME: this promulgates https://github.com/matrix-org/synapse/issues/3305
870
+ last_events, _ = await self.store.get_recent_event_ids_for_room(
871
+ room_id, end_token=now_token.room_key, limit=1
872
+ )
873
+
874
+ if not last_events:
875
+ return None
876
+
877
+ last_event = last_events[-1]
878
+ state_ids = await self._state_storage_controller.get_state_ids_for_event(
879
+ last_event.event_id,
880
+ state_filter=StateFilter.from_types(
881
+ [(EventTypes.Name, ""), (EventTypes.CanonicalAlias, "")]
882
+ ),
883
+ )
884
+
885
+ # this is heavily cached, thus: fast.
886
+ details = await self.store.get_room_summary(room_id)
887
+
888
+ name_id = state_ids.get((EventTypes.Name, ""))
889
+ canonical_alias_id = state_ids.get((EventTypes.CanonicalAlias, ""))
890
+
891
+ summary: JsonDict = {}
892
+ empty_ms = MemberSummary([], 0)
893
+
894
+ # TODO: only send these when they change.
895
+ summary["m.joined_member_count"] = details.get(Membership.JOIN, empty_ms).count
896
+ summary["m.invited_member_count"] = details.get(
897
+ Membership.INVITE, empty_ms
898
+ ).count
899
+
900
+ # if the room has a name or canonical_alias set, we can skip
901
+ # calculating heroes. Empty strings are falsey, so we check
902
+ # for the "name" value and default to an empty string.
903
+ if name_id:
904
+ name = await self.store.get_event(name_id, allow_none=True)
905
+ if name and name.content.get("name"):
906
+ return summary
907
+
908
+ if canonical_alias_id:
909
+ canonical_alias = await self.store.get_event(
910
+ canonical_alias_id, allow_none=True
911
+ )
912
+ if canonical_alias and canonical_alias.content.get("alias"):
913
+ return summary
914
+
915
+ # FIXME: only build up a member_ids list for our heroes
916
+ member_ids = {}
917
+ for membership in (
918
+ Membership.JOIN,
919
+ Membership.INVITE,
920
+ Membership.LEAVE,
921
+ Membership.BAN,
922
+ ):
923
+ for user_id, event_id in details.get(membership, empty_ms).members:
924
+ member_ids[user_id] = event_id
925
+
926
+ me = sync_config.user.to_string()
927
+ summary["m.heroes"] = extract_heroes_from_room_summary(details, me)
928
+
929
+ if not sync_config.filter_collection.lazy_load_members():
930
+ return summary
931
+
932
+ # ensure we send membership events for heroes if needed
933
+ cache_key = (sync_config.user.to_string(), sync_config.device_id)
934
+ cache = self.get_lazy_loaded_members_cache(cache_key)
935
+
936
+ # track which members the client should already know about via LL:
937
+ # Ones which are already in state...
938
+ existing_members = {
939
+ user_id for (typ, user_id) in state.keys() if typ == EventTypes.Member
940
+ }
941
+
942
+ # ...or ones which are in the timeline...
943
+ for ev in batch.events:
944
+ if ev.type == EventTypes.Member:
945
+ existing_members.add(ev.state_key)
946
+
947
+ # ...and then ensure any missing ones get included in state.
948
+ missing_hero_event_ids = [
949
+ member_ids[hero_id]
950
+ for hero_id in summary["m.heroes"]
951
+ if (
952
+ cache.get(hero_id) != member_ids[hero_id]
953
+ and hero_id not in existing_members
954
+ )
955
+ ]
956
+
957
+ missing_hero_state = await self.store.get_events(missing_hero_event_ids)
958
+
959
+ for s in missing_hero_state.values():
960
+ cache.set(s.state_key, s.event_id)
961
+ state[(EventTypes.Member, s.state_key)] = s
962
+
963
+ return summary
964
+
965
+ def get_lazy_loaded_members_cache(
966
+ self, cache_key: tuple[str, Optional[str]]
967
+ ) -> LruCache[str, str]:
968
+ cache: Optional[LruCache[str, str]] = self.lazy_loaded_members_cache.get(
969
+ cache_key
970
+ )
971
+ if cache is None:
972
+ logger.debug("creating LruCache for %r", cache_key)
973
+ cache = LruCache(
974
+ max_size=LAZY_LOADED_MEMBERS_CACHE_MAX_SIZE,
975
+ clock=self.clock,
976
+ server_name=self.server_name,
977
+ )
978
+ self.lazy_loaded_members_cache[cache_key] = cache
979
+ else:
980
+ logger.debug("found LruCache for %r", cache_key)
981
+ return cache
982
+
983
+ async def compute_state_delta(
984
+ self,
985
+ room_id: str,
986
+ batch: TimelineBatch,
987
+ sync_config: SyncConfig,
988
+ since_token: Optional[StreamToken],
989
+ end_token: StreamToken,
990
+ full_state: bool,
991
+ joined: bool,
992
+ ) -> MutableStateMap[EventBase]:
993
+ """Works out the difference in state between the end of the previous sync and
994
+ the start of the timeline.
995
+
996
+ Args:
997
+ room_id:
998
+ batch: The timeline batch for the room that will be sent to the user.
999
+ sync_config:
1000
+ since_token: Token of the end of the previous batch. May be `None`.
1001
+ end_token: Token of the end of the current batch. Normally this will be
1002
+ the same as the global "now_token", but if the user has left the room,
1003
+ the point just after their leave event.
1004
+ full_state: Whether to force returning the full state.
1005
+ `lazy_load_members` still applies when `full_state` is `True`.
1006
+ joined: whether the user is currently joined to the room
1007
+
1008
+ Returns:
1009
+ The state to return in the sync response for the room.
1010
+
1011
+ Clients will overlay this onto the state at the end of the previous sync to
1012
+ arrive at the state at the start of the timeline.
1013
+
1014
+ Clients will then overlay state events in the timeline to arrive at the
1015
+ state at the end of the timeline, in preparation for the next sync.
1016
+ """
1017
+ # TODO(mjark) Check if the state events were received by the server
1018
+ # after the previous sync, since we need to include those state
1019
+ # updates even if they occurred logically before the previous event.
1020
+ # TODO(mjark) Check for new redactions in the state events.
1021
+
1022
+ with Measure(
1023
+ self.clock, name="compute_state_delta", server_name=self.server_name
1024
+ ):
1025
+ # The memberships needed for events in the timeline.
1026
+ # Only calculated when `lazy_load_members` is on.
1027
+ members_to_fetch: Optional[set[str]] = None
1028
+
1029
+ # A dictionary mapping user IDs to the first event in the timeline sent by
1030
+ # them. Only calculated when `lazy_load_members` is on.
1031
+ first_event_by_sender_map: Optional[dict[str, EventBase]] = None
1032
+
1033
+ # The contribution to the room state from state events in the timeline.
1034
+ # Only contains the last event for any given state key.
1035
+ timeline_state: StateMap[str]
1036
+
1037
+ lazy_load_members = sync_config.filter_collection.lazy_load_members()
1038
+ include_redundant_members = (
1039
+ sync_config.filter_collection.include_redundant_members()
1040
+ )
1041
+
1042
+ if lazy_load_members:
1043
+ # We only request state for the members needed to display the
1044
+ # timeline:
1045
+
1046
+ timeline_state = {}
1047
+
1048
+ # Membership events to fetch that can be found in the room state, or in
1049
+ # the case of partial state rooms, the auth events of timeline events.
1050
+ members_to_fetch = set()
1051
+ first_event_by_sender_map = {}
1052
+ for event in batch.events:
1053
+ # Build the map from user IDs to the first timeline event they sent.
1054
+ if event.sender not in first_event_by_sender_map:
1055
+ first_event_by_sender_map[event.sender] = event
1056
+
1057
+ # We need the event's sender, unless their membership was in a
1058
+ # previous timeline event.
1059
+ if (EventTypes.Member, event.sender) not in timeline_state:
1060
+ members_to_fetch.add(event.sender)
1061
+ # FIXME: we also care about invite targets etc.
1062
+
1063
+ if event.is_state():
1064
+ timeline_state[(event.type, event.state_key)] = event.event_id
1065
+
1066
+ else:
1067
+ timeline_state = {
1068
+ (event.type, event.state_key): event.event_id
1069
+ for event in batch.events
1070
+ if event.is_state()
1071
+ }
1072
+
1073
+ # Now calculate the state to return in the sync response for the room.
1074
+ # This is more or less the change in state between the end of the previous
1075
+ # sync's timeline and the start of the current sync's timeline.
1076
+ # See the docstring above for details.
1077
+ state_ids: StateMap[str]
1078
+ # We need to know whether the state we fetch may be partial, so check
1079
+ # whether the room is partial stated *before* fetching it.
1080
+ is_partial_state_room = await self.store.is_partial_state_room(room_id)
1081
+ if full_state:
1082
+ state_ids = await self._compute_state_delta_for_full_sync(
1083
+ room_id,
1084
+ sync_config,
1085
+ batch,
1086
+ end_token,
1087
+ members_to_fetch,
1088
+ timeline_state,
1089
+ joined,
1090
+ )
1091
+ else:
1092
+ # If this is an initial sync then full_state should be set, and
1093
+ # that case is handled above. We assert here to ensure that this
1094
+ # is indeed the case.
1095
+ assert since_token is not None
1096
+
1097
+ state_ids = await self._compute_state_delta_for_incremental_sync(
1098
+ room_id,
1099
+ sync_config,
1100
+ batch,
1101
+ since_token,
1102
+ end_token,
1103
+ members_to_fetch,
1104
+ timeline_state,
1105
+ )
1106
+
1107
+ # If we only have partial state for the room, `state_ids` may be missing the
1108
+ # memberships we wanted. We attempt to find some by digging through the auth
1109
+ # events of timeline events.
1110
+ if lazy_load_members and is_partial_state_room:
1111
+ assert members_to_fetch is not None
1112
+ assert first_event_by_sender_map is not None
1113
+
1114
+ additional_state_ids = (
1115
+ await self._find_missing_partial_state_memberships(
1116
+ room_id, members_to_fetch, first_event_by_sender_map, state_ids
1117
+ )
1118
+ )
1119
+ state_ids = {**state_ids, **additional_state_ids}
1120
+
1121
+ # At this point, if `lazy_load_members` is enabled, `state_ids` includes
1122
+ # the memberships of all event senders in the timeline. This is because we
1123
+ # may not have sent the memberships in a previous sync.
1124
+
1125
+ # When `include_redundant_members` is on, we send all the lazy-loaded
1126
+ # memberships of event senders. Otherwise we make an effort to limit the set
1127
+ # of memberships we send to those that we have not already sent to this client.
1128
+ if lazy_load_members and not include_redundant_members:
1129
+ cache_key = (sync_config.user.to_string(), sync_config.device_id)
1130
+ cache = self.get_lazy_loaded_members_cache(cache_key)
1131
+
1132
+ # if it's a new sync sequence, then assume the client has had
1133
+ # amnesia and doesn't want any recent lazy-loaded members
1134
+ # de-duplicated.
1135
+ if since_token is None:
1136
+ logger.debug("clearing LruCache for %r", cache_key)
1137
+ cache.clear()
1138
+ else:
1139
+ # only send members which aren't in our LruCache (either
1140
+ # because they're new to this client or have been pushed out
1141
+ # of the cache)
1142
+ logger.debug("filtering state from %r...", state_ids)
1143
+ state_ids = {
1144
+ t: event_id
1145
+ for t, event_id in state_ids.items()
1146
+ if cache.get(t[1]) != event_id
1147
+ }
1148
+ logger.debug("...to %r", state_ids)
1149
+
1150
+ # add any member IDs we are about to send into our LruCache
1151
+ for t, event_id in itertools.chain(
1152
+ state_ids.items(), timeline_state.items()
1153
+ ):
1154
+ if t[0] == EventTypes.Member:
1155
+ cache.set(t[1], event_id)
1156
+
1157
+ state: dict[str, EventBase] = {}
1158
+ if state_ids:
1159
+ state = await self.store.get_events(list(state_ids.values()))
1160
+
1161
+ return {
1162
+ (e.type, e.state_key): e
1163
+ for e in await sync_config.filter_collection.filter_room_state(
1164
+ list(state.values())
1165
+ )
1166
+ if e.type != EventTypes.Aliases # until MSC2261 or alternative solution
1167
+ }
1168
+
1169
+ async def _compute_state_delta_for_full_sync(
1170
+ self,
1171
+ room_id: str,
1172
+ sync_config: SyncConfig,
1173
+ batch: TimelineBatch,
1174
+ end_token: StreamToken,
1175
+ members_to_fetch: Optional[set[str]],
1176
+ timeline_state: StateMap[str],
1177
+ joined: bool,
1178
+ ) -> StateMap[str]:
1179
+ """Calculate the state events to be included in a full sync response.
1180
+
1181
+ As with `_compute_state_delta_for_incremental_sync`, the result will include
1182
+ the membership events for the senders of each event in `members_to_fetch`.
1183
+
1184
+ Note that whether this returns the state at the start or the end of the
1185
+ batch depends on `sync_config.use_state_after` (c.f. MSC4222).
1186
+
1187
+ Args:
1188
+ room_id: The room we are calculating for.
1189
+ sync_confg: The user that is calling `/sync`.
1190
+ batch: The timeline batch for the room that will be sent to the user.
1191
+ end_token: Token of the end of the current batch. Normally this will be
1192
+ the same as the global "now_token", but if the user has left the room,
1193
+ the point just after their leave event.
1194
+ members_to_fetch: If lazy-loading is enabled, the memberships needed for
1195
+ events in the timeline.
1196
+ timeline_state: The contribution to the room state from state events in
1197
+ `batch`. Only contains the last event for any given state key.
1198
+ joined: whether the user is currently joined to the room
1199
+
1200
+ Returns:
1201
+ A map from (type, state_key) to event_id, for each event that we believe
1202
+ should be included in the `state` or `state_after` part of the sync response.
1203
+ """
1204
+ if members_to_fetch is not None:
1205
+ # Lazy-loading of membership events is enabled.
1206
+ #
1207
+ # Always make sure we load our own membership event so we know if
1208
+ # we're in the room, to fix https://github.com/vector-im/riot-web/issues/7209.
1209
+ #
1210
+ # We only need apply this on full state syncs given we disabled
1211
+ # LL for incr syncs in https://github.com/matrix-org/synapse/pull/3840.
1212
+ #
1213
+ # We don't insert ourselves into `members_to_fetch`, because in some
1214
+ # rare cases (an empty event batch with a now_token after the user's
1215
+ # leave in a partial state room which another local user has
1216
+ # joined), the room state will be missing our membership and there
1217
+ # is no guarantee that our membership will be in the auth events of
1218
+ # timeline events when the room is partial stated.
1219
+ state_filter = StateFilter.from_lazy_load_member_list(
1220
+ members_to_fetch.union((sync_config.user.to_string(),))
1221
+ )
1222
+
1223
+ # We are happy to use partial state to compute the `/sync` response.
1224
+ # Since partial state may not include the lazy-loaded memberships we
1225
+ # require, we fix up the state response afterwards with memberships from
1226
+ # auth events.
1227
+ await_full_state = False
1228
+ lazy_load_members = True
1229
+ else:
1230
+ state_filter = StateFilter.all()
1231
+ await_full_state = True
1232
+ lazy_load_members = False
1233
+
1234
+ # Check if we are wanting to return the state at the start or end of the
1235
+ # timeline. If at the end we can just use the current state.
1236
+ if sync_config.use_state_after:
1237
+ # If we're getting the state at the end of the timeline, we can just
1238
+ # use the current state of the room (and roll back any changes
1239
+ # between when we fetched the current state and `end_token`).
1240
+ #
1241
+ # For rooms we're not joined to, there might be a very large number
1242
+ # of deltas between `end_token` and "now", and so instead we fetch
1243
+ # the state at the end of the timeline.
1244
+ if joined:
1245
+ state_ids = await self._state_storage_controller.get_current_state_ids(
1246
+ room_id,
1247
+ state_filter=state_filter,
1248
+ await_full_state=await_full_state,
1249
+ )
1250
+
1251
+ # Now roll back the state by looking at the state deltas between
1252
+ # end_token and now.
1253
+ deltas = await self.store.get_current_state_deltas_for_room(
1254
+ room_id,
1255
+ from_token=end_token.room_key,
1256
+ to_token=self.store.get_room_max_token(),
1257
+ )
1258
+ if deltas:
1259
+ mutable_state_ids = dict(state_ids)
1260
+
1261
+ # We iterate over the deltas backwards so that if there are
1262
+ # multiple changes of the same type/state_key we'll
1263
+ # correctly pick the earliest delta.
1264
+ for delta in reversed(deltas):
1265
+ if delta.prev_event_id:
1266
+ mutable_state_ids[(delta.event_type, delta.state_key)] = (
1267
+ delta.prev_event_id
1268
+ )
1269
+ elif (delta.event_type, delta.state_key) in mutable_state_ids:
1270
+ mutable_state_ids.pop((delta.event_type, delta.state_key))
1271
+
1272
+ state_ids = mutable_state_ids
1273
+
1274
+ return state_ids
1275
+
1276
+ else:
1277
+ # Just use state groups to get the state at the end of the
1278
+ # timeline, i.e. the state at the leave/etc event.
1279
+ state_at_timeline_end = (
1280
+ await self._state_storage_controller.get_state_ids_at(
1281
+ room_id,
1282
+ stream_position=end_token,
1283
+ state_filter=state_filter,
1284
+ await_full_state=await_full_state,
1285
+ )
1286
+ )
1287
+ return state_at_timeline_end
1288
+
1289
+ state_at_timeline_end = await self._state_storage_controller.get_state_ids_at(
1290
+ room_id,
1291
+ stream_position=end_token,
1292
+ state_filter=state_filter,
1293
+ await_full_state=await_full_state,
1294
+ )
1295
+
1296
+ if batch:
1297
+ # Strictly speaking, this returns the state *after* the first event in the
1298
+ # timeline, but that is good enough here.
1299
+ state_at_timeline_start = (
1300
+ await self._state_storage_controller.get_state_ids_for_event(
1301
+ batch.events[0].event_id,
1302
+ state_filter=state_filter,
1303
+ await_full_state=await_full_state,
1304
+ )
1305
+ )
1306
+ else:
1307
+ state_at_timeline_start = state_at_timeline_end
1308
+
1309
+ state_ids = _calculate_state(
1310
+ timeline_contains=timeline_state,
1311
+ timeline_start=state_at_timeline_start,
1312
+ timeline_end=state_at_timeline_end,
1313
+ previous_timeline_end={},
1314
+ lazy_load_members=lazy_load_members,
1315
+ )
1316
+ return state_ids
1317
+
1318
+ async def _compute_state_delta_for_incremental_sync(
1319
+ self,
1320
+ room_id: str,
1321
+ sync_config: SyncConfig,
1322
+ batch: TimelineBatch,
1323
+ since_token: StreamToken,
1324
+ end_token: StreamToken,
1325
+ members_to_fetch: Optional[set[str]],
1326
+ timeline_state: StateMap[str],
1327
+ ) -> StateMap[str]:
1328
+ """Calculate the state events to be included in an incremental sync response.
1329
+
1330
+ If lazy-loading of membership events is enabled (as indicated by
1331
+ `members_to_fetch` being not-`None`), the result will include the membership
1332
+ events for each member in `members_to_fetch`. The caller
1333
+ (`compute_state_delta`) is responsible for keeping track of which membership
1334
+ events we have already sent to the client, and hence ripping them out.
1335
+
1336
+ Note that whether this returns the state at the start or the end of the
1337
+ batch depends on `sync_config.use_state_after` (c.f. MSC4222).
1338
+
1339
+ Args:
1340
+ room_id: The room we are calculating for.
1341
+ sync_config
1342
+ batch: The timeline batch for the room that will be sent to the user.
1343
+ since_token: Token of the end of the previous batch.
1344
+ end_token: Token of the end of the current batch. Normally this will be
1345
+ the same as the global "now_token", but if the user has left the room,
1346
+ the point just after their leave event.
1347
+ members_to_fetch: If lazy-loading is enabled, the memberships needed for
1348
+ events in the timeline. Otherwise, `None`.
1349
+ timeline_state: The contribution to the room state from state events in
1350
+ `batch`. Only contains the last event for any given state key.
1351
+
1352
+ Returns:
1353
+ A map from (type, state_key) to event_id, for each event that we believe
1354
+ should be included in the `state` or `state_after` part of the sync response.
1355
+ """
1356
+ if members_to_fetch is not None:
1357
+ # Lazy-loading is enabled. Only return the state that is needed.
1358
+ state_filter = StateFilter.from_lazy_load_member_list(members_to_fetch)
1359
+ await_full_state = False
1360
+ lazy_load_members = True
1361
+ else:
1362
+ state_filter = StateFilter.all()
1363
+ await_full_state = True
1364
+ lazy_load_members = False
1365
+
1366
+ # Check if we are wanting to return the state at the start or end of the
1367
+ # timeline. If at the end we can just use the current state delta stream.
1368
+ if sync_config.use_state_after:
1369
+ delta_state_ids: MutableStateMap[str] = {}
1370
+
1371
+ if members_to_fetch:
1372
+ # We're lazy-loading, so the client might need some more member
1373
+ # events to understand the events in this timeline. So we always
1374
+ # fish out all the member events corresponding to the timeline
1375
+ # here. The caller will then dedupe any redundant ones.
1376
+ member_ids = await self._state_storage_controller.get_current_state_ids(
1377
+ room_id=room_id,
1378
+ state_filter=StateFilter.from_types(
1379
+ (EventTypes.Member, member) for member in members_to_fetch
1380
+ ),
1381
+ await_full_state=await_full_state,
1382
+ )
1383
+ delta_state_ids.update(member_ids)
1384
+
1385
+ # We don't do LL filtering for incremental syncs - see
1386
+ # https://github.com/vector-im/riot-web/issues/7211#issuecomment-419976346
1387
+ # N.B. this slows down incr syncs as we are now processing way more
1388
+ # state in the server than if we were LLing.
1389
+ #
1390
+ # i.e. we return all state deltas, including membership changes that
1391
+ # we'd normally exclude due to LL.
1392
+ deltas = await self.store.get_current_state_deltas_for_room(
1393
+ room_id=room_id,
1394
+ from_token=since_token.room_key,
1395
+ to_token=end_token.room_key,
1396
+ )
1397
+ for delta in deltas:
1398
+ if delta.event_id is None:
1399
+ # There was a state reset and this state entry is no longer
1400
+ # present, but we have no way of informing the client about
1401
+ # this, so we just skip it for now.
1402
+ continue
1403
+
1404
+ # Note that deltas are in stream ordering, so if there are
1405
+ # multiple deltas for a given type/state_key we'll always pick
1406
+ # the latest one.
1407
+ delta_state_ids[(delta.event_type, delta.state_key)] = delta.event_id
1408
+
1409
+ return delta_state_ids
1410
+
1411
+ # For a non-gappy sync if the events in the timeline are simply a linear
1412
+ # chain (i.e. no merging/branching of the graph), then we know the state
1413
+ # delta between the end of the previous sync and start of the new one is
1414
+ # empty.
1415
+ #
1416
+ # c.f. #16941 for an example of why we can't do this for all non-gappy
1417
+ # syncs.
1418
+ is_linear_timeline = True
1419
+ if batch.events:
1420
+ # We need to make sure the first event in our batch points to the
1421
+ # last event in the previous batch.
1422
+ last_event_id_prev_batch = (
1423
+ await self.store.get_last_event_id_in_room_before_stream_ordering(
1424
+ room_id,
1425
+ end_token=since_token.room_key,
1426
+ )
1427
+ )
1428
+
1429
+ prev_event_id = last_event_id_prev_batch
1430
+ for e in batch.events:
1431
+ if e.prev_event_ids() != [prev_event_id]:
1432
+ is_linear_timeline = False
1433
+ break
1434
+ prev_event_id = e.event_id
1435
+
1436
+ if is_linear_timeline and not batch.limited:
1437
+ state_ids: StateMap[str] = {}
1438
+ if lazy_load_members:
1439
+ if members_to_fetch and batch.events:
1440
+ # We're lazy-loading, so the client might need some more
1441
+ # member events to understand the events in this timeline.
1442
+ # So we fish out all the member events corresponding to the
1443
+ # timeline here. The caller will then dedupe any redundant
1444
+ # ones.
1445
+
1446
+ state_ids = (
1447
+ await self._state_storage_controller.get_state_ids_for_event(
1448
+ batch.events[0].event_id,
1449
+ # we only want members!
1450
+ state_filter=StateFilter.from_types(
1451
+ (EventTypes.Member, member)
1452
+ for member in members_to_fetch
1453
+ ),
1454
+ await_full_state=False,
1455
+ )
1456
+ )
1457
+ return state_ids
1458
+
1459
+ if batch:
1460
+ state_at_timeline_start = (
1461
+ await self._state_storage_controller.get_state_ids_for_event(
1462
+ batch.events[0].event_id,
1463
+ state_filter=state_filter,
1464
+ await_full_state=await_full_state,
1465
+ )
1466
+ )
1467
+ else:
1468
+ # We can get here if the user has ignored the senders of all
1469
+ # the recent events.
1470
+ state_at_timeline_start = (
1471
+ await self._state_storage_controller.get_state_ids_at(
1472
+ room_id,
1473
+ stream_position=end_token,
1474
+ state_filter=state_filter,
1475
+ await_full_state=await_full_state,
1476
+ )
1477
+ )
1478
+
1479
+ if batch.limited:
1480
+ # for now, we disable LL for gappy syncs - see
1481
+ # https://github.com/vector-im/riot-web/issues/7211#issuecomment-419976346
1482
+ # N.B. this slows down incr syncs as we are now processing way
1483
+ # more state in the server than if we were LLing.
1484
+ #
1485
+ # We still have to filter timeline_start to LL entries (above) in order
1486
+ # for _calculate_state's LL logic to work, as we have to include LL
1487
+ # members for timeline senders in case they weren't loaded in the initial
1488
+ # sync. We do this by (counterintuitively) by filtering timeline_start
1489
+ # members to just be ones which were timeline senders, which then ensures
1490
+ # all of the rest get included in the state block (if we need to know
1491
+ # about them).
1492
+ state_filter = StateFilter.all()
1493
+
1494
+ state_at_previous_sync = await self._state_storage_controller.get_state_ids_at(
1495
+ room_id,
1496
+ stream_position=since_token,
1497
+ state_filter=state_filter,
1498
+ await_full_state=await_full_state,
1499
+ )
1500
+
1501
+ state_at_timeline_end = await self._state_storage_controller.get_state_ids_at(
1502
+ room_id,
1503
+ stream_position=end_token,
1504
+ state_filter=state_filter,
1505
+ await_full_state=await_full_state,
1506
+ )
1507
+
1508
+ state_ids = _calculate_state(
1509
+ timeline_contains=timeline_state,
1510
+ timeline_start=state_at_timeline_start,
1511
+ timeline_end=state_at_timeline_end,
1512
+ previous_timeline_end=state_at_previous_sync,
1513
+ lazy_load_members=lazy_load_members,
1514
+ )
1515
+
1516
+ return state_ids
1517
+
1518
+ async def _find_missing_partial_state_memberships(
1519
+ self,
1520
+ room_id: str,
1521
+ members_to_fetch: StrCollection,
1522
+ events_with_membership_auth: Mapping[str, EventBase],
1523
+ found_state_ids: StateMap[str],
1524
+ ) -> StateMap[str]:
1525
+ """Finds missing memberships from a set of auth events and returns them as a
1526
+ state map.
1527
+
1528
+ Args:
1529
+ room_id: The partial state room to find the remaining memberships for.
1530
+ members_to_fetch: The memberships to find.
1531
+ events_with_membership_auth: A mapping from user IDs to events whose auth
1532
+ events would contain their prior membership, if one exists.
1533
+ Note that join events will not cite a prior membership if a user has
1534
+ never been in a room before.
1535
+ found_state_ids: A dict from (type, state_key) -> state_event_id, containing
1536
+ memberships that have been previously found. Entries in
1537
+ `members_to_fetch` that have a membership in `found_state_ids` are
1538
+ ignored.
1539
+
1540
+ Returns:
1541
+ A dict from ("m.room.member", state_key) -> state_event_id, containing the
1542
+ memberships missing from `found_state_ids`.
1543
+
1544
+ When `events_with_membership_auth` contains a join event for a given user
1545
+ which does not cite a prior membership, no membership is returned for that
1546
+ user.
1547
+
1548
+ Raises:
1549
+ KeyError: if `events_with_membership_auth` does not have an entry for a
1550
+ missing membership. Memberships in `found_state_ids` do not need an
1551
+ entry in `events_with_membership_auth`.
1552
+ """
1553
+ additional_state_ids: MutableStateMap[str] = {}
1554
+
1555
+ # Tracks the missing members for logging purposes.
1556
+ missing_members = set()
1557
+
1558
+ # Identify memberships missing from `found_state_ids` and pick out the auth
1559
+ # events in which to look for them.
1560
+ auth_event_ids: set[str] = set()
1561
+ for member in members_to_fetch:
1562
+ if (EventTypes.Member, member) in found_state_ids:
1563
+ continue
1564
+
1565
+ event_with_membership_auth = events_with_membership_auth[member]
1566
+ is_create = (
1567
+ event_with_membership_auth.is_state()
1568
+ and event_with_membership_auth.type == EventTypes.Create
1569
+ )
1570
+ is_join = (
1571
+ event_with_membership_auth.is_state()
1572
+ and event_with_membership_auth.type == EventTypes.Member
1573
+ and event_with_membership_auth.state_key == member
1574
+ and event_with_membership_auth.content.get("membership")
1575
+ == Membership.JOIN
1576
+ )
1577
+ if not is_create and not is_join:
1578
+ # The event must include the desired membership as an auth event, unless
1579
+ # it's the `m.room.create` event for a room or the first join event for
1580
+ # a given user.
1581
+ missing_members.add(member)
1582
+ auth_event_ids.update(event_with_membership_auth.auth_event_ids())
1583
+
1584
+ auth_events = await self.store.get_events(auth_event_ids)
1585
+
1586
+ # Run through the missing memberships once more, picking out the memberships
1587
+ # from the pile of auth events we have just fetched.
1588
+ for member in members_to_fetch:
1589
+ if (EventTypes.Member, member) in found_state_ids:
1590
+ continue
1591
+
1592
+ event_with_membership_auth = events_with_membership_auth[member]
1593
+
1594
+ # Dig through the auth events to find the desired membership.
1595
+ for auth_event_id in event_with_membership_auth.auth_event_ids():
1596
+ # We only store events once we have all their auth events,
1597
+ # so the auth event must be in the pile we have just
1598
+ # fetched.
1599
+ auth_event = auth_events[auth_event_id]
1600
+
1601
+ if (
1602
+ auth_event.type == EventTypes.Member
1603
+ and auth_event.state_key == member
1604
+ ):
1605
+ missing_members.discard(member)
1606
+ additional_state_ids[(EventTypes.Member, member)] = (
1607
+ auth_event.event_id
1608
+ )
1609
+ break
1610
+
1611
+ if missing_members:
1612
+ # There really shouldn't be any missing memberships now. Either:
1613
+ # * we couldn't find an auth event, which shouldn't happen because we do
1614
+ # not persist events with persisting their auth events first, or
1615
+ # * the set of auth events did not contain a membership we wanted, which
1616
+ # means our caller didn't compute the events in `members_to_fetch`
1617
+ # correctly, or we somehow accepted an event whose auth events were
1618
+ # dodgy.
1619
+ logger.error(
1620
+ "Failed to find memberships for %s in partial state room "
1621
+ "%s in the auth events of %s.",
1622
+ missing_members,
1623
+ room_id,
1624
+ [
1625
+ events_with_membership_auth[member].event_id
1626
+ for member in missing_members
1627
+ ],
1628
+ )
1629
+
1630
+ return additional_state_ids
1631
+
1632
+ async def unread_notifs_for_room_id(
1633
+ self, room_id: str, sync_config: SyncConfig
1634
+ ) -> RoomNotifCounts:
1635
+ if not self.should_calculate_push_rules:
1636
+ # If push rules have been universally disabled then we know we won't
1637
+ # have any unread counts in the DB, so we may as well skip asking
1638
+ # the DB.
1639
+ return RoomNotifCounts.empty()
1640
+
1641
+ with Measure(
1642
+ self.clock, name="unread_notifs_for_room_id", server_name=self.server_name
1643
+ ):
1644
+ return await self.store.get_unread_event_push_actions_by_room_for_user(
1645
+ room_id,
1646
+ sync_config.user.to_string(),
1647
+ )
1648
+
1649
+ async def generate_sync_result(
1650
+ self,
1651
+ sync_config: SyncConfig,
1652
+ since_token: Optional[StreamToken] = None,
1653
+ full_state: bool = False,
1654
+ ) -> SyncResult:
1655
+ """Generates the response body of a sync result.
1656
+
1657
+ This is represented by a `SyncResult` struct, which is built from small pieces
1658
+ using a `SyncResultBuilder`. See also
1659
+ https://spec.matrix.org/v1.1/client-server-api/#get_matrixclientv3sync
1660
+ the `sync_result_builder` is passed as a mutable ("inout") parameter to various
1661
+ helper functions. These retrieve and process the data which forms the sync body,
1662
+ often writing to the `sync_result_builder` to store their output.
1663
+
1664
+ At the end, we transfer data from the `sync_result_builder` to a new `SyncResult`
1665
+ instance to signify that the sync calculation is complete.
1666
+ """
1667
+
1668
+ user_id = sync_config.user.to_string()
1669
+ app_service = self.store.get_app_service_by_user_id(user_id)
1670
+ if app_service:
1671
+ # We no longer support AS users using /sync directly.
1672
+ # See https://github.com/matrix-org/matrix-doc/issues/1144
1673
+ raise NotImplementedError()
1674
+
1675
+ sync_result_builder = await self.get_sync_result_builder(
1676
+ sync_config,
1677
+ since_token,
1678
+ full_state,
1679
+ )
1680
+
1681
+ logger.debug(
1682
+ "Calculating sync response for %r between %s and %s",
1683
+ sync_config.user,
1684
+ sync_result_builder.since_token,
1685
+ sync_result_builder.now_token,
1686
+ )
1687
+
1688
+ logger.debug("Fetching account data")
1689
+
1690
+ # Global account data is included if it is not filtered out.
1691
+ if not sync_config.filter_collection.blocks_all_global_account_data():
1692
+ await self._generate_sync_entry_for_account_data(sync_result_builder)
1693
+
1694
+ # Presence data is included if the server has it enabled and not filtered out.
1695
+ include_presence_data = bool(
1696
+ self.hs_config.server.presence_enabled
1697
+ and not sync_config.filter_collection.blocks_all_presence()
1698
+ )
1699
+ # Device list updates are sent if a since token is provided.
1700
+ include_device_list_updates = bool(since_token and since_token.device_list_key)
1701
+
1702
+ # If we do not care about the rooms or things which depend on the room
1703
+ # data (namely presence and device list updates), then we can skip
1704
+ # this process completely.
1705
+ device_lists = DeviceListUpdates()
1706
+ if (
1707
+ not sync_result_builder.sync_config.filter_collection.blocks_all_rooms()
1708
+ or include_presence_data
1709
+ or include_device_list_updates
1710
+ ):
1711
+ logger.debug("Fetching room data")
1712
+
1713
+ # Note that _generate_sync_entry_for_rooms sets sync_result_builder.joined, which
1714
+ # is used in calculate_user_changes below.
1715
+ (
1716
+ newly_joined_rooms,
1717
+ newly_left_rooms,
1718
+ ) = await self._generate_sync_entry_for_rooms(sync_result_builder)
1719
+
1720
+ # Work out which users have joined or left rooms we're in. We use this
1721
+ # to build the presence and device_list parts of the sync response in
1722
+ # `_generate_sync_entry_for_presence` and
1723
+ # `_generate_sync_entry_for_device_list` respectively.
1724
+ if include_presence_data or include_device_list_updates:
1725
+ # This uses the sync_result_builder.joined which is set in
1726
+ # `_generate_sync_entry_for_rooms`, if that didn't find any joined
1727
+ # rooms for some reason it is a no-op.
1728
+ (
1729
+ newly_joined_or_invited_or_knocked_users,
1730
+ newly_left_users,
1731
+ ) = sync_result_builder.calculate_user_changes()
1732
+
1733
+ if include_presence_data:
1734
+ logger.debug("Fetching presence data")
1735
+ await self._generate_sync_entry_for_presence(
1736
+ sync_result_builder,
1737
+ newly_joined_rooms,
1738
+ newly_joined_or_invited_or_knocked_users,
1739
+ )
1740
+
1741
+ if include_device_list_updates:
1742
+ # include_device_list_updates can only be True if we have a
1743
+ # since token.
1744
+ assert since_token is not None
1745
+
1746
+ device_lists = await self._device_handler.generate_sync_entry_for_device_list(
1747
+ user_id=user_id,
1748
+ since_token=since_token,
1749
+ now_token=sync_result_builder.now_token,
1750
+ joined_room_ids=sync_result_builder.joined_room_ids,
1751
+ newly_joined_rooms=newly_joined_rooms,
1752
+ newly_joined_or_invited_or_knocked_users=newly_joined_or_invited_or_knocked_users,
1753
+ newly_left_rooms=newly_left_rooms,
1754
+ newly_left_users=newly_left_users,
1755
+ )
1756
+
1757
+ logger.debug("Fetching to-device data")
1758
+ await self._generate_sync_entry_for_to_device(sync_result_builder)
1759
+
1760
+ logger.debug("Fetching OTK data")
1761
+ device_id = sync_config.device_id
1762
+ one_time_keys_count: JsonMapping = {}
1763
+ unused_fallback_key_types: list[str] = []
1764
+ if device_id:
1765
+ # TODO: We should have a way to let clients differentiate between the states of:
1766
+ # * no change in OTK count since the provided since token
1767
+ # * the server has zero OTKs left for this device
1768
+ # Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
1769
+ one_time_keys_count = await self.store.count_e2e_one_time_keys(
1770
+ user_id, device_id
1771
+ )
1772
+ unused_fallback_key_types = list(
1773
+ await self.store.get_e2e_unused_fallback_key_types(user_id, device_id)
1774
+ )
1775
+
1776
+ num_events = 0
1777
+
1778
+ # debug for https://github.com/matrix-org/synapse/issues/9424
1779
+ for joined_room in sync_result_builder.joined:
1780
+ num_events += len(joined_room.timeline.events)
1781
+
1782
+ log_kv(
1783
+ {
1784
+ "joined_rooms_in_result": len(sync_result_builder.joined),
1785
+ "events_in_result": num_events,
1786
+ }
1787
+ )
1788
+
1789
+ logger.debug("Sync response calculation complete")
1790
+ return SyncResult(
1791
+ presence=sync_result_builder.presence,
1792
+ account_data=sync_result_builder.account_data,
1793
+ joined=sync_result_builder.joined,
1794
+ invited=sync_result_builder.invited,
1795
+ knocked=sync_result_builder.knocked,
1796
+ archived=sync_result_builder.archived,
1797
+ to_device=sync_result_builder.to_device,
1798
+ device_lists=device_lists,
1799
+ device_one_time_keys_count=one_time_keys_count,
1800
+ device_unused_fallback_key_types=unused_fallback_key_types,
1801
+ next_batch=sync_result_builder.now_token,
1802
+ )
1803
+
1804
+ async def get_sync_result_builder(
1805
+ self,
1806
+ sync_config: SyncConfig,
1807
+ since_token: Optional[StreamToken] = None,
1808
+ full_state: bool = False,
1809
+ ) -> "SyncResultBuilder":
1810
+ """
1811
+ Assemble a `SyncResultBuilder` with all of the initial context to
1812
+ start building up the sync response:
1813
+
1814
+ - Membership changes between the last sync and the current sync.
1815
+ - Joined room IDs (minus any rooms to exclude).
1816
+ - Rooms that became fully-stated/un-partial stated since the last sync.
1817
+
1818
+ Args:
1819
+ sync_config: Config/info necessary to process the sync request.
1820
+ since_token: The point in the stream to sync from.
1821
+ full_state: Whether to return the full state for each room.
1822
+
1823
+ Returns:
1824
+ `SyncResultBuilder` ready to start generating parts of the sync response.
1825
+ """
1826
+ user_id = sync_config.user.to_string()
1827
+
1828
+ # Note: we get the users room list *before* we get the `now_token`, this
1829
+ # avoids checking back in history if rooms are joined after the token is fetched.
1830
+ token_before_rooms = self.event_sources.get_current_token()
1831
+ mutable_joined_room_ids = set(await self.store.get_rooms_for_user(user_id))
1832
+
1833
+ # NB: The `now_token` gets changed by some of the `generate_sync_*` methods,
1834
+ # this is due to some of the underlying streams not supporting the ability
1835
+ # to query up to a given point.
1836
+ # Always use the `now_token` in `SyncResultBuilder`
1837
+ now_token = self.event_sources.get_current_token()
1838
+ log_kv({"now_token": now_token})
1839
+
1840
+ # Since we fetched the users room list before calculating the `now_token` (see
1841
+ # above), there's a small window during which membership events may have been
1842
+ # persisted, so we fetch these now and modify the joined room list for any
1843
+ # changes between the get_rooms_for_user call and the get_current_token call.
1844
+ membership_change_events = []
1845
+ if since_token:
1846
+ membership_change_events = await self.store.get_membership_changes_for_user(
1847
+ user_id,
1848
+ since_token.room_key,
1849
+ now_token.room_key,
1850
+ self.rooms_to_exclude_globally,
1851
+ )
1852
+
1853
+ last_membership_change_by_room_id: dict[str, EventBase] = {}
1854
+ for event in membership_change_events:
1855
+ last_membership_change_by_room_id[event.room_id] = event
1856
+
1857
+ # For the latest membership event in each room found, add/remove the room ID
1858
+ # from the joined room list accordingly. In this case we only care if the
1859
+ # latest change is JOIN.
1860
+
1861
+ for room_id, event in last_membership_change_by_room_id.items():
1862
+ assert event.internal_metadata.stream_ordering
1863
+ # As a shortcut, skip any events that happened before we got our
1864
+ # `get_rooms_for_user()` snapshot (any changes are already represented
1865
+ # in that list).
1866
+ if (
1867
+ event.internal_metadata.stream_ordering
1868
+ < token_before_rooms.room_key.stream
1869
+ ):
1870
+ continue
1871
+
1872
+ logger.info(
1873
+ "User membership change between getting rooms and current token: %s %s %s",
1874
+ user_id,
1875
+ event.membership,
1876
+ room_id,
1877
+ )
1878
+ # User joined a room - we have to then check the room state to ensure we
1879
+ # respect any bans if there's a race between the join and ban events.
1880
+ if event.membership == Membership.JOIN:
1881
+ user_ids_in_room = await self.store.get_users_in_room(room_id)
1882
+ if user_id in user_ids_in_room:
1883
+ mutable_joined_room_ids.add(room_id)
1884
+ # The user left the room, or left and was re-invited but not joined yet
1885
+ else:
1886
+ mutable_joined_room_ids.discard(room_id)
1887
+
1888
+ # Tweak the set of rooms to return to the client for eager (non-lazy) syncs.
1889
+ mutable_rooms_to_exclude = set(self.rooms_to_exclude_globally)
1890
+ if not sync_config.filter_collection.lazy_load_members():
1891
+ # Non-lazy syncs should never include partially stated rooms.
1892
+ # Exclude all partially stated rooms from this sync.
1893
+ results = await self.store.is_partial_state_room_batched(
1894
+ mutable_joined_room_ids
1895
+ )
1896
+ mutable_rooms_to_exclude.update(
1897
+ room_id
1898
+ for room_id, is_partial_state in results.items()
1899
+ if is_partial_state
1900
+ )
1901
+ membership_change_events = [
1902
+ event
1903
+ for event in membership_change_events
1904
+ if not results.get(event.room_id, False)
1905
+ ]
1906
+
1907
+ # Incremental eager syncs should additionally include rooms that
1908
+ # - we are joined to
1909
+ # - are full-stated
1910
+ # - became fully-stated at some point during the sync period
1911
+ # (These rooms will have been omitted during a previous eager sync.)
1912
+ forced_newly_joined_room_ids: set[str] = set()
1913
+ if since_token and not sync_config.filter_collection.lazy_load_members():
1914
+ un_partial_stated_rooms = (
1915
+ await self.store.get_un_partial_stated_rooms_between(
1916
+ since_token.un_partial_stated_rooms_key,
1917
+ now_token.un_partial_stated_rooms_key,
1918
+ mutable_joined_room_ids,
1919
+ )
1920
+ )
1921
+ results = await self.store.is_partial_state_room_batched(
1922
+ un_partial_stated_rooms
1923
+ )
1924
+ forced_newly_joined_room_ids.update(
1925
+ room_id
1926
+ for room_id, is_partial_state in results.items()
1927
+ if not is_partial_state
1928
+ )
1929
+
1930
+ # Now we have our list of joined room IDs, exclude as configured and freeze
1931
+ joined_room_ids = frozenset(
1932
+ room_id
1933
+ for room_id in mutable_joined_room_ids
1934
+ if room_id not in mutable_rooms_to_exclude
1935
+ )
1936
+
1937
+ sync_result_builder = SyncResultBuilder(
1938
+ sync_config,
1939
+ full_state,
1940
+ since_token=since_token,
1941
+ now_token=now_token,
1942
+ joined_room_ids=joined_room_ids,
1943
+ excluded_room_ids=frozenset(mutable_rooms_to_exclude),
1944
+ forced_newly_joined_room_ids=frozenset(forced_newly_joined_room_ids),
1945
+ membership_change_events=membership_change_events,
1946
+ )
1947
+
1948
+ return sync_result_builder
1949
+
1950
+ @trace
1951
+ async def _generate_sync_entry_for_to_device(
1952
+ self, sync_result_builder: "SyncResultBuilder"
1953
+ ) -> None:
1954
+ """Generates the portion of the sync response. Populates
1955
+ `sync_result_builder` with the result.
1956
+ """
1957
+ user_id = sync_result_builder.sync_config.user.to_string()
1958
+ device_id = sync_result_builder.sync_config.device_id
1959
+ now_token = sync_result_builder.now_token
1960
+ since_stream_id = 0
1961
+ if sync_result_builder.since_token is not None:
1962
+ since_stream_id = int(sync_result_builder.since_token.to_device_key)
1963
+
1964
+ if device_id is not None and since_stream_id != int(now_token.to_device_key):
1965
+ messages, stream_id = await self.store.get_messages_for_device(
1966
+ user_id, device_id, since_stream_id, now_token.to_device_key
1967
+ )
1968
+
1969
+ for message in messages:
1970
+ log_kv(
1971
+ {
1972
+ "event": "to_device_message",
1973
+ "sender": message["sender"],
1974
+ "type": message["type"],
1975
+ EventContentFields.TO_DEVICE_MSGID: message["content"].get(
1976
+ EventContentFields.TO_DEVICE_MSGID
1977
+ ),
1978
+ }
1979
+ )
1980
+
1981
+ if messages and issue9533_logger.isEnabledFor(logging.DEBUG):
1982
+ issue9533_logger.debug(
1983
+ "Returning to-device messages with stream_ids (%d, %d]; now: %d;"
1984
+ " msgids: %s",
1985
+ since_stream_id,
1986
+ stream_id,
1987
+ now_token.to_device_key,
1988
+ [
1989
+ message["content"].get(EventContentFields.TO_DEVICE_MSGID)
1990
+ for message in messages
1991
+ ],
1992
+ )
1993
+ sync_result_builder.now_token = now_token.copy_and_replace(
1994
+ StreamKeyType.TO_DEVICE, stream_id
1995
+ )
1996
+ sync_result_builder.to_device = messages
1997
+ else:
1998
+ sync_result_builder.to_device = []
1999
+
2000
+ async def _generate_sync_entry_for_account_data(
2001
+ self, sync_result_builder: "SyncResultBuilder"
2002
+ ) -> None:
2003
+ """Generates the global account data portion of the sync response.
2004
+
2005
+ Account data (called "Client Config" in the spec) can be set either globally
2006
+ or for a specific room. Account data consists of a list of events which
2007
+ accumulate state, much like a room.
2008
+
2009
+ This function retrieves global account data and writes it to the given
2010
+ `sync_result_builder`. See `_generate_sync_entry_for_rooms` for handling
2011
+ of per-room account data.
2012
+
2013
+ Args:
2014
+ sync_result_builder
2015
+ """
2016
+ sync_config = sync_result_builder.sync_config
2017
+ user_id = sync_result_builder.sync_config.user.to_string()
2018
+ since_token = sync_result_builder.since_token
2019
+
2020
+ if since_token and not sync_result_builder.full_state:
2021
+ global_account_data = (
2022
+ await self.store.get_updated_global_account_data_for_user(
2023
+ user_id, since_token.account_data_key
2024
+ )
2025
+ )
2026
+
2027
+ push_rules_changed = await self.store.have_push_rules_changed_for_user(
2028
+ user_id, int(since_token.push_rules_key)
2029
+ )
2030
+
2031
+ if push_rules_changed:
2032
+ global_account_data = dict(global_account_data)
2033
+ global_account_data[
2034
+ AccountDataTypes.PUSH_RULES
2035
+ ] = await self._push_rules_handler.push_rules_for_user(sync_config.user)
2036
+ else:
2037
+ all_global_account_data = await self.store.get_global_account_data_for_user(
2038
+ user_id
2039
+ )
2040
+
2041
+ global_account_data = dict(all_global_account_data)
2042
+ global_account_data[
2043
+ AccountDataTypes.PUSH_RULES
2044
+ ] = await self._push_rules_handler.push_rules_for_user(sync_config.user)
2045
+
2046
+ account_data_for_user = (
2047
+ await sync_config.filter_collection.filter_global_account_data(
2048
+ [
2049
+ {"type": account_data_type, "content": content}
2050
+ for account_data_type, content in global_account_data.items()
2051
+ ]
2052
+ )
2053
+ )
2054
+
2055
+ sync_result_builder.account_data = account_data_for_user
2056
+
2057
+ async def _generate_sync_entry_for_presence(
2058
+ self,
2059
+ sync_result_builder: "SyncResultBuilder",
2060
+ newly_joined_rooms: AbstractSet[str],
2061
+ newly_joined_or_invited_users: AbstractSet[str],
2062
+ ) -> None:
2063
+ """Generates the presence portion of the sync response. Populates the
2064
+ `sync_result_builder` with the result.
2065
+
2066
+ Args:
2067
+ sync_result_builder
2068
+ newly_joined_rooms: Set of rooms that the user has joined since
2069
+ the last sync (or empty if an initial sync)
2070
+ newly_joined_or_invited_users: Set of users that have joined or
2071
+ been invited to rooms since the last sync (or empty if an
2072
+ initial sync)
2073
+ """
2074
+ now_token = sync_result_builder.now_token
2075
+ sync_config = sync_result_builder.sync_config
2076
+ user = sync_result_builder.sync_config.user
2077
+
2078
+ presence_source = self.event_sources.sources.presence
2079
+
2080
+ since_token = sync_result_builder.since_token
2081
+ presence_key = None
2082
+ include_offline = False
2083
+ if since_token and not sync_result_builder.full_state:
2084
+ presence_key = since_token.presence_key
2085
+ include_offline = True
2086
+
2087
+ presence, presence_key = await presence_source.get_new_events(
2088
+ user=user,
2089
+ from_key=presence_key,
2090
+ is_guest=sync_config.is_guest,
2091
+ include_offline=(
2092
+ True
2093
+ if self.hs_config.server.presence_include_offline_users_on_sync
2094
+ else include_offline
2095
+ ),
2096
+ )
2097
+ assert presence_key
2098
+ sync_result_builder.now_token = now_token.copy_and_replace(
2099
+ StreamKeyType.PRESENCE, presence_key
2100
+ )
2101
+
2102
+ extra_users_ids = set(newly_joined_or_invited_users)
2103
+ for room_id in newly_joined_rooms:
2104
+ users = await self.store.get_users_in_room(room_id)
2105
+ extra_users_ids.update(users)
2106
+ extra_users_ids.discard(user.to_string())
2107
+
2108
+ if extra_users_ids:
2109
+ states = await self.presence_handler.get_states(extra_users_ids)
2110
+ presence.extend(states)
2111
+
2112
+ # Deduplicate the presence entries so that there's at most one per user
2113
+ presence = list({p.user_id: p for p in presence}.values())
2114
+
2115
+ presence = await sync_config.filter_collection.filter_presence(presence)
2116
+
2117
+ sync_result_builder.presence = presence
2118
+
2119
+ async def _generate_sync_entry_for_rooms(
2120
+ self, sync_result_builder: "SyncResultBuilder"
2121
+ ) -> tuple[AbstractSet[str], AbstractSet[str]]:
2122
+ """Generates the rooms portion of the sync response. Populates the
2123
+ `sync_result_builder` with the result.
2124
+
2125
+ In the response that reaches the client, rooms are divided into four categories:
2126
+ `invite`, `join`, `knock`, `leave`. These aren't the same as the four sets of
2127
+ room ids returned by this function.
2128
+
2129
+ Args:
2130
+ sync_result_builder
2131
+
2132
+ Returns:
2133
+ Returns a 2-tuple describing rooms the user has joined or left.
2134
+
2135
+ Its entries are:
2136
+ - newly_joined_rooms
2137
+ - newly_left_rooms
2138
+ """
2139
+
2140
+ since_token = sync_result_builder.since_token
2141
+ user_id = sync_result_builder.sync_config.user.to_string()
2142
+
2143
+ blocks_all_rooms = (
2144
+ sync_result_builder.sync_config.filter_collection.blocks_all_rooms()
2145
+ )
2146
+
2147
+ # 0. Start by fetching room account data (if required).
2148
+ if (
2149
+ blocks_all_rooms
2150
+ or sync_result_builder.sync_config.filter_collection.blocks_all_room_account_data()
2151
+ ):
2152
+ account_data_by_room: Mapping[str, Mapping[str, JsonMapping]] = {}
2153
+ elif since_token and not sync_result_builder.full_state:
2154
+ account_data_by_room = (
2155
+ await self.store.get_updated_room_account_data_for_user(
2156
+ user_id, since_token.account_data_key
2157
+ )
2158
+ )
2159
+ else:
2160
+ account_data_by_room = await self.store.get_room_account_data_for_user(
2161
+ user_id
2162
+ )
2163
+
2164
+ # 1. Start by fetching all ephemeral events in rooms we've joined (if required).
2165
+ block_all_room_ephemeral = (
2166
+ blocks_all_rooms
2167
+ or sync_result_builder.sync_config.filter_collection.blocks_all_room_ephemeral()
2168
+ )
2169
+ if block_all_room_ephemeral:
2170
+ ephemeral_by_room: dict[str, list[JsonDict]] = {}
2171
+ else:
2172
+ now_token, ephemeral_by_room = await self.ephemeral_by_room(
2173
+ sync_result_builder,
2174
+ now_token=sync_result_builder.now_token,
2175
+ since_token=sync_result_builder.since_token,
2176
+ )
2177
+ sync_result_builder.now_token = now_token
2178
+
2179
+ # 2. We check up front if anything has changed, if it hasn't then there is
2180
+ # no point in going further.
2181
+ if not sync_result_builder.full_state:
2182
+ if since_token and not ephemeral_by_room and not account_data_by_room:
2183
+ have_changed = await self._have_rooms_changed(sync_result_builder)
2184
+ log_kv({"rooms_have_changed": have_changed})
2185
+ if not have_changed:
2186
+ tags_by_room = await self.store.get_updated_tags(
2187
+ user_id, since_token.account_data_key
2188
+ )
2189
+ if not tags_by_room:
2190
+ logger.debug("no-oping sync")
2191
+ return set(), set()
2192
+
2193
+ # 3. Work out which rooms need reporting in the sync response.
2194
+ ignored_users = await self.store.ignored_users(user_id)
2195
+ if since_token:
2196
+ room_changes = await self._get_room_changes_for_incremental_sync(
2197
+ sync_result_builder, ignored_users
2198
+ )
2199
+ tags_by_room = await self.store.get_updated_tags(
2200
+ user_id, since_token.account_data_key
2201
+ )
2202
+ else:
2203
+ room_changes = await self._get_room_changes_for_initial_sync(
2204
+ sync_result_builder, ignored_users
2205
+ )
2206
+ tags_by_room = await self.store.get_tags_for_user(user_id)
2207
+
2208
+ log_kv({"rooms_changed": len(room_changes.room_entries)})
2209
+
2210
+ room_entries = room_changes.room_entries
2211
+ invited = room_changes.invited
2212
+ knocked = room_changes.knocked
2213
+ newly_joined_rooms = room_changes.newly_joined_rooms
2214
+ newly_left_rooms = room_changes.newly_left_rooms
2215
+
2216
+ # 4. We need to apply further processing to `room_entries` (rooms considered
2217
+ # joined or archived).
2218
+ async def handle_room_entries(room_entry: "RoomSyncResultBuilder") -> None:
2219
+ logger.debug("Generating room entry for %s", room_entry.room_id)
2220
+ # Note that this mutates sync_result_builder.{joined,archived}.
2221
+ await self._generate_room_entry(
2222
+ sync_result_builder,
2223
+ room_entry,
2224
+ ephemeral=ephemeral_by_room.get(room_entry.room_id, []),
2225
+ tags=tags_by_room.get(room_entry.room_id),
2226
+ account_data=account_data_by_room.get(room_entry.room_id, {}),
2227
+ always_include=sync_result_builder.full_state,
2228
+ )
2229
+ logger.debug("Generated room entry for %s", room_entry.room_id)
2230
+
2231
+ with start_active_span("sync.generate_room_entries"):
2232
+ await concurrently_execute(handle_room_entries, room_entries, 10)
2233
+
2234
+ sync_result_builder.invited.extend(invited)
2235
+ sync_result_builder.knocked.extend(knocked)
2236
+
2237
+ return set(newly_joined_rooms), set(newly_left_rooms)
2238
+
2239
+ async def _have_rooms_changed(
2240
+ self, sync_result_builder: "SyncResultBuilder"
2241
+ ) -> bool:
2242
+ """Returns whether there may be any new events that should be sent down
2243
+ the sync. Returns True if there are.
2244
+
2245
+ Does not modify the `sync_result_builder`.
2246
+ """
2247
+ since_token = sync_result_builder.since_token
2248
+ membership_change_events = sync_result_builder.membership_change_events
2249
+
2250
+ assert since_token
2251
+
2252
+ if membership_change_events or sync_result_builder.forced_newly_joined_room_ids:
2253
+ return True
2254
+
2255
+ stream_id = since_token.room_key.stream
2256
+ for room_id in sync_result_builder.joined_room_ids:
2257
+ if self.store.has_room_changed_since(room_id, stream_id):
2258
+ return True
2259
+ return False
2260
+
2261
+ async def _get_room_changes_for_incremental_sync(
2262
+ self,
2263
+ sync_result_builder: "SyncResultBuilder",
2264
+ ignored_users: frozenset[str],
2265
+ ) -> _RoomChanges:
2266
+ """Determine the changes in rooms to report to the user.
2267
+
2268
+ This function is a first pass at generating the rooms part of the sync response.
2269
+ It determines which rooms have changed during the sync period, and categorises
2270
+ them into four buckets: "knock", "invite", "join" and "leave". It also excludes
2271
+ from that list any room that appears in the list of rooms to exclude from sync
2272
+ results in the server configuration.
2273
+
2274
+ 1. Finds all membership changes for the user in the sync period (from
2275
+ `since_token` up to `now_token`).
2276
+ 2. Uses those to place the room in one of the four categories above.
2277
+ 3. Builds a `_RoomChanges` struct to record this, and return that struct.
2278
+
2279
+ For rooms classified as "knock", "invite" or "leave", we just need to report
2280
+ a single membership event in the eventual /sync response. For "join" we need
2281
+ to fetch additional non-membership events, e.g. messages in the room. That is
2282
+ more complicated, so instead we report an intermediary `RoomSyncResultBuilder`
2283
+ struct, and leave the additional work to `_generate_room_entry`.
2284
+
2285
+ The sync_result_builder is not modified by this function.
2286
+ """
2287
+ user_id = sync_result_builder.sync_config.user.to_string()
2288
+ since_token = sync_result_builder.since_token
2289
+ now_token = sync_result_builder.now_token
2290
+ sync_config = sync_result_builder.sync_config
2291
+ membership_change_events = sync_result_builder.membership_change_events
2292
+
2293
+ assert since_token
2294
+
2295
+ mem_change_events_by_room_id: dict[str, list[EventBase]] = {}
2296
+ for event in membership_change_events:
2297
+ mem_change_events_by_room_id.setdefault(event.room_id, []).append(event)
2298
+
2299
+ newly_joined_rooms: list[str] = list(
2300
+ sync_result_builder.forced_newly_joined_room_ids
2301
+ )
2302
+ newly_left_rooms: list[str] = []
2303
+ room_entries: list[RoomSyncResultBuilder] = []
2304
+ invited: list[InvitedSyncResult] = []
2305
+ knocked: list[KnockedSyncResult] = []
2306
+ invite_config = await self.store.get_invite_config_for_user(user_id)
2307
+ for room_id, events in mem_change_events_by_room_id.items():
2308
+ # The body of this loop will add this room to at least one of the five lists
2309
+ # above. Things get messy if you've e.g. joined, left, joined then left the
2310
+ # room all in the same sync period.
2311
+ logger.debug(
2312
+ "Membership changes in %s: [%s]",
2313
+ room_id,
2314
+ ", ".join("%s (%s)" % (e.event_id, e.membership) for e in events),
2315
+ )
2316
+
2317
+ non_joins = [e for e in events if e.membership != Membership.JOIN]
2318
+ has_join = len(non_joins) != len(events)
2319
+
2320
+ # We want to figure out if we joined the room at some point since
2321
+ # the last sync (even if we have since left). This is to make sure
2322
+ # we do send down the room, and with full state, where necessary
2323
+
2324
+ old_state_ids = None
2325
+ if room_id in sync_result_builder.joined_room_ids and non_joins:
2326
+ # Always include if the user (re)joined the room, especially
2327
+ # important so that device list changes are calculated correctly.
2328
+ # If there are non-join member events, but we are still in the room,
2329
+ # then the user must have left and joined
2330
+ newly_joined_rooms.append(room_id)
2331
+
2332
+ # User is in the room so we don't need to do the invite/leave checks
2333
+ continue
2334
+
2335
+ if room_id in sync_result_builder.joined_room_ids or has_join:
2336
+ old_state_ids = await self._state_storage_controller.get_state_ids_at(
2337
+ room_id,
2338
+ since_token,
2339
+ state_filter=StateFilter.from_types([(EventTypes.Member, user_id)]),
2340
+ )
2341
+ old_mem_ev_id = old_state_ids.get((EventTypes.Member, user_id), None)
2342
+ old_mem_ev = None
2343
+ if old_mem_ev_id:
2344
+ old_mem_ev = await self.store.get_event(
2345
+ old_mem_ev_id, allow_none=True
2346
+ )
2347
+
2348
+ if not old_mem_ev or old_mem_ev.membership != Membership.JOIN:
2349
+ newly_joined_rooms.append(room_id)
2350
+
2351
+ # If user is in the room then we don't need to do the invite/leave checks
2352
+ if room_id in sync_result_builder.joined_room_ids:
2353
+ continue
2354
+
2355
+ if not non_joins:
2356
+ continue
2357
+ last_non_join = non_joins[-1]
2358
+
2359
+ # Check if we have left the room. This can either be because we were
2360
+ # joined before *or* that we since joined and then left.
2361
+ if events[-1].membership != Membership.JOIN:
2362
+ if has_join:
2363
+ newly_left_rooms.append(room_id)
2364
+ else:
2365
+ if not old_state_ids:
2366
+ old_state_ids = (
2367
+ await self._state_storage_controller.get_state_ids_at(
2368
+ room_id,
2369
+ since_token,
2370
+ state_filter=StateFilter.from_types(
2371
+ [(EventTypes.Member, user_id)]
2372
+ ),
2373
+ )
2374
+ )
2375
+ old_mem_ev_id = old_state_ids.get(
2376
+ (EventTypes.Member, user_id), None
2377
+ )
2378
+ old_mem_ev = None
2379
+ if old_mem_ev_id:
2380
+ old_mem_ev = await self.store.get_event(
2381
+ old_mem_ev_id, allow_none=True
2382
+ )
2383
+ if old_mem_ev and old_mem_ev.membership == Membership.JOIN:
2384
+ newly_left_rooms.append(room_id)
2385
+
2386
+ # Only bother if we're still currently invited
2387
+ should_invite = last_non_join.membership == Membership.INVITE
2388
+ if should_invite:
2389
+ if (
2390
+ last_non_join.sender not in ignored_users
2391
+ and invite_config.get_invite_rule(last_non_join.sender)
2392
+ != InviteRule.IGNORE
2393
+ ):
2394
+ invite_room_sync = InvitedSyncResult(room_id, invite=last_non_join)
2395
+ if invite_room_sync:
2396
+ invited.append(invite_room_sync)
2397
+
2398
+ # Only bother if our latest membership in the room is knock (and we haven't
2399
+ # been accepted/rejected in the meantime).
2400
+ should_knock = last_non_join.membership == Membership.KNOCK
2401
+ if should_knock:
2402
+ knock_room_sync = KnockedSyncResult(room_id, knock=last_non_join)
2403
+ if knock_room_sync:
2404
+ knocked.append(knock_room_sync)
2405
+
2406
+ # Always include leave/ban events. Just take the last one.
2407
+ # TODO: How do we handle ban -> leave in same batch?
2408
+ leave_events = [
2409
+ e
2410
+ for e in non_joins
2411
+ if e.membership in (Membership.LEAVE, Membership.BAN)
2412
+ ]
2413
+
2414
+ if leave_events:
2415
+ leave_event = leave_events[-1]
2416
+ leave_position = await self.store.get_position_for_event(
2417
+ leave_event.event_id
2418
+ )
2419
+
2420
+ # If the leave event happened before the since token then we
2421
+ # bail.
2422
+ if since_token and not leave_position.persisted_after(
2423
+ since_token.room_key
2424
+ ):
2425
+ continue
2426
+
2427
+ # We can safely convert the position of the leave event into a
2428
+ # stream token as it'll only be used in the context of this
2429
+ # room. (c.f. the docstring of `to_room_stream_token`).
2430
+ leave_token = since_token.copy_and_replace(
2431
+ StreamKeyType.ROOM, leave_position.to_room_stream_token()
2432
+ )
2433
+
2434
+ # If this is an out of band message, like a remote invite
2435
+ # rejection, we include it in the recents batch. Otherwise, we
2436
+ # let _load_filtered_recents handle fetching the correct
2437
+ # batches.
2438
+ #
2439
+ # This is all screaming out for a refactor, as the logic here is
2440
+ # subtle and the moving parts numerous.
2441
+ if leave_event.internal_metadata.is_out_of_band_membership():
2442
+ batch_events: Optional[list[EventBase]] = [leave_event]
2443
+ else:
2444
+ batch_events = None
2445
+
2446
+ room_entries.append(
2447
+ RoomSyncResultBuilder(
2448
+ room_id=room_id,
2449
+ rtype="archived",
2450
+ events=batch_events,
2451
+ newly_joined=room_id in newly_joined_rooms,
2452
+ full_state=False,
2453
+ since_token=since_token,
2454
+ upto_token=leave_token,
2455
+ end_token=leave_token,
2456
+ out_of_band=leave_event.internal_metadata.is_out_of_band_membership(),
2457
+ )
2458
+ )
2459
+
2460
+ timeline_limit = sync_config.filter_collection.timeline_limit()
2461
+
2462
+ # Get all events since the `from_key` in rooms we're currently joined to.
2463
+ # If there are too many, we get the most recent events only. This leaves
2464
+ # a "gap" in the timeline, as described by the spec for /sync.
2465
+ room_to_events = await self.store.get_room_events_stream_for_rooms(
2466
+ room_ids=sync_result_builder.joined_room_ids,
2467
+ from_key=now_token.room_key,
2468
+ to_key=since_token.room_key,
2469
+ limit=timeline_limit + 1,
2470
+ direction=Direction.BACKWARDS,
2471
+ )
2472
+
2473
+ # We loop through all room ids, even if there are no new events, in case
2474
+ # there are non room events that we need to notify about.
2475
+ for room_id in sync_result_builder.joined_room_ids:
2476
+ room_entry = room_to_events.get(room_id, None)
2477
+
2478
+ newly_joined = room_id in newly_joined_rooms
2479
+ if room_entry:
2480
+ events, start_key, _ = room_entry
2481
+ # We want to return the events in ascending order (the last event is the
2482
+ # most recent).
2483
+ events.reverse()
2484
+
2485
+ prev_batch_token = now_token.copy_and_replace(
2486
+ StreamKeyType.ROOM, start_key
2487
+ )
2488
+
2489
+ entry = RoomSyncResultBuilder(
2490
+ room_id=room_id,
2491
+ rtype="joined",
2492
+ events=events,
2493
+ newly_joined=newly_joined,
2494
+ full_state=False,
2495
+ since_token=None if newly_joined else since_token,
2496
+ upto_token=prev_batch_token,
2497
+ end_token=now_token,
2498
+ )
2499
+ else:
2500
+ entry = RoomSyncResultBuilder(
2501
+ room_id=room_id,
2502
+ rtype="joined",
2503
+ events=[],
2504
+ newly_joined=newly_joined,
2505
+ full_state=False,
2506
+ since_token=since_token,
2507
+ upto_token=since_token,
2508
+ end_token=now_token,
2509
+ )
2510
+
2511
+ room_entries.append(entry)
2512
+
2513
+ return _RoomChanges(
2514
+ room_entries,
2515
+ invited,
2516
+ knocked,
2517
+ newly_joined_rooms,
2518
+ newly_left_rooms,
2519
+ )
2520
+
2521
+ async def _get_room_changes_for_initial_sync(
2522
+ self,
2523
+ sync_result_builder: "SyncResultBuilder",
2524
+ ignored_users: frozenset[str],
2525
+ ) -> _RoomChanges:
2526
+ """Returns entries for all rooms for the user.
2527
+
2528
+ Like `_get_rooms_changed`, but assumes the `since_token` is `None`.
2529
+
2530
+ This function does not modify the sync_result_builder.
2531
+
2532
+ Args:
2533
+ sync_result_builder
2534
+ ignored_users: Set of users ignored by user.
2535
+ ignored_rooms: List of rooms to ignore.
2536
+ """
2537
+
2538
+ user_id = sync_result_builder.sync_config.user.to_string()
2539
+ since_token = sync_result_builder.since_token
2540
+ now_token = sync_result_builder.now_token
2541
+ sync_config = sync_result_builder.sync_config
2542
+
2543
+ room_list = await self.store.get_rooms_for_local_user_where_membership_is(
2544
+ user_id=user_id,
2545
+ membership_list=Membership.LIST,
2546
+ excluded_rooms=sync_result_builder.excluded_room_ids,
2547
+ )
2548
+ invite_config = await self.store.get_invite_config_for_user(user_id)
2549
+
2550
+ room_entries = []
2551
+ invited = []
2552
+ knocked = []
2553
+
2554
+ for event in room_list:
2555
+ if event.room_version_id not in KNOWN_ROOM_VERSIONS:
2556
+ continue
2557
+
2558
+ if event.membership == Membership.JOIN:
2559
+ room_entries.append(
2560
+ RoomSyncResultBuilder(
2561
+ room_id=event.room_id,
2562
+ rtype="joined",
2563
+ events=None,
2564
+ newly_joined=False,
2565
+ full_state=True,
2566
+ since_token=since_token,
2567
+ upto_token=now_token,
2568
+ end_token=now_token,
2569
+ )
2570
+ )
2571
+ elif event.membership == Membership.INVITE:
2572
+ if event.sender in ignored_users:
2573
+ continue
2574
+ if invite_config.get_invite_rule(event.sender) == InviteRule.IGNORE:
2575
+ continue
2576
+ invite = await self.store.get_event(event.event_id)
2577
+ invited.append(InvitedSyncResult(room_id=event.room_id, invite=invite))
2578
+ elif event.membership == Membership.KNOCK:
2579
+ knock = await self.store.get_event(event.event_id)
2580
+ knocked.append(KnockedSyncResult(room_id=event.room_id, knock=knock))
2581
+ elif event.membership in (Membership.LEAVE, Membership.BAN):
2582
+ # Always send down rooms we were banned from or kicked from.
2583
+ if not sync_config.filter_collection.include_leave:
2584
+ if event.membership == Membership.LEAVE:
2585
+ if user_id == event.sender:
2586
+ continue
2587
+
2588
+ leave_token = now_token.copy_and_replace(
2589
+ StreamKeyType.ROOM, RoomStreamToken(stream=event.event_pos.stream)
2590
+ )
2591
+ room_entries.append(
2592
+ RoomSyncResultBuilder(
2593
+ room_id=event.room_id,
2594
+ rtype="archived",
2595
+ events=None,
2596
+ newly_joined=False,
2597
+ full_state=True,
2598
+ since_token=since_token,
2599
+ upto_token=leave_token,
2600
+ end_token=leave_token,
2601
+ )
2602
+ )
2603
+
2604
+ return _RoomChanges(room_entries, invited, knocked, [], [])
2605
+
2606
+ async def _generate_room_entry(
2607
+ self,
2608
+ sync_result_builder: "SyncResultBuilder",
2609
+ room_builder: "RoomSyncResultBuilder",
2610
+ ephemeral: list[JsonDict],
2611
+ tags: Optional[Mapping[str, JsonMapping]],
2612
+ account_data: Mapping[str, JsonMapping],
2613
+ always_include: bool = False,
2614
+ ) -> None:
2615
+ """Populates the `joined` and `archived` section of `sync_result_builder`
2616
+ based on the `room_builder`.
2617
+
2618
+ Ideally, we want to report all events whose stream ordering `s` lies in the
2619
+ range `since_token < s <= now_token`, where the two tokens are read from the
2620
+ sync_result_builder.
2621
+
2622
+ If there are too many events in that range to report, things get complicated.
2623
+ In this situation we return a truncated list of the most recent events, and
2624
+ indicate in the response that there is a "gap" of omitted events. Lots of this
2625
+ is handled in `_load_filtered_recents`, but some of is handled in this method.
2626
+
2627
+ Additionally:
2628
+ - we include a "state_delta", to describe the changes in state over the gap,
2629
+ - we include all membership events applying to the user making the request,
2630
+ even those in the gap.
2631
+
2632
+ See the spec for the rationale:
2633
+ https://spec.matrix.org/v1.1/client-server-api/#syncing
2634
+
2635
+ Args:
2636
+ sync_result_builder
2637
+ room_builder
2638
+ ephemeral: List of new ephemeral events for room
2639
+ tags: List of *all* tags for room, or None if there has been
2640
+ no change.
2641
+ account_data: List of new account data for room
2642
+ always_include: Always include this room in the sync response,
2643
+ even if empty.
2644
+ """
2645
+ newly_joined = room_builder.newly_joined
2646
+ full_state = (
2647
+ room_builder.full_state or newly_joined or sync_result_builder.full_state
2648
+ )
2649
+ events = room_builder.events
2650
+
2651
+ # We want to shortcut out as early as possible.
2652
+ if not (always_include or account_data or ephemeral or full_state):
2653
+ if events == [] and tags is None:
2654
+ return
2655
+
2656
+ now_token = sync_result_builder.now_token
2657
+ sync_config = sync_result_builder.sync_config
2658
+
2659
+ room_id = room_builder.room_id
2660
+ since_token = room_builder.since_token
2661
+ upto_token = room_builder.upto_token
2662
+
2663
+ with start_active_span("sync.generate_room_entry"):
2664
+ set_tag("room_id", room_id)
2665
+ log_kv({"events": len(events or ())})
2666
+
2667
+ log_kv(
2668
+ {
2669
+ "since_token": since_token,
2670
+ "upto_token": upto_token,
2671
+ "end_token": room_builder.end_token,
2672
+ }
2673
+ )
2674
+
2675
+ batch = await self._load_filtered_recents(
2676
+ room_id,
2677
+ sync_result_builder,
2678
+ sync_config,
2679
+ upto_token=upto_token,
2680
+ since_token=since_token,
2681
+ potential_recents=events,
2682
+ newly_joined_room=newly_joined,
2683
+ )
2684
+ log_kv(
2685
+ {
2686
+ "batch_events": len(batch.events),
2687
+ "prev_batch": batch.prev_batch,
2688
+ "batch_limited": batch.limited,
2689
+ }
2690
+ )
2691
+
2692
+ # Note: `batch` can be both empty and limited here in the case where
2693
+ # `_load_filtered_recents` can't find any events the user should see
2694
+ # (e.g. due to having ignored the sender of the last 50 events).
2695
+
2696
+ # When we join the room (or the client requests full_state), we should
2697
+ # send down any existing tags. Usually the user won't have tags in a
2698
+ # newly joined room, unless either a) they've joined before or b) the
2699
+ # tag was added by synapse e.g. for server notice rooms.
2700
+ if full_state:
2701
+ user_id = sync_result_builder.sync_config.user.to_string()
2702
+ tags = await self.store.get_tags_for_room(user_id, room_id)
2703
+
2704
+ # If there aren't any tags, don't send the empty tags list down
2705
+ # sync
2706
+ if not tags:
2707
+ tags = None
2708
+
2709
+ account_data_events = []
2710
+ if tags is not None:
2711
+ account_data_events.append(
2712
+ {"type": AccountDataTypes.TAG, "content": {"tags": tags}}
2713
+ )
2714
+
2715
+ for account_data_type, content in account_data.items():
2716
+ account_data_events.append(
2717
+ {"type": account_data_type, "content": content}
2718
+ )
2719
+
2720
+ account_data_events = (
2721
+ await sync_config.filter_collection.filter_room_account_data(
2722
+ account_data_events
2723
+ )
2724
+ )
2725
+
2726
+ ephemeral = [
2727
+ # per spec, ephemeral events (typing notifications and read receipts)
2728
+ # should not have a `room_id` field when sent to clients
2729
+ # refs:
2730
+ # - https://spec.matrix.org/v1.16/client-server-api/#mtyping
2731
+ # - https://spec.matrix.org/v1.16/client-server-api/#mreceipt
2732
+ {k: v for (k, v) in event.items() if k != "room_id"}
2733
+ for event in await sync_config.filter_collection.filter_room_ephemeral(
2734
+ ephemeral
2735
+ )
2736
+ ]
2737
+
2738
+ if not (
2739
+ always_include
2740
+ or batch
2741
+ or account_data_events
2742
+ or ephemeral
2743
+ or full_state
2744
+ ):
2745
+ return
2746
+
2747
+ if not room_builder.out_of_band:
2748
+ state = await self.compute_state_delta(
2749
+ room_id,
2750
+ batch,
2751
+ sync_config,
2752
+ since_token,
2753
+ room_builder.end_token,
2754
+ full_state=full_state,
2755
+ joined=room_builder.rtype == "joined",
2756
+ )
2757
+ else:
2758
+ # An out of band room won't have any state changes.
2759
+ state = {}
2760
+
2761
+ summary: Optional[JsonDict] = {}
2762
+
2763
+ # we include a summary in room responses when we're lazy loading
2764
+ # members (as the client otherwise doesn't have enough info to form
2765
+ # the name itself).
2766
+ if (
2767
+ not room_builder.out_of_band
2768
+ and sync_config.filter_collection.lazy_load_members()
2769
+ and (
2770
+ # we recalculate the summary:
2771
+ # if there are membership changes in the timeline, or
2772
+ # if membership has changed during a gappy sync, or
2773
+ # if this is an initial sync.
2774
+ any(ev.type == EventTypes.Member for ev in batch.events)
2775
+ or (
2776
+ # XXX: this may include false positives in the form of LL
2777
+ # members which have snuck into state
2778
+ batch.limited
2779
+ and any(t == EventTypes.Member for (t, k) in state)
2780
+ )
2781
+ or since_token is None
2782
+ )
2783
+ ):
2784
+ summary = await self.compute_summary(
2785
+ room_id, sync_config, batch, state, now_token
2786
+ )
2787
+
2788
+ if room_builder.rtype == "joined":
2789
+ unread_notifications: dict[str, int] = {}
2790
+ room_sync = JoinedSyncResult(
2791
+ room_id=room_id,
2792
+ timeline=batch,
2793
+ state=state,
2794
+ ephemeral=ephemeral,
2795
+ account_data=account_data_events,
2796
+ unread_notifications=unread_notifications,
2797
+ unread_thread_notifications={},
2798
+ summary=summary,
2799
+ unread_count=0,
2800
+ )
2801
+
2802
+ if room_sync or always_include:
2803
+ notifs = await self.unread_notifs_for_room_id(room_id, sync_config)
2804
+
2805
+ # Notifications for the main timeline.
2806
+ notify_count = notifs.main_timeline.notify_count
2807
+ highlight_count = notifs.main_timeline.highlight_count
2808
+ unread_count = notifs.main_timeline.unread_count
2809
+
2810
+ # Check the sync configuration.
2811
+ if sync_config.filter_collection.unread_thread_notifications():
2812
+ # And add info for each thread.
2813
+ room_sync.unread_thread_notifications = {
2814
+ thread_id: {
2815
+ "notification_count": thread_notifs.notify_count,
2816
+ "highlight_count": thread_notifs.highlight_count,
2817
+ }
2818
+ for thread_id, thread_notifs in notifs.threads.items()
2819
+ if thread_id is not None
2820
+ }
2821
+
2822
+ else:
2823
+ # Combine the unread counts for all threads and main timeline.
2824
+ for thread_notifs in notifs.threads.values():
2825
+ notify_count += thread_notifs.notify_count
2826
+ highlight_count += thread_notifs.highlight_count
2827
+ unread_count += thread_notifs.unread_count
2828
+
2829
+ unread_notifications["notification_count"] = notify_count
2830
+ unread_notifications["highlight_count"] = highlight_count
2831
+ room_sync.unread_count = unread_count
2832
+
2833
+ sync_result_builder.joined.append(room_sync)
2834
+
2835
+ if batch.limited and since_token:
2836
+ user_id = sync_result_builder.sync_config.user.to_string()
2837
+ logger.debug(
2838
+ "Incremental gappy sync of %s for user %s with %d state events",
2839
+ room_id,
2840
+ user_id,
2841
+ len(state),
2842
+ )
2843
+ elif room_builder.rtype == "archived":
2844
+ archived_room_sync = ArchivedSyncResult(
2845
+ room_id=room_id,
2846
+ timeline=batch,
2847
+ state=state,
2848
+ account_data=account_data_events,
2849
+ )
2850
+ if archived_room_sync or always_include:
2851
+ sync_result_builder.archived.append(archived_room_sync)
2852
+ else:
2853
+ raise Exception("Unrecognized rtype: %r", room_builder.rtype)
2854
+
2855
+
2856
+ def _action_has_highlight(actions: list[JsonDict]) -> bool:
2857
+ for action in actions:
2858
+ try:
2859
+ if action.get("set_tweak", None) == "highlight":
2860
+ return action.get("value", True)
2861
+ except AttributeError:
2862
+ pass
2863
+
2864
+ return False
2865
+
2866
+
2867
+ def _calculate_state(
2868
+ timeline_contains: StateMap[str],
2869
+ timeline_start: StateMap[str],
2870
+ timeline_end: StateMap[str],
2871
+ previous_timeline_end: StateMap[str],
2872
+ lazy_load_members: bool,
2873
+ ) -> StateMap[str]:
2874
+ """Works out what state to include in a sync response.
2875
+
2876
+ Args:
2877
+ timeline_contains: state in the timeline
2878
+ timeline_start: state at the start of the timeline
2879
+ timeline_end: state at the end of the timeline
2880
+ previous_timeline_end: state at the end of the previous sync (or empty dict
2881
+ if this is an initial sync)
2882
+ lazy_load_members: whether to return members from timeline_start
2883
+ or not. assumes that timeline_start has already been filtered to
2884
+ include only the members the client needs to know about.
2885
+ """
2886
+ event_id_to_state_key = {
2887
+ event_id: state_key
2888
+ for state_key, event_id in itertools.chain(
2889
+ timeline_contains.items(),
2890
+ timeline_start.items(),
2891
+ timeline_end.items(),
2892
+ previous_timeline_end.items(),
2893
+ )
2894
+ }
2895
+
2896
+ timeline_end_ids = set(timeline_end.values())
2897
+ timeline_start_ids = set(timeline_start.values())
2898
+ previous_timeline_end_ids = set(previous_timeline_end.values())
2899
+ timeline_contains_ids = set(timeline_contains.values())
2900
+
2901
+ # If we are lazyloading room members, we explicitly add the membership events
2902
+ # for the senders in the timeline into the state block returned by /sync,
2903
+ # as we may not have sent them to the client before. We find these membership
2904
+ # events by filtering them out of timeline_start, which has already been filtered
2905
+ # to only include membership events for the senders in the timeline.
2906
+ # In practice, we can do this by removing them from the previous_timeline_end_ids
2907
+ # list, which is the list of relevant state we know we have already sent to the
2908
+ # client.
2909
+ # see https://github.com/matrix-org/synapse/pull/2970/files/efcdacad7d1b7f52f879179701c7e0d9b763511f#r204732809
2910
+
2911
+ if lazy_load_members:
2912
+ previous_timeline_end_ids.difference_update(
2913
+ e for t, e in timeline_start.items() if t[0] == EventTypes.Member
2914
+ )
2915
+
2916
+ # Naively, we would just return the difference between the state at the start
2917
+ # of the timeline (`timeline_start_ids`) and that at the end of the previous sync
2918
+ # (`previous_timeline_end_ids`). However, that fails in the presence of forks in
2919
+ # the DAG.
2920
+ #
2921
+ # For example, consider a DAG such as the following:
2922
+ #
2923
+ # E1
2924
+ # ↗ ↖
2925
+ # | S2
2926
+ # | ↑
2927
+ # --|------|----
2928
+ # | |
2929
+ # E3 |
2930
+ # ↖ /
2931
+ # E4
2932
+ #
2933
+ # ... and a filter that means we only return 2 events, represented by the dashed
2934
+ # horizontal line. Assuming S2 was *not* included in the previous sync, we need to
2935
+ # include it in the `state` section.
2936
+ #
2937
+ # Note that the state at the start of the timeline (E3) does not include S2. So,
2938
+ # to make sure it gets included in the calculation here, we actually look at
2939
+ # the state at the *end* of the timeline, and subtract any events that are present
2940
+ # in the timeline.
2941
+ #
2942
+ # ----------
2943
+ #
2944
+ # Aside 1: You may then wonder if we need to include `timeline_start` in the
2945
+ # calculation. Consider a linear DAG:
2946
+ #
2947
+ # E1
2948
+ # ↑
2949
+ # S2
2950
+ # ↑
2951
+ # ----|------
2952
+ # |
2953
+ # E3
2954
+ # ↑
2955
+ # S4
2956
+ # ↑
2957
+ # E5
2958
+ #
2959
+ # ... where S2 and S4 change the same piece of state; and where we have a filter
2960
+ # that returns 3 events (E3, S4, E5). We still need to tell the client about S2,
2961
+ # because it might affect the display of E3. However, the state at the end of the
2962
+ # timeline only tells us about S4; if we don't inspect `timeline_start` we won't
2963
+ # find out about S2.
2964
+ #
2965
+ # (There are yet more complicated cases in which a state event is excluded from the
2966
+ # timeline, but whose effect actually lands in the DAG in the *middle* of the
2967
+ # timeline. We have no way to represent that in the /sync response, and we don't
2968
+ # even try; it is ether omitted or plonked into `state` as if it were at the start
2969
+ # of the timeline, depending on what else is in the timeline.)
2970
+
2971
+ state_ids = (
2972
+ (timeline_end_ids | timeline_start_ids)
2973
+ - previous_timeline_end_ids
2974
+ - timeline_contains_ids
2975
+ )
2976
+
2977
+ return {event_id_to_state_key[e]: e for e in state_ids}
2978
+
2979
+
2980
+ @attr.s(slots=True, auto_attribs=True)
2981
+ class SyncResultBuilder:
2982
+ """Used to help build up a new SyncResult for a user
2983
+
2984
+ Attributes:
2985
+ sync_config
2986
+ full_state: The full_state flag as specified by user
2987
+ since_token: The token supplied by user, or None.
2988
+ now_token: The token to sync up to.
2989
+ joined_room_ids: List of rooms the user is joined to
2990
+ excluded_room_ids: Set of room ids we should omit from the /sync response.
2991
+ forced_newly_joined_room_ids:
2992
+ Rooms that should be presented in the /sync response as if they were
2993
+ newly joined during the sync period, even if that's not the case.
2994
+ (This is useful if the room was previously excluded from a /sync response,
2995
+ and now the client should be made aware of it.)
2996
+ Only used by incremental syncs.
2997
+
2998
+ # The following mirror the fields in a sync response
2999
+ presence
3000
+ account_data
3001
+ joined
3002
+ invited
3003
+ knocked
3004
+ archived
3005
+ to_device
3006
+ """
3007
+
3008
+ sync_config: SyncConfig
3009
+ full_state: bool
3010
+ since_token: Optional[StreamToken]
3011
+ now_token: StreamToken
3012
+ joined_room_ids: frozenset[str]
3013
+ excluded_room_ids: frozenset[str]
3014
+ forced_newly_joined_room_ids: frozenset[str]
3015
+ membership_change_events: list[EventBase]
3016
+
3017
+ presence: list[UserPresenceState] = attr.Factory(list)
3018
+ account_data: list[JsonDict] = attr.Factory(list)
3019
+ joined: list[JoinedSyncResult] = attr.Factory(list)
3020
+ invited: list[InvitedSyncResult] = attr.Factory(list)
3021
+ knocked: list[KnockedSyncResult] = attr.Factory(list)
3022
+ archived: list[ArchivedSyncResult] = attr.Factory(list)
3023
+ to_device: list[JsonDict] = attr.Factory(list)
3024
+
3025
+ def calculate_user_changes(self) -> tuple[AbstractSet[str], AbstractSet[str]]:
3026
+ """Work out which other users have joined or left rooms we are joined to.
3027
+
3028
+ This data only is only useful for an incremental sync.
3029
+
3030
+ The SyncResultBuilder is not modified by this function.
3031
+ """
3032
+ newly_joined_or_invited_or_knocked_users = set()
3033
+ newly_left_users = set()
3034
+ if self.since_token:
3035
+ for joined_sync in self.joined:
3036
+ it = itertools.chain(
3037
+ joined_sync.state.values(), joined_sync.timeline.events
3038
+ )
3039
+ for event in it:
3040
+ if event.type == EventTypes.Member:
3041
+ if (
3042
+ event.membership == Membership.JOIN
3043
+ or event.membership == Membership.INVITE
3044
+ or event.membership == Membership.KNOCK
3045
+ ):
3046
+ newly_joined_or_invited_or_knocked_users.add(
3047
+ event.state_key
3048
+ )
3049
+ # If the user left and rejoined in the same batch, they
3050
+ # count as a newly-joined user, *not* a newly-left user.
3051
+ newly_left_users.discard(event.state_key)
3052
+ else:
3053
+ prev_content = event.unsigned.get("prev_content", {})
3054
+ prev_membership = prev_content.get("membership", None)
3055
+ if prev_membership == Membership.JOIN:
3056
+ newly_left_users.add(event.state_key)
3057
+ # If the user joined and left in the same batch, they
3058
+ # count as a newly-left user, not a newly-joined user.
3059
+ newly_joined_or_invited_or_knocked_users.discard(
3060
+ event.state_key
3061
+ )
3062
+
3063
+ return newly_joined_or_invited_or_knocked_users, newly_left_users
3064
+
3065
+
3066
+ @attr.s(slots=True, auto_attribs=True)
3067
+ class RoomSyncResultBuilder:
3068
+ """Stores information needed to create either a `JoinedSyncResult` or
3069
+ `ArchivedSyncResult`.
3070
+
3071
+ Attributes:
3072
+ room_id
3073
+
3074
+ rtype: One of `"joined"` or `"archived"`
3075
+
3076
+ events: List of events to include in the room (more events may be added
3077
+ when generating result).
3078
+
3079
+ newly_joined: If the user has newly joined the room
3080
+
3081
+ full_state: Whether the full state should be sent in result
3082
+
3083
+ since_token: Earliest point to return events from, or None
3084
+
3085
+ upto_token: Latest point to return events from. If `events` is populated,
3086
+ this is set to the token at the start of `events`
3087
+
3088
+ end_token: The last point in the timeline that the client should see events
3089
+ from. Normally this will be the same as the global `now_token`, but in
3090
+ the case of rooms where the user has left the room, this will be the point
3091
+ just after their leave event.
3092
+
3093
+ This is used in the calculation of the state which is returned in `state`:
3094
+ any state changes *up to* `end_token` (and not beyond!) which are not
3095
+ reflected in the timeline need to be returned in `state`.
3096
+
3097
+ out_of_band: whether the events in the room are "out of band" events
3098
+ and the server isn't in the room.
3099
+ """
3100
+
3101
+ room_id: str
3102
+ rtype: str
3103
+ events: Optional[list[EventBase]]
3104
+ newly_joined: bool
3105
+ full_state: bool
3106
+ since_token: Optional[StreamToken]
3107
+ upto_token: StreamToken
3108
+ end_token: StreamToken
3109
+ out_of_band: bool = False