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,1936 @@
1
+ #
2
+ # This file is licensed under the Affero General Public License (AGPL) version 3.
3
+ #
4
+ # Copyright 2015 OpenMarket Ltd
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
+
22
+ """Responsible for storing and fetching push actions / notifications.
23
+
24
+ There are two main uses for push actions:
25
+ 1. Sending out push to a user's device; and
26
+ 2. Tracking per-room per-user notification counts (used in sync requests).
27
+
28
+ For the former we simply use the `event_push_actions` table, which contains all
29
+ the calculated actions for a given user (which were calculated by the
30
+ `BulkPushRuleEvaluator`).
31
+
32
+ For the latter we could simply count the number of rows in `event_push_actions`
33
+ table for a given room/user, but in practice this is *very* heavyweight when
34
+ there were a large number of notifications (due to e.g. the user never reading a
35
+ room). Plus, keeping all push actions indefinitely uses a lot of disk space.
36
+
37
+ To fix these issues, we add a new table `event_push_summary` that tracks
38
+ per-user per-room counts of all notifications that happened before a stream
39
+ ordering S. Thus, to get the notification count for a user / room we can simply
40
+ query a single row in `event_push_summary` and count the number of rows in
41
+ `event_push_actions` with a stream ordering larger than S (and as long as S is
42
+ "recent", the number of rows needing to be scanned will be small).
43
+
44
+ The `event_push_summary` table is updated via a background job that periodically
45
+ chooses a new stream ordering S' (usually the latest stream ordering), counts
46
+ all notifications in `event_push_actions` between the existing S and S', and
47
+ adds them to the existing counts in `event_push_summary`.
48
+
49
+ This allows us to delete old rows from `event_push_actions` once those rows have
50
+ been counted and added to `event_push_summary` (we call this process
51
+ "rotation").
52
+
53
+
54
+ We need to handle when a user sends a read receipt to the room. Again this is
55
+ done as a background process. For each receipt we clear the row in
56
+ `event_push_summary` and count the number of notifications in
57
+ `event_push_actions` that happened after the receipt but before S, and insert
58
+ that count into `event_push_summary` (If the receipt happened *after* S then we
59
+ simply clear the `event_push_summary`.)
60
+
61
+ Note that its possible that if the read receipt is for an old event the relevant
62
+ `event_push_actions` rows will have been rotated and we get the wrong count
63
+ (it'll be too low). We accept this as a rare edge case that is unlikely to
64
+ impact the user much (since the vast majority of read receipts will be for the
65
+ latest event).
66
+
67
+ The last complication is to handle the race where we request the notifications
68
+ counts after a user sends a read receipt into the room, but *before* the
69
+ background update handles the receipt (without any special handling the counts
70
+ would be outdated). We fix this by including in `event_push_summary` the read
71
+ receipt we used when updating `event_push_summary`, and every time we query the
72
+ table we check if that matches the most recent read receipt in the room. If yes,
73
+ continue as above, if not we simply query the `event_push_actions` table
74
+ directly.
75
+
76
+ Since read receipts are almost always for recent events, scanning the
77
+ `event_push_actions` table in this case is unlikely to be a problem. Even if it
78
+ is a problem, it is temporary until the background job handles the new read
79
+ receipt.
80
+ """
81
+
82
+ import logging
83
+ from collections import defaultdict
84
+ from typing import (
85
+ TYPE_CHECKING,
86
+ Collection,
87
+ Mapping,
88
+ cast,
89
+ )
90
+
91
+ import attr
92
+
93
+ from twisted.internet.task import LoopingCall
94
+
95
+ from synapse.api.constants import MAIN_TIMELINE, ReceiptTypes
96
+ from synapse.metrics.background_process_metrics import wrap_as_background_process
97
+ from synapse.storage._base import SQLBaseStore, db_to_json, make_in_list_sql_clause
98
+ from synapse.storage.database import (
99
+ DatabasePool,
100
+ LoggingDatabaseConnection,
101
+ LoggingTransaction,
102
+ PostgresEngine,
103
+ )
104
+ from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
105
+ from synapse.storage.databases.main.stream import StreamWorkerStore
106
+ from synapse.types import JsonDict, StrCollection
107
+ from synapse.util.caches.descriptors import cached
108
+ from synapse.util.duration import Duration
109
+ from synapse.util.json import json_encoder
110
+
111
+ if TYPE_CHECKING:
112
+ from synapse.server import HomeServer
113
+
114
+ logger = logging.getLogger(__name__)
115
+
116
+
117
+ DEFAULT_NOTIF_ACTION: list[dict | str] = [
118
+ "notify",
119
+ {"set_tweak": "highlight", "value": False},
120
+ ]
121
+ DEFAULT_HIGHLIGHT_ACTION: list[dict | str] = [
122
+ "notify",
123
+ {"set_tweak": "sound", "value": "default"},
124
+ {"set_tweak": "highlight"},
125
+ ]
126
+
127
+
128
+ @attr.s(slots=True, auto_attribs=True)
129
+ class _RoomReceipt:
130
+ """
131
+ HttpPushAction instances include the information used to generate HTTP
132
+ requests to a push gateway.
133
+ """
134
+
135
+ unthreaded_stream_ordering: int = 0
136
+ # threaded_stream_ordering includes the main pseudo-thread.
137
+ threaded_stream_ordering: dict[str, int] = attr.Factory(dict)
138
+
139
+ def is_unread(self, thread_id: str, stream_ordering: int) -> bool:
140
+ """Returns True if the stream ordering is unread according to the receipt information."""
141
+
142
+ # Only include push actions with a stream ordering after both the unthreaded
143
+ # and threaded receipt. Properly handles a user without any receipts present.
144
+ return (
145
+ self.unthreaded_stream_ordering < stream_ordering
146
+ and self.threaded_stream_ordering.get(thread_id, 0) < stream_ordering
147
+ )
148
+
149
+
150
+ # A _RoomReceipt with no receipts in it.
151
+ MISSING_ROOM_RECEIPT = _RoomReceipt()
152
+
153
+
154
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
155
+ class HttpPushAction:
156
+ """
157
+ HttpPushAction instances include the information used to generate HTTP
158
+ requests to a push gateway.
159
+ """
160
+
161
+ event_id: str
162
+ room_id: str
163
+ stream_ordering: int
164
+ actions: list[dict | str]
165
+
166
+
167
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
168
+ class EmailPushAction(HttpPushAction):
169
+ """
170
+ EmailPushAction instances include the information used to render an email
171
+ push notification.
172
+ """
173
+
174
+ received_ts: int | None
175
+
176
+
177
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
178
+ class UserPushAction(EmailPushAction):
179
+ """
180
+ UserPushAction instances include the necessary information to respond to
181
+ /notifications requests.
182
+ """
183
+
184
+ topological_ordering: int
185
+ highlight: bool
186
+ profile_tag: str
187
+
188
+
189
+ # TODO This is used as a cached value and is mutable.
190
+ @attr.s(slots=True, auto_attribs=True)
191
+ class NotifCounts:
192
+ """
193
+ The per-user, per-room, per-thread count of notifications. Used by sync and push.
194
+ """
195
+
196
+ notify_count: int = 0
197
+ unread_count: int = 0
198
+ highlight_count: int = 0
199
+
200
+
201
+ @attr.s(slots=True, frozen=True, auto_attribs=True)
202
+ class RoomNotifCounts:
203
+ """
204
+ The per-user, per-room count of notifications. Used by sync and push.
205
+ """
206
+
207
+ main_timeline: NotifCounts
208
+ # Map of thread ID to the notification counts.
209
+ threads: Mapping[str, NotifCounts]
210
+
211
+ @staticmethod
212
+ def empty() -> "RoomNotifCounts":
213
+ return _EMPTY_ROOM_NOTIF_COUNTS
214
+
215
+ def __len__(self) -> int:
216
+ # To properly account for the amount of space in any caches.
217
+ return len(self.threads) + 1
218
+
219
+
220
+ _EMPTY_ROOM_NOTIF_COUNTS = RoomNotifCounts(NotifCounts(), {})
221
+
222
+
223
+ def _serialize_action(actions: Collection[Mapping | str], is_highlight: bool) -> str:
224
+ """Custom serializer for actions. This allows us to "compress" common actions.
225
+
226
+ We use the fact that most users have the same actions for notifs (and for
227
+ highlights).
228
+ We store these default actions as the empty string rather than the full JSON.
229
+ Since the empty string isn't valid JSON there is no risk of this clashing with
230
+ any real JSON actions
231
+ """
232
+ if is_highlight:
233
+ if actions == DEFAULT_HIGHLIGHT_ACTION:
234
+ return "" # We use empty string as the column is non-NULL
235
+ else:
236
+ if actions == DEFAULT_NOTIF_ACTION:
237
+ return ""
238
+ return json_encoder.encode(actions)
239
+
240
+
241
+ def _deserialize_action(actions: str, is_highlight: bool) -> list[dict | str]:
242
+ """Custom deserializer for actions. This allows us to "compress" common actions"""
243
+ if actions:
244
+ return db_to_json(actions)
245
+
246
+ if is_highlight:
247
+ return DEFAULT_HIGHLIGHT_ACTION
248
+ else:
249
+ return DEFAULT_NOTIF_ACTION
250
+
251
+
252
+ class EventPushActionsWorkerStore(ReceiptsWorkerStore, StreamWorkerStore, SQLBaseStore):
253
+ _background_tasks: list[LoopingCall] = []
254
+
255
+ def __init__(
256
+ self,
257
+ database: DatabasePool,
258
+ db_conn: LoggingDatabaseConnection,
259
+ hs: "HomeServer",
260
+ ):
261
+ super().__init__(database, db_conn, hs)
262
+
263
+ # Track when the process started.
264
+ self._started_ts = self.clock.time_msec()
265
+
266
+ # These get correctly set by _find_stream_orderings_for_times_txn
267
+ self.stream_ordering_month_ago: int | None = None
268
+ self.stream_ordering_day_ago: int | None = None
269
+
270
+ cur = db_conn.cursor(txn_name="_find_stream_orderings_for_times_txn")
271
+ self._find_stream_orderings_for_times_txn(cur)
272
+ cur.close()
273
+
274
+ self.clock.looping_call(
275
+ self._find_stream_orderings_for_times, Duration(minutes=10)
276
+ )
277
+
278
+ self._rotate_count = 10000
279
+ self._doing_notif_rotation = False
280
+ if hs.config.worker.run_background_tasks:
281
+ self.clock.looping_call(self._rotate_notifs, Duration(seconds=30))
282
+
283
+ self.clock.looping_call(
284
+ self._clear_old_push_actions_staging, Duration(minutes=30)
285
+ )
286
+
287
+ self.db_pool.updates.register_background_index_update(
288
+ "event_push_summary_unique_index2",
289
+ index_name="event_push_summary_unique_index2",
290
+ table="event_push_summary",
291
+ columns=["user_id", "room_id", "thread_id"],
292
+ unique=True,
293
+ )
294
+
295
+ self.db_pool.updates.register_background_validate_constraint(
296
+ "event_push_actions_staging_thread_id",
297
+ constraint_name="event_push_actions_staging_thread_id",
298
+ table="event_push_actions_staging",
299
+ )
300
+ self.db_pool.updates.register_background_validate_constraint(
301
+ "event_push_actions_thread_id",
302
+ constraint_name="event_push_actions_thread_id",
303
+ table="event_push_actions",
304
+ )
305
+ self.db_pool.updates.register_background_validate_constraint(
306
+ "event_push_summary_thread_id",
307
+ constraint_name="event_push_summary_thread_id",
308
+ table="event_push_summary",
309
+ )
310
+
311
+ self.db_pool.updates.register_background_update_handler(
312
+ "event_push_drop_null_thread_id_indexes",
313
+ self._background_drop_null_thread_id_indexes,
314
+ )
315
+
316
+ # Add a room ID index to speed up room deletion
317
+ self.db_pool.updates.register_background_index_update(
318
+ "event_push_summary_index_room_id",
319
+ index_name="event_push_summary_index_room_id",
320
+ table="event_push_summary",
321
+ columns=["room_id"],
322
+ )
323
+
324
+ async def _background_drop_null_thread_id_indexes(
325
+ self, progress: JsonDict, batch_size: int
326
+ ) -> int:
327
+ """
328
+ Drop the indexes used to find null thread_ids for event_push_actions and
329
+ event_push_summary.
330
+ """
331
+
332
+ def drop_null_thread_id_indexes_txn(txn: LoggingTransaction) -> None:
333
+ sql = "DROP INDEX IF EXISTS event_push_actions_thread_id_null"
334
+ logger.debug("[SQL] %s", sql)
335
+ txn.execute(sql)
336
+
337
+ sql = "DROP INDEX IF EXISTS event_push_summary_thread_id_null"
338
+ logger.debug("[SQL] %s", sql)
339
+ txn.execute(sql)
340
+
341
+ await self.db_pool.runInteraction(
342
+ "drop_null_thread_id_indexes_txn",
343
+ drop_null_thread_id_indexes_txn,
344
+ )
345
+ await self.db_pool.updates._end_background_update(
346
+ "event_push_drop_null_thread_id_indexes"
347
+ )
348
+ return 0
349
+
350
+ async def get_unread_counts_by_room_for_user(self, user_id: str) -> dict[str, int]:
351
+ """Get the notification count by room for a user. Only considers notifications,
352
+ not highlight or unread counts, and threads are currently aggregated under their room.
353
+
354
+ This function is intentionally not cached because it is called to calculate the
355
+ unread badge for push notifications and thus the result is expected to change.
356
+
357
+ Note that this function assumes the user is a member of the room. Because
358
+ summary rows are not removed when a user leaves a room, the caller must
359
+ filter out those results from the result.
360
+
361
+ Returns:
362
+ A map of room ID to notification counts for the given user.
363
+ """
364
+ return await self.db_pool.runInteraction(
365
+ "get_unread_counts_by_room_for_user",
366
+ self._get_unread_counts_by_room_for_user_txn,
367
+ user_id,
368
+ )
369
+
370
+ def _get_unread_counts_by_room_for_user_txn(
371
+ self, txn: LoggingTransaction, user_id: str
372
+ ) -> dict[str, int]:
373
+ receipt_types_clause, args = make_in_list_sql_clause(
374
+ self.database_engine,
375
+ "receipt_type",
376
+ (ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
377
+ )
378
+ args.extend([user_id, user_id])
379
+
380
+ receipts_cte = f"""
381
+ WITH all_receipts AS (
382
+ SELECT room_id, thread_id, MAX(event_stream_ordering) AS max_receipt_stream_ordering
383
+ FROM receipts_linearized
384
+ WHERE
385
+ {receipt_types_clause}
386
+ AND user_id = ?
387
+ GROUP BY room_id, thread_id
388
+ )
389
+ """
390
+
391
+ receipts_joins = """
392
+ LEFT JOIN (
393
+ SELECT room_id, thread_id,
394
+ max_receipt_stream_ordering AS threaded_receipt_stream_ordering
395
+ FROM all_receipts
396
+ WHERE thread_id IS NOT NULL
397
+ ) AS threaded_receipts USING (room_id, thread_id)
398
+ LEFT JOIN (
399
+ SELECT room_id, thread_id,
400
+ max_receipt_stream_ordering AS unthreaded_receipt_stream_ordering
401
+ FROM all_receipts
402
+ WHERE thread_id IS NULL
403
+ ) AS unthreaded_receipts USING (room_id)
404
+ """
405
+
406
+ # First get summary counts by room / thread for the user. We use the max receipt
407
+ # stream ordering of both threaded & unthreaded receipts to compare against the
408
+ # summary table.
409
+ #
410
+ # PostgreSQL and SQLite differ in comparing scalar numerics.
411
+ if isinstance(self.database_engine, PostgresEngine):
412
+ # GREATEST ignores NULLs.
413
+ max_clause = """GREATEST(
414
+ threaded_receipt_stream_ordering,
415
+ unthreaded_receipt_stream_ordering
416
+ )"""
417
+ else:
418
+ # MAX returns NULL if any are NULL, so COALESCE to 0 first.
419
+ max_clause = """MAX(
420
+ COALESCE(threaded_receipt_stream_ordering, 0),
421
+ COALESCE(unthreaded_receipt_stream_ordering, 0)
422
+ )"""
423
+
424
+ sql = f"""
425
+ {receipts_cte}
426
+ SELECT eps.room_id, eps.thread_id, notif_count
427
+ FROM event_push_summary AS eps
428
+ {receipts_joins}
429
+ WHERE user_id = ?
430
+ AND notif_count != 0
431
+ AND (
432
+ (last_receipt_stream_ordering IS NULL AND stream_ordering > {max_clause})
433
+ OR last_receipt_stream_ordering = {max_clause}
434
+ )
435
+ """
436
+ txn.execute(sql, args)
437
+
438
+ seen_thread_ids = set()
439
+ room_to_count: dict[str, int] = defaultdict(int)
440
+
441
+ for room_id, thread_id, notif_count in txn:
442
+ room_to_count[room_id] += notif_count
443
+ seen_thread_ids.add(thread_id)
444
+
445
+ # Now get any event push actions that haven't been rotated using the same OR
446
+ # join and filter by receipt and event push summary rotated up to stream ordering.
447
+ sql = f"""
448
+ {receipts_cte}
449
+ SELECT epa.room_id, epa.thread_id, COUNT(CASE WHEN epa.notif = 1 THEN 1 END) AS notif_count
450
+ FROM event_push_actions AS epa
451
+ {receipts_joins}
452
+ WHERE user_id = ?
453
+ AND epa.notif = 1
454
+ AND stream_ordering > (SELECT stream_ordering FROM event_push_summary_stream_ordering)
455
+ AND (threaded_receipt_stream_ordering IS NULL OR stream_ordering > threaded_receipt_stream_ordering)
456
+ AND (unthreaded_receipt_stream_ordering IS NULL OR stream_ordering > unthreaded_receipt_stream_ordering)
457
+ GROUP BY epa.room_id, epa.thread_id
458
+ """
459
+ txn.execute(sql, args)
460
+
461
+ for room_id, thread_id, notif_count in txn:
462
+ # Note: only count push actions we have valid summaries for with up to date receipt.
463
+ if thread_id not in seen_thread_ids:
464
+ continue
465
+ room_to_count[room_id] += notif_count
466
+
467
+ thread_id_clause, thread_ids_args = make_in_list_sql_clause(
468
+ self.database_engine, "epa.thread_id", seen_thread_ids
469
+ )
470
+
471
+ # Finally re-check event_push_actions for any rooms not in the summary, ignoring
472
+ # the rotated up-to position. This handles the case where a read receipt has arrived
473
+ # but not been rotated meaning the summary table is out of date, so we go back to
474
+ # the push actions table.
475
+ sql = f"""
476
+ {receipts_cte}
477
+ SELECT epa.room_id, COUNT(CASE WHEN epa.notif = 1 THEN 1 END) AS notif_count
478
+ FROM event_push_actions AS epa
479
+ {receipts_joins}
480
+ WHERE user_id = ?
481
+ AND NOT {thread_id_clause}
482
+ AND epa.notif = 1
483
+ AND (threaded_receipt_stream_ordering IS NULL OR stream_ordering > threaded_receipt_stream_ordering)
484
+ AND (unthreaded_receipt_stream_ordering IS NULL OR stream_ordering > unthreaded_receipt_stream_ordering)
485
+ GROUP BY epa.room_id
486
+ """
487
+
488
+ args.extend(thread_ids_args)
489
+ txn.execute(sql, args)
490
+
491
+ for room_id, notif_count in txn:
492
+ room_to_count[room_id] += notif_count
493
+
494
+ return room_to_count
495
+
496
+ @cached(tree=True, max_entries=5000, iterable=True) # type: ignore[synapse-@cached-mutable]
497
+ async def get_unread_event_push_actions_by_room_for_user(
498
+ self,
499
+ room_id: str,
500
+ user_id: str,
501
+ ) -> RoomNotifCounts:
502
+ """Get the notification count, the highlight count and the unread message count
503
+ for a given user in a given room after their latest read receipt.
504
+
505
+ Note that this function assumes the user to be a current member of the room,
506
+ since it's either called by the sync handler to handle joined room entries, or by
507
+ the HTTP pusher to calculate the badge of unread joined rooms.
508
+
509
+ Args:
510
+ room_id: The room to retrieve the counts in.
511
+ user_id: The user to retrieve the counts for.
512
+
513
+ Returns
514
+ A RoomNotifCounts object containing the notification count, the
515
+ highlight count and the unread message count for both the main timeline
516
+ and threads.
517
+ """
518
+ return await self.db_pool.runInteraction(
519
+ "get_unread_event_push_actions_by_room",
520
+ self._get_unread_counts_by_receipt_txn,
521
+ room_id,
522
+ user_id,
523
+ )
524
+
525
+ def _get_unread_counts_by_receipt_txn(
526
+ self,
527
+ txn: LoggingTransaction,
528
+ room_id: str,
529
+ user_id: str,
530
+ ) -> RoomNotifCounts:
531
+ # Get the stream ordering of the user's latest receipt in the room.
532
+ result = self.get_last_unthreaded_receipt_for_user_txn(
533
+ txn,
534
+ user_id,
535
+ room_id,
536
+ receipt_types=(ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
537
+ )
538
+
539
+ if result:
540
+ _, stream_ordering = result
541
+
542
+ else:
543
+ # If the user has no receipts in the room, retrieve the stream ordering for
544
+ # the latest membership event from this user in this room (which we assume is
545
+ # a join).
546
+ event_id = self.db_pool.simple_select_one_onecol_txn(
547
+ txn=txn,
548
+ table="local_current_membership",
549
+ keyvalues={"room_id": room_id, "user_id": user_id},
550
+ retcol="event_id",
551
+ )
552
+
553
+ stream_ordering = self.get_stream_id_for_event_txn(txn, event_id)
554
+
555
+ return self._get_unread_counts_by_pos_txn(
556
+ txn, room_id, user_id, stream_ordering
557
+ )
558
+
559
+ def _get_unread_counts_by_pos_txn(
560
+ self,
561
+ txn: LoggingTransaction,
562
+ room_id: str,
563
+ user_id: str,
564
+ unthreaded_receipt_stream_ordering: int,
565
+ ) -> RoomNotifCounts:
566
+ """Get the number of unread messages for a user/room that have happened
567
+ since the given stream ordering.
568
+
569
+ Args:
570
+ txn: The database transaction.
571
+ room_id: The room ID to get unread counts for.
572
+ user_id: The user ID to get unread counts for.
573
+ unthreaded_receipt_stream_ordering: The stream ordering of the user's latest
574
+ unthreaded receipt in the room. If there are no unthreaded receipts,
575
+ the stream ordering of the user's join event.
576
+
577
+ Returns:
578
+ A RoomNotifCounts object containing the notification count, the
579
+ highlight count and the unread message count for both the main timeline
580
+ and threads.
581
+ """
582
+
583
+ main_counts = NotifCounts()
584
+ thread_counts: dict[str, NotifCounts] = {}
585
+
586
+ def _get_thread(thread_id: str) -> NotifCounts:
587
+ if thread_id == MAIN_TIMELINE:
588
+ return main_counts
589
+ return thread_counts.setdefault(thread_id, NotifCounts())
590
+
591
+ receipt_types_clause, receipts_args = make_in_list_sql_clause(
592
+ self.database_engine,
593
+ "receipt_type",
594
+ (ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
595
+ )
596
+
597
+ # First we pull the counts from the summary table.
598
+ #
599
+ # We check that `last_receipt_stream_ordering` matches the stream ordering of the
600
+ # latest receipt for the thread (which may be either the unthreaded read receipt
601
+ # or the threaded read receipt).
602
+ #
603
+ # If it doesn't match then a new read receipt has arrived and we haven't yet
604
+ # updated the counts in `event_push_summary` to reflect that; in that case we
605
+ # simply ignore `event_push_summary` counts.
606
+ #
607
+ # We then do a manual count of all the rows in the `event_push_actions` table
608
+ # for any user/room/thread which did not have a valid summary found.
609
+ #
610
+ # If `last_receipt_stream_ordering` is null then that means it's up-to-date
611
+ # (as the row was written by an older version of Synapse that
612
+ # updated `event_push_summary` synchronously when persisting a new read
613
+ # receipt).
614
+ txn.execute(
615
+ f"""
616
+ SELECT notif_count, COALESCE(unread_count, 0), thread_id
617
+ FROM event_push_summary
618
+ LEFT JOIN (
619
+ SELECT thread_id, MAX(event_stream_ordering) AS threaded_receipt_stream_ordering
620
+ FROM receipts_linearized
621
+ WHERE
622
+ user_id = ?
623
+ AND room_id = ?
624
+ AND event_stream_ordering > ?
625
+ AND {receipt_types_clause}
626
+ GROUP BY thread_id
627
+ ) AS receipts USING (thread_id)
628
+ WHERE room_id = ? AND user_id = ?
629
+ AND (
630
+ (last_receipt_stream_ordering IS NULL AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?))
631
+ OR last_receipt_stream_ordering = COALESCE(threaded_receipt_stream_ordering, ?)
632
+ ) AND (notif_count != 0 OR COALESCE(unread_count, 0) != 0)
633
+ """,
634
+ (
635
+ user_id,
636
+ room_id,
637
+ unthreaded_receipt_stream_ordering,
638
+ *receipts_args,
639
+ room_id,
640
+ user_id,
641
+ unthreaded_receipt_stream_ordering,
642
+ unthreaded_receipt_stream_ordering,
643
+ ),
644
+ )
645
+ summarised_threads = set()
646
+ for notif_count, unread_count, thread_id in txn:
647
+ summarised_threads.add(thread_id)
648
+ counts = _get_thread(thread_id)
649
+ counts.notify_count += notif_count
650
+ counts.unread_count += unread_count
651
+
652
+ # Next we need to count highlights, which aren't summarised
653
+ sql = f"""
654
+ SELECT COUNT(*), thread_id FROM event_push_actions
655
+ LEFT JOIN (
656
+ SELECT thread_id, MAX(event_stream_ordering) AS threaded_receipt_stream_ordering
657
+ FROM receipts_linearized
658
+ WHERE
659
+ user_id = ?
660
+ AND room_id = ?
661
+ AND event_stream_ordering > ?
662
+ AND {receipt_types_clause}
663
+ GROUP BY thread_id
664
+ ) AS receipts USING (thread_id)
665
+ WHERE user_id = ?
666
+ AND room_id = ?
667
+ AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?)
668
+ AND highlight = 1
669
+ GROUP BY thread_id
670
+ """
671
+ txn.execute(
672
+ sql,
673
+ (
674
+ user_id,
675
+ room_id,
676
+ unthreaded_receipt_stream_ordering,
677
+ *receipts_args,
678
+ user_id,
679
+ room_id,
680
+ unthreaded_receipt_stream_ordering,
681
+ ),
682
+ )
683
+ for highlight_count, thread_id in txn:
684
+ _get_thread(thread_id).highlight_count += highlight_count
685
+
686
+ # For threads which were summarised we need to count actions since the last
687
+ # rotation.
688
+ thread_id_clause, thread_id_args = make_in_list_sql_clause(
689
+ self.database_engine, "thread_id", summarised_threads
690
+ )
691
+
692
+ # The (inclusive) event stream ordering that was previously summarised.
693
+ rotated_upto_stream_ordering = self.db_pool.simple_select_one_onecol_txn(
694
+ txn,
695
+ table="event_push_summary_stream_ordering",
696
+ keyvalues={},
697
+ retcol="stream_ordering",
698
+ )
699
+
700
+ unread_counts = self._get_notif_unread_count_for_user_room(
701
+ txn, room_id, user_id, rotated_upto_stream_ordering
702
+ )
703
+ for notif_count, unread_count, thread_id in unread_counts:
704
+ if thread_id not in summarised_threads:
705
+ continue
706
+
707
+ if thread_id == MAIN_TIMELINE:
708
+ counts.notify_count += notif_count
709
+ counts.unread_count += unread_count
710
+ elif thread_id in thread_counts:
711
+ thread_counts[thread_id].notify_count += notif_count
712
+ thread_counts[thread_id].unread_count += unread_count
713
+ else:
714
+ # Previous thread summaries of 0 are discarded above.
715
+ #
716
+ # TODO If empty summaries are deleted this can be removed.
717
+ thread_counts[thread_id] = NotifCounts(
718
+ notify_count=notif_count,
719
+ unread_count=unread_count,
720
+ highlight_count=0,
721
+ )
722
+
723
+ # Finally we need to count push actions that aren't included in the
724
+ # summary returned above. This might be due to recent events that haven't
725
+ # been summarised yet or the summary is out of date due to a recent read
726
+ # receipt.
727
+ sql = f"""
728
+ SELECT
729
+ COUNT(CASE WHEN notif = 1 THEN 1 END),
730
+ COUNT(CASE WHEN unread = 1 THEN 1 END),
731
+ thread_id
732
+ FROM event_push_actions
733
+ LEFT JOIN (
734
+ SELECT thread_id, MAX(event_stream_ordering) AS threaded_receipt_stream_ordering
735
+ FROM receipts_linearized
736
+ WHERE
737
+ user_id = ?
738
+ AND room_id = ?
739
+ AND event_stream_ordering > ?
740
+ AND {receipt_types_clause}
741
+ GROUP BY thread_id
742
+ ) AS receipts USING (thread_id)
743
+ WHERE user_id = ?
744
+ AND room_id = ?
745
+ AND stream_ordering > COALESCE(threaded_receipt_stream_ordering, ?)
746
+ AND NOT {thread_id_clause}
747
+ GROUP BY thread_id
748
+ """
749
+ txn.execute(
750
+ sql,
751
+ (
752
+ user_id,
753
+ room_id,
754
+ unthreaded_receipt_stream_ordering,
755
+ *receipts_args,
756
+ user_id,
757
+ room_id,
758
+ unthreaded_receipt_stream_ordering,
759
+ *thread_id_args,
760
+ ),
761
+ )
762
+ for notif_count, unread_count, thread_id in txn:
763
+ counts = _get_thread(thread_id)
764
+ counts.notify_count += notif_count
765
+ counts.unread_count += unread_count
766
+
767
+ return RoomNotifCounts(main_counts, thread_counts)
768
+
769
+ def _get_notif_unread_count_for_user_room(
770
+ self,
771
+ txn: LoggingTransaction,
772
+ room_id: str,
773
+ user_id: str,
774
+ stream_ordering: int,
775
+ max_stream_ordering: int | None = None,
776
+ thread_id: str | None = None,
777
+ ) -> list[tuple[int, int, str]]:
778
+ """Returns the notify and unread counts from `event_push_actions` for
779
+ the given user/room in the given range.
780
+
781
+ Does not consult `event_push_summary` table, which may include push
782
+ actions that have been deleted from `event_push_actions` table.
783
+
784
+ Args:
785
+ txn: The database transaction.
786
+ room_id: The room ID to get unread counts for.
787
+ user_id: The user ID to get unread counts for.
788
+ stream_ordering: The (exclusive) minimum stream ordering to consider.
789
+ max_stream_ordering: The (inclusive) maximum stream ordering to consider.
790
+ If this is not given, then no maximum is applied.
791
+ thread_id: The thread ID to fetch unread counts for. If this is not provided
792
+ then the results for *all* threads is returned.
793
+
794
+ Note that if this is provided the resulting list will only have 0 or
795
+ 1 tuples in it.
796
+
797
+ Return:
798
+ A tuple of the notif count and unread count in the given range for
799
+ each thread.
800
+ """
801
+
802
+ # If there have been no events in the room since the stream ordering,
803
+ # there can't be any push actions either.
804
+ if not self._events_stream_cache.has_entity_changed(room_id, stream_ordering):
805
+ return []
806
+
807
+ stream_ordering_clause = ""
808
+ args = [user_id, room_id, stream_ordering]
809
+ if max_stream_ordering is not None:
810
+ stream_ordering_clause = "AND ea.stream_ordering <= ?"
811
+ args.append(max_stream_ordering)
812
+
813
+ # If the max stream ordering is less than the min stream ordering,
814
+ # then obviously there are zero push actions in that range.
815
+ if max_stream_ordering <= stream_ordering:
816
+ return []
817
+
818
+ # Either limit the results to a specific thread or fetch all threads.
819
+ thread_id_clause = ""
820
+ if thread_id is not None:
821
+ thread_id_clause = "AND thread_id = ?"
822
+ args.append(thread_id)
823
+
824
+ sql = f"""
825
+ SELECT
826
+ COUNT(CASE WHEN notif = 1 THEN 1 END),
827
+ COUNT(CASE WHEN unread = 1 THEN 1 END),
828
+ thread_id
829
+ FROM event_push_actions ea
830
+ WHERE user_id = ?
831
+ AND room_id = ?
832
+ AND ea.stream_ordering > ?
833
+ {stream_ordering_clause}
834
+ {thread_id_clause}
835
+ GROUP BY thread_id
836
+ """
837
+
838
+ txn.execute(sql, args)
839
+ return cast(list[tuple[int, int, str]], txn.fetchall())
840
+
841
+ async def get_push_action_users_in_range(
842
+ self, min_stream_ordering: int, max_stream_ordering: int
843
+ ) -> list[str]:
844
+ def f(txn: LoggingTransaction) -> list[str]:
845
+ sql = (
846
+ "SELECT DISTINCT(user_id) FROM event_push_actions WHERE"
847
+ " stream_ordering >= ? AND stream_ordering <= ? AND notif = 1"
848
+ )
849
+ txn.execute(sql, (min_stream_ordering, max_stream_ordering))
850
+ return [r[0] for r in txn]
851
+
852
+ return await self.db_pool.runInteraction("get_push_action_users_in_range", f)
853
+
854
+ def _get_receipts_for_room_and_threads_txn(
855
+ self,
856
+ txn: LoggingTransaction,
857
+ user_id: str,
858
+ room_ids: StrCollection,
859
+ thread_ids: StrCollection,
860
+ ) -> dict[str, _RoomReceipt]:
861
+ """
862
+ Get (private) read receipts for a user in each of the given room IDs
863
+ and thread IDs.
864
+
865
+ Note: The corresponding room ID for each thread must appear in
866
+ `room_ids` arg.
867
+
868
+ Returns:
869
+ A map including all rooms the user is in with a receipt. It maps
870
+ room IDs to _RoomReceipt instances
871
+ """
872
+
873
+ receipt_types_clause, receipts_args = make_in_list_sql_clause(
874
+ self.database_engine,
875
+ "receipt_type",
876
+ (ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
877
+ )
878
+
879
+ thread_ids_clause, thread_ids_args = make_in_list_sql_clause(
880
+ self.database_engine,
881
+ "thread_id",
882
+ thread_ids,
883
+ )
884
+
885
+ room_ids_clause, room_ids_args = make_in_list_sql_clause(
886
+ self.database_engine,
887
+ "room_id",
888
+ room_ids,
889
+ )
890
+
891
+ # We use the union of two (almost identical) queries here, the first to
892
+ # fetch the specific thread receipts and the second to fetch the
893
+ # unthreaded receipts.
894
+ #
895
+ # This SQL is optimized to use the indices we have on
896
+ # `receipts_linearized`.
897
+ #
898
+ # We compare room ID and thread IDs independently due to the above,
899
+ # which means that this query might return more rows than we need if the
900
+ # same thread ID appears across different rooms (e.g. 'main' thread ID).
901
+ # This doesn't cause any logic issues, and isn't a performance concern
902
+ # given this function generally gets called with only one room and
903
+ # thread ID.
904
+ sql = f"""
905
+ SELECT room_id, thread_id, MAX(event_stream_ordering)
906
+ FROM receipts_linearized
907
+ WHERE {receipt_types_clause}
908
+ AND {thread_ids_clause}
909
+ AND {room_ids_clause}
910
+ AND user_id = ?
911
+ GROUP BY room_id, thread_id
912
+
913
+ UNION ALL
914
+
915
+ SELECT room_id, thread_id, MAX(stream_ordering)
916
+ FROM receipts_linearized
917
+ INNER JOIN events USING (room_id, event_id)
918
+ WHERE {receipt_types_clause}
919
+ AND {room_ids_clause}
920
+ AND thread_id IS NULL
921
+ AND user_id = ?
922
+ GROUP BY room_id, thread_id
923
+ """
924
+
925
+ args = list(receipts_args)
926
+ args.extend(thread_ids_args)
927
+ args.extend(room_ids_args)
928
+ args.append(user_id)
929
+ args.extend(receipts_args)
930
+ args.extend(room_ids_args)
931
+ args.append(user_id)
932
+
933
+ txn.execute(sql, args)
934
+
935
+ result: dict[str, _RoomReceipt] = {}
936
+ for room_id, thread_id, stream_ordering in txn:
937
+ room_receipt = result.setdefault(room_id, _RoomReceipt())
938
+ if thread_id is None:
939
+ room_receipt.unthreaded_stream_ordering = stream_ordering
940
+ else:
941
+ room_receipt.threaded_stream_ordering[thread_id] = stream_ordering
942
+
943
+ return result
944
+
945
+ async def get_unread_push_actions_for_user_in_range_for_http(
946
+ self,
947
+ user_id: str,
948
+ min_stream_ordering: int,
949
+ max_stream_ordering: int,
950
+ limit: int = 20,
951
+ ) -> list[HttpPushAction]:
952
+ """Get a list of the most recent unread push actions for a given user,
953
+ within the given stream ordering range. Called by the httppusher.
954
+
955
+ Args:
956
+ user_id: The user to fetch push actions for.
957
+ min_stream_ordering: The exclusive lower bound on the
958
+ stream ordering of event push actions to fetch.
959
+ max_stream_ordering: The inclusive upper bound on the
960
+ stream ordering of event push actions to fetch.
961
+ limit: The maximum number of rows to return.
962
+ Returns:
963
+ A list of dicts with the keys "event_id", "room_id", "stream_ordering", "actions".
964
+ The list will be ordered by ascending stream_ordering.
965
+ The list will have between 0~limit entries.
966
+ """
967
+
968
+ def get_push_actions_txn(
969
+ txn: LoggingTransaction,
970
+ ) -> list[tuple[str, str, str, int, str, bool]]:
971
+ sql = """
972
+ SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
973
+ ep.actions, ep.highlight
974
+ FROM event_push_actions AS ep
975
+ WHERE
976
+ ep.user_id = ?
977
+ AND ep.stream_ordering > ?
978
+ AND ep.stream_ordering <= ?
979
+ AND ep.notif = 1
980
+ ORDER BY ep.stream_ordering ASC LIMIT ?
981
+ """
982
+ txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
983
+ return cast(list[tuple[str, str, str, int, str, bool]], txn.fetchall())
984
+
985
+ push_actions = await self.db_pool.runInteraction(
986
+ "get_unread_push_actions_for_user_in_range_http", get_push_actions_txn
987
+ )
988
+
989
+ room_ids = set()
990
+ thread_ids = []
991
+ for (
992
+ _,
993
+ room_id,
994
+ thread_id,
995
+ _,
996
+ _,
997
+ _,
998
+ ) in push_actions:
999
+ room_ids.add(room_id)
1000
+ thread_ids.append(thread_id)
1001
+
1002
+ receipts_by_room = await self.db_pool.runInteraction(
1003
+ "get_unread_push_actions_for_user_in_range_http_receipts",
1004
+ self._get_receipts_for_room_and_threads_txn,
1005
+ user_id=user_id,
1006
+ room_ids=room_ids,
1007
+ thread_ids=thread_ids,
1008
+ )
1009
+
1010
+ notifs = [
1011
+ HttpPushAction(
1012
+ event_id=event_id,
1013
+ room_id=room_id,
1014
+ stream_ordering=stream_ordering,
1015
+ actions=_deserialize_action(actions, highlight),
1016
+ )
1017
+ for event_id, room_id, thread_id, stream_ordering, actions, highlight in push_actions
1018
+ if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
1019
+ thread_id, stream_ordering
1020
+ )
1021
+ ]
1022
+
1023
+ # Now sort it so it's ordered correctly, since currently it will
1024
+ # contain results from the first query, correctly ordered, followed
1025
+ # by results from the second query, but we want them all ordered
1026
+ # by stream_ordering, oldest first.
1027
+ notifs.sort(key=lambda r: r.stream_ordering)
1028
+
1029
+ # Take only up to the limit. We have to stop at the limit because
1030
+ # one of the subqueries may have hit the limit.
1031
+ return notifs[:limit]
1032
+
1033
+ async def get_unread_push_actions_for_user_in_range_for_email(
1034
+ self,
1035
+ user_id: str,
1036
+ min_stream_ordering: int,
1037
+ max_stream_ordering: int,
1038
+ limit: int = 20,
1039
+ ) -> list[EmailPushAction]:
1040
+ """Get a list of the most recent unread push actions for a given user,
1041
+ within the given stream ordering range. Called by the emailpusher
1042
+
1043
+ Args:
1044
+ user_id: The user to fetch push actions for.
1045
+ min_stream_ordering: The exclusive lower bound on the
1046
+ stream ordering of event push actions to fetch.
1047
+ max_stream_ordering: The inclusive upper bound on the
1048
+ stream ordering of event push actions to fetch.
1049
+ limit: The maximum number of rows to return.
1050
+ Returns:
1051
+ A list of dicts with the keys "event_id", "room_id", "stream_ordering", "actions", "received_ts".
1052
+ The list will be ordered by descending received_ts.
1053
+ The list will have between 0~limit entries.
1054
+ """
1055
+
1056
+ def get_push_actions_txn(
1057
+ txn: LoggingTransaction,
1058
+ ) -> list[tuple[str, str, str, int, str, bool, int]]:
1059
+ sql = """
1060
+ SELECT ep.event_id, ep.room_id, ep.thread_id, ep.stream_ordering,
1061
+ ep.actions, ep.highlight, e.received_ts
1062
+ FROM event_push_actions AS ep
1063
+ INNER JOIN events AS e USING (room_id, event_id)
1064
+ WHERE
1065
+ ep.user_id = ?
1066
+ AND ep.stream_ordering > ?
1067
+ AND ep.stream_ordering <= ?
1068
+ AND ep.notif = 1
1069
+ ORDER BY ep.stream_ordering DESC LIMIT ?
1070
+ """
1071
+ txn.execute(sql, (user_id, min_stream_ordering, max_stream_ordering, limit))
1072
+ return cast(list[tuple[str, str, str, int, str, bool, int]], txn.fetchall())
1073
+
1074
+ push_actions = await self.db_pool.runInteraction(
1075
+ "get_unread_push_actions_for_user_in_range_email", get_push_actions_txn
1076
+ )
1077
+
1078
+ room_ids = set()
1079
+ thread_ids = []
1080
+ for (
1081
+ _,
1082
+ room_id,
1083
+ thread_id,
1084
+ _,
1085
+ _,
1086
+ _,
1087
+ _,
1088
+ ) in push_actions:
1089
+ room_ids.add(room_id)
1090
+ thread_ids.append(thread_id)
1091
+
1092
+ receipts_by_room = await self.db_pool.runInteraction(
1093
+ "get_unread_push_actions_for_user_in_range_email_receipts",
1094
+ self._get_receipts_for_room_and_threads_txn,
1095
+ user_id=user_id,
1096
+ room_ids=room_ids,
1097
+ thread_ids=thread_ids,
1098
+ )
1099
+
1100
+ # Make a list of dicts from the two sets of results.
1101
+ notifs = [
1102
+ EmailPushAction(
1103
+ event_id=event_id,
1104
+ room_id=room_id,
1105
+ stream_ordering=stream_ordering,
1106
+ actions=_deserialize_action(actions, highlight),
1107
+ received_ts=received_ts,
1108
+ )
1109
+ for event_id, room_id, thread_id, stream_ordering, actions, highlight, received_ts in push_actions
1110
+ if receipts_by_room.get(room_id, MISSING_ROOM_RECEIPT).is_unread(
1111
+ thread_id, stream_ordering
1112
+ )
1113
+ ]
1114
+
1115
+ # Now sort it so it's ordered correctly, since currently it will
1116
+ # contain results from the first query, correctly ordered, followed
1117
+ # by results from the second query, but we want them all ordered
1118
+ # by received_ts (most recent first)
1119
+ notifs.sort(key=lambda r: -(r.received_ts or 0))
1120
+
1121
+ # Now return the first `limit`
1122
+ return notifs[:limit]
1123
+
1124
+ async def get_if_maybe_push_in_range_for_user(
1125
+ self, user_id: str, min_stream_ordering: int
1126
+ ) -> bool:
1127
+ """A fast check to see if there might be something to push for the
1128
+ user since the given stream ordering. May return false positives.
1129
+
1130
+ Useful to know whether to bother starting a pusher on start up or not.
1131
+
1132
+ Args:
1133
+ user_id
1134
+ min_stream_ordering
1135
+
1136
+ Returns:
1137
+ True if there may be push to process, False if there definitely isn't.
1138
+ """
1139
+
1140
+ def _get_if_maybe_push_in_range_for_user_txn(txn: LoggingTransaction) -> bool:
1141
+ sql = """
1142
+ SELECT 1 FROM event_push_actions
1143
+ WHERE user_id = ? AND stream_ordering > ? AND notif = 1
1144
+ LIMIT 1
1145
+ """
1146
+
1147
+ txn.execute(sql, (user_id, min_stream_ordering))
1148
+ return bool(txn.fetchone())
1149
+
1150
+ return await self.db_pool.runInteraction(
1151
+ "get_if_maybe_push_in_range_for_user",
1152
+ _get_if_maybe_push_in_range_for_user_txn,
1153
+ )
1154
+
1155
+ async def add_push_actions_to_staging(
1156
+ self,
1157
+ event_id: str,
1158
+ user_id_actions: dict[str, Collection[Mapping | str]],
1159
+ count_as_unread: bool,
1160
+ thread_id: str,
1161
+ ) -> None:
1162
+ """Add the push actions for the event to the push action staging area.
1163
+
1164
+ Args:
1165
+ event_id
1166
+ user_id_actions: A mapping of user_id to list of push actions, where
1167
+ an action can either be a string or dict.
1168
+ count_as_unread: Whether this event should increment unread counts.
1169
+ thread_id: The thread this event is parent of, if applicable.
1170
+ """
1171
+ if not user_id_actions:
1172
+ return
1173
+
1174
+ # This is a helper function for generating the necessary tuple that
1175
+ # can be used to insert into the `event_push_actions_staging` table.
1176
+ def _gen_entry(
1177
+ user_id: str, actions: Collection[Mapping | str]
1178
+ ) -> tuple[str, str, str, int, int, int, str, int]:
1179
+ is_highlight = 1 if _action_has_highlight(actions) else 0
1180
+ notif = 1 if "notify" in actions else 0
1181
+ return (
1182
+ event_id, # event_id column
1183
+ user_id, # user_id column
1184
+ _serialize_action(actions, bool(is_highlight)), # actions column
1185
+ notif, # notif column
1186
+ is_highlight, # highlight column
1187
+ int(count_as_unread), # unread column
1188
+ thread_id, # thread_id column
1189
+ self.clock.time_msec(), # inserted_ts column
1190
+ )
1191
+
1192
+ await self.db_pool.simple_insert_many(
1193
+ "event_push_actions_staging",
1194
+ keys=(
1195
+ "event_id",
1196
+ "user_id",
1197
+ "actions",
1198
+ "notif",
1199
+ "highlight",
1200
+ "unread",
1201
+ "thread_id",
1202
+ "inserted_ts",
1203
+ ),
1204
+ values=[
1205
+ _gen_entry(user_id, actions)
1206
+ for user_id, actions in user_id_actions.items()
1207
+ ],
1208
+ desc="add_push_actions_to_staging",
1209
+ )
1210
+
1211
+ async def remove_push_actions_from_staging(self, event_id: str) -> None:
1212
+ """Called if we failed to persist the event to ensure that stale push
1213
+ actions don't build up in the DB
1214
+ """
1215
+
1216
+ try:
1217
+ await self.db_pool.simple_delete(
1218
+ table="event_push_actions_staging",
1219
+ keyvalues={"event_id": event_id},
1220
+ desc="remove_push_actions_from_staging",
1221
+ )
1222
+ except Exception:
1223
+ # this method is called from an exception handler, so propagating
1224
+ # another exception here really isn't helpful - there's nothing
1225
+ # the caller can do about it. Just log the exception and move on.
1226
+ logger.exception(
1227
+ "Error removing push actions after event persistence failure"
1228
+ )
1229
+
1230
+ @wrap_as_background_process("event_push_action_stream_orderings")
1231
+ async def _find_stream_orderings_for_times(self) -> None:
1232
+ await self.db_pool.runInteraction(
1233
+ "_find_stream_orderings_for_times",
1234
+ self._find_stream_orderings_for_times_txn,
1235
+ )
1236
+
1237
+ def _find_stream_orderings_for_times_txn(self, txn: LoggingTransaction) -> None:
1238
+ logger.info("Searching for stream ordering 1 month ago")
1239
+ self.stream_ordering_month_ago = self._find_first_stream_ordering_after_ts_txn(
1240
+ txn, self.clock.time_msec() - 30 * 24 * 60 * 60 * 1000
1241
+ )
1242
+ logger.info(
1243
+ "Found stream ordering 1 month ago: it's %d", self.stream_ordering_month_ago
1244
+ )
1245
+ logger.info("Searching for stream ordering 1 day ago")
1246
+ self.stream_ordering_day_ago = self._find_first_stream_ordering_after_ts_txn(
1247
+ txn, self.clock.time_msec() - 24 * 60 * 60 * 1000
1248
+ )
1249
+ logger.info(
1250
+ "Found stream ordering 1 day ago: it's %d", self.stream_ordering_day_ago
1251
+ )
1252
+
1253
+ async def find_first_stream_ordering_after_ts(self, ts: int) -> int:
1254
+ """Gets the stream ordering corresponding to a given timestamp.
1255
+
1256
+ Specifically, finds the stream_ordering of the first event that was
1257
+ received on or after the timestamp. This is done by a binary search on
1258
+ the events table, since there is no index on received_ts, so is
1259
+ relatively slow.
1260
+
1261
+ Args:
1262
+ ts: timestamp in millis
1263
+
1264
+ Returns:
1265
+ stream ordering of the first event received on/after the timestamp
1266
+ """
1267
+ return await self.db_pool.runInteraction(
1268
+ "_find_first_stream_ordering_after_ts_txn",
1269
+ self._find_first_stream_ordering_after_ts_txn,
1270
+ ts,
1271
+ )
1272
+
1273
+ @staticmethod
1274
+ def _find_first_stream_ordering_after_ts_txn(
1275
+ txn: LoggingTransaction, ts: int
1276
+ ) -> int:
1277
+ """
1278
+ Find the stream_ordering of the first event that was received on or
1279
+ after a given timestamp. This is relatively slow as there is no index
1280
+ on received_ts but we can then use this to delete push actions before
1281
+ this.
1282
+
1283
+ received_ts must necessarily be in the same order as stream_ordering
1284
+ and stream_ordering is indexed, so we manually binary search using
1285
+ stream_ordering
1286
+
1287
+ Args:
1288
+ txn:
1289
+ ts: timestamp to search for
1290
+
1291
+ Returns:
1292
+ The stream ordering
1293
+ """
1294
+ txn.execute("SELECT MAX(stream_ordering) FROM events")
1295
+ max_stream_ordering = cast(tuple[int | None], txn.fetchone())[0]
1296
+
1297
+ if max_stream_ordering is None:
1298
+ return 0
1299
+
1300
+ # We want the first stream_ordering in which received_ts is greater
1301
+ # than or equal to ts. Call this point X.
1302
+ #
1303
+ # We maintain the invariants:
1304
+ #
1305
+ # range_start <= X <= range_end
1306
+ #
1307
+ range_start = 0
1308
+ range_end = max_stream_ordering + 1
1309
+
1310
+ # Given a stream_ordering, look up the timestamp at that
1311
+ # stream_ordering.
1312
+ #
1313
+ # The array may be sparse (we may be missing some stream_orderings).
1314
+ # We treat the gaps as the same as having the same value as the
1315
+ # preceding entry, because we will pick the lowest stream_ordering
1316
+ # which satisfies our requirement of received_ts >= ts.
1317
+ #
1318
+ # For example, if our array of events indexed by stream_ordering is
1319
+ # [10, <none>, 20], we should treat this as being equivalent to
1320
+ # [10, 10, 20].
1321
+ #
1322
+ sql = """
1323
+ SELECT received_ts FROM events
1324
+ WHERE stream_ordering <= ?
1325
+ ORDER BY stream_ordering DESC
1326
+ LIMIT 1
1327
+ """
1328
+
1329
+ while range_end - range_start > 0:
1330
+ middle = (range_end + range_start) // 2
1331
+ txn.execute(sql, (middle,))
1332
+ row = txn.fetchone()
1333
+ if row is None:
1334
+ # no rows with stream_ordering<=middle
1335
+ range_start = middle + 1
1336
+ continue
1337
+
1338
+ middle_ts = row[0]
1339
+ if ts > middle_ts:
1340
+ # we got a timestamp lower than the one we were looking for.
1341
+ # definitely need to look higher: X > middle.
1342
+ range_start = middle + 1
1343
+ else:
1344
+ # we got a timestamp higher than (or the same as) the one we
1345
+ # were looking for. We aren't yet sure about the point we
1346
+ # looked up, but we can be sure that X <= middle.
1347
+ range_end = middle
1348
+
1349
+ return range_end
1350
+
1351
+ async def get_time_of_last_push_action_before(
1352
+ self, stream_ordering: int
1353
+ ) -> int | None:
1354
+ def f(txn: LoggingTransaction) -> tuple[int] | None:
1355
+ sql = """
1356
+ SELECT e.received_ts
1357
+ FROM event_push_actions AS ep
1358
+ JOIN events e ON ep.room_id = e.room_id AND ep.event_id = e.event_id
1359
+ WHERE ep.stream_ordering > ? AND notif = 1
1360
+ ORDER BY ep.stream_ordering ASC
1361
+ LIMIT 1
1362
+ """
1363
+ txn.execute(sql, (stream_ordering,))
1364
+ return cast(tuple[int] | None, txn.fetchone())
1365
+
1366
+ result = await self.db_pool.runInteraction(
1367
+ "get_time_of_last_push_action_before", f
1368
+ )
1369
+ return result[0] if result else None
1370
+
1371
+ @wrap_as_background_process("rotate_notifs")
1372
+ async def _rotate_notifs(self) -> None:
1373
+ if self._doing_notif_rotation or self.stream_ordering_day_ago is None:
1374
+ return
1375
+ self._doing_notif_rotation = True
1376
+
1377
+ try:
1378
+ # First we recalculate push summaries and delete stale push actions
1379
+ # for rooms/users with new receipts.
1380
+ while True:
1381
+ logger.debug("Handling new receipts")
1382
+
1383
+ caught_up = await self.db_pool.runInteraction(
1384
+ "_handle_new_receipts_for_notifs_txn",
1385
+ self._handle_new_receipts_for_notifs_txn,
1386
+ )
1387
+ if caught_up:
1388
+ break
1389
+
1390
+ # Then we update the event push summaries for any new events
1391
+ while True:
1392
+ logger.info("Rotating notifications")
1393
+
1394
+ caught_up = await self.db_pool.runInteraction(
1395
+ "_rotate_notifs", self._rotate_notifs_txn
1396
+ )
1397
+ if caught_up:
1398
+ break
1399
+
1400
+ # Finally we clear out old event push actions.
1401
+ await self._remove_old_push_actions_that_have_rotated()
1402
+ finally:
1403
+ self._doing_notif_rotation = False
1404
+
1405
+ def _handle_new_receipts_for_notifs_txn(self, txn: LoggingTransaction) -> bool:
1406
+ """Check for new read receipts and delete from event push actions.
1407
+
1408
+ Any push actions which predate the user's most recent read receipt are
1409
+ now redundant, so we can remove them from `event_push_actions` and
1410
+ update `event_push_summary`.
1411
+
1412
+ Returns true if all new receipts have been processed.
1413
+ """
1414
+
1415
+ limit = 100
1416
+
1417
+ # The (inclusive) receipt stream ID that was previously processed..
1418
+ min_receipts_stream_id = self.db_pool.simple_select_one_onecol_txn(
1419
+ txn,
1420
+ table="event_push_summary_last_receipt_stream_id",
1421
+ keyvalues={},
1422
+ retcol="stream_id",
1423
+ )
1424
+
1425
+ max_receipts_stream_id = self._receipts_id_gen.get_current_token()
1426
+
1427
+ # The (inclusive) event stream ordering that was previously summarised.
1428
+ old_rotate_stream_ordering = self.db_pool.simple_select_one_onecol_txn(
1429
+ txn,
1430
+ table="event_push_summary_stream_ordering",
1431
+ keyvalues={},
1432
+ retcol="stream_ordering",
1433
+ )
1434
+
1435
+ sql = """
1436
+ SELECT r.stream_id, r.room_id, r.user_id, r.thread_id, r.event_stream_ordering
1437
+ FROM receipts_linearized AS r
1438
+ WHERE ? < r.stream_id AND r.stream_id <= ? AND user_id LIKE ?
1439
+ ORDER BY r.stream_id ASC
1440
+ LIMIT ?
1441
+ """
1442
+
1443
+ # We only want local users, so we add a dodgy filter to the above query
1444
+ # and recheck it below.
1445
+ user_filter = "%:" + self.hs.hostname
1446
+
1447
+ txn.execute(
1448
+ sql,
1449
+ (
1450
+ min_receipts_stream_id,
1451
+ max_receipts_stream_id,
1452
+ user_filter,
1453
+ limit,
1454
+ ),
1455
+ )
1456
+ rows = cast(list[tuple[int, str, str, str | None, int]], txn.fetchall())
1457
+
1458
+ # For each new read receipt we delete push actions from before it and
1459
+ # recalculate the summary.
1460
+ #
1461
+ # Care must be taken of whether it is a threaded or unthreaded receipt.
1462
+ for _, room_id, user_id, thread_id, stream_ordering in rows:
1463
+ # Only handle our own read receipts.
1464
+ if not self.hs.is_mine_id(user_id):
1465
+ continue
1466
+
1467
+ thread_clause = ""
1468
+ thread_args: tuple = ()
1469
+ if thread_id is not None:
1470
+ thread_clause = "AND thread_id = ?"
1471
+ thread_args = (thread_id,)
1472
+
1473
+ # For each new read receipt we delete push actions from before it and
1474
+ # recalculate the summary.
1475
+ txn.execute(
1476
+ f"""
1477
+ DELETE FROM event_push_actions
1478
+ WHERE room_id = ?
1479
+ AND user_id = ?
1480
+ AND stream_ordering <= ?
1481
+ AND highlight = 0
1482
+ {thread_clause}
1483
+ """,
1484
+ (room_id, user_id, stream_ordering, *thread_args),
1485
+ )
1486
+
1487
+ # Fetch the notification counts between the stream ordering of the
1488
+ # latest receipt and what was previously summarised.
1489
+ unread_counts = self._get_notif_unread_count_for_user_room(
1490
+ txn,
1491
+ room_id,
1492
+ user_id,
1493
+ stream_ordering,
1494
+ old_rotate_stream_ordering,
1495
+ thread_id,
1496
+ )
1497
+
1498
+ # For an unthreaded receipt, mark the summary for all threads in the room
1499
+ # as cleared.
1500
+ if thread_id is None:
1501
+ self.db_pool.simple_update_txn(
1502
+ txn,
1503
+ table="event_push_summary",
1504
+ keyvalues={"user_id": user_id, "room_id": room_id},
1505
+ updatevalues={
1506
+ "notif_count": 0,
1507
+ "unread_count": 0,
1508
+ "stream_ordering": old_rotate_stream_ordering,
1509
+ "last_receipt_stream_ordering": stream_ordering,
1510
+ },
1511
+ )
1512
+
1513
+ # For a threaded receipt, we *always* want to update that receipt,
1514
+ # event if there are no new notifications in that thread. This ensures
1515
+ # the stream_ordering & last_receipt_stream_ordering are updated.
1516
+ elif not unread_counts:
1517
+ unread_counts = [(0, 0, thread_id)]
1518
+
1519
+ # Then any updated threads get their notification count and unread
1520
+ # count updated.
1521
+ self.db_pool.simple_update_many_txn(
1522
+ txn,
1523
+ table="event_push_summary",
1524
+ key_names=("room_id", "user_id", "thread_id"),
1525
+ key_values=[(room_id, user_id, row[2]) for row in unread_counts],
1526
+ value_names=(
1527
+ "notif_count",
1528
+ "unread_count",
1529
+ "stream_ordering",
1530
+ "last_receipt_stream_ordering",
1531
+ ),
1532
+ value_values=[
1533
+ (row[0], row[1], old_rotate_stream_ordering, stream_ordering)
1534
+ for row in unread_counts
1535
+ ],
1536
+ )
1537
+
1538
+ # We always update `event_push_summary_last_receipt_stream_id` to
1539
+ # ensure that we don't rescan the same receipts for remote users.
1540
+
1541
+ receipts_last_processed_stream_id = max_receipts_stream_id
1542
+ if len(rows) >= limit:
1543
+ # If we pulled out a limited number of rows we only update the
1544
+ # position to the last receipt we processed, so we continue
1545
+ # processing the rest next iteration.
1546
+ receipts_last_processed_stream_id = rows[-1][0]
1547
+
1548
+ self.db_pool.simple_update_txn(
1549
+ txn,
1550
+ table="event_push_summary_last_receipt_stream_id",
1551
+ keyvalues={},
1552
+ updatevalues={"stream_id": receipts_last_processed_stream_id},
1553
+ )
1554
+
1555
+ return len(rows) < limit
1556
+
1557
+ def _rotate_notifs_txn(self, txn: LoggingTransaction) -> bool:
1558
+ """Archives older notifications (from event_push_actions) into event_push_summary.
1559
+
1560
+ Returns whether the archiving process has caught up or not.
1561
+ """
1562
+
1563
+ # The (inclusive) event stream ordering that was previously summarised.
1564
+ old_rotate_stream_ordering = self.db_pool.simple_select_one_onecol_txn(
1565
+ txn,
1566
+ table="event_push_summary_stream_ordering",
1567
+ keyvalues={},
1568
+ retcol="stream_ordering",
1569
+ )
1570
+
1571
+ # We don't to try and rotate millions of rows at once, so we cap the
1572
+ # maximum stream ordering we'll rotate before.
1573
+ txn.execute(
1574
+ """
1575
+ SELECT stream_ordering FROM event_push_actions
1576
+ WHERE stream_ordering > ?
1577
+ ORDER BY stream_ordering ASC LIMIT 1 OFFSET ?
1578
+ """,
1579
+ (old_rotate_stream_ordering, self._rotate_count),
1580
+ )
1581
+ stream_row = txn.fetchone()
1582
+ if stream_row:
1583
+ (offset_stream_ordering,) = stream_row
1584
+
1585
+ # We need to bound by the current token to ensure that we handle
1586
+ # out-of-order writes correctly.
1587
+ rotate_to_stream_ordering = min(
1588
+ offset_stream_ordering, self._stream_id_gen.get_current_token()
1589
+ )
1590
+ caught_up = False
1591
+ else:
1592
+ rotate_to_stream_ordering = self._stream_id_gen.get_current_token()
1593
+ caught_up = True
1594
+
1595
+ logger.info("Rotating notifications up to: %s", rotate_to_stream_ordering)
1596
+
1597
+ self._rotate_notifs_before_txn(
1598
+ txn, old_rotate_stream_ordering, rotate_to_stream_ordering
1599
+ )
1600
+
1601
+ return caught_up
1602
+
1603
+ def _rotate_notifs_before_txn(
1604
+ self,
1605
+ txn: LoggingTransaction,
1606
+ old_rotate_stream_ordering: int,
1607
+ rotate_to_stream_ordering: int,
1608
+ ) -> None:
1609
+ """Archives older notifications (from event_push_actions) into event_push_summary.
1610
+
1611
+ Any event_push_actions between old_rotate_stream_ordering (exclusive) and
1612
+ rotate_to_stream_ordering (inclusive) will be added to the event_push_summary
1613
+ table.
1614
+
1615
+ Args:
1616
+ txn: The database transaction.
1617
+ old_rotate_stream_ordering: The previous maximum event stream ordering.
1618
+ rotate_to_stream_ordering: The new maximum event stream ordering to summarise.
1619
+ """
1620
+
1621
+ # Calculate the new counts that should be upserted into event_push_summary
1622
+ sql = """
1623
+ SELECT user_id, room_id, thread_id,
1624
+ coalesce(old.%s, 0) + upd.cnt,
1625
+ upd.stream_ordering
1626
+ FROM (
1627
+ SELECT user_id, room_id, thread_id, count(*) as cnt,
1628
+ max(ea.stream_ordering) as stream_ordering
1629
+ FROM event_push_actions AS ea
1630
+ LEFT JOIN event_push_summary AS old USING (user_id, room_id, thread_id)
1631
+ WHERE ? < ea.stream_ordering AND ea.stream_ordering <= ?
1632
+ AND (
1633
+ old.last_receipt_stream_ordering IS NULL
1634
+ OR old.last_receipt_stream_ordering < ea.stream_ordering
1635
+ )
1636
+ AND %s = 1
1637
+ GROUP BY user_id, room_id, thread_id
1638
+ ) AS upd
1639
+ LEFT JOIN event_push_summary AS old USING (user_id, room_id, thread_id)
1640
+ """
1641
+
1642
+ # First get the count of unread messages.
1643
+ txn.execute(
1644
+ sql % ("unread_count", "unread"),
1645
+ (old_rotate_stream_ordering, rotate_to_stream_ordering),
1646
+ )
1647
+
1648
+ # We need to merge results from the two requests (the one that retrieves the
1649
+ # unread count and the one that retrieves the notifications count) into a single
1650
+ # object because we might not have the same amount of rows in each of them. To do
1651
+ # this, we use a dict indexed on the user ID and room ID to make it easier to
1652
+ # populate.
1653
+ summaries: dict[tuple[str, str, str], _EventPushSummary] = {}
1654
+ for row in txn:
1655
+ summaries[(row[0], row[1], row[2])] = _EventPushSummary(
1656
+ unread_count=row[3],
1657
+ stream_ordering=row[4],
1658
+ notif_count=0,
1659
+ )
1660
+
1661
+ # Then get the count of notifications.
1662
+ txn.execute(
1663
+ sql % ("notif_count", "notif"),
1664
+ (old_rotate_stream_ordering, rotate_to_stream_ordering),
1665
+ )
1666
+
1667
+ for row in txn:
1668
+ if (row[0], row[1], row[2]) in summaries:
1669
+ summaries[(row[0], row[1], row[2])].notif_count = row[3]
1670
+ else:
1671
+ # Because the rules on notifying are different than the rules on marking
1672
+ # a message unread, we might end up with messages that notify but aren't
1673
+ # marked unread, so we might not have a summary for this (user, room)
1674
+ # tuple to complete.
1675
+ summaries[(row[0], row[1], row[2])] = _EventPushSummary(
1676
+ unread_count=0,
1677
+ stream_ordering=row[4],
1678
+ notif_count=row[3],
1679
+ )
1680
+
1681
+ logger.info("Rotating notifications, handling %d rows", len(summaries))
1682
+
1683
+ self.db_pool.simple_upsert_many_txn(
1684
+ txn,
1685
+ table="event_push_summary",
1686
+ key_names=("user_id", "room_id", "thread_id"),
1687
+ key_values=list(summaries),
1688
+ value_names=("notif_count", "unread_count", "stream_ordering"),
1689
+ value_values=[
1690
+ (
1691
+ summary.notif_count,
1692
+ summary.unread_count,
1693
+ summary.stream_ordering,
1694
+ )
1695
+ for summary in summaries.values()
1696
+ ],
1697
+ )
1698
+
1699
+ txn.execute(
1700
+ "UPDATE event_push_summary_stream_ordering SET stream_ordering = ?",
1701
+ (rotate_to_stream_ordering,),
1702
+ )
1703
+
1704
+ async def _remove_old_push_actions_that_have_rotated(self) -> None:
1705
+ """
1706
+ Clear out old push actions that have been summarised (and are older than
1707
+ 1 day ago).
1708
+ """
1709
+
1710
+ # We want to clear out anything that is older than a day that *has* already
1711
+ # been rotated.
1712
+ rotated_upto_stream_ordering = await self.db_pool.simple_select_one_onecol(
1713
+ table="event_push_summary_stream_ordering",
1714
+ keyvalues={},
1715
+ retcol="stream_ordering",
1716
+ )
1717
+
1718
+ max_stream_ordering_to_delete = min(
1719
+ rotated_upto_stream_ordering, self.stream_ordering_day_ago
1720
+ )
1721
+
1722
+ def remove_old_push_actions_that_have_rotated_txn(
1723
+ txn: LoggingTransaction,
1724
+ ) -> bool:
1725
+ # We don't want to clear out too much at a time, so we bound our
1726
+ # deletes.
1727
+ batch_size = self._rotate_count
1728
+
1729
+ if isinstance(self.database_engine, PostgresEngine):
1730
+ # Temporarily disable sequential scans in this transaction. We
1731
+ # need to do this as the postgres statistics don't take into
1732
+ # account the `highlight = 0` part when estimating the
1733
+ # distribution of `stream_ordering`. I.e. since we keep old
1734
+ # highlight rows the query planner thinks there are way more old
1735
+ # rows to delete than there actually are.
1736
+ txn.execute("SET LOCAL enable_seqscan=off")
1737
+
1738
+ txn.execute(
1739
+ """
1740
+ SELECT stream_ordering FROM event_push_actions
1741
+ WHERE stream_ordering <= ? AND highlight = 0
1742
+ ORDER BY stream_ordering ASC LIMIT 1 OFFSET ?
1743
+ """,
1744
+ (
1745
+ max_stream_ordering_to_delete,
1746
+ batch_size,
1747
+ ),
1748
+ )
1749
+ stream_row = txn.fetchone()
1750
+
1751
+ if stream_row:
1752
+ (stream_ordering,) = stream_row
1753
+ else:
1754
+ stream_ordering = max_stream_ordering_to_delete
1755
+
1756
+ # We need to use a inclusive bound here to handle the case where a
1757
+ # single stream ordering has more than `batch_size` rows.
1758
+ txn.execute(
1759
+ """
1760
+ DELETE FROM event_push_actions
1761
+ WHERE stream_ordering <= ? AND highlight = 0
1762
+ """,
1763
+ (stream_ordering,),
1764
+ )
1765
+
1766
+ logger.info("Rotating notifications, deleted %s push actions", txn.rowcount)
1767
+
1768
+ return txn.rowcount < batch_size
1769
+
1770
+ while True:
1771
+ done = await self.db_pool.runInteraction(
1772
+ "_remove_old_push_actions_that_have_rotated",
1773
+ remove_old_push_actions_that_have_rotated_txn,
1774
+ )
1775
+ if done:
1776
+ break
1777
+
1778
+ @wrap_as_background_process("_clear_old_push_actions_staging")
1779
+ async def _clear_old_push_actions_staging(self) -> None:
1780
+ """Clear out any old event push actions from the staging table for
1781
+ events that we failed to persist.
1782
+ """
1783
+
1784
+ # We delete anything more than an hour old, on the assumption that we'll
1785
+ # never take more than an hour to persist an event.
1786
+ delete_before_ts = self.clock.time_msec() - 60 * 60 * 1000
1787
+
1788
+ if self._started_ts > delete_before_ts:
1789
+ # We need to wait for at least an hour before we started deleting,
1790
+ # so that we know it's safe to delete rows with NULL `inserted_ts`.
1791
+ return
1792
+
1793
+ # We don't have an index on `inserted_ts`, instead we assume that the
1794
+ # number of "live" rows in `event_push_actions_staging` is small enough
1795
+ # that an infrequent periodic scan won't cause a problem.
1796
+ #
1797
+ # Note: we also delete any columns with NULL `inserted_ts`, this is safe
1798
+ # as we added a default value to new rows and so they must be at least
1799
+ # an hour old.
1800
+ limit = 1000
1801
+ sql = """
1802
+ DELETE FROM event_push_actions_staging WHERE event_id IN (
1803
+ SELECT event_id FROM event_push_actions_staging WHERE
1804
+ inserted_ts < ? OR inserted_ts IS NULL
1805
+ LIMIT ?
1806
+ )
1807
+ """
1808
+
1809
+ def _clear_old_push_actions_staging_txn(txn: LoggingTransaction) -> bool:
1810
+ txn.execute(sql, (delete_before_ts, limit))
1811
+ return txn.rowcount >= limit
1812
+
1813
+ while True:
1814
+ # Returns true if we have more stuff to delete from the table.
1815
+ deleted = await self.db_pool.runInteraction(
1816
+ "_clear_old_push_actions_staging", _clear_old_push_actions_staging_txn
1817
+ )
1818
+
1819
+ if not deleted:
1820
+ return
1821
+
1822
+ # We sleep to ensure that we don't overwhelm the DB.
1823
+ await self.clock.sleep(Duration(seconds=1))
1824
+
1825
+ async def get_push_actions_for_user(
1826
+ self,
1827
+ user_id: str,
1828
+ before: int | None = None,
1829
+ limit: int = 50,
1830
+ only_highlight: bool = False,
1831
+ ) -> list[UserPushAction]:
1832
+ def f(
1833
+ txn: LoggingTransaction,
1834
+ ) -> list[tuple[str, str, int, int, str, bool, str, int]]:
1835
+ before_clause = ""
1836
+ if before:
1837
+ before_clause = "AND epa.stream_ordering < ?"
1838
+ args = [user_id, before, limit]
1839
+ else:
1840
+ args = [user_id, limit]
1841
+
1842
+ if only_highlight:
1843
+ if len(before_clause) > 0:
1844
+ before_clause += " "
1845
+ before_clause += "AND epa.highlight = 1"
1846
+
1847
+ # NB. This assumes event_ids are globally unique since
1848
+ # it makes the query easier to index
1849
+ sql = """
1850
+ SELECT epa.event_id, epa.room_id,
1851
+ epa.stream_ordering, epa.topological_ordering,
1852
+ epa.actions, epa.highlight, epa.profile_tag, e.received_ts
1853
+ FROM event_push_actions epa, events e
1854
+ WHERE epa.event_id = e.event_id
1855
+ AND epa.user_id = ? %s
1856
+ AND epa.notif = 1
1857
+ ORDER BY epa.stream_ordering DESC
1858
+ LIMIT ?
1859
+ """ % (before_clause,)
1860
+ txn.execute(sql, args)
1861
+ return cast(
1862
+ list[tuple[str, str, int, int, str, bool, str, int]], txn.fetchall()
1863
+ )
1864
+
1865
+ push_actions = await self.db_pool.runInteraction("get_push_actions_for_user", f)
1866
+ return [
1867
+ UserPushAction(
1868
+ event_id=row[0],
1869
+ room_id=row[1],
1870
+ stream_ordering=row[2],
1871
+ actions=_deserialize_action(row[4], row[5]),
1872
+ received_ts=row[7],
1873
+ topological_ordering=row[3],
1874
+ highlight=row[5],
1875
+ profile_tag=row[6],
1876
+ )
1877
+ for row in push_actions
1878
+ ]
1879
+
1880
+
1881
+ class EventPushActionsStore(EventPushActionsWorkerStore):
1882
+ EPA_HIGHLIGHT_INDEX = "epa_highlight_index"
1883
+
1884
+ def __init__(
1885
+ self,
1886
+ database: DatabasePool,
1887
+ db_conn: LoggingDatabaseConnection,
1888
+ hs: "HomeServer",
1889
+ ):
1890
+ super().__init__(database, db_conn, hs)
1891
+
1892
+ self.db_pool.updates.register_background_index_update(
1893
+ self.EPA_HIGHLIGHT_INDEX,
1894
+ index_name="event_push_actions_u_highlight",
1895
+ table="event_push_actions",
1896
+ columns=["user_id", "stream_ordering"],
1897
+ )
1898
+
1899
+ self.db_pool.updates.register_background_index_update(
1900
+ "event_push_actions_highlights_index",
1901
+ index_name="event_push_actions_highlights_index",
1902
+ table="event_push_actions",
1903
+ columns=["user_id", "room_id", "topological_ordering", "stream_ordering"],
1904
+ where_clause="highlight=1",
1905
+ )
1906
+
1907
+ # Add index to make deleting old push actions faster.
1908
+ self.db_pool.updates.register_background_index_update(
1909
+ "event_push_actions_stream_highlight_index",
1910
+ index_name="event_push_actions_stream_highlight_index",
1911
+ table="event_push_actions",
1912
+ columns=["highlight", "stream_ordering"],
1913
+ where_clause="highlight=0",
1914
+ )
1915
+
1916
+
1917
+ def _action_has_highlight(actions: Collection[Mapping | str]) -> bool:
1918
+ for action in actions:
1919
+ if not isinstance(action, dict):
1920
+ continue
1921
+
1922
+ if action.get("set_tweak", None) == "highlight":
1923
+ return action.get("value", True)
1924
+
1925
+ return False
1926
+
1927
+
1928
+ @attr.s(slots=True, auto_attribs=True)
1929
+ class _EventPushSummary:
1930
+ """Summary of pending event push actions for a given user in a given room.
1931
+ Used in _rotate_notifs_before_txn to manipulate results from event_push_actions.
1932
+ """
1933
+
1934
+ unread_count: int
1935
+ stream_ordering: int
1936
+ notif_count: int