2023-11-21 15:29:58 -05:00
|
|
|
#
|
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
# Copyright 2017 Vector Creations Ltd
|
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
|
|
|
#
|
|
|
|
#
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Contains constants from the specification."""
|
|
|
|
|
2023-01-27 07:27:55 -05:00
|
|
|
import enum
|
2023-08-15 08:11:20 -04:00
|
|
|
from typing import Final
|
2021-11-25 11:14:23 -05:00
|
|
|
|
2021-04-23 14:20:44 -04:00
|
|
|
# the max size of a (canonical-json-encoded) event
|
|
|
|
MAX_PDU_SIZE = 65536
|
|
|
|
|
2018-05-01 11:19:39 -04:00
|
|
|
# the "depth" field on events is limited to 2**63 - 1
|
2022-03-29 06:41:19 -04:00
|
|
|
MAX_DEPTH = 2**63 - 1
|
2018-05-01 11:19:39 -04:00
|
|
|
|
2019-05-08 12:01:30 -04:00
|
|
|
# the maximum length for a room alias is 255 characters
|
|
|
|
MAX_ALIAS_LENGTH = 255
|
|
|
|
|
2019-05-20 06:20:08 -04:00
|
|
|
# the maximum length for a user id is 255 characters
|
|
|
|
MAX_USERID_LENGTH = 255
|
|
|
|
|
2022-10-04 09:47:04 -04:00
|
|
|
# Constant value used for the pseudo-thread which is the main timeline.
|
|
|
|
MAIN_TIMELINE: Final = "main"
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class Membership:
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Represents the membership states of a user in a room."""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
INVITE: Final = "invite"
|
|
|
|
JOIN: Final = "join"
|
|
|
|
KNOCK: Final = "knock"
|
|
|
|
LEAVE: Final = "leave"
|
|
|
|
BAN: Final = "ban"
|
2024-07-19 11:19:15 -04:00
|
|
|
LIST: Final = frozenset((INVITE, JOIN, KNOCK, LEAVE, BAN))
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class PresenceState:
|
2014-08-12 10:10:52 -04:00
|
|
|
"""Represents the presence state of a user."""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
OFFLINE: Final = "offline"
|
|
|
|
UNAVAILABLE: Final = "unavailable"
|
|
|
|
ONLINE: Final = "online"
|
|
|
|
BUSY: Final = "org.matrix.msc3026.busy"
|
2014-08-28 05:59:15 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class JoinRules:
|
2021-11-25 11:14:23 -05:00
|
|
|
PUBLIC: Final = "public"
|
|
|
|
KNOCK: Final = "knock"
|
|
|
|
INVITE: Final = "invite"
|
|
|
|
PRIVATE: Final = "private"
|
2021-03-31 16:39:08 -04:00
|
|
|
# As defined for MSC3083.
|
2021-11-25 11:14:23 -05:00
|
|
|
RESTRICTED: Final = "restricted"
|
2022-05-17 06:41:39 -04:00
|
|
|
# As defined for MSC3787.
|
|
|
|
KNOCK_RESTRICTED: Final = "knock_restricted"
|
2014-09-15 05:23:20 -04:00
|
|
|
|
|
|
|
|
2021-06-17 12:53:27 -04:00
|
|
|
class RestrictedJoinRuleTypes:
|
|
|
|
"""Understood types for the allow rules in restricted join rules."""
|
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
ROOM_MEMBERSHIP: Final = "m.room_membership"
|
2021-06-17 12:53:27 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class LoginType:
|
2021-11-25 11:14:23 -05:00
|
|
|
PASSWORD: Final = "m.login.password"
|
|
|
|
EMAIL_IDENTITY: Final = "m.login.email.identity"
|
|
|
|
MSISDN: Final = "m.login.msisdn"
|
|
|
|
RECAPTCHA: Final = "m.login.recaptcha"
|
|
|
|
TERMS: Final = "m.login.terms"
|
|
|
|
SSO: Final = "m.login.sso"
|
|
|
|
DUMMY: Final = "m.login.dummy"
|
2022-02-04 07:15:13 -05:00
|
|
|
REGISTRATION_TOKEN: Final = "m.login.registration_token"
|
2015-04-02 12:01:29 -04:00
|
|
|
|
2014-12-03 11:07:21 -05:00
|
|
|
|
2021-04-12 10:13:55 -04:00
|
|
|
# This is used in the `type` parameter for /register when called by
|
|
|
|
# an appservice to register a new user.
|
2021-11-25 11:14:23 -05:00
|
|
|
APP_SERVICE_REGISTRATION_TYPE: Final = "m.login.application_service"
|
2021-04-12 10:13:55 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class EventTypes:
|
2021-11-25 11:14:23 -05:00
|
|
|
Member: Final = "m.room.member"
|
|
|
|
Create: Final = "m.room.create"
|
|
|
|
Tombstone: Final = "m.room.tombstone"
|
|
|
|
JoinRules: Final = "m.room.join_rules"
|
|
|
|
PowerLevels: Final = "m.room.power_levels"
|
|
|
|
Aliases: Final = "m.room.aliases"
|
|
|
|
Redaction: Final = "m.room.redaction"
|
|
|
|
ThirdPartyInvite: Final = "m.room.third_party_invite"
|
|
|
|
|
|
|
|
RoomHistoryVisibility: Final = "m.room.history_visibility"
|
|
|
|
CanonicalAlias: Final = "m.room.canonical_alias"
|
|
|
|
Encrypted: Final = "m.room.encrypted"
|
|
|
|
RoomAvatar: Final = "m.room.avatar"
|
|
|
|
RoomEncryption: Final = "m.room.encryption"
|
|
|
|
GuestAccess: Final = "m.room.guest_access"
|
2015-07-02 11:20:10 -04:00
|
|
|
|
2014-12-12 05:56:14 -05:00
|
|
|
# These are used for validation
|
2021-11-25 11:14:23 -05:00
|
|
|
Message: Final = "m.room.message"
|
|
|
|
Topic: Final = "m.room.topic"
|
|
|
|
Name: Final = "m.room.name"
|
2015-01-28 11:16:53 -05:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
ServerACL: Final = "m.room.server_acl"
|
|
|
|
Pinned: Final = "m.room.pinned_events"
|
2018-07-04 10:31:00 -04:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
Retention: Final = "m.room.retention"
|
2019-11-04 12:09:22 -05:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
Dummy: Final = "org.matrix.dummy_event"
|
2020-12-18 04:49:18 -05:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
SpaceChild: Final = "m.space.child"
|
|
|
|
SpaceParent: Final = "m.space.parent"
|
2021-03-18 14:24:16 -04:00
|
|
|
|
2022-11-03 12:21:31 -04:00
|
|
|
Reaction: Final = "m.reaction"
|
2024-07-08 14:17:08 -04:00
|
|
|
Sticker: Final = "m.sticker"
|
|
|
|
LiveLocationShareStart: Final = "m.beacon_info"
|
2022-11-03 12:21:31 -04:00
|
|
|
|
2024-03-19 13:52:53 -04:00
|
|
|
CallInvite: Final = "m.call.invite"
|
|
|
|
|
2024-07-08 14:17:08 -04:00
|
|
|
PollStart: Final = "m.poll.start"
|
|
|
|
|
2015-01-28 11:16:53 -05:00
|
|
|
|
2021-05-11 06:02:56 -04:00
|
|
|
class ToDeviceEventTypes:
|
2021-11-25 11:14:23 -05:00
|
|
|
RoomKeyRequest: Final = "m.room_key_request"
|
2021-05-11 06:02:56 -04:00
|
|
|
|
|
|
|
|
2021-07-27 09:36:38 -04:00
|
|
|
class DeviceKeyAlgorithms:
|
|
|
|
"""Spec'd algorithms for the generation of per-device keys"""
|
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
ED25519: Final = "ed25519"
|
|
|
|
CURVE25519: Final = "curve25519"
|
|
|
|
SIGNED_CURVE25519: Final = "signed_curve25519"
|
2021-07-27 09:36:38 -04:00
|
|
|
|
|
|
|
|
2021-02-19 13:20:34 -05:00
|
|
|
class EduTypes:
|
2022-05-27 07:14:36 -04:00
|
|
|
PRESENCE: Final = "m.presence"
|
|
|
|
TYPING: Final = "m.typing"
|
|
|
|
RECEIPT: Final = "m.receipt"
|
|
|
|
DEVICE_LIST_UPDATE: Final = "m.device_list_update"
|
|
|
|
SIGNING_KEY_UPDATE: Final = "m.signing_key_update"
|
|
|
|
UNSTABLE_SIGNING_KEY_UPDATE: Final = "org.matrix.signing_key_update"
|
|
|
|
DIRECT_TO_DEVICE: Final = "m.direct_to_device"
|
2021-02-19 13:20:34 -05:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class RejectedReason:
|
2021-11-25 11:14:23 -05:00
|
|
|
AUTH_ERROR: Final = "auth_error"
|
2022-12-13 08:19:19 -05:00
|
|
|
OVERSIZED_EVENT: Final = "oversized_event"
|
2015-07-13 11:48:06 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class RoomCreationPreset:
|
2021-11-25 11:14:23 -05:00
|
|
|
PRIVATE_CHAT: Final = "private_chat"
|
|
|
|
PUBLIC_CHAT: Final = "public_chat"
|
|
|
|
TRUSTED_PRIVATE_CHAT: Final = "trusted_private_chat"
|
2016-08-25 13:34:47 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class ThirdPartyEntityKind:
|
2021-11-25 11:14:23 -05:00
|
|
|
USER: Final = "user"
|
|
|
|
LOCATION: Final = "location"
|
2018-07-25 17:10:39 -04:00
|
|
|
|
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
ServerNoticeMsgType: Final = "m.server_notice"
|
|
|
|
ServerNoticeLimitReached: Final = "m.server_notice.usage_limit_reached"
|
2018-12-14 13:20:59 -05:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class UserTypes:
|
2018-12-14 13:20:59 -05:00
|
|
|
"""Allows for user type specific behaviour. With the benefit of hindsight
|
|
|
|
'admin' and 'guest' users should also be UserTypes. Normal users are type None
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
SUPPORT: Final = "support"
|
|
|
|
BOT: Final = "bot"
|
|
|
|
ALL_USER_TYPES: Final = (SUPPORT, BOT)
|
2019-05-14 11:59:21 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class RelationTypes:
|
2019-05-14 11:59:21 -04:00
|
|
|
"""The types of relations known to this server."""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
ANNOTATION: Final = "m.annotation"
|
|
|
|
REPLACE: Final = "m.replace"
|
|
|
|
REFERENCE: Final = "m.reference"
|
2022-03-10 10:36:13 -05:00
|
|
|
THREAD: Final = "m.thread"
|
2019-10-24 06:48:46 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class LimitBlockingTypes:
|
2019-10-24 06:48:46 -04:00
|
|
|
"""Reasons that a server may be blocked"""
|
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
MONTHLY_ACTIVE_USER: Final = "monthly_active_user"
|
|
|
|
HS_DISABLED: Final = "hs_disabled"
|
2019-10-29 14:35:49 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class EventContentFields:
|
2019-11-01 06:30:51 -04:00
|
|
|
"""Fields found in events' content, regardless of type."""
|
2019-11-01 06:39:14 -04:00
|
|
|
|
2019-11-01 06:30:51 -04:00
|
|
|
# Labels for the event, cf https://github.com/matrix-org/matrix-doc/pull/2326
|
2021-11-25 11:14:23 -05:00
|
|
|
LABELS: Final = "org.matrix.labels"
|
2019-12-03 14:19:45 -05:00
|
|
|
|
|
|
|
# Timestamp to delete the event after
|
|
|
|
# cf https://github.com/matrix-org/matrix-doc/pull/2228
|
2021-11-25 11:14:23 -05:00
|
|
|
SELF_DESTRUCT_AFTER: Final = "org.matrix.self_destruct_after"
|
2020-06-10 12:44:34 -04:00
|
|
|
|
2021-03-18 14:24:16 -04:00
|
|
|
# cf https://github.com/matrix-org/matrix-doc/pull/1772
|
2021-11-25 11:14:23 -05:00
|
|
|
ROOM_TYPE: Final = "type"
|
2021-03-18 14:24:16 -04:00
|
|
|
|
2021-09-08 10:00:43 -04:00
|
|
|
# Whether a room can federate.
|
2021-11-25 11:14:23 -05:00
|
|
|
FEDERATE: Final = "m.federate"
|
2021-09-08 10:00:43 -04:00
|
|
|
|
2021-09-01 11:27:58 -04:00
|
|
|
# The creator of the room, as used in `m.room.create` events.
|
2023-04-06 16:26:28 -04:00
|
|
|
#
|
|
|
|
# This is deprecated in MSC2175.
|
2021-11-25 11:14:23 -05:00
|
|
|
ROOM_CREATOR: Final = "creator"
|
2021-09-01 11:27:58 -04:00
|
|
|
|
2024-07-30 14:20:29 -04:00
|
|
|
# The version of the room for `m.room.create` events.
|
|
|
|
ROOM_VERSION: Final = "room_version"
|
|
|
|
|
|
|
|
ROOM_NAME: Final = "name"
|
|
|
|
|
2021-09-06 07:17:16 -04:00
|
|
|
# Used in m.room.guest_access events.
|
2021-11-25 11:14:23 -05:00
|
|
|
GUEST_ACCESS: Final = "guest_access"
|
2021-09-06 07:17:16 -04:00
|
|
|
|
2021-09-30 11:13:59 -04:00
|
|
|
# The authorising user for joining a restricted room.
|
2021-11-25 11:14:23 -05:00
|
|
|
AUTHORISING_USER: Final = "join_authorised_via_users_server"
|
2021-09-30 11:13:59 -04:00
|
|
|
|
2023-01-27 10:16:21 -05:00
|
|
|
# Use for mentioning users.
|
2023-06-06 04:11:07 -04:00
|
|
|
MENTIONS: Final = "m.mentions"
|
2023-01-27 10:16:21 -05:00
|
|
|
|
2022-12-06 04:52:55 -05:00
|
|
|
# an unspecced field added to to-device messages to identify them uniquely-ish
|
|
|
|
TO_DEVICE_MSGID: Final = "org.matrix.msgid"
|
|
|
|
|
2024-07-30 14:20:29 -04:00
|
|
|
# `m.room.encryption`` algorithm field
|
|
|
|
ENCRYPTION_ALGORITHM: Final = "algorithm"
|
|
|
|
|
Sliding Sync: Pre-populate room data for quick filtering/sorting (#17512)
Pre-populate room data for quick filtering/sorting in the Sliding Sync
API
Spawning from
https://github.com/element-hq/synapse/pull/17450#discussion_r1697335578
This PR is acting as the Synapse version `N+1` step in the gradual
migration being tracked by
https://github.com/element-hq/synapse/issues/17623
Adding two new database tables:
- `sliding_sync_joined_rooms`: A table for storing room meta data that
the local server is still participating in. The info here can be shared
across all `Membership.JOIN`. Keyed on `(room_id)` and updated when the
relevant room current state changes or a new event is sent in the room.
- `sliding_sync_membership_snapshots`: A table for storing a snapshot of
room meta data at the time of the local user's membership. Keyed on
`(room_id, user_id)` and only updated when a user's membership in a room
changes.
Also adds background updates to populate these tables with all of the
existing data.
We want to have the guarantee that if a row exists in the sliding sync
tables, we are able to rely on it (accurate data). And if a row doesn't
exist, we use a fallback to get the same info until the background
updates fill in the rows or a new event comes in triggering it to be
fully inserted. This means we need a couple extra things in place until
we bump `SCHEMA_COMPAT_VERSION` and run the foreground update in the
`N+2` part of the gradual migration. For context on why we can't rely on
the tables without these things see [1].
1. On start-up, block until we clear out any rows for the rooms that
have had events since the max-`stream_ordering` of the
`sliding_sync_joined_rooms` table (compare to max-`stream_ordering` of
the `events` table). For `sliding_sync_membership_snapshots`, we can
compare to the max-`stream_ordering` of `local_current_membership`
- This accounts for when someone downgrades their Synapse version and
then upgrades it again. This will ensure that we don't have any
stale/out-of-date data in the
`sliding_sync_joined_rooms`/`sliding_sync_membership_snapshots` tables
since any new events sent in rooms would have also needed to be written
to the sliding sync tables. For example a new event needs to bump
`event_stream_ordering` in `sliding_sync_joined_rooms` table or some
state in the room changing (like the room name). Or another example of
someone's membership changing in a room affecting
`sliding_sync_membership_snapshots`.
1. Add another background update that will catch-up with any rows that
were just deleted from the sliding sync tables (based on the activity in
the `events`/`local_current_membership`). The rooms that need
recalculating are added to the
`sliding_sync_joined_rooms_to_recalculate` table.
1. Making sure rows are fully inserted. Instead of partially inserting,
we need to check if the row already exists and fully insert all data if
not.
All of this extra functionality can be removed once the
`SCHEMA_COMPAT_VERSION` is bumped with support for the new sliding sync
tables so people can no longer downgrade (the `N+2` part of the gradual
migration).
<details>
<summary><sup>[1]</sup></summary>
For `sliding_sync_joined_rooms`, since we partially insert rows as state
comes in, we can't rely on the existence of the row for a given
`room_id`. We can't even rely on looking at whether the background
update has finished. There could still be partial rows from when someone
reverted their Synapse version after the background update finished, had
some state changes (or new rooms), then upgraded again and more state
changes happen leaving a partial row.
For `sliding_sync_membership_snapshots`, we insert items as a whole
except for the `forgotten` column ~~so we can rely on rows existing and
just need to always use a fallback for the `forgotten` data. We can't
use the `forgotten` column in the table for the same reasons above about
`sliding_sync_joined_rooms`.~~ We could have an out-of-date membership
from when someone reverted their Synapse version. (same problems as
outlined for `sliding_sync_joined_rooms` above)
Discussed in an [internal
meeting](https://docs.google.com/document/d/1MnuvPkaCkT_wviSQZ6YKBjiWciCBFMd-7hxyCO-OCbQ/edit#bookmark=id.dz5x6ef4mxz7)
</details>
### TODO
- [x] Update `stream_ordering`/`bump_stamp`
- [x] Handle remote invites
- [x] Handle state resets
- [x] Consider adding `sender` so we can filter `LEAVE` memberships and
distinguish from kicks.
- [x] We should add it to be able to tell leaves from kicks
- [x] Consider adding `tombstone` state to help address
https://github.com/element-hq/synapse/issues/17540
- [x] We should add it `tombstone_successor_room_id`
- [x] Consider adding `forgotten` status to avoid extra
lookup/table-join on `room_memberships`
- [x] We should add it
- [x] Background update to fill in values for all joined rooms and
non-join membership
- [x] Clean-up tables when room is deleted
- [ ] Make sure tables are useful to our use case
- First explored in
https://github.com/element-hq/synapse/compare/erikj/ss_use_new_tables
- Also explored in
https://github.com/element-hq/synapse/commit/76b5a576eb363496315dfd39510cad7d02b0fc73
- [x] Plan for how can we use this with a fallback
- See plan discussed above in main area of the issue description
- Discussed in an [internal
meeting](https://docs.google.com/document/d/1MnuvPkaCkT_wviSQZ6YKBjiWciCBFMd-7hxyCO-OCbQ/edit#bookmark=id.dz5x6ef4mxz7)
- [x] Plan for how we can rely on this new table without a fallback
- Synapse version `N+1`: (this PR) Bump `SCHEMA_VERSION` to `87`. Add
new tables and background update to backfill all rows. Since this is a
new table, we don't have to add any `NOT VALID` constraints and validate
them when the background update completes. Read from new tables with a
fallback in cases where the rows aren't filled in yet.
- Synapse version `N+2`: Bump `SCHEMA_VERSION` to `88` and bump
`SCHEMA_COMPAT_VERSION` to `87` because we don't want people to
downgrade and miss writes while they are on an older version. Add a
foreground update to finish off the backfill so we can read from new
tables without the fallback. Application code can now rely on the new
tables being populated.
- Discussed in an [internal
meeting](https://docs.google.com/document/d/1MnuvPkaCkT_wviSQZ6YKBjiWciCBFMd-7hxyCO-OCbQ/edit#bookmark=id.hh7shg4cxdhj)
### Dev notes
```
SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.storage.test_events.SlidingSyncPrePopulatedTablesTestCase
SYNAPSE_POSTGRES=1 SYNAPSE_POSTGRES_USER=postgres SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.storage.test_events.SlidingSyncPrePopulatedTablesTestCase
```
```
SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.handlers.test_sliding_sync.FilterRoomsTestCase
```
Reference:
- [Development docs on background updates and worked examples of gradual
migrations
](https://github.com/element-hq/synapse/blob/1dfa59b238cee0dc62163588cc9481896c288979/docs/development/database_schema.md#background-updates)
- A real example of a gradual migration:
https://github.com/matrix-org/synapse/pull/15649#discussion_r1213779514
- Adding `rooms.creator` field that needed a background update to
backfill data, https://github.com/matrix-org/synapse/pull/10697
- Adding `rooms.room_version` that needed a background update to
backfill data, https://github.com/matrix-org/synapse/pull/6729
- Adding `room_stats_state.room_type` that needed a background update to
backfill data, https://github.com/matrix-org/synapse/pull/13031
- Tables from MSC2716: `insertion_events`, `insertion_event_edges`,
`insertion_event_extremities`, `batch_events`
- `current_state_events` updated in
`synapse/storage/databases/main/events.py`
---
```
persist_event (adds to queue)
_persist_event_batch
_persist_events_and_state_updates (assigns `stream_ordering` to events)
_persist_events_txn
_store_event_txn
_update_metadata_tables_txn
_store_room_members_txn
_update_current_state_txn
```
---
> Concatenated Indexes [...] (also known as multi-column, composite or
combined index)
>
> [...] key consists of multiple columns.
>
> We can take advantage of the fact that the first index column is
always usable for searching
>
> *--
https://use-the-index-luke.com/sql/where-clause/the-equals-operator/concatenated-keys*
---
Dealing with `portdb` (`synapse/_scripts/synapse_port_db.py`),
https://github.com/element-hq/synapse/pull/17512#discussion_r1725998219
---
<details>
<summary>SQL queries:</summary>
Both of these are equivalent and work in SQLite and Postgres
Options 1:
```sql
WITH data_table (room_id, user_id, membership_event_id, membership, event_stream_ordering, {", ".join(insert_keys)}) AS (
VALUES (
?, ?, ?,
(SELECT membership FROM room_memberships WHERE event_id = ?),
(SELECT stream_ordering FROM events WHERE event_id = ?),
{", ".join("?" for _ in insert_values)}
)
)
INSERT INTO sliding_sync_non_join_memberships
(room_id, user_id, membership_event_id, membership, event_stream_ordering, {", ".join(insert_keys)})
SELECT * FROM data_table
WHERE membership != ?
ON CONFLICT (room_id, user_id)
DO UPDATE SET
membership_event_id = EXCLUDED.membership_event_id,
membership = EXCLUDED.membership,
event_stream_ordering = EXCLUDED.event_stream_ordering,
{", ".join(f"{key} = EXCLUDED.{key}" for key in insert_keys)}
```
Option 2:
```sql
INSERT INTO sliding_sync_non_join_memberships
(room_id, user_id, membership_event_id, membership, event_stream_ordering, {", ".join(insert_keys)})
SELECT
column1 as room_id,
column2 as user_id,
column3 as membership_event_id,
column4 as membership,
column5 as event_stream_ordering,
{", ".join("column" + str(i) for i in range(6, 6 + len(insert_keys)))}
FROM (
VALUES (
?, ?, ?,
(SELECT membership FROM room_memberships WHERE event_id = ?),
(SELECT stream_ordering FROM events WHERE event_id = ?),
{", ".join("?" for _ in insert_values)}
)
) as v
WHERE membership != ?
ON CONFLICT (room_id, user_id)
DO UPDATE SET
membership_event_id = EXCLUDED.membership_event_id,
membership = EXCLUDED.membership,
event_stream_ordering = EXCLUDED.event_stream_ordering,
{", ".join(f"{key} = EXCLUDED.{key}" for key in insert_keys)}
```
If we don't need the `membership` condition, we could use:
```sql
INSERT INTO sliding_sync_non_join_memberships
(room_id, membership_event_id, user_id, membership, event_stream_ordering, {", ".join(insert_keys)})
VALUES (
?, ?, ?,
(SELECT membership FROM room_memberships WHERE event_id = ?),
(SELECT stream_ordering FROM events WHERE event_id = ?),
{", ".join("?" for _ in insert_values)}
)
ON CONFLICT (room_id, user_id)
DO UPDATE SET
membership_event_id = EXCLUDED.membership_event_id,
membership = EXCLUDED.membership,
event_stream_ordering = EXCLUDED.event_stream_ordering,
{", ".join(f"{key} = EXCLUDED.{key}" for key in insert_keys)}
```
</details>
### Pull Request Checklist
<!-- Please read
https://element-hq.github.io/synapse/latest/development/contributing_guide.html
before submitting your pull request -->
* [x] Pull request is based on the develop branch
* [x] Pull request includes a [changelog
file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog).
The entry should:
- Be a short description of your change which makes sense to users.
"Fixed a bug that prevented receiving messages from other servers."
instead of "Moved X method from `EventStore` to `EventWorkerStore`.".
- Use markdown where necessary, mostly for `code blocks`.
- End with either a period (.) or an exclamation mark (!).
- Start with a capital letter.
- Feel free to credit yourself, by adding a sentence "Contributed by
@github_username." or "Contributed by [Your Name]." to the end of the
entry.
* [x] [Code
style](https://element-hq.github.io/synapse/latest/code_style.html) is
correct
(run the
[linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters))
---------
Co-authored-by: Erik Johnston <erik@matrix.org>
2024-08-29 11:09:51 -04:00
|
|
|
TOMBSTONE_SUCCESSOR_ROOM: Final = "replacement_room"
|
|
|
|
|
2020-06-10 12:44:34 -04:00
|
|
|
|
2024-04-29 10:22:13 -04:00
|
|
|
class EventUnsignedContentFields:
|
|
|
|
"""Fields found inside the 'unsigned' data on events"""
|
|
|
|
|
|
|
|
# Requesting user's membership, per MSC4115
|
2024-06-13 17:45:54 -04:00
|
|
|
MEMBERSHIP: Final = "membership"
|
2024-04-29 10:22:13 -04:00
|
|
|
|
|
|
|
|
2021-06-29 12:00:04 -04:00
|
|
|
class RoomTypes:
|
|
|
|
"""Understood values of the room_type field of m.room.create events."""
|
|
|
|
|
2021-11-25 11:14:23 -05:00
|
|
|
SPACE: Final = "m.space"
|
2021-06-29 12:00:04 -04:00
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class RoomEncryptionAlgorithms:
|
2021-11-25 11:14:23 -05:00
|
|
|
MEGOLM_V1_AES_SHA2: Final = "m.megolm.v1.aes-sha2"
|
|
|
|
DEFAULT: Final = MEGOLM_V1_AES_SHA2
|
2020-10-05 09:28:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
class AccountDataTypes:
|
2021-11-25 11:14:23 -05:00
|
|
|
DIRECT: Final = "m.direct"
|
|
|
|
IGNORED_USER_LIST: Final = "m.ignored_user_list"
|
2023-01-13 09:57:43 -05:00
|
|
|
TAG: Final = "m.tag"
|
2023-05-09 07:23:27 -04:00
|
|
|
PUSH_RULES: Final = "m.push_rules"
|
2020-12-16 08:46:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
class HistoryVisibility:
|
2021-11-25 11:14:23 -05:00
|
|
|
INVITED: Final = "invited"
|
|
|
|
JOINED: Final = "joined"
|
|
|
|
SHARED: Final = "shared"
|
|
|
|
WORLD_READABLE: Final = "world_readable"
|
2021-07-28 04:05:11 -04:00
|
|
|
|
|
|
|
|
2021-09-06 07:17:16 -04:00
|
|
|
class GuestAccess:
|
2021-11-25 11:14:23 -05:00
|
|
|
CAN_JOIN: Final = "can_join"
|
2021-09-06 07:17:16 -04:00
|
|
|
# anything that is not "can_join" is considered "forbidden", but for completeness:
|
2021-11-25 11:14:23 -05:00
|
|
|
FORBIDDEN: Final = "forbidden"
|
2021-09-06 07:17:16 -04:00
|
|
|
|
|
|
|
|
2021-12-08 12:26:29 -05:00
|
|
|
class ReceiptTypes:
|
|
|
|
READ: Final = "m.read"
|
2022-08-05 11:09:33 -04:00
|
|
|
READ_PRIVATE: Final = "m.read.private"
|
2022-05-04 11:59:22 -04:00
|
|
|
FULLY_READ: Final = "m.fully_read"
|
2022-06-29 13:12:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PublicRoomsFilterFields:
|
|
|
|
"""Fields in the search filter for `/publicRooms` that we understand.
|
|
|
|
|
|
|
|
As defined in https://spec.matrix.org/v1.3/client-server-api/#post_matrixclientv3publicrooms
|
|
|
|
"""
|
|
|
|
|
|
|
|
GENERIC_SEARCH_TERM: Final = "generic_search_term"
|
2022-07-27 14:46:57 -04:00
|
|
|
ROOM_TYPES: Final = "room_types"
|
2022-09-29 09:23:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ApprovalNoticeMedium:
|
|
|
|
"""Identifier for the medium this server will use to serve notice of approval for a
|
|
|
|
specific user's registration.
|
|
|
|
|
|
|
|
As defined in https://github.com/matrix-org/matrix-spec-proposals/blob/babolivier/m_not_approved/proposals/3866-user-not-approved-error.md
|
|
|
|
"""
|
|
|
|
|
|
|
|
NONE = "org.matrix.msc3866.none"
|
|
|
|
EMAIL = "org.matrix.msc3866.email"
|
2023-01-27 07:27:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
class Direction(enum.Enum):
|
|
|
|
BACKWARDS = "b"
|
|
|
|
FORWARDS = "f"
|