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