2015-01-26 13:53:31 -05:00
|
|
|
#
|
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 2015-2021 The Matrix.org Foundation C.I.C.
|
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]
|
2015-01-26 13:53:31 -05:00
|
|
|
#
|
|
|
|
#
|
2018-07-09 02:09:20 -04:00
|
|
|
import itertools
|
|
|
|
import logging
|
2024-05-16 06:36:54 -04:00
|
|
|
from enum import Enum
|
2022-08-10 07:58:20 -04:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
2022-09-08 15:13:39 -04:00
|
|
|
AbstractSet,
|
2022-08-10 07:58:20 -04:00
|
|
|
Any,
|
|
|
|
Dict,
|
|
|
|
FrozenSet,
|
|
|
|
List,
|
2024-05-23 13:06:16 -04:00
|
|
|
Literal,
|
2022-08-18 06:53:02 -04:00
|
|
|
Mapping,
|
2022-08-10 07:58:20 -04:00
|
|
|
Optional,
|
|
|
|
Sequence,
|
|
|
|
Set,
|
|
|
|
Tuple,
|
2024-05-23 13:06:16 -04:00
|
|
|
Union,
|
|
|
|
overload,
|
2022-08-10 07:58:20 -04:00
|
|
|
)
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
import attr
|
2018-10-09 09:15:49 -04:00
|
|
|
from prometheus_client import Counter
|
|
|
|
|
2023-01-13 09:57:43 -05:00
|
|
|
from synapse.api.constants import (
|
|
|
|
AccountDataTypes,
|
2024-08-07 12:27:50 -04:00
|
|
|
Direction,
|
2023-01-13 09:57:43 -05:00
|
|
|
EventContentFields,
|
|
|
|
EventTypes,
|
2024-03-19 13:52:53 -04:00
|
|
|
JoinRules,
|
2023-01-13 09:57:43 -05:00
|
|
|
Membership,
|
|
|
|
)
|
2020-02-03 13:05:44 -05:00
|
|
|
from synapse.api.filtering import FilterCollection
|
2021-08-23 08:14:42 -04:00
|
|
|
from synapse.api.presence import UserPresenceState
|
2021-08-19 11:12:55 -04:00
|
|
|
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
|
2020-02-03 13:05:44 -05:00
|
|
|
from synapse.events import EventBase
|
2022-03-18 13:49:32 -04:00
|
|
|
from synapse.handlers.relations import BundledAggregations
|
2022-12-23 09:04:50 -05:00
|
|
|
from synapse.logging import issue9533_logger
|
2020-03-24 10:45:33 -04:00
|
|
|
from synapse.logging.context import current_context
|
2022-12-06 04:52:55 -05:00
|
|
|
from synapse.logging.opentracing import (
|
|
|
|
SynapseTags,
|
|
|
|
log_kv,
|
|
|
|
set_tag,
|
|
|
|
start_active_span,
|
|
|
|
trace,
|
|
|
|
)
|
2022-10-04 09:47:04 -04:00
|
|
|
from synapse.storage.databases.main.event_push_actions import RoomNotifCounts
|
2022-11-15 12:35:19 -05:00
|
|
|
from synapse.storage.databases.main.roommember import extract_heroes_from_room_summary
|
2024-08-07 12:27:50 -04:00
|
|
|
from synapse.storage.databases.main.stream import PaginateFunction
|
2018-09-11 19:50:39 -04:00
|
|
|
from synapse.storage.roommember import MemberSummary
|
2020-02-03 13:05:44 -05:00
|
|
|
from synapse.types import (
|
2022-03-30 09:39:27 -04:00
|
|
|
DeviceListUpdates,
|
2020-02-03 13:05:44 -05:00
|
|
|
JsonDict,
|
2023-09-18 09:55:04 -04:00
|
|
|
JsonMapping,
|
2023-10-25 11:16:19 -04:00
|
|
|
MultiWriterStreamToken,
|
2020-08-28 07:28:53 -04:00
|
|
|
MutableStateMap,
|
2020-11-17 05:51:25 -05:00
|
|
|
Requester,
|
2020-02-03 13:05:44 -05:00
|
|
|
RoomStreamToken,
|
|
|
|
StateMap,
|
2023-01-26 12:31:58 -05:00
|
|
|
StrCollection,
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType,
|
2020-02-03 13:05:44 -05:00
|
|
|
StreamToken,
|
|
|
|
UserID,
|
|
|
|
)
|
2022-12-12 11:19:30 -05:00
|
|
|
from synapse.types.state import StateFilter
|
2018-08-10 09:50:21 -04:00
|
|
|
from synapse.util.async_helpers import concurrently_execute
|
2018-07-26 17:51:30 -04:00
|
|
|
from synapse.util.caches.expiringcache import ExpiringCache
|
|
|
|
from synapse.util.caches.lrucache import LruCache
|
2021-06-17 11:23:11 -04:00
|
|
|
from synapse.util.caches.response_cache import ResponseCache, ResponseCacheContext
|
2024-08-09 06:59:44 -04:00
|
|
|
from synapse.util.metrics import Measure
|
2016-05-11 08:42:37 -04:00
|
|
|
from synapse.visibility import filter_events_for_client
|
2018-04-28 07:19:12 -04:00
|
|
|
|
2020-09-03 07:54:10 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2015-01-26 13:53:31 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2018-10-10 06:23:17 -04:00
|
|
|
# Counts the number of times we returned a non-empty sync. `type` is one of
|
|
|
|
# "initial_sync", "full_state_sync" or "incremental_sync", `lazy_loaded` is
|
|
|
|
# "true" or "false" depending on if the request asked for lazy loaded members or
|
|
|
|
# not.
|
|
|
|
non_empty_sync_counter = Counter(
|
2018-10-10 06:40:43 -04:00
|
|
|
"synapse_handlers_sync_nonempty_total",
|
|
|
|
"Count of non empty sync responses. type is initial_sync/full_state_sync"
|
|
|
|
"/incremental_sync. lazy_loaded indicates if lazy loaded members were "
|
|
|
|
"enabled for that request.",
|
|
|
|
["type", "lazy_loaded"],
|
2018-10-09 09:15:49 -04:00
|
|
|
)
|
|
|
|
|
2018-07-26 17:51:30 -04:00
|
|
|
# Store the cache that tracks which lazy-loaded members have been sent to a given
|
|
|
|
# client for no more than 30 minutes.
|
|
|
|
LAZY_LOADED_MEMBERS_CACHE_MAX_AGE = 30 * 60 * 1000
|
|
|
|
|
|
|
|
# Remember the last 100 members we sent to a client for the purposes of
|
|
|
|
# avoiding redundantly sending the same lazy-loaded members to the client
|
|
|
|
LAZY_LOADED_MEMBERS_CACHE_MAX_SIZE = 100
|
|
|
|
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2021-06-17 11:23:11 -04:00
|
|
|
SyncRequestKey = Tuple[Any, ...]
|
|
|
|
|
|
|
|
|
2024-05-16 06:36:54 -04:00
|
|
|
class SyncVersion(Enum):
|
|
|
|
"""
|
|
|
|
Enum for specifying the version of sync request. This is used to key which type of
|
|
|
|
sync response that we are generating.
|
|
|
|
|
|
|
|
This is different than the `sync_type` you might see used in other code below; which
|
|
|
|
specifies the sub-type sync request (e.g. initial_sync, full_state_sync,
|
|
|
|
incremental_sync) and is really only relevant for the `/sync` v2 endpoint.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# These string values are semantically significant because they are used in the the
|
|
|
|
# metrics
|
|
|
|
|
|
|
|
# Traditional `/sync` endpoint
|
|
|
|
SYNC_V2 = "sync_v2"
|
2024-05-23 13:06:16 -04:00
|
|
|
# Part of MSC3575 Sliding Sync
|
|
|
|
E2EE_SYNC = "e2ee_sync"
|
2024-05-16 06:36:54 -04:00
|
|
|
|
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class SyncConfig:
|
2021-08-18 07:27:32 -04:00
|
|
|
user: UserID
|
|
|
|
filter_collection: FilterCollection
|
|
|
|
is_guest: bool
|
|
|
|
device_id: Optional[str]
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class TimelineBatch:
|
2021-08-18 07:27:32 -04:00
|
|
|
prev_batch: StreamToken
|
2022-08-10 07:58:20 -04:00
|
|
|
events: Sequence[EventBase]
|
2021-08-18 07:27:32 -04:00
|
|
|
limited: bool
|
2022-01-13 10:45:28 -05:00
|
|
|
# A mapping of event ID to the bundled aggregations for the above events.
|
|
|
|
# This is only calculated if limited is true.
|
2022-01-26 08:27:04 -05:00
|
|
|
bundled_aggregations: Optional[Dict[str, BundledAggregations]] = None
|
2015-10-01 12:53:07 -04:00
|
|
|
|
2020-09-14 12:50:06 -04:00
|
|
|
def __bool__(self) -> bool:
|
2015-10-01 12:53:07 -04:00
|
|
|
"""Make the result appear empty if there are no updates. This is used
|
|
|
|
to tell if room needs to be part of the sync result.
|
|
|
|
"""
|
|
|
|
return bool(self.events)
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2015-10-05 11:39:22 -04:00
|
|
|
|
2020-09-02 12:19:37 -04:00
|
|
|
# We can't freeze this class, because we need to update it after it's instantiated to
|
|
|
|
# update its unread count. This is because we calculate the unread count for a room only
|
|
|
|
# if there are updates for it, which we check after the instance has been created.
|
|
|
|
# This should not be a big deal because we update the notification counts afterwards as
|
|
|
|
# well anyway.
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class JoinedSyncResult:
|
2021-08-18 07:27:32 -04:00
|
|
|
room_id: str
|
|
|
|
timeline: TimelineBatch
|
|
|
|
state: StateMap[EventBase]
|
|
|
|
ephemeral: List[JsonDict]
|
|
|
|
account_data: List[JsonDict]
|
|
|
|
unread_notifications: JsonDict
|
2022-10-04 09:47:04 -04:00
|
|
|
unread_thread_notifications: JsonDict
|
2021-08-18 07:27:32 -04:00
|
|
|
summary: Optional[JsonDict]
|
|
|
|
unread_count: int
|
2020-02-03 13:05:44 -05:00
|
|
|
|
2020-09-14 12:50:06 -04:00
|
|
|
def __bool__(self) -> bool:
|
2015-01-30 10:52:05 -05:00
|
|
|
"""Make the result appear empty if there are no updates. This is used
|
|
|
|
to tell if room needs to be part of the sync result.
|
|
|
|
"""
|
2015-11-02 11:23:15 -05:00
|
|
|
return bool(
|
2024-09-02 07:39:04 -04:00
|
|
|
self.timeline or self.state or self.ephemeral or self.account_data
|
2015-12-18 12:47:00 -05:00
|
|
|
# nb the notification count does not, er, count: if there's nothing
|
|
|
|
# else in the result, we don't need to send it.
|
2015-11-02 11:23:15 -05:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class ArchivedSyncResult:
|
2021-08-18 07:27:32 -04:00
|
|
|
room_id: str
|
|
|
|
timeline: TimelineBatch
|
|
|
|
state: StateMap[EventBase]
|
|
|
|
account_data: List[JsonDict]
|
2020-02-03 13:05:44 -05:00
|
|
|
|
2020-09-14 12:50:06 -04:00
|
|
|
def __bool__(self) -> bool:
|
2015-10-19 12:26:18 -04:00
|
|
|
"""Make the result appear empty if there are no updates. This is used
|
|
|
|
to tell if room needs to be part of the sync result.
|
|
|
|
"""
|
2015-11-18 10:31:04 -05:00
|
|
|
return bool(self.timeline or self.state or self.account_data)
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2015-10-19 12:26:18 -04:00
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class InvitedSyncResult:
|
2021-08-18 07:27:32 -04:00
|
|
|
room_id: str
|
|
|
|
invite: EventBase
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2020-09-14 12:50:06 -04:00
|
|
|
def __bool__(self) -> bool:
|
2015-10-19 12:26:18 -04:00
|
|
|
"""Invited rooms should always be reported to the client"""
|
|
|
|
return True
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2021-06-09 14:39:51 -04:00
|
|
|
class KnockedSyncResult:
|
2021-08-18 07:27:32 -04:00
|
|
|
room_id: str
|
|
|
|
knock: EventBase
|
2021-06-09 14:39:51 -04:00
|
|
|
|
|
|
|
def __bool__(self) -> bool:
|
|
|
|
"""Knocked rooms should always be reported to the client"""
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class _RoomChanges:
|
|
|
|
"""The set of room entries to include in the sync, plus the set of joined
|
|
|
|
and left room IDs since last sync.
|
|
|
|
"""
|
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
room_entries: List["RoomSyncResultBuilder"]
|
|
|
|
invited: List[InvitedSyncResult]
|
|
|
|
knocked: List[KnockedSyncResult]
|
|
|
|
newly_joined_rooms: List[str]
|
|
|
|
newly_left_rooms: List[str]
|
2020-02-03 13:05:44 -05:00
|
|
|
|
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class SyncResult:
|
|
|
|
"""
|
|
|
|
Attributes:
|
|
|
|
next_batch: Token for the next sync
|
|
|
|
presence: List of presence events for the user.
|
|
|
|
account_data: List of account_data events for the user.
|
|
|
|
joined: JoinedSyncResult for each joined room.
|
|
|
|
invited: InvitedSyncResult for each invited room.
|
2021-06-09 14:39:51 -04:00
|
|
|
knocked: KnockedSyncResult for each knocked on room.
|
2020-02-03 13:05:44 -05:00
|
|
|
archived: ArchivedSyncResult for each archived room.
|
|
|
|
to_device: List of direct messages for the device.
|
|
|
|
device_lists: List of user_ids whose devices have changed
|
|
|
|
device_one_time_keys_count: Dict of algorithm to count for one time keys
|
|
|
|
for this device
|
2020-10-06 13:26:29 -04:00
|
|
|
device_unused_fallback_key_types: List of key types that have an unused fallback
|
|
|
|
key
|
2020-02-03 13:05:44 -05:00
|
|
|
"""
|
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
next_batch: StreamToken
|
2021-08-23 08:14:42 -04:00
|
|
|
presence: List[UserPresenceState]
|
2021-08-18 07:27:32 -04:00
|
|
|
account_data: List[JsonDict]
|
|
|
|
joined: List[JoinedSyncResult]
|
|
|
|
invited: List[InvitedSyncResult]
|
|
|
|
knocked: List[KnockedSyncResult]
|
|
|
|
archived: List[ArchivedSyncResult]
|
|
|
|
to_device: List[JsonDict]
|
2022-03-30 09:39:27 -04:00
|
|
|
device_lists: DeviceListUpdates
|
2023-09-19 15:26:44 -04:00
|
|
|
device_one_time_keys_count: JsonMapping
|
2021-08-18 07:27:32 -04:00
|
|
|
device_unused_fallback_key_types: List[str]
|
2020-02-03 13:05:44 -05:00
|
|
|
|
2020-09-14 12:50:06 -04:00
|
|
|
def __bool__(self) -> bool:
|
2015-01-30 10:52:05 -05:00
|
|
|
"""Make the result appear empty if there are no updates. This is used
|
|
|
|
to tell if the notifier needs to wait for more events when polling for
|
|
|
|
events.
|
|
|
|
"""
|
2015-01-27 11:24:22 -05:00
|
|
|
return bool(
|
2016-02-23 08:40:02 -05:00
|
|
|
self.presence
|
|
|
|
or self.joined
|
|
|
|
or self.invited
|
2021-06-09 14:39:51 -04:00
|
|
|
or self.knocked
|
2016-02-23 08:40:02 -05:00
|
|
|
or self.archived
|
2016-08-25 12:35:37 -04:00
|
|
|
or self.account_data
|
2017-02-01 10:15:16 -05:00
|
|
|
or self.to_device
|
2017-07-10 09:53:19 -04:00
|
|
|
or self.device_lists
|
2015-01-27 11:24:22 -05:00
|
|
|
)
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2024-05-30 09:03:49 -04:00
|
|
|
@staticmethod
|
2024-06-06 12:10:58 -04:00
|
|
|
def empty(
|
|
|
|
next_batch: StreamToken,
|
|
|
|
device_one_time_keys_count: JsonMapping,
|
|
|
|
device_unused_fallback_key_types: List[str],
|
|
|
|
) -> "SyncResult":
|
2024-05-30 09:03:49 -04:00
|
|
|
"Return a new empty result"
|
|
|
|
return SyncResult(
|
|
|
|
next_batch=next_batch,
|
|
|
|
presence=[],
|
|
|
|
account_data=[],
|
|
|
|
joined=[],
|
|
|
|
invited=[],
|
|
|
|
knocked=[],
|
|
|
|
archived=[],
|
|
|
|
to_device=[],
|
|
|
|
device_lists=DeviceListUpdates(),
|
2024-06-06 12:10:58 -04:00
|
|
|
device_one_time_keys_count=device_one_time_keys_count,
|
|
|
|
device_unused_fallback_key_types=device_unused_fallback_key_types,
|
2024-05-30 09:03:49 -04:00
|
|
|
)
|
|
|
|
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
class E2eeSyncResult:
|
|
|
|
"""
|
|
|
|
Attributes:
|
|
|
|
next_batch: Token for the next sync
|
|
|
|
to_device: List of direct messages for the device.
|
|
|
|
device_lists: List of user_ids whose devices have changed
|
|
|
|
device_one_time_keys_count: Dict of algorithm to count for one time keys
|
|
|
|
for this device
|
|
|
|
device_unused_fallback_key_types: List of key types that have an unused fallback
|
|
|
|
key
|
|
|
|
"""
|
|
|
|
|
|
|
|
next_batch: StreamToken
|
|
|
|
to_device: List[JsonDict]
|
|
|
|
device_lists: DeviceListUpdates
|
|
|
|
device_one_time_keys_count: JsonMapping
|
|
|
|
device_unused_fallback_key_types: List[str]
|
|
|
|
|
|
|
|
|
2020-09-04 06:54:56 -04:00
|
|
|
class SyncHandler:
|
2020-09-03 07:54:10 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2018-08-17 11:08:45 -04:00
|
|
|
self.hs_config = hs.config
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2016-05-16 15:19:26 -04:00
|
|
|
self.notifier = hs.get_notifier()
|
|
|
|
self.presence_handler = hs.get_presence_handler()
|
2022-03-18 13:49:32 -04:00
|
|
|
self._relations_handler = hs.get_relations_handler()
|
2023-05-09 10:34:10 -04:00
|
|
|
self._push_rules_handler = hs.get_push_rules_handler()
|
2015-01-26 10:46:31 -05:00
|
|
|
self.event_sources = hs.get_event_sources()
|
2015-01-26 13:53:31 -05:00
|
|
|
self.clock = hs.get_clock()
|
2016-08-26 09:54:30 -04:00
|
|
|
self.state = hs.get_state_handler()
|
2022-06-14 04:51:15 -04:00
|
|
|
self.auth_blocking = hs.get_auth_blocking()
|
2022-05-31 08:17:50 -04:00
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
|
|
|
self._state_storage_controller = self._storage_controllers.state
|
2022-06-17 06:42:03 -04:00
|
|
|
self._device_handler = hs.get_device_handler()
|
2023-09-06 03:30:53 -04:00
|
|
|
self._task_scheduler = hs.get_task_scheduler()
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2023-02-14 06:10:29 -05:00
|
|
|
self.should_calculate_push_rules = hs.config.push.enable_push
|
|
|
|
|
2021-08-03 09:45:04 -04:00
|
|
|
# TODO: flush cache entries on subsequent sync request.
|
|
|
|
# Once we get the next /sync request (ie, one with the same access token
|
|
|
|
# that sets 'since' to 'next_batch'), we know that device won't need a
|
|
|
|
# cached result any more, and we could flush the entry from the cache to save
|
|
|
|
# memory.
|
|
|
|
self.response_cache: ResponseCache[SyncRequestKey] = ResponseCache(
|
|
|
|
hs.get_clock(),
|
|
|
|
"sync",
|
|
|
|
timeout_ms=hs.config.caches.sync_response_cache_duration,
|
|
|
|
)
|
|
|
|
|
2021-04-06 08:58:18 -04:00
|
|
|
# ExpiringCache((User, Device)) -> LruCache(user_id => event_id)
|
2021-07-16 13:22:36 -04:00
|
|
|
self.lazy_loaded_members_cache: ExpiringCache[
|
|
|
|
Tuple[str, Optional[str]], LruCache[str, str]
|
|
|
|
] = ExpiringCache(
|
2018-07-26 17:51:30 -04:00
|
|
|
"lazy_loaded_members_cache",
|
|
|
|
self.clock,
|
|
|
|
max_len=0,
|
|
|
|
expiry_ms=LAZY_LOADED_MEMBERS_CACHE_MAX_AGE,
|
2021-07-16 13:22:36 -04:00
|
|
|
)
|
2018-07-26 17:51:30 -04:00
|
|
|
|
2023-01-23 10:44:39 -05:00
|
|
|
self.rooms_to_exclude_globally = hs.config.server.rooms_to_exclude_from_sync
|
2022-03-30 05:43:04 -04:00
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
@overload
|
|
|
|
async def wait_for_sync_for_user(
|
|
|
|
self,
|
|
|
|
requester: Requester,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: Literal[SyncVersion.SYNC_V2],
|
|
|
|
request_key: SyncRequestKey,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
timeout: int = 0,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> SyncResult: ...
|
|
|
|
|
|
|
|
@overload
|
|
|
|
async def wait_for_sync_for_user(
|
|
|
|
self,
|
|
|
|
requester: Requester,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: Literal[SyncVersion.E2EE_SYNC],
|
|
|
|
request_key: SyncRequestKey,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
timeout: int = 0,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> E2eeSyncResult: ...
|
|
|
|
|
|
|
|
@overload
|
2019-12-05 12:58:25 -05:00
|
|
|
async def wait_for_sync_for_user(
|
2020-02-03 13:05:44 -05:00
|
|
|
self,
|
2020-11-17 05:51:25 -05:00
|
|
|
requester: Requester,
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_config: SyncConfig,
|
2024-05-16 06:36:54 -04:00
|
|
|
sync_version: SyncVersion,
|
2024-05-16 12:54:46 -04:00
|
|
|
request_key: SyncRequestKey,
|
2020-02-03 13:05:44 -05:00
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
timeout: int = 0,
|
|
|
|
full_state: bool = False,
|
2024-05-23 13:06:16 -04:00
|
|
|
) -> Union[SyncResult, E2eeSyncResult]: ...
|
|
|
|
|
|
|
|
async def wait_for_sync_for_user(
|
|
|
|
self,
|
|
|
|
requester: Requester,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: SyncVersion,
|
|
|
|
request_key: SyncRequestKey,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
timeout: int = 0,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> Union[SyncResult, E2eeSyncResult]:
|
2015-01-27 11:24:22 -05:00
|
|
|
"""Get the sync for a client if we have new data for it now. Otherwise
|
|
|
|
wait for new data to arrive on the server. If the timeout expires, then
|
|
|
|
return an empty sync result.
|
2024-05-16 06:36:54 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
requester: The user requesting the sync response.
|
|
|
|
sync_config: Config/info necessary to process the sync request.
|
|
|
|
sync_version: Determines what kind of sync response to generate.
|
2024-05-16 12:54:46 -04:00
|
|
|
request_key: The key to use for caching the response.
|
2024-05-16 06:36:54 -04:00
|
|
|
since_token: The point in the stream to sync from.
|
|
|
|
timeout: How long to wait for new data to arrive before giving up.
|
|
|
|
full_state: Whether to return the full state for each room.
|
2024-05-23 13:06:16 -04:00
|
|
|
|
2024-05-16 06:36:54 -04:00
|
|
|
Returns:
|
|
|
|
When `SyncVersion.SYNC_V2`, returns a full `SyncResult`.
|
2024-05-23 13:06:16 -04:00
|
|
|
When `SyncVersion.E2EE_SYNC`, returns a `E2eeSyncResult`.
|
2015-01-27 11:24:22 -05:00
|
|
|
"""
|
2018-08-09 12:39:12 -04:00
|
|
|
# If the user is not part of the mau group, then check that limits have
|
|
|
|
# not been exceeded (if not part of the group by this point, almost certain
|
|
|
|
# auth_blocking will occur)
|
|
|
|
user_id = sync_config.user.to_string()
|
2022-06-14 04:51:15 -04:00
|
|
|
await self.auth_blocking.check_auth_blocking(requester=requester)
|
2018-08-09 12:39:12 -04:00
|
|
|
|
2021-03-02 09:43:34 -05:00
|
|
|
res = await self.response_cache.wrap(
|
2024-05-16 12:54:46 -04:00
|
|
|
request_key,
|
2018-04-12 07:08:59 -04:00
|
|
|
self._wait_for_sync_for_user,
|
|
|
|
sync_config,
|
2024-05-16 06:36:54 -04:00
|
|
|
sync_version,
|
2018-04-12 07:08:59 -04:00
|
|
|
since_token,
|
|
|
|
timeout,
|
|
|
|
full_state,
|
2021-06-17 11:23:11 -04:00
|
|
|
cache_context=True,
|
2018-04-12 07:08:59 -04:00
|
|
|
)
|
2020-07-22 08:43:10 -04:00
|
|
|
logger.debug("Returning sync response for %s", user_id)
|
2019-07-23 09:00:55 -04:00
|
|
|
return res
|
2015-10-26 14:47:18 -04:00
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
@overload
|
|
|
|
async def _wait_for_sync_for_user(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: Literal[SyncVersion.SYNC_V2],
|
|
|
|
since_token: Optional[StreamToken],
|
|
|
|
timeout: int,
|
|
|
|
full_state: bool,
|
|
|
|
cache_context: ResponseCacheContext[SyncRequestKey],
|
|
|
|
) -> SyncResult: ...
|
|
|
|
|
|
|
|
@overload
|
|
|
|
async def _wait_for_sync_for_user(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: Literal[SyncVersion.E2EE_SYNC],
|
|
|
|
since_token: Optional[StreamToken],
|
|
|
|
timeout: int,
|
|
|
|
full_state: bool,
|
|
|
|
cache_context: ResponseCacheContext[SyncRequestKey],
|
|
|
|
) -> E2eeSyncResult: ...
|
|
|
|
|
|
|
|
@overload
|
2019-12-05 12:58:25 -05:00
|
|
|
async def _wait_for_sync_for_user(
|
2020-02-03 13:05:44 -05:00
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
2024-05-16 06:36:54 -04:00
|
|
|
sync_version: SyncVersion,
|
2021-06-17 11:23:11 -04:00
|
|
|
since_token: Optional[StreamToken],
|
|
|
|
timeout: int,
|
|
|
|
full_state: bool,
|
|
|
|
cache_context: ResponseCacheContext[SyncRequestKey],
|
2024-05-23 13:06:16 -04:00
|
|
|
) -> Union[SyncResult, E2eeSyncResult]: ...
|
|
|
|
|
|
|
|
async def _wait_for_sync_for_user(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: SyncVersion,
|
|
|
|
since_token: Optional[StreamToken],
|
|
|
|
timeout: int,
|
|
|
|
full_state: bool,
|
|
|
|
cache_context: ResponseCacheContext[SyncRequestKey],
|
|
|
|
) -> Union[SyncResult, E2eeSyncResult]:
|
2021-12-02 15:58:32 -05:00
|
|
|
"""The start of the machinery that produces a /sync response.
|
|
|
|
|
|
|
|
See https://spec.matrix.org/v1.1/client-server-api/#syncing for full details.
|
|
|
|
|
|
|
|
This method does high-level bookkeeping:
|
|
|
|
- tracking the kind of sync in the logging context
|
|
|
|
- deleting any to_device messages whose delivery has been acknowledged.
|
|
|
|
- deciding if we should dispatch an instant or delayed response
|
|
|
|
- marking the sync as being lazily loaded, if appropriate
|
|
|
|
|
|
|
|
Computing the body of the response begins in the next method,
|
|
|
|
`current_sync_for_user`.
|
|
|
|
"""
|
2018-10-09 09:15:49 -04:00
|
|
|
if since_token is None:
|
|
|
|
sync_type = "initial_sync"
|
|
|
|
elif full_state:
|
|
|
|
sync_type = "full_state_sync"
|
|
|
|
else:
|
|
|
|
sync_type = "incremental_sync"
|
|
|
|
|
2024-05-16 06:36:54 -04:00
|
|
|
sync_label = f"{sync_version}:{sync_type}"
|
|
|
|
|
2020-03-24 10:45:33 -04:00
|
|
|
context = current_context()
|
2016-02-03 08:51:25 -05:00
|
|
|
if context:
|
2024-05-16 06:36:54 -04:00
|
|
|
context.tag = sync_label
|
2018-10-09 09:15:49 -04:00
|
|
|
|
2024-05-30 09:03:49 -04:00
|
|
|
if since_token is not None:
|
|
|
|
# We need to make sure this worker has caught up with the token. If
|
|
|
|
# this returns false it means we timed out waiting, and we should
|
|
|
|
# just return an empty response.
|
|
|
|
start = self.clock.time_msec()
|
|
|
|
if not await self.notifier.wait_for_stream_token(since_token):
|
|
|
|
logger.warning(
|
|
|
|
"Timed out waiting for worker to catch up. Returning empty response"
|
|
|
|
)
|
2024-06-06 12:10:58 -04:00
|
|
|
device_id = sync_config.device_id
|
|
|
|
one_time_keys_count: JsonMapping = {}
|
|
|
|
unused_fallback_key_types: List[str] = []
|
|
|
|
if device_id:
|
|
|
|
user_id = sync_config.user.to_string()
|
|
|
|
# TODO: We should have a way to let clients differentiate between the states of:
|
|
|
|
# * no change in OTK count since the provided since token
|
|
|
|
# * the server has zero OTKs left for this device
|
|
|
|
# Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
|
|
|
|
one_time_keys_count = await self.store.count_e2e_one_time_keys(
|
|
|
|
user_id, device_id
|
|
|
|
)
|
|
|
|
unused_fallback_key_types = list(
|
|
|
|
await self.store.get_e2e_unused_fallback_key_types(
|
|
|
|
user_id, device_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
cache_context.should_cache = False # Don't cache empty responses
|
|
|
|
return SyncResult.empty(
|
|
|
|
since_token, one_time_keys_count, unused_fallback_key_types
|
|
|
|
)
|
2024-05-30 09:03:49 -04:00
|
|
|
|
|
|
|
# If we've spent significant time waiting to catch up, take it off
|
|
|
|
# the timeout.
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
if now - start > 1_000:
|
|
|
|
timeout -= now - start
|
|
|
|
timeout = max(timeout, 0)
|
|
|
|
|
2021-06-07 04:19:06 -04:00
|
|
|
# if we have a since token, delete any to-device messages before that token
|
|
|
|
# (since we now know that the device has received them)
|
|
|
|
if since_token is not None:
|
|
|
|
since_stream_id = since_token.to_device_key
|
2023-09-13 06:59:44 -04:00
|
|
|
deleted = await self.store.delete_messages_for_device(
|
|
|
|
sync_config.user.to_string(),
|
|
|
|
sync_config.device_id,
|
|
|
|
since_stream_id,
|
2021-06-07 04:19:06 -04:00
|
|
|
)
|
|
|
|
logger.debug(
|
2023-09-13 06:59:44 -04:00
|
|
|
"Deleted %d to-device messages up to %d", deleted, since_stream_id
|
2021-06-07 04:19:06 -04:00
|
|
|
)
|
|
|
|
|
2015-10-26 14:47:18 -04:00
|
|
|
if timeout == 0 or since_token is None or full_state:
|
|
|
|
# we are going to return immediately, so don't bother calling
|
|
|
|
# notifier.wait_for_events.
|
2024-09-02 07:39:04 -04:00
|
|
|
result: Union[
|
|
|
|
SyncResult, E2eeSyncResult
|
|
|
|
] = await self.current_sync_for_user(
|
|
|
|
sync_config, sync_version, since_token, full_state=full_state
|
2016-01-25 05:10:44 -05:00
|
|
|
)
|
2015-01-26 10:46:31 -05:00
|
|
|
else:
|
2021-12-02 15:58:32 -05:00
|
|
|
# Otherwise, we wait for something to happen and report it to the user.
|
2021-09-20 08:56:23 -04:00
|
|
|
async def current_sync_callback(
|
|
|
|
before_token: StreamToken, after_token: StreamToken
|
2024-05-23 13:06:16 -04:00
|
|
|
) -> Union[SyncResult, E2eeSyncResult]:
|
2024-05-16 06:36:54 -04:00
|
|
|
return await self.current_sync_for_user(
|
|
|
|
sync_config, sync_version, since_token
|
|
|
|
)
|
2015-01-27 15:09:52 -05:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
result = await self.notifier.wait_for_events(
|
2016-01-20 10:34:07 -05:00
|
|
|
sync_config.user.to_string(),
|
|
|
|
timeout,
|
|
|
|
current_sync_callback,
|
2016-01-25 05:10:44 -05:00
|
|
|
from_token=since_token,
|
2015-01-26 10:46:31 -05:00
|
|
|
)
|
2018-10-10 06:23:17 -04:00
|
|
|
|
2023-08-17 05:22:50 -04:00
|
|
|
# if nothing has happened in any of the users' rooms since /sync was called,
|
|
|
|
# the resultant next_batch will be the same as since_token (since the result
|
|
|
|
# is generated when wait_for_events is first called, and not regenerated
|
|
|
|
# when wait_for_events times out).
|
|
|
|
#
|
|
|
|
# If that happens, we mustn't cache it, so that when the client comes back
|
|
|
|
# with the same cache token, we don't immediately return the same empty
|
2023-11-15 08:02:11 -05:00
|
|
|
# result, causing a tightloop. (https://github.com/matrix-org/synapse/issues/8518)
|
2023-08-17 05:22:50 -04:00
|
|
|
if result.next_batch == since_token:
|
|
|
|
cache_context.should_cache = False
|
2021-06-17 11:23:11 -04:00
|
|
|
|
2018-10-10 06:23:17 -04:00
|
|
|
if result:
|
|
|
|
if sync_config.filter_collection.lazy_load_members():
|
|
|
|
lazy_loaded = "true"
|
|
|
|
else:
|
|
|
|
lazy_loaded = "false"
|
2024-05-16 06:36:54 -04:00
|
|
|
non_empty_sync_counter.labels(sync_label, lazy_loaded).inc()
|
2018-10-10 06:23:17 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return result
|
2015-01-26 10:46:31 -05:00
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
@overload
|
|
|
|
async def current_sync_for_user(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: Literal[SyncVersion.SYNC_V2],
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> SyncResult: ...
|
|
|
|
|
|
|
|
@overload
|
|
|
|
async def current_sync_for_user(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: Literal[SyncVersion.E2EE_SYNC],
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> E2eeSyncResult: ...
|
|
|
|
|
|
|
|
@overload
|
2020-02-03 13:05:44 -05:00
|
|
|
async def current_sync_for_user(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
2024-05-16 06:36:54 -04:00
|
|
|
sync_version: SyncVersion,
|
2020-02-03 13:05:44 -05:00
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
full_state: bool = False,
|
2024-05-23 13:06:16 -04:00
|
|
|
) -> Union[SyncResult, E2eeSyncResult]: ...
|
|
|
|
|
|
|
|
async def current_sync_for_user(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
sync_version: SyncVersion,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> Union[SyncResult, E2eeSyncResult]:
|
|
|
|
"""
|
|
|
|
Generates the response body of a sync result, represented as a
|
|
|
|
`SyncResult`/`E2eeSyncResult`.
|
2021-12-02 15:58:32 -05:00
|
|
|
|
|
|
|
This is a wrapper around `generate_sync_result` which starts an open tracing
|
|
|
|
span to track the sync. See `generate_sync_result` for the next part of your
|
|
|
|
indoctrination.
|
2024-05-16 06:36:54 -04:00
|
|
|
|
|
|
|
Args:
|
|
|
|
sync_config: Config/info necessary to process the sync request.
|
|
|
|
sync_version: Determines what kind of sync response to generate.
|
|
|
|
since_token: The point in the stream to sync from.p.
|
|
|
|
full_state: Whether to return the full state for each room.
|
2024-05-23 13:06:16 -04:00
|
|
|
|
2024-05-16 06:36:54 -04:00
|
|
|
Returns:
|
|
|
|
When `SyncVersion.SYNC_V2`, returns a full `SyncResult`.
|
2024-05-23 13:06:16 -04:00
|
|
|
When `SyncVersion.E2EE_SYNC`, returns a `E2eeSyncResult`.
|
2021-12-02 15:58:32 -05:00
|
|
|
"""
|
2021-12-21 06:10:36 -05:00
|
|
|
with start_active_span("sync.current_sync_for_user"):
|
2021-04-01 12:08:21 -04:00
|
|
|
log_kv({"since_token": since_token})
|
2024-05-23 13:06:16 -04:00
|
|
|
|
2024-05-16 06:36:54 -04:00
|
|
|
# Go through the `/sync` v2 path
|
|
|
|
if sync_version == SyncVersion.SYNC_V2:
|
2024-09-02 07:39:04 -04:00
|
|
|
sync_result: Union[
|
|
|
|
SyncResult, E2eeSyncResult
|
|
|
|
] = await self.generate_sync_result(
|
|
|
|
sync_config, since_token, full_state
|
2024-05-23 13:06:16 -04:00
|
|
|
)
|
|
|
|
# Go through the MSC3575 Sliding Sync `/sync/e2ee` path
|
|
|
|
elif sync_version == SyncVersion.E2EE_SYNC:
|
|
|
|
sync_result = await self.generate_e2ee_sync_result(
|
|
|
|
sync_config, since_token
|
2024-05-16 06:36:54 -04:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise Exception(
|
|
|
|
f"Unknown sync_version (this is a Synapse problem): {sync_version}"
|
|
|
|
)
|
2021-04-01 12:08:21 -04:00
|
|
|
|
|
|
|
set_tag(SynapseTags.SYNC_RESULT, bool(sync_result))
|
|
|
|
return sync_result
|
2015-01-27 11:24:22 -05:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
async def ephemeral_by_room(
|
|
|
|
self,
|
|
|
|
sync_result_builder: "SyncResultBuilder",
|
|
|
|
now_token: StreamToken,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
) -> Tuple[StreamToken, Dict[str, List[JsonDict]]]:
|
2015-11-02 12:54:04 -05:00
|
|
|
"""Get the ephemeral events for each room the user is in
|
2015-10-21 10:45:37 -04:00
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_result_builder
|
|
|
|
now_token: Where the server is currently up to.
|
|
|
|
since_token: Where the server was when the client
|
2015-10-21 10:45:37 -04:00
|
|
|
last synced.
|
|
|
|
Returns:
|
|
|
|
A tuple of the now StreamToken, updated to reflect the which typing
|
|
|
|
events are included, and a dict mapping from room_id to a list of
|
|
|
|
typing events for that room.
|
|
|
|
"""
|
|
|
|
|
2018-03-05 07:06:19 -05:00
|
|
|
sync_config = sync_result_builder.sync_config
|
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
with Measure(self.clock, "ephemeral_by_room"):
|
2020-09-11 07:22:55 -04:00
|
|
|
typing_key = since_token.typing_key if since_token else 0
|
2015-10-20 11:36:20 -04:00
|
|
|
|
2018-03-05 07:06:19 -05:00
|
|
|
room_ids = sync_result_builder.joined_room_ids
|
2015-10-20 11:36:20 -04:00
|
|
|
|
2021-09-21 13:34:26 -04:00
|
|
|
typing_source = self.event_sources.sources.typing
|
2019-12-06 05:14:59 -05:00
|
|
|
typing, typing_key = await typing_source.get_new_events(
|
2016-02-09 06:31:04 -05:00
|
|
|
user=sync_config.user,
|
|
|
|
from_key=typing_key,
|
|
|
|
limit=sync_config.filter_collection.ephemeral_limit(),
|
|
|
|
room_ids=room_ids,
|
|
|
|
is_guest=sync_config.is_guest,
|
|
|
|
)
|
2022-05-16 11:35:31 -04:00
|
|
|
now_token = now_token.copy_and_replace(StreamKeyType.TYPING, typing_key)
|
2016-02-09 06:31:04 -05:00
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
ephemeral_by_room: JsonDict = {}
|
2016-02-09 06:31:04 -05:00
|
|
|
|
|
|
|
for event in typing:
|
|
|
|
# we want to exclude the room_id from the event, but modifying the
|
|
|
|
# result returned by the event source is poor form (it might cache
|
|
|
|
# the object)
|
|
|
|
room_id = event["room_id"]
|
2020-06-15 07:03:36 -04:00
|
|
|
event_copy = {k: v for (k, v) in event.items() if k != "room_id"}
|
2016-02-09 06:31:04 -05:00
|
|
|
ephemeral_by_room.setdefault(room_id, []).append(event_copy)
|
|
|
|
|
2023-10-25 11:16:19 -04:00
|
|
|
receipt_key = (
|
|
|
|
since_token.receipt_key
|
|
|
|
if since_token
|
|
|
|
else MultiWriterStreamToken(stream=0)
|
|
|
|
)
|
2016-02-09 06:31:04 -05:00
|
|
|
|
2021-09-21 13:34:26 -04:00
|
|
|
receipt_source = self.event_sources.sources.receipt
|
2019-12-05 12:58:25 -05:00
|
|
|
receipts, receipt_key = await receipt_source.get_new_events(
|
2016-02-09 06:31:04 -05:00
|
|
|
user=sync_config.user,
|
|
|
|
from_key=receipt_key,
|
|
|
|
limit=sync_config.filter_collection.ephemeral_limit(),
|
|
|
|
room_ids=room_ids,
|
|
|
|
is_guest=sync_config.is_guest,
|
|
|
|
)
|
2022-05-16 11:35:31 -04:00
|
|
|
now_token = now_token.copy_and_replace(StreamKeyType.RECEIPT, receipt_key)
|
2015-11-02 12:54:04 -05:00
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
for event in receipts:
|
|
|
|
room_id = event["room_id"]
|
|
|
|
# exclude room id, as above
|
2020-06-15 07:03:36 -04:00
|
|
|
event_copy = {k: v for (k, v) in event.items() if k != "room_id"}
|
2016-02-09 06:31:04 -05:00
|
|
|
ephemeral_by_room.setdefault(room_id, []).append(event_copy)
|
2015-10-20 11:36:20 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return now_token, ephemeral_by_room
|
2015-10-20 11:36:20 -04:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
async def _load_filtered_recents(
|
2016-05-24 05:22:24 -04:00
|
|
|
self,
|
2020-02-03 13:05:44 -05:00
|
|
|
room_id: str,
|
2023-10-19 10:04:18 -04:00
|
|
|
sync_result_builder: "SyncResultBuilder",
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_config: SyncConfig,
|
2023-10-19 10:04:18 -04:00
|
|
|
upto_token: StreamToken,
|
2020-02-03 13:05:44 -05:00
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
potential_recents: Optional[List[EventBase]] = None,
|
|
|
|
newly_joined_room: bool = False,
|
|
|
|
) -> TimelineBatch:
|
2023-10-19 10:04:18 -04:00
|
|
|
"""Create a timeline batch for the room
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_id
|
|
|
|
sync_result_builder
|
|
|
|
sync_config
|
|
|
|
upto_token: The token up to which we should fetch (more) events.
|
|
|
|
If `potential_results` is non-empty then this is *start* of
|
|
|
|
the the list.
|
|
|
|
since_token
|
|
|
|
potential_recents: If non-empty, the events between the since token
|
|
|
|
and current token to send down to clients.
|
|
|
|
newly_joined_room
|
|
|
|
"""
|
2016-02-09 06:31:04 -05:00
|
|
|
with Measure(self.clock, "load_filtered_recents"):
|
|
|
|
timeline_limit = sync_config.filter_collection.timeline_limit()
|
2016-11-22 11:38:35 -05:00
|
|
|
block_all_timeline = (
|
|
|
|
sync_config.filter_collection.blocks_all_room_timeline()
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2016-02-09 06:31:04 -05:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
if (
|
|
|
|
potential_recents is None
|
|
|
|
or newly_joined_room
|
|
|
|
or timeline_limit < len(potential_recents)
|
|
|
|
):
|
2016-02-09 08:50:29 -05:00
|
|
|
limited = True
|
|
|
|
else:
|
|
|
|
limited = False
|
2016-02-09 06:31:04 -05:00
|
|
|
|
2023-10-19 10:04:18 -04:00
|
|
|
# Check if there is a gap, if so we need to mark this as limited and
|
|
|
|
# recalculate which events to send down.
|
|
|
|
gap_token = await self.store.get_timeline_gaps(
|
|
|
|
room_id,
|
|
|
|
since_token.room_key if since_token else None,
|
|
|
|
sync_result_builder.now_token.room_key,
|
|
|
|
)
|
|
|
|
if gap_token:
|
|
|
|
# There's a gap, so we need to ignore the passed in
|
|
|
|
# `potential_recents`, and reset `upto_token` to match.
|
|
|
|
potential_recents = None
|
|
|
|
upto_token = sync_result_builder.now_token
|
|
|
|
limited = True
|
|
|
|
|
2021-09-06 11:08:25 -04:00
|
|
|
log_kv({"limited": limited})
|
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
if potential_recents:
|
2021-11-09 08:10:58 -05:00
|
|
|
recents = await sync_config.filter_collection.filter_room_timeline(
|
2020-02-03 13:05:44 -05:00
|
|
|
potential_recents
|
|
|
|
)
|
2021-09-06 11:08:25 -04:00
|
|
|
log_kv({"recents_after_sync_filtering": len(recents)})
|
2017-09-25 12:35:39 -04:00
|
|
|
|
|
|
|
# We check if there are any state events, if there are then we pass
|
|
|
|
# all current state events to the filter_events function. This is to
|
|
|
|
# ensure that we always include current state in the timeline
|
2021-07-16 13:22:36 -04:00
|
|
|
current_state_ids: FrozenSet[str] = frozenset()
|
2017-09-25 12:35:39 -04:00
|
|
|
if any(e.is_state() for e in recents):
|
2022-08-18 06:53:02 -04:00
|
|
|
# FIXME(faster_joins): We use the partial state here as
|
|
|
|
# we don't want to block `/sync` on finishing a lazy join.
|
|
|
|
# Which should be fine once
|
|
|
|
# https://github.com/matrix-org/synapse/issues/12989 is resolved,
|
|
|
|
# since we shouldn't reach here anymore?
|
|
|
|
# Note that we use the current state as a whitelist for filtering
|
|
|
|
# `recents`, so partial state is only a problem when a membership
|
|
|
|
# event turns up in `recents` but has not made it into the current
|
|
|
|
# state.
|
2024-01-10 10:11:59 -05:00
|
|
|
current_state_ids = (
|
|
|
|
await self.store.check_if_events_in_current_state(
|
|
|
|
{e.event_id for e in recents if e.is_state()}
|
|
|
|
)
|
2020-02-03 13:05:44 -05:00
|
|
|
)
|
2017-09-25 12:35:39 -04:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
recents = await filter_events_for_client(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._storage_controllers,
|
2016-02-09 06:31:04 -05:00
|
|
|
sync_config.user.to_string(),
|
|
|
|
recents,
|
2017-09-18 12:13:03 -04:00
|
|
|
always_include_ids=current_state_ids,
|
2016-02-09 06:31:04 -05:00
|
|
|
)
|
2021-09-06 11:08:25 -04:00
|
|
|
log_kv({"recents_after_visibility_filtering": len(recents)})
|
2016-02-09 06:31:04 -05:00
|
|
|
else:
|
|
|
|
recents = []
|
|
|
|
|
2016-11-22 11:38:35 -05:00
|
|
|
if not limited or block_all_timeline:
|
2023-10-19 10:04:18 -04:00
|
|
|
prev_batch_token = upto_token
|
2020-10-07 08:49:40 -04:00
|
|
|
if recents:
|
2024-01-05 08:03:20 -05:00
|
|
|
assert recents[0].internal_metadata.stream_ordering
|
|
|
|
room_key = RoomStreamToken(
|
|
|
|
stream=recents[0].internal_metadata.stream_ordering - 1
|
|
|
|
)
|
2023-10-19 10:04:18 -04:00
|
|
|
prev_batch_token = upto_token.copy_and_replace(
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType.ROOM, room_key
|
|
|
|
)
|
2020-10-07 08:49:40 -04:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return TimelineBatch(
|
2020-10-07 08:49:40 -04:00
|
|
|
events=recents, prev_batch=prev_batch_token, limited=False
|
2016-05-24 09:00:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
filtering_factor = 2
|
|
|
|
load_limit = max(timeline_limit * filtering_factor, 10)
|
|
|
|
max_repeat = 5 # Only try a few times per room, otherwise
|
2023-10-19 10:04:18 -04:00
|
|
|
room_key = upto_token.room_key
|
2016-05-24 09:00:43 -04:00
|
|
|
end_key = room_key
|
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
since_key = None
|
2023-10-19 10:04:18 -04:00
|
|
|
if since_token and gap_token:
|
|
|
|
# If there is a gap then we need to only include events after
|
|
|
|
# it.
|
|
|
|
since_key = gap_token
|
|
|
|
elif since_token and not newly_joined_room:
|
2016-02-09 06:31:04 -05:00
|
|
|
since_key = since_token.room_key
|
|
|
|
|
|
|
|
while limited and len(recents) < timeline_limit and max_repeat:
|
2024-08-07 12:27:50 -04:00
|
|
|
# For initial `/sync`, we want to view a historical section of the
|
|
|
|
# timeline; to fetch events by `topological_ordering` (best
|
|
|
|
# representation of the room DAG as others were seeing it at the time).
|
|
|
|
# This also aligns with the order that `/messages` returns events in.
|
|
|
|
#
|
|
|
|
# For incremental `/sync`, we want to get all updates for rooms since
|
|
|
|
# the last `/sync` (regardless if those updates arrived late or happened
|
|
|
|
# a while ago in the past); to fetch events by `stream_ordering` (in the
|
|
|
|
# order they were received by the server).
|
|
|
|
#
|
|
|
|
# Relevant spec issue: https://github.com/matrix-org/matrix-spec/issues/1917
|
|
|
|
#
|
|
|
|
# FIXME: Using workaround for mypy,
|
|
|
|
# https://github.com/python/mypy/issues/10740#issuecomment-1997047277 and
|
|
|
|
# https://github.com/python/mypy/issues/17479
|
|
|
|
paginate_room_events_by_topological_ordering: PaginateFunction = (
|
|
|
|
self.store.paginate_room_events_by_topological_ordering
|
|
|
|
)
|
|
|
|
paginate_room_events_by_stream_ordering: PaginateFunction = (
|
|
|
|
self.store.paginate_room_events_by_stream_ordering
|
|
|
|
)
|
|
|
|
pagination_method: PaginateFunction = (
|
|
|
|
# Use `topographical_ordering` for historical events
|
|
|
|
paginate_room_events_by_topological_ordering
|
|
|
|
if since_key is None
|
|
|
|
# Use `stream_ordering` for updates
|
|
|
|
else paginate_room_events_by_stream_ordering
|
|
|
|
)
|
2024-09-10 04:52:42 -04:00
|
|
|
events, end_key, limited = await pagination_method(
|
2024-08-07 12:27:50 -04:00
|
|
|
room_id=room_id,
|
|
|
|
# The bounds are reversed so we can paginate backwards
|
|
|
|
# (from newer to older events) starting at to_bound.
|
|
|
|
# This ensures we fill the `limit` with the newest events first,
|
|
|
|
from_key=end_key,
|
|
|
|
to_key=since_key,
|
|
|
|
direction=Direction.BACKWARDS,
|
2024-09-10 04:52:42 -04:00
|
|
|
limit=load_limit,
|
2024-08-07 12:27:50 -04:00
|
|
|
)
|
|
|
|
# We want to return the events in ascending order (the last event is the
|
|
|
|
# most recent).
|
|
|
|
events.reverse()
|
2021-09-06 11:08:25 -04:00
|
|
|
|
|
|
|
log_kv({"loaded_recents": len(events)})
|
|
|
|
|
2021-11-09 08:10:58 -05:00
|
|
|
loaded_recents = (
|
|
|
|
await sync_config.filter_collection.filter_room_timeline(events)
|
2016-02-09 06:31:04 -05:00
|
|
|
)
|
2017-09-25 12:35:39 -04:00
|
|
|
|
2021-09-06 11:08:25 -04:00
|
|
|
log_kv({"loaded_recents_after_sync_filtering": len(loaded_recents)})
|
|
|
|
|
2017-09-25 12:35:39 -04:00
|
|
|
# We check if there are any state events, if there are then we pass
|
|
|
|
# all current state events to the filter_events function. This is to
|
|
|
|
# ensure that we always include current state in the timeline
|
|
|
|
current_state_ids = frozenset()
|
|
|
|
if any(e.is_state() for e in loaded_recents):
|
2022-06-01 11:02:53 -04:00
|
|
|
# FIXME(faster_joins): We use the partial state here as
|
|
|
|
# we don't want to block `/sync` on finishing a lazy join.
|
2022-08-18 06:53:02 -04:00
|
|
|
# Which should be fine once
|
|
|
|
# https://github.com/matrix-org/synapse/issues/12989 is resolved,
|
|
|
|
# since we shouldn't reach here anymore?
|
|
|
|
# Note that we use the current state as a whitelist for filtering
|
|
|
|
# `loaded_recents`, so partial state is only a problem when a
|
|
|
|
# membership event turns up in `loaded_recents` but has not made it
|
|
|
|
# into the current state.
|
2024-01-10 10:11:59 -05:00
|
|
|
current_state_ids = (
|
|
|
|
await self.store.check_if_events_in_current_state(
|
|
|
|
{e.event_id for e in loaded_recents if e.is_state()}
|
|
|
|
)
|
2020-02-03 13:05:44 -05:00
|
|
|
)
|
2017-09-25 12:35:39 -04:00
|
|
|
|
2024-03-19 13:52:53 -04:00
|
|
|
filtered_recents = await filter_events_for_client(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._storage_controllers,
|
2016-02-09 06:31:04 -05:00
|
|
|
sync_config.user.to_string(),
|
|
|
|
loaded_recents,
|
2017-09-18 12:13:03 -04:00
|
|
|
always_include_ids=current_state_ids,
|
2016-02-09 06:31:04 -05:00
|
|
|
)
|
2021-09-06 11:08:25 -04:00
|
|
|
|
2024-03-19 13:52:53 -04:00
|
|
|
loaded_recents = []
|
|
|
|
for event in filtered_recents:
|
|
|
|
if event.type == EventTypes.CallInvite:
|
|
|
|
room_info = await self.store.get_room_with_stats(event.room_id)
|
|
|
|
assert room_info is not None
|
|
|
|
if room_info.join_rules == JoinRules.PUBLIC:
|
|
|
|
continue
|
|
|
|
loaded_recents.append(event)
|
|
|
|
|
2021-09-06 11:08:25 -04:00
|
|
|
log_kv({"loaded_recents_after_client_filtering": len(loaded_recents)})
|
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
loaded_recents.extend(recents)
|
|
|
|
recents = loaded_recents
|
2016-01-27 12:06:52 -05:00
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
max_repeat -= 1
|
2015-01-30 06:32:35 -05:00
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
if len(recents) > timeline_limit:
|
|
|
|
limited = True
|
|
|
|
recents = recents[-timeline_limit:]
|
2024-01-05 08:03:20 -05:00
|
|
|
assert recents[0].internal_metadata.stream_ordering
|
|
|
|
room_key = RoomStreamToken(
|
|
|
|
stream=recents[0].internal_metadata.stream_ordering - 1
|
|
|
|
)
|
2015-01-30 06:32:35 -05:00
|
|
|
|
2023-10-19 10:04:18 -04:00
|
|
|
prev_batch_token = upto_token.copy_and_replace(StreamKeyType.ROOM, room_key)
|
2015-01-30 06:32:35 -05:00
|
|
|
|
2022-01-13 10:45:28 -05:00
|
|
|
# Don't bother to bundle aggregations if the timeline is unlimited,
|
|
|
|
# as clients will have all the necessary information.
|
|
|
|
bundled_aggregations = None
|
|
|
|
if limited or newly_joined_room:
|
2022-03-18 13:49:32 -04:00
|
|
|
bundled_aggregations = (
|
|
|
|
await self._relations_handler.get_bundled_aggregations(
|
|
|
|
recents, sync_config.user.to_string()
|
|
|
|
)
|
2022-01-18 11:38:57 -05:00
|
|
|
)
|
2022-01-13 10:45:28 -05:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return TimelineBatch(
|
|
|
|
events=recents,
|
|
|
|
prev_batch=prev_batch_token,
|
2023-10-19 10:04:18 -04:00
|
|
|
# Also mark as limited if this is a new room or there has been a gap
|
|
|
|
# (to force client to paginate the gap).
|
|
|
|
limited=limited or newly_joined_room or gap_token is not None,
|
2022-01-13 10:45:28 -05:00
|
|
|
bundled_aggregations=bundled_aggregations,
|
2015-10-01 12:53:07 -04:00
|
|
|
)
|
2015-01-30 06:32:35 -05:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
async def compute_summary(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
batch: TimelineBatch,
|
2020-08-28 07:28:53 -04:00
|
|
|
state: MutableStateMap[EventBase],
|
2020-02-03 13:05:44 -05:00
|
|
|
now_token: StreamToken,
|
|
|
|
) -> Optional[JsonDict]:
|
2018-08-16 04:46:50 -04:00
|
|
|
"""Works out a room summary block for this room, summarising the number
|
|
|
|
of joined members in the room, and providing the 'hero' members if the
|
|
|
|
room has no name so clients can consistently name rooms. Also adds
|
|
|
|
state events to 'state' if needed to describe the heroes.
|
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
Args
|
|
|
|
room_id
|
|
|
|
sync_config
|
|
|
|
batch: The timeline batch for the room that will be sent to the user.
|
|
|
|
state: State as returned by compute_state_delta
|
|
|
|
now_token: Token of the end of the current batch.
|
2018-08-16 04:46:50 -04:00
|
|
|
"""
|
|
|
|
|
2018-09-11 19:50:39 -04:00
|
|
|
# FIXME: we could/should get this from room_stats when matthew/stats lands
|
|
|
|
|
2018-08-16 04:46:50 -04:00
|
|
|
# FIXME: this promulgates https://github.com/matrix-org/synapse/issues/3305
|
2019-12-05 12:58:25 -05:00
|
|
|
last_events, _ = await self.store.get_recent_event_ids_for_room(
|
2018-08-16 04:46:50 -04:00
|
|
|
room_id, end_token=now_token.room_key, limit=1
|
|
|
|
)
|
|
|
|
|
|
|
|
if not last_events:
|
2019-07-23 09:00:55 -04:00
|
|
|
return None
|
2018-08-16 04:46:50 -04:00
|
|
|
|
|
|
|
last_event = last_events[-1]
|
2022-05-31 08:17:50 -04:00
|
|
|
state_ids = await self._state_storage_controller.get_state_ids_for_event(
|
2018-10-25 12:49:55 -04:00
|
|
|
last_event.event_id,
|
|
|
|
state_filter=StateFilter.from_types(
|
2018-08-16 04:46:50 -04:00
|
|
|
[(EventTypes.Name, ""), (EventTypes.CanonicalAlias, "")]
|
2018-10-25 12:49:55 -04:00
|
|
|
),
|
2018-08-16 04:46:50 -04:00
|
|
|
)
|
|
|
|
|
2018-09-11 19:50:39 -04:00
|
|
|
# this is heavily cached, thus: fast.
|
2019-12-05 12:58:25 -05:00
|
|
|
details = await self.store.get_room_summary(room_id)
|
2018-09-11 19:50:39 -04:00
|
|
|
|
2018-08-16 04:46:50 -04:00
|
|
|
name_id = state_ids.get((EventTypes.Name, ""))
|
|
|
|
canonical_alias_id = state_ids.get((EventTypes.CanonicalAlias, ""))
|
|
|
|
|
2021-08-18 09:22:07 -04:00
|
|
|
summary: JsonDict = {}
|
2018-09-11 19:50:39 -04:00
|
|
|
empty_ms = MemberSummary([], 0)
|
2018-08-16 04:46:50 -04:00
|
|
|
|
|
|
|
# TODO: only send these when they change.
|
2018-09-11 19:50:39 -04:00
|
|
|
summary["m.joined_member_count"] = details.get(Membership.JOIN, empty_ms).count
|
|
|
|
summary["m.invited_member_count"] = details.get(
|
|
|
|
Membership.INVITE, empty_ms
|
|
|
|
).count
|
2018-08-16 04:46:50 -04:00
|
|
|
|
2018-09-12 12:11:05 -04:00
|
|
|
# if the room has a name or canonical_alias set, we can skip
|
2019-04-22 12:59:00 -04:00
|
|
|
# calculating heroes. Empty strings are falsey, so we check
|
|
|
|
# for the "name" value and default to an empty string.
|
2018-09-12 12:11:05 -04:00
|
|
|
if name_id:
|
2019-12-05 12:58:25 -05:00
|
|
|
name = await self.store.get_event(name_id, allow_none=True)
|
2019-06-06 05:34:12 -04:00
|
|
|
if name and name.content.get("name"):
|
2019-07-23 09:00:55 -04:00
|
|
|
return summary
|
2018-09-12 12:11:05 -04:00
|
|
|
|
|
|
|
if canonical_alias_id:
|
2019-12-05 12:58:25 -05:00
|
|
|
canonical_alias = await self.store.get_event(
|
2018-10-01 09:19:36 -04:00
|
|
|
canonical_alias_id, allow_none=True
|
2018-09-12 12:11:05 -04:00
|
|
|
)
|
2019-06-06 05:34:12 -04:00
|
|
|
if canonical_alias and canonical_alias.content.get("alias"):
|
2019-07-23 09:00:55 -04:00
|
|
|
return summary
|
2018-08-16 04:46:50 -04:00
|
|
|
|
2018-09-11 19:50:39 -04:00
|
|
|
# FIXME: only build up a member_ids list for our heroes
|
|
|
|
member_ids = {}
|
|
|
|
for membership in (
|
|
|
|
Membership.JOIN,
|
|
|
|
Membership.INVITE,
|
|
|
|
Membership.LEAVE,
|
|
|
|
Membership.BAN,
|
|
|
|
):
|
|
|
|
for user_id, event_id in details.get(membership, empty_ms).members:
|
|
|
|
member_ids[user_id] = event_id
|
2018-08-16 04:46:50 -04:00
|
|
|
|
2022-11-15 12:35:19 -05:00
|
|
|
me = sync_config.user.to_string()
|
|
|
|
summary["m.heroes"] = extract_heroes_from_room_summary(details, me)
|
2018-08-16 04:46:50 -04:00
|
|
|
|
|
|
|
if not sync_config.filter_collection.lazy_load_members():
|
2019-07-23 09:00:55 -04:00
|
|
|
return summary
|
2018-08-16 04:46:50 -04:00
|
|
|
|
|
|
|
# ensure we send membership events for heroes if needed
|
|
|
|
cache_key = (sync_config.user.to_string(), sync_config.device_id)
|
|
|
|
cache = self.get_lazy_loaded_members_cache(cache_key)
|
|
|
|
|
|
|
|
# track which members the client should already know about via LL:
|
|
|
|
# Ones which are already in state...
|
2020-02-21 07:15:07 -05:00
|
|
|
existing_members = {
|
2018-08-16 04:46:50 -04:00
|
|
|
user_id for (typ, user_id) in state.keys() if typ == EventTypes.Member
|
2020-02-21 07:15:07 -05:00
|
|
|
}
|
2018-08-16 04:46:50 -04:00
|
|
|
|
|
|
|
# ...or ones which are in the timeline...
|
|
|
|
for ev in batch.events:
|
|
|
|
if ev.type == EventTypes.Member:
|
|
|
|
existing_members.add(ev.state_key)
|
|
|
|
|
|
|
|
# ...and then ensure any missing ones get included in state.
|
|
|
|
missing_hero_event_ids = [
|
|
|
|
member_ids[hero_id]
|
|
|
|
for hero_id in summary["m.heroes"]
|
|
|
|
if (
|
|
|
|
cache.get(hero_id) != member_ids[hero_id]
|
|
|
|
and hero_id not in existing_members
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
missing_hero_state = await self.store.get_events(missing_hero_event_ids)
|
2018-08-16 04:46:50 -04:00
|
|
|
|
2020-09-03 07:54:10 -04:00
|
|
|
for s in missing_hero_state.values():
|
2018-08-16 04:46:50 -04:00
|
|
|
cache.set(s.state_key, s.event_id)
|
|
|
|
state[(EventTypes.Member, s.state_key)] = s
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return summary
|
2018-08-16 04:46:50 -04:00
|
|
|
|
2021-03-24 06:48:46 -04:00
|
|
|
def get_lazy_loaded_members_cache(
|
|
|
|
self, cache_key: Tuple[str, Optional[str]]
|
2021-04-06 08:58:18 -04:00
|
|
|
) -> LruCache[str, str]:
|
2021-07-16 13:22:36 -04:00
|
|
|
cache: Optional[LruCache[str, str]] = self.lazy_loaded_members_cache.get(
|
2021-04-06 08:58:18 -04:00
|
|
|
cache_key
|
2021-07-16 13:22:36 -04:00
|
|
|
)
|
2018-08-16 04:46:50 -04:00
|
|
|
if cache is None:
|
|
|
|
logger.debug("creating LruCache for %r", cache_key)
|
|
|
|
cache = LruCache(LAZY_LOADED_MEMBERS_CACHE_MAX_SIZE)
|
|
|
|
self.lazy_loaded_members_cache[cache_key] = cache
|
|
|
|
else:
|
|
|
|
logger.debug("found LruCache for %r", cache_key)
|
|
|
|
return cache
|
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
async def compute_state_delta(
|
2020-02-03 13:05:44 -05:00
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
batch: TimelineBatch,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
since_token: Optional[StreamToken],
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token: StreamToken,
|
2020-02-03 13:05:44 -05:00
|
|
|
full_state: bool,
|
2020-08-28 07:28:53 -04:00
|
|
|
) -> MutableStateMap[EventBase]:
|
2022-08-10 07:58:20 -04:00
|
|
|
"""Works out the difference in state between the end of the previous sync and
|
|
|
|
the start of the timeline.
|
2016-02-01 10:59:40 -05:00
|
|
|
|
2016-04-01 11:08:59 -04:00
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
room_id:
|
|
|
|
batch: The timeline batch for the room that will be sent to the user.
|
|
|
|
sync_config:
|
2022-08-10 07:58:20 -04:00
|
|
|
since_token: Token of the end of the previous batch. May be `None`.
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token: Token of the end of the current batch. Normally this will be
|
|
|
|
the same as the global "now_token", but if the user has left the room,
|
|
|
|
the point just after their leave event.
|
2020-02-03 13:05:44 -05:00
|
|
|
full_state: Whether to force returning the full state.
|
2022-08-10 07:58:20 -04:00
|
|
|
`lazy_load_members` still applies when `full_state` is `True`.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The state to return in the sync response for the room.
|
|
|
|
|
|
|
|
Clients will overlay this onto the state at the end of the previous sync to
|
|
|
|
arrive at the state at the start of the timeline.
|
|
|
|
|
|
|
|
Clients will then overlay state events in the timeline to arrive at the
|
|
|
|
state at the end of the timeline, in preparation for the next sync.
|
2015-01-27 11:24:22 -05:00
|
|
|
"""
|
|
|
|
# TODO(mjark) Check if the state events were received by the server
|
|
|
|
# after the previous sync, since we need to include those state
|
2020-10-23 12:38:40 -04:00
|
|
|
# updates even if they occurred logically before the previous event.
|
2015-01-27 11:24:22 -05:00
|
|
|
# TODO(mjark) Check for new redactions in the state events.
|
2015-11-12 11:34:42 -05:00
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
with Measure(self.clock, "compute_state_delta"):
|
2022-08-10 07:58:20 -04:00
|
|
|
# The memberships needed for events in the timeline.
|
|
|
|
# Only calculated when `lazy_load_members` is on.
|
2022-08-18 06:53:02 -04:00
|
|
|
members_to_fetch: Optional[Set[str]] = None
|
|
|
|
|
|
|
|
# A dictionary mapping user IDs to the first event in the timeline sent by
|
|
|
|
# them. Only calculated when `lazy_load_members` is on.
|
|
|
|
first_event_by_sender_map: Optional[Dict[str, EventBase]] = None
|
|
|
|
|
|
|
|
# The contribution to the room state from state events in the timeline.
|
|
|
|
# Only contains the last event for any given state key.
|
|
|
|
timeline_state: StateMap[str]
|
2018-03-13 18:03:42 -04:00
|
|
|
|
2018-07-26 17:51:30 -04:00
|
|
|
lazy_load_members = sync_config.filter_collection.lazy_load_members()
|
|
|
|
include_redundant_members = (
|
|
|
|
sync_config.filter_collection.include_redundant_members()
|
|
|
|
)
|
|
|
|
|
2018-03-18 21:15:13 -04:00
|
|
|
if lazy_load_members:
|
2018-03-11 16:01:41 -04:00
|
|
|
# We only request state for the members needed to display the
|
|
|
|
# timeline:
|
2018-03-15 21:37:53 -04:00
|
|
|
|
2022-08-18 06:53:02 -04:00
|
|
|
timeline_state = {}
|
|
|
|
|
Fix a rare bug where initial /syncs would fail (#15383)
This change fixes a rare bug where initial /syncs would fail with a
`KeyError` under the following circumstances:
1. A user fast joins a remote room.
2. The user is kicked from the room before the room's full state has
been synced.
3. A second local user fast joins the room.
4. Events are backfilled into the room with a higher topological
ordering than the original user's leave. They are assigned a
negative stream ordering. It's not clear how backfill happened here,
since it is expected to be equivalent to syncing the full state.
5. The second local user leaves the room before the room's full state
has been synced. The homeserver does not complete the sync.
6. The original user performs an initial /sync with lazy_load_members
enabled.
* Because they were kicked from the room, the room is included in
the /sync response even though the include_leave option is not
specified.
* To populate the room's timeline, `_load_filtered_recents` /
`get_recent_events_for_room` fetches events with a lower stream
ordering than the leave event and picks the ones with the highest
topological orderings (which are most recent). This captures the
backfilled events after the leave, since they have a negative
stream ordering. These events are filtered out of the timeline,
since the user was not in the room at the time and cannot view
them. The sync code ends up with an empty timeline for the room
that notably does not include the user's leave event.
This seems buggy, but at least we don't disclose events the user
isn't allowed to see.
* Normally, `compute_state_delta` would fetch the state at the
start and end of the room's timeline to generate the sync
response. Since the timeline is empty, it fetches the state at
`min(now, last event in the room)`, which corresponds with the
second user's leave. The state during the entirety of the second
user's membership does not include the membership for the first
user because of partial state.
This part is also questionable, since we are fetching state from
outside the bounds of the user's membership.
* `compute_state_delta` then tries and fails to find the user's
membership in the auth events of timeline events. Because there
is no timeline event whose auth events are expected to contain
the user's membership, a `KeyError` is raised.
Also contains a drive-by fix for a separate unlikely race condition.
Signed-off-by: Sean Quah <seanq@matrix.org>
2023-04-04 08:10:25 -04:00
|
|
|
# Membership events to fetch that can be found in the room state, or in
|
|
|
|
# the case of partial state rooms, the auth events of timeline events.
|
2022-08-18 06:53:02 -04:00
|
|
|
members_to_fetch = set()
|
|
|
|
first_event_by_sender_map = {}
|
|
|
|
for event in batch.events:
|
|
|
|
# Build the map from user IDs to the first timeline event they sent.
|
|
|
|
if event.sender not in first_event_by_sender_map:
|
|
|
|
first_event_by_sender_map[event.sender] = event
|
|
|
|
|
|
|
|
# We need the event's sender, unless their membership was in a
|
|
|
|
# previous timeline event.
|
|
|
|
if (EventTypes.Member, event.sender) not in timeline_state:
|
|
|
|
members_to_fetch.add(event.sender)
|
|
|
|
# FIXME: we also care about invite targets etc.
|
|
|
|
|
|
|
|
if event.is_state():
|
|
|
|
timeline_state[(event.type, event.state_key)] = event.event_id
|
2018-03-11 16:01:41 -04:00
|
|
|
|
2018-10-25 12:49:55 -04:00
|
|
|
else:
|
2022-08-18 06:53:02 -04:00
|
|
|
timeline_state = {
|
|
|
|
(event.type, event.state_key): event.event_id
|
|
|
|
for event in batch.events
|
|
|
|
if event.is_state()
|
|
|
|
}
|
2018-03-15 21:37:53 -04:00
|
|
|
|
2022-08-10 07:58:20 -04:00
|
|
|
# Now calculate the state to return in the sync response for the room.
|
|
|
|
# This is more or less the change in state between the end of the previous
|
|
|
|
# sync's timeline and the start of the current sync's timeline.
|
|
|
|
# See the docstring above for details.
|
|
|
|
state_ids: StateMap[str]
|
Fix a rare bug where initial /syncs would fail (#15383)
This change fixes a rare bug where initial /syncs would fail with a
`KeyError` under the following circumstances:
1. A user fast joins a remote room.
2. The user is kicked from the room before the room's full state has
been synced.
3. A second local user fast joins the room.
4. Events are backfilled into the room with a higher topological
ordering than the original user's leave. They are assigned a
negative stream ordering. It's not clear how backfill happened here,
since it is expected to be equivalent to syncing the full state.
5. The second local user leaves the room before the room's full state
has been synced. The homeserver does not complete the sync.
6. The original user performs an initial /sync with lazy_load_members
enabled.
* Because they were kicked from the room, the room is included in
the /sync response even though the include_leave option is not
specified.
* To populate the room's timeline, `_load_filtered_recents` /
`get_recent_events_for_room` fetches events with a lower stream
ordering than the leave event and picks the ones with the highest
topological orderings (which are most recent). This captures the
backfilled events after the leave, since they have a negative
stream ordering. These events are filtered out of the timeline,
since the user was not in the room at the time and cannot view
them. The sync code ends up with an empty timeline for the room
that notably does not include the user's leave event.
This seems buggy, but at least we don't disclose events the user
isn't allowed to see.
* Normally, `compute_state_delta` would fetch the state at the
start and end of the room's timeline to generate the sync
response. Since the timeline is empty, it fetches the state at
`min(now, last event in the room)`, which corresponds with the
second user's leave. The state during the entirety of the second
user's membership does not include the membership for the first
user because of partial state.
This part is also questionable, since we are fetching state from
outside the bounds of the user's membership.
* `compute_state_delta` then tries and fails to find the user's
membership in the auth events of timeline events. Because there
is no timeline event whose auth events are expected to contain
the user's membership, a `KeyError` is raised.
Also contains a drive-by fix for a separate unlikely race condition.
Signed-off-by: Sean Quah <seanq@matrix.org>
2023-04-04 08:10:25 -04:00
|
|
|
# We need to know whether the state we fetch may be partial, so check
|
|
|
|
# whether the room is partial stated *before* fetching it.
|
|
|
|
is_partial_state_room = await self.store.is_partial_state_room(room_id)
|
2016-02-09 06:31:04 -05:00
|
|
|
if full_state:
|
2024-03-14 13:18:48 -04:00
|
|
|
state_ids = await self._compute_state_delta_for_full_sync(
|
|
|
|
room_id,
|
|
|
|
sync_config.user,
|
|
|
|
batch,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token,
|
2024-03-14 13:18:48 -04:00
|
|
|
members_to_fetch,
|
|
|
|
timeline_state,
|
2016-02-09 06:31:04 -05:00
|
|
|
)
|
2024-03-14 13:18:48 -04:00
|
|
|
else:
|
2020-02-03 13:05:44 -05:00
|
|
|
# If this is an initial sync then full_state should be set, and
|
|
|
|
# that case is handled above. We assert here to ensure that this
|
|
|
|
# is indeed the case.
|
|
|
|
assert since_token is not None
|
2016-02-01 10:59:40 -05:00
|
|
|
|
2024-03-14 13:18:48 -04:00
|
|
|
state_ids = await self._compute_state_delta_for_incremental_sync(
|
|
|
|
room_id,
|
|
|
|
batch,
|
|
|
|
since_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token,
|
2024-03-14 13:18:48 -04:00
|
|
|
members_to_fetch,
|
|
|
|
timeline_state,
|
2016-02-09 06:31:04 -05:00
|
|
|
)
|
2016-08-25 13:59:44 -04:00
|
|
|
|
2022-08-18 06:53:02 -04:00
|
|
|
# If we only have partial state for the room, `state_ids` may be missing the
|
|
|
|
# memberships we wanted. We attempt to find some by digging through the auth
|
|
|
|
# events of timeline events.
|
Fix a rare bug where initial /syncs would fail (#15383)
This change fixes a rare bug where initial /syncs would fail with a
`KeyError` under the following circumstances:
1. A user fast joins a remote room.
2. The user is kicked from the room before the room's full state has
been synced.
3. A second local user fast joins the room.
4. Events are backfilled into the room with a higher topological
ordering than the original user's leave. They are assigned a
negative stream ordering. It's not clear how backfill happened here,
since it is expected to be equivalent to syncing the full state.
5. The second local user leaves the room before the room's full state
has been synced. The homeserver does not complete the sync.
6. The original user performs an initial /sync with lazy_load_members
enabled.
* Because they were kicked from the room, the room is included in
the /sync response even though the include_leave option is not
specified.
* To populate the room's timeline, `_load_filtered_recents` /
`get_recent_events_for_room` fetches events with a lower stream
ordering than the leave event and picks the ones with the highest
topological orderings (which are most recent). This captures the
backfilled events after the leave, since they have a negative
stream ordering. These events are filtered out of the timeline,
since the user was not in the room at the time and cannot view
them. The sync code ends up with an empty timeline for the room
that notably does not include the user's leave event.
This seems buggy, but at least we don't disclose events the user
isn't allowed to see.
* Normally, `compute_state_delta` would fetch the state at the
start and end of the room's timeline to generate the sync
response. Since the timeline is empty, it fetches the state at
`min(now, last event in the room)`, which corresponds with the
second user's leave. The state during the entirety of the second
user's membership does not include the membership for the first
user because of partial state.
This part is also questionable, since we are fetching state from
outside the bounds of the user's membership.
* `compute_state_delta` then tries and fails to find the user's
membership in the auth events of timeline events. Because there
is no timeline event whose auth events are expected to contain
the user's membership, a `KeyError` is raised.
Also contains a drive-by fix for a separate unlikely race condition.
Signed-off-by: Sean Quah <seanq@matrix.org>
2023-04-04 08:10:25 -04:00
|
|
|
if lazy_load_members and is_partial_state_room:
|
2022-08-18 06:53:02 -04:00
|
|
|
assert members_to_fetch is not None
|
|
|
|
assert first_event_by_sender_map is not None
|
|
|
|
|
|
|
|
additional_state_ids = (
|
|
|
|
await self._find_missing_partial_state_memberships(
|
|
|
|
room_id, members_to_fetch, first_event_by_sender_map, state_ids
|
|
|
|
)
|
|
|
|
)
|
|
|
|
state_ids = {**state_ids, **additional_state_ids}
|
|
|
|
|
2022-08-10 07:58:20 -04:00
|
|
|
# At this point, if `lazy_load_members` is enabled, `state_ids` includes
|
|
|
|
# the memberships of all event senders in the timeline. This is because we
|
|
|
|
# may not have sent the memberships in a previous sync.
|
|
|
|
|
|
|
|
# When `include_redundant_members` is on, we send all the lazy-loaded
|
|
|
|
# memberships of event senders. Otherwise we make an effort to limit the set
|
|
|
|
# of memberships we send to those that we have not already sent to this client.
|
2018-07-26 17:51:30 -04:00
|
|
|
if lazy_load_members and not include_redundant_members:
|
|
|
|
cache_key = (sync_config.user.to_string(), sync_config.device_id)
|
2018-08-16 04:46:50 -04:00
|
|
|
cache = self.get_lazy_loaded_members_cache(cache_key)
|
2018-07-26 17:51:30 -04:00
|
|
|
|
|
|
|
# if it's a new sync sequence, then assume the client has had
|
|
|
|
# amnesia and doesn't want any recent lazy-loaded members
|
|
|
|
# de-duplicated.
|
|
|
|
if since_token is None:
|
|
|
|
logger.debug("clearing LruCache for %r", cache_key)
|
|
|
|
cache.clear()
|
|
|
|
else:
|
|
|
|
# only send members which aren't in our LruCache (either
|
|
|
|
# because they're new to this client or have been pushed out
|
|
|
|
# of the cache)
|
|
|
|
logger.debug("filtering state from %r...", state_ids)
|
|
|
|
state_ids = {
|
|
|
|
t: event_id
|
2020-06-15 07:03:36 -04:00
|
|
|
for t, event_id in state_ids.items()
|
2018-07-26 17:51:30 -04:00
|
|
|
if cache.get(t[1]) != event_id
|
|
|
|
}
|
|
|
|
logger.debug("...to %r", state_ids)
|
|
|
|
|
|
|
|
# add any member IDs we are about to send into our LruCache
|
|
|
|
for t, event_id in itertools.chain(
|
|
|
|
state_ids.items(), timeline_state.items()
|
|
|
|
):
|
|
|
|
if t[0] == EventTypes.Member:
|
|
|
|
cache.set(t[1], event_id)
|
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
state: Dict[str, EventBase] = {}
|
2016-08-25 13:59:44 -04:00
|
|
|
if state_ids:
|
2019-12-05 12:58:25 -05:00
|
|
|
state = await self.store.get_events(list(state_ids.values()))
|
2016-02-01 10:59:40 -05:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return {
|
|
|
|
(e.type, e.state_key): e
|
2021-11-09 08:10:58 -05:00
|
|
|
for e in await sync_config.filter_collection.filter_room_state(
|
2019-07-23 09:00:55 -04:00
|
|
|
list(state.values())
|
|
|
|
)
|
2020-02-10 13:07:35 -05:00
|
|
|
if e.type != EventTypes.Aliases # until MSC2261 or alternative solution
|
2019-07-23 09:00:55 -04:00
|
|
|
}
|
2015-01-30 08:33:41 -05:00
|
|
|
|
2024-03-14 13:18:48 -04:00
|
|
|
async def _compute_state_delta_for_full_sync(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
syncing_user: UserID,
|
|
|
|
batch: TimelineBatch,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token: StreamToken,
|
2024-03-14 13:18:48 -04:00
|
|
|
members_to_fetch: Optional[Set[str]],
|
|
|
|
timeline_state: StateMap[str],
|
|
|
|
) -> StateMap[str]:
|
|
|
|
"""Calculate the state events to be included in a full sync response.
|
|
|
|
|
|
|
|
As with `_compute_state_delta_for_incremental_sync`, the result will include
|
|
|
|
the membership events for the senders of each event in `members_to_fetch`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_id: The room we are calculating for.
|
|
|
|
syncing_user: The user that is calling `/sync`.
|
|
|
|
batch: The timeline batch for the room that will be sent to the user.
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token: Token of the end of the current batch. Normally this will be
|
|
|
|
the same as the global "now_token", but if the user has left the room,
|
|
|
|
the point just after their leave event.
|
2024-03-14 13:18:48 -04:00
|
|
|
members_to_fetch: If lazy-loading is enabled, the memberships needed for
|
|
|
|
events in the timeline.
|
|
|
|
timeline_state: The contribution to the room state from state events in
|
|
|
|
`batch`. Only contains the last event for any given state key.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A map from (type, state_key) to event_id, for each event that we believe
|
|
|
|
should be included in the `state` part of the sync response.
|
|
|
|
"""
|
|
|
|
if members_to_fetch is not None:
|
|
|
|
# Lazy-loading of membership events is enabled.
|
|
|
|
#
|
|
|
|
# Always make sure we load our own membership event so we know if
|
|
|
|
# we're in the room, to fix https://github.com/vector-im/riot-web/issues/7209.
|
|
|
|
#
|
|
|
|
# We only need apply this on full state syncs given we disabled
|
|
|
|
# LL for incr syncs in https://github.com/matrix-org/synapse/pull/3840.
|
|
|
|
#
|
|
|
|
# We don't insert ourselves into `members_to_fetch`, because in some
|
|
|
|
# rare cases (an empty event batch with a now_token after the user's
|
|
|
|
# leave in a partial state room which another local user has
|
|
|
|
# joined), the room state will be missing our membership and there
|
|
|
|
# is no guarantee that our membership will be in the auth events of
|
|
|
|
# timeline events when the room is partial stated.
|
|
|
|
state_filter = StateFilter.from_lazy_load_member_list(
|
|
|
|
members_to_fetch.union((syncing_user.to_string(),))
|
|
|
|
)
|
|
|
|
|
|
|
|
# We are happy to use partial state to compute the `/sync` response.
|
|
|
|
# Since partial state may not include the lazy-loaded memberships we
|
|
|
|
# require, we fix up the state response afterwards with memberships from
|
|
|
|
# auth events.
|
|
|
|
await_full_state = False
|
|
|
|
lazy_load_members = True
|
|
|
|
else:
|
|
|
|
state_filter = StateFilter.all()
|
|
|
|
await_full_state = True
|
|
|
|
lazy_load_members = False
|
|
|
|
|
2024-07-04 13:25:36 -04:00
|
|
|
state_at_timeline_end = await self._state_storage_controller.get_state_ids_at(
|
2024-04-04 08:14:24 -04:00
|
|
|
room_id,
|
|
|
|
stream_position=end_token,
|
|
|
|
state_filter=state_filter,
|
|
|
|
await_full_state=await_full_state,
|
|
|
|
)
|
2024-03-14 13:18:48 -04:00
|
|
|
|
2024-04-04 08:14:24 -04:00
|
|
|
if batch:
|
2024-04-04 12:15:35 -04:00
|
|
|
# Strictly speaking, this returns the state *after* the first event in the
|
|
|
|
# timeline, but that is good enough here.
|
2024-03-14 13:18:48 -04:00
|
|
|
state_at_timeline_start = (
|
|
|
|
await self._state_storage_controller.get_state_ids_for_event(
|
|
|
|
batch.events[0].event_id,
|
|
|
|
state_filter=state_filter,
|
|
|
|
await_full_state=await_full_state,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
state_at_timeline_start = state_at_timeline_end
|
|
|
|
|
|
|
|
state_ids = _calculate_state(
|
|
|
|
timeline_contains=timeline_state,
|
|
|
|
timeline_start=state_at_timeline_start,
|
|
|
|
timeline_end=state_at_timeline_end,
|
|
|
|
previous_timeline_end={},
|
|
|
|
lazy_load_members=lazy_load_members,
|
|
|
|
)
|
|
|
|
return state_ids
|
|
|
|
|
|
|
|
async def _compute_state_delta_for_incremental_sync(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
batch: TimelineBatch,
|
|
|
|
since_token: StreamToken,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token: StreamToken,
|
2024-03-14 13:18:48 -04:00
|
|
|
members_to_fetch: Optional[Set[str]],
|
|
|
|
timeline_state: StateMap[str],
|
|
|
|
) -> StateMap[str]:
|
|
|
|
"""Calculate the state events to be included in an incremental sync response.
|
|
|
|
|
|
|
|
If lazy-loading of membership events is enabled (as indicated by
|
|
|
|
`members_to_fetch` being not-`None`), the result will include the membership
|
|
|
|
events for each member in `members_to_fetch`. The caller
|
|
|
|
(`compute_state_delta`) is responsible for keeping track of which membership
|
|
|
|
events we have already sent to the client, and hence ripping them out.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_id: The room we are calculating for.
|
|
|
|
batch: The timeline batch for the room that will be sent to the user.
|
|
|
|
since_token: Token of the end of the previous batch.
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token: Token of the end of the current batch. Normally this will be
|
|
|
|
the same as the global "now_token", but if the user has left the room,
|
|
|
|
the point just after their leave event.
|
2024-03-14 13:18:48 -04:00
|
|
|
members_to_fetch: If lazy-loading is enabled, the memberships needed for
|
|
|
|
events in the timeline. Otherwise, `None`.
|
|
|
|
timeline_state: The contribution to the room state from state events in
|
|
|
|
`batch`. Only contains the last event for any given state key.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A map from (type, state_key) to event_id, for each event that we believe
|
|
|
|
should be included in the `state` part of the sync response.
|
|
|
|
"""
|
|
|
|
if members_to_fetch is not None:
|
|
|
|
# Lazy-loading is enabled. Only return the state that is needed.
|
|
|
|
state_filter = StateFilter.from_lazy_load_member_list(members_to_fetch)
|
|
|
|
await_full_state = False
|
|
|
|
lazy_load_members = True
|
|
|
|
else:
|
|
|
|
state_filter = StateFilter.all()
|
|
|
|
await_full_state = True
|
|
|
|
lazy_load_members = False
|
|
|
|
|
2024-04-08 09:25:28 -04:00
|
|
|
# For a non-gappy sync if the events in the timeline are simply a linear
|
|
|
|
# chain (i.e. no merging/branching of the graph), then we know the state
|
|
|
|
# delta between the end of the previous sync and start of the new one is
|
|
|
|
# empty.
|
|
|
|
#
|
|
|
|
# c.f. #16941 for an example of why we can't do this for all non-gappy
|
|
|
|
# syncs.
|
2024-04-09 10:01:12 -04:00
|
|
|
is_linear_timeline = True
|
2024-04-08 09:25:28 -04:00
|
|
|
if batch.events:
|
2024-04-09 10:01:12 -04:00
|
|
|
# We need to make sure the first event in our batch points to the
|
|
|
|
# last event in the previous batch.
|
|
|
|
last_event_id_prev_batch = (
|
2024-06-17 12:27:14 -04:00
|
|
|
await self.store.get_last_event_id_in_room_before_stream_ordering(
|
2024-04-09 10:01:12 -04:00
|
|
|
room_id,
|
|
|
|
end_token=since_token.room_key,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
prev_event_id = last_event_id_prev_batch
|
|
|
|
for e in batch.events:
|
2024-04-08 09:25:28 -04:00
|
|
|
if e.prev_event_ids() != [prev_event_id]:
|
2024-04-09 10:01:12 -04:00
|
|
|
is_linear_timeline = False
|
2024-04-08 09:25:28 -04:00
|
|
|
break
|
2024-04-08 09:55:19 -04:00
|
|
|
prev_event_id = e.event_id
|
2024-04-08 09:25:28 -04:00
|
|
|
|
|
|
|
if is_linear_timeline and not batch.limited:
|
|
|
|
state_ids: StateMap[str] = {}
|
|
|
|
if lazy_load_members:
|
|
|
|
if members_to_fetch and batch.events:
|
|
|
|
# We're lazy-loading, so the client might need some more
|
|
|
|
# member events to understand the events in this timeline.
|
|
|
|
# So we fish out all the member events corresponding to the
|
|
|
|
# timeline here. The caller will then dedupe any redundant
|
|
|
|
# ones.
|
|
|
|
|
2024-09-02 07:39:04 -04:00
|
|
|
state_ids = (
|
|
|
|
await self._state_storage_controller.get_state_ids_for_event(
|
|
|
|
batch.events[0].event_id,
|
|
|
|
# we only want members!
|
|
|
|
state_filter=StateFilter.from_types(
|
|
|
|
(EventTypes.Member, member)
|
|
|
|
for member in members_to_fetch
|
|
|
|
),
|
|
|
|
await_full_state=False,
|
|
|
|
)
|
2024-04-08 09:25:28 -04:00
|
|
|
)
|
|
|
|
return state_ids
|
|
|
|
|
2024-04-04 12:15:35 -04:00
|
|
|
if batch:
|
|
|
|
state_at_timeline_start = (
|
|
|
|
await self._state_storage_controller.get_state_ids_for_event(
|
|
|
|
batch.events[0].event_id,
|
2024-03-14 13:18:48 -04:00
|
|
|
state_filter=state_filter,
|
|
|
|
await_full_state=await_full_state,
|
|
|
|
)
|
2024-04-04 12:15:35 -04:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
# We can get here if the user has ignored the senders of all
|
|
|
|
# the recent events.
|
2024-07-04 13:25:36 -04:00
|
|
|
state_at_timeline_start = (
|
|
|
|
await self._state_storage_controller.get_state_ids_at(
|
|
|
|
room_id,
|
|
|
|
stream_position=end_token,
|
|
|
|
state_filter=state_filter,
|
|
|
|
await_full_state=await_full_state,
|
|
|
|
)
|
2024-04-04 12:15:35 -04:00
|
|
|
)
|
2024-03-14 13:18:48 -04:00
|
|
|
|
2024-04-04 12:15:35 -04:00
|
|
|
if batch.limited:
|
2024-03-14 13:18:48 -04:00
|
|
|
# for now, we disable LL for gappy syncs - see
|
|
|
|
# https://github.com/vector-im/riot-web/issues/7211#issuecomment-419976346
|
|
|
|
# N.B. this slows down incr syncs as we are now processing way
|
|
|
|
# more state in the server than if we were LLing.
|
|
|
|
#
|
|
|
|
# We still have to filter timeline_start to LL entries (above) in order
|
|
|
|
# for _calculate_state's LL logic to work, as we have to include LL
|
|
|
|
# members for timeline senders in case they weren't loaded in the initial
|
|
|
|
# sync. We do this by (counterintuitively) by filtering timeline_start
|
|
|
|
# members to just be ones which were timeline senders, which then ensures
|
|
|
|
# all of the rest get included in the state block (if we need to know
|
|
|
|
# about them).
|
|
|
|
state_filter = StateFilter.all()
|
|
|
|
|
2024-07-04 13:25:36 -04:00
|
|
|
state_at_previous_sync = await self._state_storage_controller.get_state_ids_at(
|
2024-04-04 12:15:35 -04:00
|
|
|
room_id,
|
|
|
|
stream_position=since_token,
|
|
|
|
state_filter=state_filter,
|
|
|
|
await_full_state=await_full_state,
|
|
|
|
)
|
2024-03-14 13:18:48 -04:00
|
|
|
|
2024-07-04 13:25:36 -04:00
|
|
|
state_at_timeline_end = await self._state_storage_controller.get_state_ids_at(
|
2024-04-04 12:15:35 -04:00
|
|
|
room_id,
|
|
|
|
stream_position=end_token,
|
|
|
|
state_filter=state_filter,
|
|
|
|
await_full_state=await_full_state,
|
|
|
|
)
|
|
|
|
|
|
|
|
state_ids = _calculate_state(
|
|
|
|
timeline_contains=timeline_state,
|
|
|
|
timeline_start=state_at_timeline_start,
|
|
|
|
timeline_end=state_at_timeline_end,
|
|
|
|
previous_timeline_end=state_at_previous_sync,
|
|
|
|
lazy_load_members=lazy_load_members,
|
|
|
|
)
|
2024-03-14 13:18:48 -04:00
|
|
|
|
|
|
|
return state_ids
|
|
|
|
|
2022-08-18 06:53:02 -04:00
|
|
|
async def _find_missing_partial_state_memberships(
|
|
|
|
self,
|
|
|
|
room_id: str,
|
2023-01-26 12:31:58 -05:00
|
|
|
members_to_fetch: StrCollection,
|
2022-08-18 06:53:02 -04:00
|
|
|
events_with_membership_auth: Mapping[str, EventBase],
|
|
|
|
found_state_ids: StateMap[str],
|
|
|
|
) -> StateMap[str]:
|
|
|
|
"""Finds missing memberships from a set of auth events and returns them as a
|
|
|
|
state map.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
room_id: The partial state room to find the remaining memberships for.
|
|
|
|
members_to_fetch: The memberships to find.
|
|
|
|
events_with_membership_auth: A mapping from user IDs to events whose auth
|
2022-09-27 06:17:23 -04:00
|
|
|
events would contain their prior membership, if one exists.
|
|
|
|
Note that join events will not cite a prior membership if a user has
|
|
|
|
never been in a room before.
|
2022-08-18 06:53:02 -04:00
|
|
|
found_state_ids: A dict from (type, state_key) -> state_event_id, containing
|
|
|
|
memberships that have been previously found. Entries in
|
|
|
|
`members_to_fetch` that have a membership in `found_state_ids` are
|
|
|
|
ignored.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dict from ("m.room.member", state_key) -> state_event_id, containing the
|
|
|
|
memberships missing from `found_state_ids`.
|
|
|
|
|
2022-09-27 06:17:23 -04:00
|
|
|
When `events_with_membership_auth` contains a join event for a given user
|
|
|
|
which does not cite a prior membership, no membership is returned for that
|
|
|
|
user.
|
|
|
|
|
2022-08-18 06:53:02 -04:00
|
|
|
Raises:
|
|
|
|
KeyError: if `events_with_membership_auth` does not have an entry for a
|
|
|
|
missing membership. Memberships in `found_state_ids` do not need an
|
|
|
|
entry in `events_with_membership_auth`.
|
|
|
|
"""
|
|
|
|
additional_state_ids: MutableStateMap[str] = {}
|
|
|
|
|
|
|
|
# Tracks the missing members for logging purposes.
|
|
|
|
missing_members = set()
|
|
|
|
|
|
|
|
# Identify memberships missing from `found_state_ids` and pick out the auth
|
|
|
|
# events in which to look for them.
|
|
|
|
auth_event_ids: Set[str] = set()
|
|
|
|
for member in members_to_fetch:
|
|
|
|
if (EventTypes.Member, member) in found_state_ids:
|
|
|
|
continue
|
|
|
|
|
|
|
|
event_with_membership_auth = events_with_membership_auth[member]
|
2023-03-09 09:18:39 -05:00
|
|
|
is_create = (
|
|
|
|
event_with_membership_auth.is_state()
|
|
|
|
and event_with_membership_auth.type == EventTypes.Create
|
|
|
|
)
|
2022-09-27 06:17:23 -04:00
|
|
|
is_join = (
|
|
|
|
event_with_membership_auth.is_state()
|
|
|
|
and event_with_membership_auth.type == EventTypes.Member
|
|
|
|
and event_with_membership_auth.state_key == member
|
|
|
|
and event_with_membership_auth.content.get("membership")
|
|
|
|
== Membership.JOIN
|
|
|
|
)
|
2023-03-09 09:18:39 -05:00
|
|
|
if not is_create and not is_join:
|
2022-09-27 06:17:23 -04:00
|
|
|
# The event must include the desired membership as an auth event, unless
|
2023-03-09 09:18:39 -05:00
|
|
|
# it's the `m.room.create` event for a room or the first join event for
|
|
|
|
# a given user.
|
2022-09-27 06:17:23 -04:00
|
|
|
missing_members.add(member)
|
2022-08-18 06:53:02 -04:00
|
|
|
auth_event_ids.update(event_with_membership_auth.auth_event_ids())
|
|
|
|
|
|
|
|
auth_events = await self.store.get_events(auth_event_ids)
|
|
|
|
|
|
|
|
# Run through the missing memberships once more, picking out the memberships
|
|
|
|
# from the pile of auth events we have just fetched.
|
|
|
|
for member in members_to_fetch:
|
|
|
|
if (EventTypes.Member, member) in found_state_ids:
|
|
|
|
continue
|
|
|
|
|
|
|
|
event_with_membership_auth = events_with_membership_auth[member]
|
|
|
|
|
|
|
|
# Dig through the auth events to find the desired membership.
|
|
|
|
for auth_event_id in event_with_membership_auth.auth_event_ids():
|
|
|
|
# We only store events once we have all their auth events,
|
|
|
|
# so the auth event must be in the pile we have just
|
|
|
|
# fetched.
|
|
|
|
auth_event = auth_events[auth_event_id]
|
|
|
|
|
|
|
|
if (
|
|
|
|
auth_event.type == EventTypes.Member
|
|
|
|
and auth_event.state_key == member
|
|
|
|
):
|
2022-09-27 06:17:23 -04:00
|
|
|
missing_members.discard(member)
|
2024-03-13 12:46:44 -04:00
|
|
|
additional_state_ids[(EventTypes.Member, member)] = (
|
|
|
|
auth_event.event_id
|
|
|
|
)
|
2022-08-18 06:53:02 -04:00
|
|
|
break
|
|
|
|
|
|
|
|
if missing_members:
|
|
|
|
# There really shouldn't be any missing memberships now. Either:
|
|
|
|
# * we couldn't find an auth event, which shouldn't happen because we do
|
|
|
|
# not persist events with persisting their auth events first, or
|
|
|
|
# * the set of auth events did not contain a membership we wanted, which
|
|
|
|
# means our caller didn't compute the events in `members_to_fetch`
|
|
|
|
# correctly, or we somehow accepted an event whose auth events were
|
|
|
|
# dodgy.
|
|
|
|
logger.error(
|
|
|
|
"Failed to find memberships for %s in partial state room "
|
|
|
|
"%s in the auth events of %s.",
|
|
|
|
missing_members,
|
|
|
|
room_id,
|
|
|
|
[
|
|
|
|
events_with_membership_auth[member].event_id
|
|
|
|
for member in missing_members
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
return additional_state_ids
|
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
async def unread_notifs_for_room_id(
|
|
|
|
self, room_id: str, sync_config: SyncConfig
|
2022-10-04 09:47:04 -04:00
|
|
|
) -> RoomNotifCounts:
|
2023-02-14 06:10:29 -05:00
|
|
|
if not self.should_calculate_push_rules:
|
|
|
|
# If push rules have been universally disabled then we know we won't
|
|
|
|
# have any unread counts in the DB, so we may as well skip asking
|
|
|
|
# the DB.
|
|
|
|
return RoomNotifCounts.empty()
|
|
|
|
|
2016-02-09 06:31:04 -05:00
|
|
|
with Measure(self.clock, "unread_notifs_for_room_id"):
|
2021-12-21 08:25:34 -05:00
|
|
|
return await self.store.get_unread_event_push_actions_by_room_for_user(
|
2022-06-15 11:17:14 -04:00
|
|
|
room_id,
|
|
|
|
sync_config.user.to_string(),
|
2020-09-02 12:19:37 -04:00
|
|
|
)
|
2016-01-19 06:35:50 -05:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
async def generate_sync_result(
|
2020-02-03 13:05:44 -05:00
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> SyncResult:
|
2021-12-02 15:58:32 -05:00
|
|
|
"""Generates the response body of a sync result.
|
|
|
|
|
|
|
|
This is represented by a `SyncResult` struct, which is built from small pieces
|
|
|
|
using a `SyncResultBuilder`. See also
|
|
|
|
https://spec.matrix.org/v1.1/client-server-api/#get_matrixclientv3sync
|
|
|
|
the `sync_result_builder` is passed as a mutable ("inout") parameter to various
|
|
|
|
helper functions. These retrieve and process the data which forms the sync body,
|
|
|
|
often writing to the `sync_result_builder` to store their output.
|
|
|
|
|
|
|
|
At the end, we transfer data from the `sync_result_builder` to a new `SyncResult`
|
|
|
|
instance to signify that the sync calculation is complete.
|
|
|
|
"""
|
2022-10-04 11:42:59 -04:00
|
|
|
|
|
|
|
user_id = sync_config.user.to_string()
|
|
|
|
app_service = self.store.get_app_service_by_user_id(user_id)
|
|
|
|
if app_service:
|
|
|
|
# We no longer support AS users using /sync directly.
|
|
|
|
# See https://github.com/matrix-org/matrix-doc/issues/1144
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2024-05-16 14:05:31 -04:00
|
|
|
sync_result_builder = await self.get_sync_result_builder(
|
|
|
|
sync_config,
|
|
|
|
since_token,
|
|
|
|
full_state,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
"Calculating sync response for %r between %s and %s",
|
|
|
|
sync_config.user,
|
|
|
|
sync_result_builder.since_token,
|
|
|
|
sync_result_builder.now_token,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("Fetching account data")
|
|
|
|
|
|
|
|
# Global account data is included if it is not filtered out.
|
|
|
|
if not sync_config.filter_collection.blocks_all_global_account_data():
|
|
|
|
await self._generate_sync_entry_for_account_data(sync_result_builder)
|
|
|
|
|
|
|
|
# Presence data is included if the server has it enabled and not filtered out.
|
|
|
|
include_presence_data = bool(
|
|
|
|
self.hs_config.server.presence_enabled
|
|
|
|
and not sync_config.filter_collection.blocks_all_presence()
|
|
|
|
)
|
|
|
|
# Device list updates are sent if a since token is provided.
|
|
|
|
include_device_list_updates = bool(since_token and since_token.device_list_key)
|
|
|
|
|
|
|
|
# If we do not care about the rooms or things which depend on the room
|
|
|
|
# data (namely presence and device list updates), then we can skip
|
|
|
|
# this process completely.
|
|
|
|
device_lists = DeviceListUpdates()
|
|
|
|
if (
|
|
|
|
not sync_result_builder.sync_config.filter_collection.blocks_all_rooms()
|
|
|
|
or include_presence_data
|
|
|
|
or include_device_list_updates
|
|
|
|
):
|
|
|
|
logger.debug("Fetching room data")
|
|
|
|
|
|
|
|
# Note that _generate_sync_entry_for_rooms sets sync_result_builder.joined, which
|
|
|
|
# is used in calculate_user_changes below.
|
|
|
|
(
|
|
|
|
newly_joined_rooms,
|
|
|
|
newly_left_rooms,
|
|
|
|
) = await self._generate_sync_entry_for_rooms(sync_result_builder)
|
|
|
|
|
|
|
|
# Work out which users have joined or left rooms we're in. We use this
|
|
|
|
# to build the presence and device_list parts of the sync response in
|
|
|
|
# `_generate_sync_entry_for_presence` and
|
|
|
|
# `_generate_sync_entry_for_device_list` respectively.
|
|
|
|
if include_presence_data or include_device_list_updates:
|
|
|
|
# This uses the sync_result_builder.joined which is set in
|
|
|
|
# `_generate_sync_entry_for_rooms`, if that didn't find any joined
|
|
|
|
# rooms for some reason it is a no-op.
|
|
|
|
(
|
|
|
|
newly_joined_or_invited_or_knocked_users,
|
|
|
|
newly_left_users,
|
|
|
|
) = sync_result_builder.calculate_user_changes()
|
|
|
|
|
|
|
|
if include_presence_data:
|
|
|
|
logger.debug("Fetching presence data")
|
|
|
|
await self._generate_sync_entry_for_presence(
|
|
|
|
sync_result_builder,
|
|
|
|
newly_joined_rooms,
|
|
|
|
newly_joined_or_invited_or_knocked_users,
|
|
|
|
)
|
|
|
|
|
|
|
|
if include_device_list_updates:
|
2024-08-09 06:59:44 -04:00
|
|
|
# include_device_list_updates can only be True if we have a
|
|
|
|
# since token.
|
|
|
|
assert since_token is not None
|
|
|
|
|
|
|
|
device_lists = await self._device_handler.generate_sync_entry_for_device_list(
|
|
|
|
user_id=user_id,
|
|
|
|
since_token=since_token,
|
|
|
|
now_token=sync_result_builder.now_token,
|
|
|
|
joined_room_ids=sync_result_builder.joined_room_ids,
|
2024-05-16 14:05:31 -04:00
|
|
|
newly_joined_rooms=newly_joined_rooms,
|
|
|
|
newly_joined_or_invited_or_knocked_users=newly_joined_or_invited_or_knocked_users,
|
|
|
|
newly_left_rooms=newly_left_rooms,
|
|
|
|
newly_left_users=newly_left_users,
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("Fetching to-device data")
|
|
|
|
await self._generate_sync_entry_for_to_device(sync_result_builder)
|
|
|
|
|
|
|
|
logger.debug("Fetching OTK data")
|
|
|
|
device_id = sync_config.device_id
|
|
|
|
one_time_keys_count: JsonMapping = {}
|
|
|
|
unused_fallback_key_types: List[str] = []
|
|
|
|
if device_id:
|
|
|
|
# TODO: We should have a way to let clients differentiate between the states of:
|
|
|
|
# * no change in OTK count since the provided since token
|
|
|
|
# * the server has zero OTKs left for this device
|
|
|
|
# Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
|
|
|
|
one_time_keys_count = await self.store.count_e2e_one_time_keys(
|
|
|
|
user_id, device_id
|
|
|
|
)
|
|
|
|
unused_fallback_key_types = list(
|
|
|
|
await self.store.get_e2e_unused_fallback_key_types(user_id, device_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
num_events = 0
|
|
|
|
|
|
|
|
# debug for https://github.com/matrix-org/synapse/issues/9424
|
|
|
|
for joined_room in sync_result_builder.joined:
|
|
|
|
num_events += len(joined_room.timeline.events)
|
|
|
|
|
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"joined_rooms_in_result": len(sync_result_builder.joined),
|
|
|
|
"events_in_result": num_events,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
logger.debug("Sync response calculation complete")
|
|
|
|
return SyncResult(
|
|
|
|
presence=sync_result_builder.presence,
|
|
|
|
account_data=sync_result_builder.account_data,
|
|
|
|
joined=sync_result_builder.joined,
|
|
|
|
invited=sync_result_builder.invited,
|
|
|
|
knocked=sync_result_builder.knocked,
|
|
|
|
archived=sync_result_builder.archived,
|
|
|
|
to_device=sync_result_builder.to_device,
|
|
|
|
device_lists=device_lists,
|
|
|
|
device_one_time_keys_count=one_time_keys_count,
|
|
|
|
device_unused_fallback_key_types=unused_fallback_key_types,
|
|
|
|
next_batch=sync_result_builder.now_token,
|
|
|
|
)
|
|
|
|
|
2024-05-23 13:06:16 -04:00
|
|
|
async def generate_e2ee_sync_result(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
) -> E2eeSyncResult:
|
|
|
|
"""
|
|
|
|
Generates the response body of a MSC3575 Sliding Sync `/sync/e2ee` result.
|
|
|
|
|
|
|
|
This is represented by a `E2eeSyncResult` struct, which is built from small
|
|
|
|
pieces using a `SyncResultBuilder`. The `sync_result_builder` is passed as a
|
|
|
|
mutable ("inout") parameter to various helper functions. These retrieve and
|
|
|
|
process the data which forms the sync body, often writing to the
|
|
|
|
`sync_result_builder` to store their output.
|
|
|
|
|
|
|
|
At the end, we transfer data from the `sync_result_builder` to a new `E2eeSyncResult`
|
|
|
|
instance to signify that the sync calculation is complete.
|
|
|
|
"""
|
|
|
|
user_id = sync_config.user.to_string()
|
|
|
|
app_service = self.store.get_app_service_by_user_id(user_id)
|
|
|
|
if app_service:
|
|
|
|
# We no longer support AS users using /sync directly.
|
|
|
|
# See https://github.com/matrix-org/matrix-doc/issues/1144
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
sync_result_builder = await self.get_sync_result_builder(
|
|
|
|
sync_config,
|
|
|
|
since_token,
|
|
|
|
full_state=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
# 1. Calculate `to_device` events
|
|
|
|
await self._generate_sync_entry_for_to_device(sync_result_builder)
|
|
|
|
|
|
|
|
# 2. Calculate `device_lists`
|
|
|
|
# Device list updates are sent if a since token is provided.
|
|
|
|
device_lists = DeviceListUpdates()
|
|
|
|
include_device_list_updates = bool(since_token and since_token.device_list_key)
|
|
|
|
if include_device_list_updates:
|
|
|
|
# Note that _generate_sync_entry_for_rooms sets sync_result_builder.joined, which
|
|
|
|
# is used in calculate_user_changes below.
|
|
|
|
#
|
|
|
|
# TODO: Running `_generate_sync_entry_for_rooms()` is a lot of work just to
|
|
|
|
# figure out the membership changes/derived info needed for
|
|
|
|
# `_generate_sync_entry_for_device_list()`. In the future, we should try to
|
|
|
|
# refactor this away.
|
|
|
|
(
|
|
|
|
newly_joined_rooms,
|
|
|
|
newly_left_rooms,
|
|
|
|
) = await self._generate_sync_entry_for_rooms(sync_result_builder)
|
|
|
|
|
|
|
|
# This uses the sync_result_builder.joined which is set in
|
|
|
|
# `_generate_sync_entry_for_rooms`, if that didn't find any joined
|
|
|
|
# rooms for some reason it is a no-op.
|
|
|
|
(
|
|
|
|
newly_joined_or_invited_or_knocked_users,
|
|
|
|
newly_left_users,
|
|
|
|
) = sync_result_builder.calculate_user_changes()
|
|
|
|
|
2024-08-09 06:59:44 -04:00
|
|
|
# include_device_list_updates can only be True if we have a
|
|
|
|
# since token.
|
|
|
|
assert since_token is not None
|
|
|
|
device_lists = await self._device_handler.generate_sync_entry_for_device_list(
|
|
|
|
user_id=user_id,
|
|
|
|
since_token=since_token,
|
|
|
|
now_token=sync_result_builder.now_token,
|
|
|
|
joined_room_ids=sync_result_builder.joined_room_ids,
|
2024-05-23 13:06:16 -04:00
|
|
|
newly_joined_rooms=newly_joined_rooms,
|
|
|
|
newly_joined_or_invited_or_knocked_users=newly_joined_or_invited_or_knocked_users,
|
|
|
|
newly_left_rooms=newly_left_rooms,
|
|
|
|
newly_left_users=newly_left_users,
|
|
|
|
)
|
|
|
|
|
|
|
|
# 3. Calculate `device_one_time_keys_count` and `device_unused_fallback_key_types`
|
|
|
|
device_id = sync_config.device_id
|
|
|
|
one_time_keys_count: JsonMapping = {}
|
|
|
|
unused_fallback_key_types: List[str] = []
|
|
|
|
if device_id:
|
|
|
|
# TODO: We should have a way to let clients differentiate between the states of:
|
|
|
|
# * no change in OTK count since the provided since token
|
|
|
|
# * the server has zero OTKs left for this device
|
|
|
|
# Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
|
|
|
|
one_time_keys_count = await self.store.count_e2e_one_time_keys(
|
|
|
|
user_id, device_id
|
|
|
|
)
|
|
|
|
unused_fallback_key_types = list(
|
|
|
|
await self.store.get_e2e_unused_fallback_key_types(user_id, device_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
return E2eeSyncResult(
|
|
|
|
to_device=sync_result_builder.to_device,
|
|
|
|
device_lists=device_lists,
|
|
|
|
device_one_time_keys_count=one_time_keys_count,
|
|
|
|
device_unused_fallback_key_types=unused_fallback_key_types,
|
|
|
|
next_batch=sync_result_builder.now_token,
|
|
|
|
)
|
|
|
|
|
2024-05-16 14:05:31 -04:00
|
|
|
async def get_sync_result_builder(
|
|
|
|
self,
|
|
|
|
sync_config: SyncConfig,
|
|
|
|
since_token: Optional[StreamToken] = None,
|
|
|
|
full_state: bool = False,
|
|
|
|
) -> "SyncResultBuilder":
|
|
|
|
"""
|
|
|
|
Assemble a `SyncResultBuilder` with all of the initial context to
|
|
|
|
start building up the sync response:
|
|
|
|
|
|
|
|
- Membership changes between the last sync and the current sync.
|
|
|
|
- Joined room IDs (minus any rooms to exclude).
|
|
|
|
- Rooms that became fully-stated/un-partial stated since the last sync.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sync_config: Config/info necessary to process the sync request.
|
|
|
|
since_token: The point in the stream to sync from.
|
|
|
|
full_state: Whether to return the full state for each room.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
`SyncResultBuilder` ready to start generating parts of the sync response.
|
|
|
|
"""
|
|
|
|
user_id = sync_config.user.to_string()
|
|
|
|
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
# Note: we get the users room list *before* we get the `now_token`, this
|
2022-10-04 11:42:59 -04:00
|
|
|
# avoids checking back in history if rooms are joined after the token is fetched.
|
|
|
|
token_before_rooms = self.event_sources.get_current_token()
|
|
|
|
mutable_joined_room_ids = set(await self.store.get_rooms_for_user(user_id))
|
|
|
|
|
2024-05-16 14:05:31 -04:00
|
|
|
# NB: The `now_token` gets changed by some of the `generate_sync_*` methods,
|
2016-05-24 04:43:35 -04:00
|
|
|
# this is due to some of the underlying streams not supporting the ability
|
|
|
|
# to query up to a given point.
|
|
|
|
# Always use the `now_token` in `SyncResultBuilder`
|
2020-08-04 07:21:47 -04:00
|
|
|
now_token = self.event_sources.get_current_token()
|
2021-04-01 12:08:21 -04:00
|
|
|
log_kv({"now_token": now_token})
|
2016-05-23 12:37:01 -04:00
|
|
|
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
# Since we fetched the users room list before calculating the `now_token` (see
|
|
|
|
# above), there's a small window during which membership events may have been
|
|
|
|
# persisted, so we fetch these now and modify the joined room list for any
|
|
|
|
# changes between the get_rooms_for_user call and the get_current_token call.
|
2022-10-04 11:42:59 -04:00
|
|
|
membership_change_events = []
|
|
|
|
if since_token:
|
|
|
|
membership_change_events = await self.store.get_membership_changes_for_user(
|
2023-01-23 10:44:39 -05:00
|
|
|
user_id,
|
|
|
|
since_token.room_key,
|
|
|
|
now_token.room_key,
|
|
|
|
self.rooms_to_exclude_globally,
|
2022-10-04 11:42:59 -04:00
|
|
|
)
|
|
|
|
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
last_membership_change_by_room_id: Dict[str, EventBase] = {}
|
2022-10-04 11:42:59 -04:00
|
|
|
for event in membership_change_events:
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
last_membership_change_by_room_id[event.room_id] = event
|
2022-10-04 11:42:59 -04:00
|
|
|
|
|
|
|
# For the latest membership event in each room found, add/remove the room ID
|
|
|
|
# from the joined room list accordingly. In this case we only care if the
|
|
|
|
# latest change is JOIN.
|
|
|
|
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
for room_id, event in last_membership_change_by_room_id.items():
|
2022-10-04 11:42:59 -04:00
|
|
|
assert event.internal_metadata.stream_ordering
|
Add Sliding Sync `/sync` endpoint (initial implementation) (#17187)
Based on [MSC3575](https://github.com/matrix-org/matrix-spec-proposals/pull/3575): Sliding Sync
This iteration only focuses on returning the list of room IDs in the sliding window API (without sorting/filtering).
Rooms appear in the Sliding sync response based on:
- `invite`, `join`, `knock`, `ban` membership events
- Kicks (`leave` membership events where `sender` is different from the `user_id`/`state_key`)
- `newly_left` (rooms that were left during the given token range, > `from_token` and <= `to_token`)
- In order for bans/kicks to not show up, you need to `/forget` those rooms. This doesn't modify the event itself though and only adds the `forgotten` flag to `room_memberships` in Synapse. There isn't a way to tell when a room was forgotten at the moment so we can't factor it into the from/to range.
### Example request
`POST http://localhost:8008/_matrix/client/unstable/org.matrix.msc3575/sync`
```json
{
"lists": {
"foo-list": {
"ranges": [ [0, 99] ],
"sort": [ "by_notification_level", "by_recency", "by_name" ],
"required_state": [
["m.room.join_rules", ""],
["m.room.history_visibility", ""],
["m.space.child", "*"]
],
"timeline_limit": 100
}
}
}
```
Response:
```json
{
"next_pos": "s58_224_0_13_10_1_1_16_0_1",
"lists": {
"foo-list": {
"count": 1,
"ops": [
{
"op": "SYNC",
"range": [0, 99],
"room_ids": [
"!MmgikIyFzsuvtnbvVG:my.synapse.linux.server"
]
}
]
}
},
"rooms": {},
"extensions": {}
}
```
2024-06-06 15:44:32 -04:00
|
|
|
# As a shortcut, skip any events that happened before we got our
|
|
|
|
# `get_rooms_for_user()` snapshot (any changes are already represented
|
|
|
|
# in that list).
|
2022-10-04 11:42:59 -04:00
|
|
|
if (
|
|
|
|
event.internal_metadata.stream_ordering
|
|
|
|
< token_before_rooms.room_key.stream
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"User membership change between getting rooms and current token: %s %s %s",
|
|
|
|
user_id,
|
|
|
|
event.membership,
|
|
|
|
room_id,
|
|
|
|
)
|
|
|
|
# User joined a room - we have to then check the room state to ensure we
|
|
|
|
# respect any bans if there's a race between the join and ban events.
|
|
|
|
if event.membership == Membership.JOIN:
|
|
|
|
user_ids_in_room = await self.store.get_users_in_room(room_id)
|
|
|
|
if user_id in user_ids_in_room:
|
|
|
|
mutable_joined_room_ids.add(room_id)
|
|
|
|
# The user left the room, or left and was re-invited but not joined yet
|
|
|
|
else:
|
|
|
|
mutable_joined_room_ids.discard(room_id)
|
|
|
|
|
2023-01-23 10:44:39 -05:00
|
|
|
# Tweak the set of rooms to return to the client for eager (non-lazy) syncs.
|
|
|
|
mutable_rooms_to_exclude = set(self.rooms_to_exclude_globally)
|
|
|
|
if not sync_config.filter_collection.lazy_load_members():
|
|
|
|
# Non-lazy syncs should never include partially stated rooms.
|
|
|
|
# Exclude all partially stated rooms from this sync.
|
2023-01-26 12:15:36 -05:00
|
|
|
results = await self.store.is_partial_state_room_batched(
|
|
|
|
mutable_joined_room_ids
|
|
|
|
)
|
|
|
|
mutable_rooms_to_exclude.update(
|
|
|
|
room_id
|
|
|
|
for room_id, is_partial_state in results.items()
|
|
|
|
if is_partial_state
|
|
|
|
)
|
2023-02-14 07:32:19 -05:00
|
|
|
membership_change_events = [
|
|
|
|
event
|
|
|
|
for event in membership_change_events
|
|
|
|
if not results.get(event.room_id, False)
|
|
|
|
]
|
2023-01-23 10:44:39 -05:00
|
|
|
|
|
|
|
# Incremental eager syncs should additionally include rooms that
|
|
|
|
# - we are joined to
|
|
|
|
# - are full-stated
|
|
|
|
# - became fully-stated at some point during the sync period
|
|
|
|
# (These rooms will have been omitted during a previous eager sync.)
|
2023-01-26 12:15:36 -05:00
|
|
|
forced_newly_joined_room_ids: Set[str] = set()
|
2023-01-23 10:44:39 -05:00
|
|
|
if since_token and not sync_config.filter_collection.lazy_load_members():
|
|
|
|
un_partial_stated_rooms = (
|
|
|
|
await self.store.get_un_partial_stated_rooms_between(
|
|
|
|
since_token.un_partial_stated_rooms_key,
|
|
|
|
now_token.un_partial_stated_rooms_key,
|
|
|
|
mutable_joined_room_ids,
|
|
|
|
)
|
|
|
|
)
|
2023-01-26 12:15:36 -05:00
|
|
|
results = await self.store.is_partial_state_room_batched(
|
|
|
|
un_partial_stated_rooms
|
|
|
|
)
|
|
|
|
forced_newly_joined_room_ids.update(
|
|
|
|
room_id
|
|
|
|
for room_id, is_partial_state in results.items()
|
|
|
|
if not is_partial_state
|
|
|
|
)
|
2023-01-23 10:44:39 -05:00
|
|
|
|
2022-10-04 11:42:59 -04:00
|
|
|
# Now we have our list of joined room IDs, exclude as configured and freeze
|
|
|
|
joined_room_ids = frozenset(
|
2023-08-15 08:11:20 -04:00
|
|
|
room_id
|
|
|
|
for room_id in mutable_joined_room_ids
|
|
|
|
if room_id not in mutable_rooms_to_exclude
|
2022-10-04 11:42:59 -04:00
|
|
|
)
|
|
|
|
|
2016-05-24 05:53:03 -04:00
|
|
|
sync_result_builder = SyncResultBuilder(
|
2016-05-23 12:37:01 -04:00
|
|
|
sync_config,
|
|
|
|
full_state,
|
|
|
|
since_token=since_token,
|
|
|
|
now_token=now_token,
|
2018-03-05 07:06:19 -05:00
|
|
|
joined_room_ids=joined_room_ids,
|
2023-01-23 10:44:39 -05:00
|
|
|
excluded_room_ids=frozenset(mutable_rooms_to_exclude),
|
|
|
|
forced_newly_joined_room_ids=frozenset(forced_newly_joined_room_ids),
|
2022-10-04 11:42:59 -04:00
|
|
|
membership_change_events=membership_change_events,
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
|
2024-05-16 14:05:31 -04:00
|
|
|
return sync_result_builder
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2022-12-06 04:52:55 -05:00
|
|
|
@trace
|
2020-02-03 13:05:44 -05:00
|
|
|
async def _generate_sync_entry_for_to_device(
|
|
|
|
self, sync_result_builder: "SyncResultBuilder"
|
|
|
|
) -> None:
|
2016-08-25 12:35:37 -04:00
|
|
|
"""Generates the portion of the sync response. Populates
|
|
|
|
`sync_result_builder` with the result.
|
|
|
|
"""
|
|
|
|
user_id = sync_result_builder.sync_config.user.to_string()
|
|
|
|
device_id = sync_result_builder.sync_config.device_id
|
|
|
|
now_token = sync_result_builder.now_token
|
|
|
|
since_stream_id = 0
|
|
|
|
if sync_result_builder.since_token is not None:
|
|
|
|
since_stream_id = int(sync_result_builder.since_token.to_device_key)
|
|
|
|
|
2022-02-01 09:13:38 -05:00
|
|
|
if device_id is not None and since_stream_id != int(now_token.to_device_key):
|
|
|
|
messages, stream_id = await self.store.get_messages_for_device(
|
2016-09-02 10:50:37 -04:00
|
|
|
user_id, device_id, since_stream_id, now_token.to_device_key
|
2016-08-30 05:58:46 -04:00
|
|
|
)
|
2016-12-15 13:13:58 -05:00
|
|
|
|
2021-04-01 12:08:21 -04:00
|
|
|
for message in messages:
|
2022-12-06 04:52:55 -05:00
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"event": "to_device_message",
|
|
|
|
"sender": message["sender"],
|
|
|
|
"type": message["type"],
|
|
|
|
EventContentFields.TO_DEVICE_MSGID: message["content"].get(
|
|
|
|
EventContentFields.TO_DEVICE_MSGID
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2021-04-01 12:08:21 -04:00
|
|
|
|
2022-12-23 09:04:50 -05:00
|
|
|
if messages and issue9533_logger.isEnabledFor(logging.DEBUG):
|
|
|
|
issue9533_logger.debug(
|
|
|
|
"Returning to-device messages with stream_ids (%d, %d]; now: %d;"
|
|
|
|
" msgids: %s",
|
|
|
|
since_stream_id,
|
|
|
|
stream_id,
|
|
|
|
now_token.to_device_key,
|
|
|
|
[
|
|
|
|
message["content"].get(EventContentFields.TO_DEVICE_MSGID)
|
|
|
|
for message in messages
|
|
|
|
],
|
|
|
|
)
|
2016-08-30 05:58:46 -04:00
|
|
|
sync_result_builder.now_token = now_token.copy_and_replace(
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType.TO_DEVICE, stream_id
|
2016-08-30 05:58:46 -04:00
|
|
|
)
|
|
|
|
sync_result_builder.to_device = messages
|
|
|
|
else:
|
|
|
|
sync_result_builder.to_device = []
|
2016-08-25 12:35:37 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
async def _generate_sync_entry_for_account_data(
|
|
|
|
self, sync_result_builder: "SyncResultBuilder"
|
2023-02-10 09:22:16 -05:00
|
|
|
) -> None:
|
|
|
|
"""Generates the global account data portion of the sync response.
|
2021-12-02 15:58:32 -05:00
|
|
|
|
|
|
|
Account data (called "Client Config" in the spec) can be set either globally
|
|
|
|
or for a specific room. Account data consists of a list of events which
|
|
|
|
accumulate state, much like a room.
|
|
|
|
|
2023-02-10 09:22:16 -05:00
|
|
|
This function retrieves global account data and writes it to the given
|
|
|
|
`sync_result_builder`. See `_generate_sync_entry_for_rooms` for handling
|
|
|
|
of per-room account data.
|
2016-05-24 05:14:53 -04:00
|
|
|
|
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_result_builder
|
2016-05-24 05:14:53 -04:00
|
|
|
"""
|
2016-05-24 05:53:03 -04:00
|
|
|
sync_config = sync_result_builder.sync_config
|
|
|
|
user_id = sync_result_builder.sync_config.user.to_string()
|
|
|
|
since_token = sync_result_builder.since_token
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2016-05-24 05:53:03 -04:00
|
|
|
if since_token and not sync_result_builder.full_state:
|
2023-02-10 09:22:16 -05:00
|
|
|
global_account_data = (
|
|
|
|
await self.store.get_updated_global_account_data_for_user(
|
|
|
|
user_id, since_token.account_data_key
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
push_rules_changed = await self.store.have_push_rules_changed_for_user(
|
2016-05-23 12:37:01 -04:00
|
|
|
user_id, int(since_token.push_rules_key)
|
|
|
|
)
|
|
|
|
|
|
|
|
if push_rules_changed:
|
2024-05-16 10:04:14 -04:00
|
|
|
global_account_data = dict(global_account_data)
|
2024-09-02 07:39:04 -04:00
|
|
|
global_account_data[
|
|
|
|
AccountDataTypes.PUSH_RULES
|
|
|
|
] = await self._push_rules_handler.push_rules_for_user(sync_config.user)
|
2016-05-23 12:37:01 -04:00
|
|
|
else:
|
2023-02-10 09:22:16 -05:00
|
|
|
all_global_account_data = await self.store.get_global_account_data_for_user(
|
|
|
|
user_id
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2024-05-16 10:04:14 -04:00
|
|
|
global_account_data = dict(all_global_account_data)
|
2024-09-02 07:39:04 -04:00
|
|
|
global_account_data[
|
|
|
|
AccountDataTypes.PUSH_RULES
|
|
|
|
] = await self._push_rules_handler.push_rules_for_user(sync_config.user)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2023-02-10 09:22:16 -05:00
|
|
|
account_data_for_user = (
|
|
|
|
await sync_config.filter_collection.filter_global_account_data(
|
|
|
|
[
|
|
|
|
{"type": account_data_type, "content": content}
|
|
|
|
for account_data_type, content in global_account_data.items()
|
|
|
|
]
|
|
|
|
)
|
2016-05-24 05:22:24 -04:00
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2016-05-24 05:53:03 -04:00
|
|
|
sync_result_builder.account_data = account_data_for_user
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
async def _generate_sync_entry_for_presence(
|
2020-02-03 13:05:44 -05:00
|
|
|
self,
|
|
|
|
sync_result_builder: "SyncResultBuilder",
|
2022-09-08 15:13:39 -04:00
|
|
|
newly_joined_rooms: AbstractSet[str],
|
|
|
|
newly_joined_or_invited_users: AbstractSet[str],
|
2020-02-03 13:05:44 -05:00
|
|
|
) -> None:
|
2016-05-24 05:14:53 -04:00
|
|
|
"""Generates the presence portion of the sync response. Populates the
|
2016-05-24 05:53:03 -04:00
|
|
|
`sync_result_builder` with the result.
|
2016-05-24 05:14:53 -04:00
|
|
|
|
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_result_builder
|
|
|
|
newly_joined_rooms: Set of rooms that the user has joined since
|
|
|
|
the last sync (or empty if an initial sync)
|
|
|
|
newly_joined_or_invited_users: Set of users that have joined or
|
|
|
|
been invited to rooms since the last sync (or empty if an
|
|
|
|
initial sync)
|
2016-05-24 05:14:53 -04:00
|
|
|
"""
|
2016-05-24 05:53:03 -04:00
|
|
|
now_token = sync_result_builder.now_token
|
|
|
|
sync_config = sync_result_builder.sync_config
|
|
|
|
user = sync_result_builder.sync_config.user
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-21 13:34:26 -04:00
|
|
|
presence_source = self.event_sources.sources.presence
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2016-05-24 05:53:03 -04:00
|
|
|
since_token = sync_result_builder.since_token
|
2020-09-08 11:48:15 -04:00
|
|
|
presence_key = None
|
|
|
|
include_offline = False
|
2016-05-24 05:53:03 -04:00
|
|
|
if since_token and not sync_result_builder.full_state:
|
2016-05-23 12:37:01 -04:00
|
|
|
presence_key = since_token.presence_key
|
2016-05-24 06:04:35 -04:00
|
|
|
include_offline = True
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
presence, presence_key = await presence_source.get_new_events(
|
2016-05-23 12:37:01 -04:00
|
|
|
user=user,
|
|
|
|
from_key=presence_key,
|
|
|
|
is_guest=sync_config.is_guest,
|
2024-07-23 05:59:24 -04:00
|
|
|
include_offline=(
|
|
|
|
True
|
|
|
|
if self.hs_config.server.presence_include_offline_users_on_sync
|
|
|
|
else include_offline
|
|
|
|
),
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
2020-09-11 07:22:55 -04:00
|
|
|
assert presence_key
|
2016-05-24 05:53:03 -04:00
|
|
|
sync_result_builder.now_token = now_token.copy_and_replace(
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType.PRESENCE, presence_key
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
|
2019-05-16 08:23:43 -04:00
|
|
|
extra_users_ids = set(newly_joined_or_invited_users)
|
2016-05-23 12:37:01 -04:00
|
|
|
for room_id in newly_joined_rooms:
|
2021-05-05 11:49:34 -04:00
|
|
|
users = await self.store.get_users_in_room(room_id)
|
2016-05-23 12:37:01 -04:00
|
|
|
extra_users_ids.update(users)
|
|
|
|
extra_users_ids.discard(user.to_string())
|
|
|
|
|
2017-03-15 10:27:34 -04:00
|
|
|
if extra_users_ids:
|
2019-12-05 12:58:25 -05:00
|
|
|
states = await self.presence_handler.get_states(extra_users_ids)
|
2017-03-15 10:27:34 -04:00
|
|
|
presence.extend(states)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2017-03-15 10:27:34 -04:00
|
|
|
# Deduplicate the presence entries so that there's at most one per user
|
2018-05-31 05:03:47 -04:00
|
|
|
presence = list({p.user_id: p for p in presence}.values())
|
2016-06-02 10:20:28 -04:00
|
|
|
|
2021-11-09 08:10:58 -05:00
|
|
|
presence = await sync_config.filter_collection.filter_presence(presence)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2016-05-24 05:53:03 -04:00
|
|
|
sync_result_builder.presence = presence
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
async def _generate_sync_entry_for_rooms(
|
2023-02-10 09:22:16 -05:00
|
|
|
self, sync_result_builder: "SyncResultBuilder"
|
2023-02-02 13:45:12 -05:00
|
|
|
) -> Tuple[AbstractSet[str], AbstractSet[str]]:
|
2016-05-24 05:14:53 -04:00
|
|
|
"""Generates the rooms portion of the sync response. Populates the
|
2016-05-24 05:53:03 -04:00
|
|
|
`sync_result_builder` with the result.
|
2016-05-24 05:14:53 -04:00
|
|
|
|
2021-12-02 15:58:32 -05:00
|
|
|
In the response that reaches the client, rooms are divided into four categories:
|
|
|
|
`invite`, `join`, `knock`, `leave`. These aren't the same as the four sets of
|
|
|
|
room ids returned by this function.
|
|
|
|
|
2016-05-24 05:14:53 -04:00
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_result_builder
|
2016-05-24 05:14:53 -04:00
|
|
|
|
|
|
|
Returns:
|
2023-02-02 13:45:12 -05:00
|
|
|
Returns a 2-tuple describing rooms the user has joined or left.
|
2021-12-07 07:42:05 -05:00
|
|
|
|
|
|
|
Its entries are:
|
2021-12-02 15:58:32 -05:00
|
|
|
- newly_joined_rooms
|
|
|
|
- newly_left_rooms
|
2016-05-24 05:14:53 -04:00
|
|
|
"""
|
2023-01-09 08:43:50 -05:00
|
|
|
|
2021-12-07 07:42:05 -05:00
|
|
|
since_token = sync_result_builder.since_token
|
2016-05-24 05:53:03 -04:00
|
|
|
user_id = sync_result_builder.sync_config.user.to_string()
|
2016-11-22 11:38:35 -05:00
|
|
|
|
2023-02-10 09:22:16 -05:00
|
|
|
blocks_all_rooms = (
|
|
|
|
sync_result_builder.sync_config.filter_collection.blocks_all_rooms()
|
|
|
|
)
|
|
|
|
|
|
|
|
# 0. Start by fetching room account data (if required).
|
|
|
|
if (
|
|
|
|
blocks_all_rooms
|
|
|
|
or sync_result_builder.sync_config.filter_collection.blocks_all_room_account_data()
|
|
|
|
):
|
2023-09-18 09:55:04 -04:00
|
|
|
account_data_by_room: Mapping[str, Mapping[str, JsonMapping]] = {}
|
2023-02-10 09:22:16 -05:00
|
|
|
elif since_token and not sync_result_builder.full_state:
|
|
|
|
account_data_by_room = (
|
|
|
|
await self.store.get_updated_room_account_data_for_user(
|
|
|
|
user_id, since_token.account_data_key
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
account_data_by_room = await self.store.get_room_account_data_for_user(
|
|
|
|
user_id
|
|
|
|
)
|
|
|
|
|
2023-02-02 11:58:20 -05:00
|
|
|
# 1. Start by fetching all ephemeral events in rooms we've joined (if required).
|
2023-02-02 13:45:12 -05:00
|
|
|
block_all_room_ephemeral = (
|
2023-02-10 09:22:16 -05:00
|
|
|
blocks_all_rooms
|
2023-02-02 13:45:12 -05:00
|
|
|
or sync_result_builder.sync_config.filter_collection.blocks_all_room_ephemeral()
|
|
|
|
)
|
|
|
|
if block_all_room_ephemeral:
|
2021-07-16 13:22:36 -04:00
|
|
|
ephemeral_by_room: Dict[str, List[JsonDict]] = {}
|
2016-11-22 11:38:35 -05:00
|
|
|
else:
|
2019-12-05 12:58:25 -05:00
|
|
|
now_token, ephemeral_by_room = await self.ephemeral_by_room(
|
2018-03-05 07:06:19 -05:00
|
|
|
sync_result_builder,
|
2016-11-22 11:38:35 -05:00
|
|
|
now_token=sync_result_builder.now_token,
|
|
|
|
since_token=sync_result_builder.since_token,
|
|
|
|
)
|
|
|
|
sync_result_builder.now_token = now_token
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-12-07 07:42:05 -05:00
|
|
|
# 2. We check up front if anything has changed, if it hasn't then there is
|
2020-06-05 08:43:21 -04:00
|
|
|
# no point in going further.
|
2017-03-15 14:13:59 -04:00
|
|
|
if not sync_result_builder.full_state:
|
|
|
|
if since_token and not ephemeral_by_room and not account_data_by_room:
|
2019-12-05 12:58:25 -05:00
|
|
|
have_changed = await self._have_rooms_changed(sync_result_builder)
|
2021-09-06 11:08:25 -04:00
|
|
|
log_kv({"rooms_have_changed": have_changed})
|
2017-03-15 14:13:59 -04:00
|
|
|
if not have_changed:
|
2019-12-05 12:58:25 -05:00
|
|
|
tags_by_room = await self.store.get_updated_tags(
|
2017-03-15 14:13:59 -04:00
|
|
|
user_id, since_token.account_data_key
|
|
|
|
)
|
|
|
|
if not tags_by_room:
|
2017-03-16 07:51:46 -04:00
|
|
|
logger.debug("no-oping sync")
|
2023-02-02 13:45:12 -05:00
|
|
|
return set(), set()
|
2017-03-15 14:13:59 -04:00
|
|
|
|
2021-12-07 07:42:05 -05:00
|
|
|
# 3. Work out which rooms need reporting in the sync response.
|
2022-03-15 14:06:05 -04:00
|
|
|
ignored_users = await self.store.ignored_users(user_id)
|
2017-03-15 14:13:59 -04:00
|
|
|
if since_token:
|
2023-01-23 10:44:39 -05:00
|
|
|
room_changes = await self._get_room_changes_for_incremental_sync(
|
2022-08-04 05:02:29 -04:00
|
|
|
sync_result_builder, ignored_users
|
2020-02-03 13:05:44 -05:00
|
|
|
)
|
2019-12-05 12:58:25 -05:00
|
|
|
tags_by_room = await self.store.get_updated_tags(
|
2017-03-15 14:13:59 -04:00
|
|
|
user_id, since_token.account_data_key
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
else:
|
2023-01-23 10:44:39 -05:00
|
|
|
room_changes = await self._get_room_changes_for_initial_sync(
|
|
|
|
sync_result_builder, ignored_users
|
|
|
|
)
|
2019-12-05 12:58:25 -05:00
|
|
|
tags_by_room = await self.store.get_tags_for_user(user_id)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-06 11:08:25 -04:00
|
|
|
log_kv({"rooms_changed": len(room_changes.room_entries)})
|
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
room_entries = room_changes.room_entries
|
|
|
|
invited = room_changes.invited
|
2021-06-09 14:39:51 -04:00
|
|
|
knocked = room_changes.knocked
|
2020-02-03 13:05:44 -05:00
|
|
|
newly_joined_rooms = room_changes.newly_joined_rooms
|
|
|
|
newly_left_rooms = room_changes.newly_left_rooms
|
|
|
|
|
2021-12-07 07:42:05 -05:00
|
|
|
# 4. We need to apply further processing to `room_entries` (rooms considered
|
|
|
|
# joined or archived).
|
2021-09-20 08:56:23 -04:00
|
|
|
async def handle_room_entries(room_entry: "RoomSyncResultBuilder") -> None:
|
2021-09-15 12:14:25 -04:00
|
|
|
logger.debug("Generating room entry for %s", room_entry.room_id)
|
2023-02-02 13:45:12 -05:00
|
|
|
# Note that this mutates sync_result_builder.{joined,archived}.
|
2021-09-20 08:56:23 -04:00
|
|
|
await self._generate_room_entry(
|
2021-09-15 12:14:25 -04:00
|
|
|
sync_result_builder,
|
|
|
|
room_entry,
|
|
|
|
ephemeral=ephemeral_by_room.get(room_entry.room_id, []),
|
|
|
|
tags=tags_by_room.get(room_entry.room_id),
|
|
|
|
account_data=account_data_by_room.get(room_entry.room_id, {}),
|
|
|
|
always_include=sync_result_builder.full_state,
|
|
|
|
)
|
|
|
|
logger.debug("Generated room entry for %s", room_entry.room_id)
|
2016-05-23 13:21:27 -04:00
|
|
|
|
2021-12-21 06:10:36 -05:00
|
|
|
with start_active_span("sync.generate_room_entries"):
|
|
|
|
await concurrently_execute(handle_room_entries, room_entries, 10)
|
2016-05-23 13:21:27 -04:00
|
|
|
|
2016-05-24 05:53:03 -04:00
|
|
|
sync_result_builder.invited.extend(invited)
|
2021-06-09 14:39:51 -04:00
|
|
|
sync_result_builder.knocked.extend(knocked)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2023-02-02 13:45:12 -05:00
|
|
|
return set(newly_joined_rooms), set(newly_left_rooms)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
async def _have_rooms_changed(
|
|
|
|
self, sync_result_builder: "SyncResultBuilder"
|
|
|
|
) -> bool:
|
2017-03-16 09:04:07 -04:00
|
|
|
"""Returns whether there may be any new events that should be sent down
|
|
|
|
the sync. Returns True if there are.
|
2021-12-02 15:58:32 -05:00
|
|
|
|
|
|
|
Does not modify the `sync_result_builder`.
|
2017-03-16 07:56:59 -04:00
|
|
|
"""
|
2017-03-15 14:13:59 -04:00
|
|
|
since_token = sync_result_builder.since_token
|
2022-10-04 11:42:59 -04:00
|
|
|
membership_change_events = sync_result_builder.membership_change_events
|
2017-03-15 14:13:59 -04:00
|
|
|
|
2017-03-16 07:56:59 -04:00
|
|
|
assert since_token
|
2017-03-15 14:13:59 -04:00
|
|
|
|
2023-01-23 10:44:39 -05:00
|
|
|
if membership_change_events or sync_result_builder.forced_newly_joined_room_ids:
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
2017-03-15 14:13:59 -04:00
|
|
|
|
2020-09-11 07:22:55 -04:00
|
|
|
stream_id = since_token.room_key.stream
|
2018-03-05 07:06:19 -05:00
|
|
|
for room_id in sync_result_builder.joined_room_ids:
|
2017-03-16 07:56:59 -04:00
|
|
|
if self.store.has_room_changed_since(room_id, stream_id):
|
2019-07-23 09:00:55 -04:00
|
|
|
return True
|
|
|
|
return False
|
2017-03-15 14:13:59 -04:00
|
|
|
|
2023-01-23 10:44:39 -05:00
|
|
|
async def _get_room_changes_for_incremental_sync(
|
2022-03-30 05:43:04 -04:00
|
|
|
self,
|
|
|
|
sync_result_builder: "SyncResultBuilder",
|
|
|
|
ignored_users: FrozenSet[str],
|
2020-02-03 13:05:44 -05:00
|
|
|
) -> _RoomChanges:
|
2021-12-02 15:58:32 -05:00
|
|
|
"""Determine the changes in rooms to report to the user.
|
|
|
|
|
2021-12-10 14:19:48 -05:00
|
|
|
This function is a first pass at generating the rooms part of the sync response.
|
|
|
|
It determines which rooms have changed during the sync period, and categorises
|
2022-08-04 05:02:29 -04:00
|
|
|
them into four buckets: "knock", "invite", "join" and "leave". It also excludes
|
|
|
|
from that list any room that appears in the list of rooms to exclude from sync
|
|
|
|
results in the server configuration.
|
2021-12-02 15:58:32 -05:00
|
|
|
|
2021-12-10 14:19:48 -05:00
|
|
|
1. Finds all membership changes for the user in the sync period (from
|
|
|
|
`since_token` up to `now_token`).
|
|
|
|
2. Uses those to place the room in one of the four categories above.
|
|
|
|
3. Builds a `_RoomChanges` struct to record this, and return that struct.
|
2021-12-02 15:58:32 -05:00
|
|
|
|
2021-12-10 14:19:48 -05:00
|
|
|
For rooms classified as "knock", "invite" or "leave", we just need to report
|
|
|
|
a single membership event in the eventual /sync response. For "join" we need
|
|
|
|
to fetch additional non-membership events, e.g. messages in the room. That is
|
|
|
|
more complicated, so instead we report an intermediary `RoomSyncResultBuilder`
|
|
|
|
struct, and leave the additional work to `_generate_room_entry`.
|
2021-12-02 15:58:32 -05:00
|
|
|
|
|
|
|
The sync_result_builder is not modified by this function.
|
|
|
|
"""
|
2016-05-24 05:53:03 -04:00
|
|
|
user_id = sync_result_builder.sync_config.user.to_string()
|
|
|
|
since_token = sync_result_builder.since_token
|
|
|
|
now_token = sync_result_builder.now_token
|
|
|
|
sync_config = sync_result_builder.sync_config
|
2022-10-04 11:42:59 -04:00
|
|
|
membership_change_events = sync_result_builder.membership_change_events
|
2016-05-23 12:37:01 -04:00
|
|
|
|
|
|
|
assert since_token
|
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
mem_change_events_by_room_id: Dict[str, List[EventBase]] = {}
|
2021-12-02 15:58:32 -05:00
|
|
|
for event in membership_change_events:
|
2016-05-23 12:37:01 -04:00
|
|
|
mem_change_events_by_room_id.setdefault(event.room_id, []).append(event)
|
|
|
|
|
2023-01-23 10:44:39 -05:00
|
|
|
newly_joined_rooms: List[str] = list(
|
|
|
|
sync_result_builder.forced_newly_joined_room_ids
|
|
|
|
)
|
2021-12-02 15:58:32 -05:00
|
|
|
newly_left_rooms: List[str] = []
|
|
|
|
room_entries: List[RoomSyncResultBuilder] = []
|
|
|
|
invited: List[InvitedSyncResult] = []
|
|
|
|
knocked: List[KnockedSyncResult] = []
|
2020-06-15 07:03:36 -04:00
|
|
|
for room_id, events in mem_change_events_by_room_id.items():
|
2021-12-02 15:58:32 -05:00
|
|
|
# The body of this loop will add this room to at least one of the five lists
|
|
|
|
# above. Things get messy if you've e.g. joined, left, joined then left the
|
|
|
|
# room all in the same sync period.
|
2020-02-06 08:31:05 -05:00
|
|
|
logger.debug(
|
2019-01-31 13:30:40 -05:00
|
|
|
"Membership changes in %s: [%s]",
|
|
|
|
room_id,
|
2021-07-19 10:28:05 -04:00
|
|
|
", ".join("%s (%s)" % (e.event_id, e.membership) for e in events),
|
2019-01-31 13:30:40 -05:00
|
|
|
)
|
|
|
|
|
2016-05-23 12:37:01 -04:00
|
|
|
non_joins = [e for e in events if e.membership != Membership.JOIN]
|
|
|
|
has_join = len(non_joins) != len(events)
|
|
|
|
|
|
|
|
# We want to figure out if we joined the room at some point since
|
|
|
|
# the last sync (even if we have since left). This is to make sure
|
|
|
|
# we do send down the room, and with full state, where necessary
|
2017-09-22 09:44:17 -04:00
|
|
|
|
2017-09-13 10:13:41 -04:00
|
|
|
old_state_ids = None
|
2018-03-05 07:06:19 -05:00
|
|
|
if room_id in sync_result_builder.joined_room_ids and non_joins:
|
2017-09-22 09:44:17 -04:00
|
|
|
# Always include if the user (re)joined the room, especially
|
|
|
|
# important so that device list changes are calculated correctly.
|
2019-05-16 08:23:43 -04:00
|
|
|
# If there are non-join member events, but we are still in the room,
|
2017-09-22 09:44:17 -04:00
|
|
|
# then the user must have left and joined
|
|
|
|
newly_joined_rooms.append(room_id)
|
|
|
|
|
|
|
|
# User is in the room so we don't need to do the invite/leave checks
|
|
|
|
continue
|
|
|
|
|
2018-03-05 07:06:19 -05:00
|
|
|
if room_id in sync_result_builder.joined_room_ids or has_join:
|
2024-07-04 13:25:36 -04:00
|
|
|
old_state_ids = await self._state_storage_controller.get_state_ids_at(
|
2022-08-18 06:53:02 -04:00
|
|
|
room_id,
|
|
|
|
since_token,
|
|
|
|
state_filter=StateFilter.from_types([(EventTypes.Member, user_id)]),
|
|
|
|
)
|
2016-08-25 13:59:44 -04:00
|
|
|
old_mem_ev_id = old_state_ids.get((EventTypes.Member, user_id), None)
|
|
|
|
old_mem_ev = None
|
|
|
|
if old_mem_ev_id:
|
2019-12-05 12:58:25 -05:00
|
|
|
old_mem_ev = await self.store.get_event(
|
2016-08-25 13:59:44 -04:00
|
|
|
old_mem_ev_id, allow_none=True
|
|
|
|
)
|
2019-03-06 12:29:15 -05:00
|
|
|
|
2016-05-23 12:37:01 -04:00
|
|
|
if not old_mem_ev or old_mem_ev.membership != Membership.JOIN:
|
|
|
|
newly_joined_rooms.append(room_id)
|
|
|
|
|
2017-09-22 09:44:17 -04:00
|
|
|
# If user is in the room then we don't need to do the invite/leave checks
|
2018-03-05 07:06:19 -05:00
|
|
|
if room_id in sync_result_builder.joined_room_ids:
|
2017-09-22 09:44:17 -04:00
|
|
|
continue
|
2016-05-23 12:37:01 -04:00
|
|
|
|
|
|
|
if not non_joins:
|
|
|
|
continue
|
2021-12-07 06:24:31 -05:00
|
|
|
last_non_join = non_joins[-1]
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2017-09-13 10:13:41 -04:00
|
|
|
# Check if we have left the room. This can either be because we were
|
|
|
|
# joined before *or* that we since joined and then left.
|
|
|
|
if events[-1].membership != Membership.JOIN:
|
|
|
|
if has_join:
|
|
|
|
newly_left_rooms.append(room_id)
|
|
|
|
else:
|
|
|
|
if not old_state_ids:
|
2024-06-17 13:06:18 -04:00
|
|
|
old_state_ids = (
|
2024-07-04 13:25:36 -04:00
|
|
|
await self._state_storage_controller.get_state_ids_at(
|
2024-06-17 13:06:18 -04:00
|
|
|
room_id,
|
|
|
|
since_token,
|
|
|
|
state_filter=StateFilter.from_types(
|
|
|
|
[(EventTypes.Member, user_id)]
|
|
|
|
),
|
|
|
|
)
|
2022-08-18 06:53:02 -04:00
|
|
|
)
|
2017-09-13 10:13:41 -04:00
|
|
|
old_mem_ev_id = old_state_ids.get(
|
|
|
|
(EventTypes.Member, user_id), None
|
|
|
|
)
|
|
|
|
old_mem_ev = None
|
|
|
|
if old_mem_ev_id:
|
2019-12-05 12:58:25 -05:00
|
|
|
old_mem_ev = await self.store.get_event(
|
2017-09-13 10:13:41 -04:00
|
|
|
old_mem_ev_id, allow_none=True
|
|
|
|
)
|
|
|
|
if old_mem_ev and old_mem_ev.membership == Membership.JOIN:
|
|
|
|
newly_left_rooms.append(room_id)
|
|
|
|
|
2016-05-23 12:37:01 -04:00
|
|
|
# Only bother if we're still currently invited
|
2021-12-07 06:24:31 -05:00
|
|
|
should_invite = last_non_join.membership == Membership.INVITE
|
2016-05-23 12:37:01 -04:00
|
|
|
if should_invite:
|
2021-12-07 06:24:31 -05:00
|
|
|
if last_non_join.sender not in ignored_users:
|
|
|
|
invite_room_sync = InvitedSyncResult(room_id, invite=last_non_join)
|
2021-06-09 14:39:51 -04:00
|
|
|
if invite_room_sync:
|
|
|
|
invited.append(invite_room_sync)
|
|
|
|
|
|
|
|
# Only bother if our latest membership in the room is knock (and we haven't
|
|
|
|
# been accepted/rejected in the meantime).
|
2021-12-07 06:24:31 -05:00
|
|
|
should_knock = last_non_join.membership == Membership.KNOCK
|
2021-06-09 14:39:51 -04:00
|
|
|
if should_knock:
|
2021-12-07 06:24:31 -05:00
|
|
|
knock_room_sync = KnockedSyncResult(room_id, knock=last_non_join)
|
2021-06-09 14:39:51 -04:00
|
|
|
if knock_room_sync:
|
|
|
|
knocked.append(knock_room_sync)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
|
|
|
# Always include leave/ban events. Just take the last one.
|
|
|
|
# TODO: How do we handle ban -> leave in same batch?
|
|
|
|
leave_events = [
|
|
|
|
e
|
|
|
|
for e in non_joins
|
|
|
|
if e.membership in (Membership.LEAVE, Membership.BAN)
|
|
|
|
]
|
|
|
|
|
|
|
|
if leave_events:
|
|
|
|
leave_event = leave_events[-1]
|
2020-09-29 16:48:33 -04:00
|
|
|
leave_position = await self.store.get_position_for_event(
|
2016-05-23 12:37:01 -04:00
|
|
|
leave_event.event_id
|
|
|
|
)
|
|
|
|
|
2020-09-29 16:48:33 -04:00
|
|
|
# If the leave event happened before the since token then we
|
|
|
|
# bail.
|
|
|
|
if since_token and not leave_position.persisted_after(
|
|
|
|
since_token.room_key
|
|
|
|
):
|
2016-05-23 12:37:01 -04:00
|
|
|
continue
|
|
|
|
|
2020-09-29 16:48:33 -04:00
|
|
|
# We can safely convert the position of the leave event into a
|
|
|
|
# stream token as it'll only be used in the context of this
|
|
|
|
# room. (c.f. the docstring of `to_room_stream_token`).
|
|
|
|
leave_token = since_token.copy_and_replace(
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType.ROOM, leave_position.to_room_stream_token()
|
2020-09-29 16:48:33 -04:00
|
|
|
)
|
|
|
|
|
2019-01-30 10:46:27 -05:00
|
|
|
# If this is an out of band message, like a remote invite
|
|
|
|
# rejection, we include it in the recents batch. Otherwise, we
|
|
|
|
# let _load_filtered_recents handle fetching the correct
|
|
|
|
# batches.
|
|
|
|
#
|
|
|
|
# This is all screaming out for a refactor, as the logic here is
|
|
|
|
# subtle and the moving parts numerous.
|
|
|
|
if leave_event.internal_metadata.is_out_of_band_membership():
|
2021-07-16 13:22:36 -04:00
|
|
|
batch_events: Optional[List[EventBase]] = [leave_event]
|
2019-01-30 10:46:27 -05:00
|
|
|
else:
|
|
|
|
batch_events = None
|
|
|
|
|
2016-05-24 04:43:35 -04:00
|
|
|
room_entries.append(
|
|
|
|
RoomSyncResultBuilder(
|
2016-05-23 12:37:01 -04:00
|
|
|
room_id=room_id,
|
2016-05-24 04:43:35 -04:00
|
|
|
rtype="archived",
|
2019-01-30 10:46:27 -05:00
|
|
|
events=batch_events,
|
2016-05-23 12:37:01 -04:00
|
|
|
newly_joined=room_id in newly_joined_rooms,
|
|
|
|
full_state=False,
|
|
|
|
since_token=since_token,
|
|
|
|
upto_token=leave_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token=leave_token,
|
2022-04-07 10:54:16 -04:00
|
|
|
out_of_band=leave_event.internal_metadata.is_out_of_band_membership(),
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
timeline_limit = sync_config.filter_collection.timeline_limit()
|
|
|
|
|
2021-12-02 15:58:32 -05:00
|
|
|
# Get all events since the `from_key` in rooms we're currently joined to.
|
|
|
|
# If there are too many, we get the most recent events only. This leaves
|
|
|
|
# a "gap" in the timeline, as described by the spec for /sync.
|
2019-12-05 12:58:25 -05:00
|
|
|
room_to_events = await self.store.get_room_events_stream_for_rooms(
|
2018-03-05 07:06:19 -05:00
|
|
|
room_ids=sync_result_builder.joined_room_ids,
|
2024-08-07 12:27:50 -04:00
|
|
|
from_key=now_token.room_key,
|
|
|
|
to_key=since_token.room_key,
|
2016-05-23 12:37:01 -04:00
|
|
|
limit=timeline_limit + 1,
|
2024-08-07 12:27:50 -04:00
|
|
|
direction=Direction.BACKWARDS,
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# We loop through all room ids, even if there are no new events, in case
|
2020-04-11 15:55:18 -04:00
|
|
|
# there are non room events that we need to notify about.
|
2018-03-05 07:06:19 -05:00
|
|
|
for room_id in sync_result_builder.joined_room_ids:
|
2016-05-23 12:37:01 -04:00
|
|
|
room_entry = room_to_events.get(room_id, None)
|
|
|
|
|
2019-03-06 12:29:15 -05:00
|
|
|
newly_joined = room_id in newly_joined_rooms
|
2016-05-23 12:37:01 -04:00
|
|
|
if room_entry:
|
2024-09-10 04:52:42 -04:00
|
|
|
events, start_key, _ = room_entry
|
2024-08-07 12:27:50 -04:00
|
|
|
# We want to return the events in ascending order (the last event is the
|
|
|
|
# most recent).
|
|
|
|
events.reverse()
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2022-05-16 11:35:31 -04:00
|
|
|
prev_batch_token = now_token.copy_and_replace(
|
|
|
|
StreamKeyType.ROOM, start_key
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2019-03-06 12:29:15 -05:00
|
|
|
entry = RoomSyncResultBuilder(
|
2016-05-23 12:37:01 -04:00
|
|
|
room_id=room_id,
|
2016-05-24 04:43:35 -04:00
|
|
|
rtype="joined",
|
2016-05-23 12:37:01 -04:00
|
|
|
events=events,
|
2019-03-06 12:29:15 -05:00
|
|
|
newly_joined=newly_joined,
|
2016-05-23 12:37:01 -04:00
|
|
|
full_state=False,
|
2019-03-06 12:29:15 -05:00
|
|
|
since_token=None if newly_joined else since_token,
|
2016-05-23 12:37:01 -04:00
|
|
|
upto_token=prev_batch_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token=now_token,
|
2019-03-06 12:29:15 -05:00
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
else:
|
2019-03-06 12:29:15 -05:00
|
|
|
entry = RoomSyncResultBuilder(
|
2016-05-23 12:37:01 -04:00
|
|
|
room_id=room_id,
|
2016-05-24 04:43:35 -04:00
|
|
|
rtype="joined",
|
2016-05-23 12:37:01 -04:00
|
|
|
events=[],
|
2019-03-06 12:29:15 -05:00
|
|
|
newly_joined=newly_joined,
|
2016-05-23 12:37:01 -04:00
|
|
|
full_state=False,
|
|
|
|
since_token=since_token,
|
|
|
|
upto_token=since_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token=now_token,
|
2019-03-06 12:29:15 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
room_entries.append(entry)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-06-09 14:39:51 -04:00
|
|
|
return _RoomChanges(
|
|
|
|
room_entries,
|
|
|
|
invited,
|
|
|
|
knocked,
|
|
|
|
newly_joined_rooms,
|
|
|
|
newly_left_rooms,
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2023-01-23 10:44:39 -05:00
|
|
|
async def _get_room_changes_for_initial_sync(
|
2022-03-30 05:43:04 -04:00
|
|
|
self,
|
|
|
|
sync_result_builder: "SyncResultBuilder",
|
|
|
|
ignored_users: FrozenSet[str],
|
2020-02-03 13:05:44 -05:00
|
|
|
) -> _RoomChanges:
|
2016-05-24 05:14:53 -04:00
|
|
|
"""Returns entries for all rooms for the user.
|
|
|
|
|
2021-12-02 15:58:32 -05:00
|
|
|
Like `_get_rooms_changed`, but assumes the `since_token` is `None`.
|
|
|
|
|
|
|
|
This function does not modify the sync_result_builder.
|
|
|
|
|
2016-05-24 05:14:53 -04:00
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_result_builder
|
|
|
|
ignored_users: Set of users ignored by user.
|
2022-03-30 05:43:04 -04:00
|
|
|
ignored_rooms: List of rooms to ignore.
|
2016-05-24 05:14:53 -04:00
|
|
|
"""
|
|
|
|
|
2016-05-24 05:53:03 -04:00
|
|
|
user_id = sync_result_builder.sync_config.user.to_string()
|
|
|
|
since_token = sync_result_builder.since_token
|
|
|
|
now_token = sync_result_builder.now_token
|
|
|
|
sync_config = sync_result_builder.sync_config
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2020-01-15 09:59:33 -05:00
|
|
|
room_list = await self.store.get_rooms_for_local_user_where_membership_is(
|
2021-12-02 15:58:32 -05:00
|
|
|
user_id=user_id,
|
|
|
|
membership_list=Membership.LIST,
|
2023-01-23 10:44:39 -05:00
|
|
|
excluded_rooms=sync_result_builder.excluded_room_ids,
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
|
2016-05-24 04:43:35 -04:00
|
|
|
room_entries = []
|
2016-05-23 12:37:01 -04:00
|
|
|
invited = []
|
2021-06-09 14:39:51 -04:00
|
|
|
knocked = []
|
2016-05-23 12:37:01 -04:00
|
|
|
|
|
|
|
for event in room_list:
|
2021-08-19 11:12:55 -04:00
|
|
|
if event.room_version_id not in KNOWN_ROOM_VERSIONS:
|
|
|
|
continue
|
|
|
|
|
2016-05-23 12:37:01 -04:00
|
|
|
if event.membership == Membership.JOIN:
|
2016-05-24 04:43:35 -04:00
|
|
|
room_entries.append(
|
|
|
|
RoomSyncResultBuilder(
|
2016-05-23 12:37:01 -04:00
|
|
|
room_id=event.room_id,
|
2016-05-24 04:43:35 -04:00
|
|
|
rtype="joined",
|
2016-05-23 12:37:01 -04:00
|
|
|
events=None,
|
|
|
|
newly_joined=False,
|
|
|
|
full_state=True,
|
|
|
|
since_token=since_token,
|
|
|
|
upto_token=now_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token=now_token,
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
elif event.membership == Membership.INVITE:
|
|
|
|
if event.sender in ignored_users:
|
|
|
|
continue
|
2019-12-05 12:58:25 -05:00
|
|
|
invite = await self.store.get_event(event.event_id)
|
2016-05-23 12:37:01 -04:00
|
|
|
invited.append(InvitedSyncResult(room_id=event.room_id, invite=invite))
|
2021-06-09 14:39:51 -04:00
|
|
|
elif event.membership == Membership.KNOCK:
|
|
|
|
knock = await self.store.get_event(event.event_id)
|
|
|
|
knocked.append(KnockedSyncResult(room_id=event.room_id, knock=knock))
|
2016-05-23 12:37:01 -04:00
|
|
|
elif event.membership in (Membership.LEAVE, Membership.BAN):
|
2021-06-09 14:39:51 -04:00
|
|
|
# Always send down rooms we were banned from or kicked from.
|
2016-05-23 12:37:01 -04:00
|
|
|
if not sync_config.filter_collection.include_leave:
|
|
|
|
if event.membership == Membership.LEAVE:
|
|
|
|
if user_id == event.sender:
|
|
|
|
continue
|
|
|
|
|
|
|
|
leave_token = now_token.copy_and_replace(
|
2024-06-04 13:58:03 -04:00
|
|
|
StreamKeyType.ROOM, RoomStreamToken(stream=event.event_pos.stream)
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
2016-05-24 04:43:35 -04:00
|
|
|
room_entries.append(
|
|
|
|
RoomSyncResultBuilder(
|
2016-05-23 12:37:01 -04:00
|
|
|
room_id=event.room_id,
|
2016-05-24 04:43:35 -04:00
|
|
|
rtype="archived",
|
2016-05-23 12:37:01 -04:00
|
|
|
events=None,
|
|
|
|
newly_joined=False,
|
|
|
|
full_state=True,
|
|
|
|
since_token=since_token,
|
|
|
|
upto_token=leave_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token=leave_token,
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
|
|
|
|
2021-06-09 14:39:51 -04:00
|
|
|
return _RoomChanges(room_entries, invited, knocked, [], [])
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2019-12-05 12:58:25 -05:00
|
|
|
async def _generate_room_entry(
|
2016-05-24 05:53:03 -04:00
|
|
|
self,
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_result_builder: "SyncResultBuilder",
|
|
|
|
room_builder: "RoomSyncResultBuilder",
|
|
|
|
ephemeral: List[JsonDict],
|
2023-09-18 09:55:04 -04:00
|
|
|
tags: Optional[Mapping[str, JsonMapping]],
|
|
|
|
account_data: Mapping[str, JsonMapping],
|
2020-02-03 13:05:44 -05:00
|
|
|
always_include: bool = False,
|
2021-09-20 08:56:23 -04:00
|
|
|
) -> None:
|
2016-05-24 05:53:03 -04:00
|
|
|
"""Populates the `joined` and `archived` section of `sync_result_builder`
|
2016-05-24 05:14:53 -04:00
|
|
|
based on the `room_builder`.
|
|
|
|
|
2021-12-10 14:19:48 -05:00
|
|
|
Ideally, we want to report all events whose stream ordering `s` lies in the
|
|
|
|
range `since_token < s <= now_token`, where the two tokens are read from the
|
|
|
|
sync_result_builder.
|
|
|
|
|
|
|
|
If there are too many events in that range to report, things get complicated.
|
|
|
|
In this situation we return a truncated list of the most recent events, and
|
|
|
|
indicate in the response that there is a "gap" of omitted events. Lots of this
|
|
|
|
is handled in `_load_filtered_recents`, but some of is handled in this method.
|
|
|
|
|
|
|
|
Additionally:
|
|
|
|
- we include a "state_delta", to describe the changes in state over the gap,
|
|
|
|
- we include all membership events applying to the user making the request,
|
|
|
|
even those in the gap.
|
|
|
|
|
|
|
|
See the spec for the rationale:
|
|
|
|
https://spec.matrix.org/v1.1/client-server-api/#syncing
|
|
|
|
|
2016-05-24 05:14:53 -04:00
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_result_builder
|
|
|
|
room_builder
|
|
|
|
ephemeral: List of new ephemeral events for room
|
|
|
|
tags: List of *all* tags for room, or None if there has been
|
2016-05-24 05:14:53 -04:00
|
|
|
no change.
|
2020-02-03 13:05:44 -05:00
|
|
|
account_data: List of new account data for room
|
|
|
|
always_include: Always include this room in the sync response,
|
2016-05-24 05:14:53 -04:00
|
|
|
even if empty.
|
|
|
|
"""
|
2016-05-23 12:37:01 -04:00
|
|
|
newly_joined = room_builder.newly_joined
|
|
|
|
full_state = (
|
2016-05-24 05:53:03 -04:00
|
|
|
room_builder.full_state or newly_joined or sync_result_builder.full_state
|
2016-05-23 12:37:01 -04:00
|
|
|
)
|
2016-05-24 09:00:43 -04:00
|
|
|
events = room_builder.events
|
|
|
|
|
|
|
|
# We want to shortcut out as early as possible.
|
|
|
|
if not (always_include or account_data or ephemeral or full_state):
|
|
|
|
if events == [] and tags is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
now_token = sync_result_builder.now_token
|
|
|
|
sync_config = sync_result_builder.sync_config
|
|
|
|
|
|
|
|
room_id = room_builder.room_id
|
2016-05-23 12:37:01 -04:00
|
|
|
since_token = room_builder.since_token
|
|
|
|
upto_token = room_builder.upto_token
|
|
|
|
|
2021-12-21 06:10:36 -05:00
|
|
|
with start_active_span("sync.generate_room_entry"):
|
2021-09-15 12:14:25 -04:00
|
|
|
set_tag("room_id", room_id)
|
|
|
|
log_kv({"events": len(events or ())})
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"since_token": since_token,
|
|
|
|
"upto_token": upto_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
"end_token": room_builder.end_token,
|
2021-09-15 12:14:25 -04:00
|
|
|
}
|
|
|
|
)
|
2019-08-06 07:55:36 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
batch = await self._load_filtered_recents(
|
2019-03-06 12:29:15 -05:00
|
|
|
room_id,
|
2023-10-19 10:04:18 -04:00
|
|
|
sync_result_builder,
|
2021-09-15 12:14:25 -04:00
|
|
|
sync_config,
|
2023-10-19 10:04:18 -04:00
|
|
|
upto_token=upto_token,
|
2021-09-15 12:14:25 -04:00
|
|
|
since_token=since_token,
|
|
|
|
potential_recents=events,
|
|
|
|
newly_joined_room=newly_joined,
|
|
|
|
)
|
|
|
|
log_kv(
|
|
|
|
{
|
|
|
|
"batch_events": len(batch.events),
|
|
|
|
"prev_batch": batch.prev_batch,
|
|
|
|
"batch_limited": batch.limited,
|
|
|
|
}
|
2019-03-06 12:29:15 -05:00
|
|
|
)
|
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
# Note: `batch` can be both empty and limited here in the case where
|
|
|
|
# `_load_filtered_recents` can't find any events the user should see
|
|
|
|
# (e.g. due to having ignored the sender of the last 50 events).
|
2018-09-06 11:46:51 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
# When we join the room (or the client requests full_state), we should
|
|
|
|
# send down any existing tags. Usually the user won't have tags in a
|
|
|
|
# newly joined room, unless either a) they've joined before or b) the
|
|
|
|
# tag was added by synapse e.g. for server notice rooms.
|
|
|
|
if full_state:
|
|
|
|
user_id = sync_result_builder.sync_config.user.to_string()
|
|
|
|
tags = await self.store.get_tags_for_room(user_id, room_id)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
# If there aren't any tags, don't send the empty tags list down
|
|
|
|
# sync
|
|
|
|
if not tags:
|
|
|
|
tags = None
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
account_data_events = []
|
|
|
|
if tags is not None:
|
2023-01-13 09:57:43 -05:00
|
|
|
account_data_events.append(
|
|
|
|
{"type": AccountDataTypes.TAG, "content": {"tags": tags}}
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
for account_data_type, content in account_data.items():
|
|
|
|
account_data_events.append(
|
|
|
|
{"type": account_data_type, "content": content}
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
account_data_events = (
|
2021-11-09 08:10:58 -05:00
|
|
|
await sync_config.filter_collection.filter_room_account_data(
|
2021-09-15 12:14:25 -04:00
|
|
|
account_data_events
|
|
|
|
)
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-11-09 08:10:58 -05:00
|
|
|
ephemeral = await sync_config.filter_collection.filter_room_ephemeral(
|
|
|
|
ephemeral
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
if not (
|
|
|
|
always_include
|
|
|
|
or batch
|
|
|
|
or account_data_events
|
|
|
|
or ephemeral
|
|
|
|
or full_state
|
|
|
|
):
|
|
|
|
return
|
2018-08-16 04:46:50 -04:00
|
|
|
|
2022-04-07 10:54:16 -04:00
|
|
|
if not room_builder.out_of_band:
|
|
|
|
state = await self.compute_state_delta(
|
|
|
|
room_id,
|
|
|
|
batch,
|
|
|
|
sync_config,
|
|
|
|
since_token,
|
2024-04-04 07:47:59 -04:00
|
|
|
room_builder.end_token,
|
2022-04-07 10:54:16 -04:00
|
|
|
full_state=full_state,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# An out of band room won't have any state changes.
|
|
|
|
state = {}
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
summary: Optional[JsonDict] = {}
|
|
|
|
|
|
|
|
# we include a summary in room responses when we're lazy loading
|
|
|
|
# members (as the client otherwise doesn't have enough info to form
|
|
|
|
# the name itself).
|
2022-04-07 10:54:16 -04:00
|
|
|
if (
|
|
|
|
not room_builder.out_of_band
|
|
|
|
and sync_config.filter_collection.lazy_load_members()
|
|
|
|
and (
|
|
|
|
# we recalculate the summary:
|
|
|
|
# if there are membership changes in the timeline, or
|
|
|
|
# if membership has changed during a gappy sync, or
|
|
|
|
# if this is an initial sync.
|
|
|
|
any(ev.type == EventTypes.Member for ev in batch.events)
|
|
|
|
or (
|
|
|
|
# XXX: this may include false positives in the form of LL
|
|
|
|
# members which have snuck into state
|
|
|
|
batch.limited
|
|
|
|
and any(t == EventTypes.Member for (t, k) in state)
|
|
|
|
)
|
|
|
|
or since_token is None
|
2021-09-15 12:14:25 -04:00
|
|
|
)
|
|
|
|
):
|
|
|
|
summary = await self.compute_summary(
|
|
|
|
room_id, sync_config, batch, state, now_token
|
|
|
|
)
|
|
|
|
|
|
|
|
if room_builder.rtype == "joined":
|
|
|
|
unread_notifications: Dict[str, int] = {}
|
|
|
|
room_sync = JoinedSyncResult(
|
|
|
|
room_id=room_id,
|
|
|
|
timeline=batch,
|
|
|
|
state=state,
|
|
|
|
ephemeral=ephemeral,
|
|
|
|
account_data=account_data_events,
|
|
|
|
unread_notifications=unread_notifications,
|
2022-10-04 09:47:04 -04:00
|
|
|
unread_thread_notifications={},
|
2021-09-15 12:14:25 -04:00
|
|
|
summary=summary,
|
|
|
|
unread_count=0,
|
|
|
|
)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
if room_sync or always_include:
|
|
|
|
notifs = await self.unread_notifs_for_room_id(room_id, sync_config)
|
2020-09-02 12:19:37 -04:00
|
|
|
|
2022-10-04 09:47:04 -04:00
|
|
|
# Notifications for the main timeline.
|
|
|
|
notify_count = notifs.main_timeline.notify_count
|
|
|
|
highlight_count = notifs.main_timeline.highlight_count
|
|
|
|
unread_count = notifs.main_timeline.unread_count
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2022-10-04 09:47:04 -04:00
|
|
|
# Check the sync configuration.
|
2022-10-07 09:26:40 -04:00
|
|
|
if sync_config.filter_collection.unread_thread_notifications():
|
2022-10-04 09:47:04 -04:00
|
|
|
# And add info for each thread.
|
|
|
|
room_sync.unread_thread_notifications = {
|
|
|
|
thread_id: {
|
|
|
|
"notification_count": thread_notifs.notify_count,
|
|
|
|
"highlight_count": thread_notifs.highlight_count,
|
|
|
|
}
|
|
|
|
for thread_id, thread_notifs in notifs.threads.items()
|
|
|
|
if thread_id is not None
|
|
|
|
}
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Combine the unread counts for all threads and main timeline.
|
|
|
|
for thread_notifs in notifs.threads.values():
|
|
|
|
notify_count += thread_notifs.notify_count
|
|
|
|
highlight_count += thread_notifs.highlight_count
|
|
|
|
unread_count += thread_notifs.unread_count
|
|
|
|
|
|
|
|
unread_notifications["notification_count"] = notify_count
|
|
|
|
unread_notifications["highlight_count"] = highlight_count
|
|
|
|
room_sync.unread_count = unread_count
|
2018-09-11 19:50:39 -04:00
|
|
|
|
2021-09-15 12:14:25 -04:00
|
|
|
sync_result_builder.joined.append(room_sync)
|
|
|
|
|
|
|
|
if batch.limited and since_token:
|
|
|
|
user_id = sync_result_builder.sync_config.user.to_string()
|
|
|
|
logger.debug(
|
|
|
|
"Incremental gappy sync of %s for user %s with %d state events"
|
|
|
|
% (room_id, user_id, len(state))
|
|
|
|
)
|
|
|
|
elif room_builder.rtype == "archived":
|
|
|
|
archived_room_sync = ArchivedSyncResult(
|
|
|
|
room_id=room_id,
|
|
|
|
timeline=batch,
|
|
|
|
state=state,
|
|
|
|
account_data=account_data_events,
|
2018-09-11 19:50:39 -04:00
|
|
|
)
|
2021-09-15 12:14:25 -04:00
|
|
|
if archived_room_sync or always_include:
|
|
|
|
sync_result_builder.archived.append(archived_room_sync)
|
|
|
|
else:
|
|
|
|
raise Exception("Unrecognized rtype: %r", room_builder.rtype)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2016-01-19 06:35:50 -05:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
def _action_has_highlight(actions: List[JsonDict]) -> bool:
|
2016-01-19 06:35:50 -05:00
|
|
|
for action in actions:
|
|
|
|
try:
|
|
|
|
if action.get("set_tweak", None) == "highlight":
|
|
|
|
return action.get("value", True)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return False
|
2016-02-01 10:59:40 -05:00
|
|
|
|
|
|
|
|
2018-07-23 14:21:20 -04:00
|
|
|
def _calculate_state(
|
2020-02-03 13:05:44 -05:00
|
|
|
timeline_contains: StateMap[str],
|
|
|
|
timeline_start: StateMap[str],
|
2022-08-10 07:58:20 -04:00
|
|
|
timeline_end: StateMap[str],
|
|
|
|
previous_timeline_end: StateMap[str],
|
2020-02-03 13:05:44 -05:00
|
|
|
lazy_load_members: bool,
|
|
|
|
) -> StateMap[str]:
|
2016-02-01 10:59:40 -05:00
|
|
|
"""Works out what state to include in a sync response.
|
|
|
|
|
|
|
|
Args:
|
2020-02-03 13:05:44 -05:00
|
|
|
timeline_contains: state in the timeline
|
|
|
|
timeline_start: state at the start of the timeline
|
2022-08-10 07:58:20 -04:00
|
|
|
timeline_end: state at the end of the timeline
|
|
|
|
previous_timeline_end: state at the end of the previous sync (or empty dict
|
2016-02-01 11:52:27 -05:00
|
|
|
if this is an initial sync)
|
2020-02-03 13:05:44 -05:00
|
|
|
lazy_load_members: whether to return members from timeline_start
|
2018-07-24 07:39:40 -04:00
|
|
|
or not. assumes that timeline_start has already been filtered to
|
|
|
|
include only the members the client needs to know about.
|
2016-02-01 10:59:40 -05:00
|
|
|
"""
|
2022-08-10 07:58:20 -04:00
|
|
|
event_id_to_state_key = {
|
|
|
|
event_id: state_key
|
|
|
|
for state_key, event_id in itertools.chain(
|
2020-06-15 07:03:36 -04:00
|
|
|
timeline_contains.items(),
|
|
|
|
timeline_start.items(),
|
2022-08-10 07:58:20 -04:00
|
|
|
timeline_end.items(),
|
|
|
|
previous_timeline_end.items(),
|
2016-02-01 10:59:40 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-10 07:58:20 -04:00
|
|
|
timeline_end_ids = set(timeline_end.values())
|
|
|
|
timeline_start_ids = set(timeline_start.values())
|
|
|
|
previous_timeline_end_ids = set(previous_timeline_end.values())
|
|
|
|
timeline_contains_ids = set(timeline_contains.values())
|
2016-02-01 10:59:40 -05:00
|
|
|
|
2018-07-24 07:39:40 -04:00
|
|
|
# If we are lazyloading room members, we explicitly add the membership events
|
|
|
|
# for the senders in the timeline into the state block returned by /sync,
|
|
|
|
# as we may not have sent them to the client before. We find these membership
|
|
|
|
# events by filtering them out of timeline_start, which has already been filtered
|
|
|
|
# to only include membership events for the senders in the timeline.
|
2022-08-10 07:58:20 -04:00
|
|
|
# In practice, we can do this by removing them from the previous_timeline_end_ids
|
|
|
|
# list, which is the list of relevant state we know we have already sent to the
|
|
|
|
# client.
|
2021-12-02 15:58:32 -05:00
|
|
|
# see https://github.com/matrix-org/synapse/pull/2970/files/efcdacad7d1b7f52f879179701c7e0d9b763511f#r204732809
|
2018-07-24 07:39:40 -04:00
|
|
|
|
2018-07-23 14:21:20 -04:00
|
|
|
if lazy_load_members:
|
2022-08-10 07:58:20 -04:00
|
|
|
previous_timeline_end_ids.difference_update(
|
2020-06-15 07:03:36 -04:00
|
|
|
e for t, e in timeline_start.items() if t[0] == EventTypes.Member
|
2018-07-23 14:21:20 -04:00
|
|
|
)
|
|
|
|
|
2024-04-04 07:47:59 -04:00
|
|
|
# Naively, we would just return the difference between the state at the start
|
|
|
|
# of the timeline (`timeline_start_ids`) and that at the end of the previous sync
|
|
|
|
# (`previous_timeline_end_ids`). However, that fails in the presence of forks in
|
|
|
|
# the DAG.
|
|
|
|
#
|
|
|
|
# For example, consider a DAG such as the following:
|
|
|
|
#
|
|
|
|
# E1
|
|
|
|
# ↗ ↖
|
|
|
|
# | S2
|
|
|
|
# | ↑
|
|
|
|
# --|------|----
|
|
|
|
# | |
|
|
|
|
# E3 |
|
|
|
|
# ↖ /
|
|
|
|
# E4
|
|
|
|
#
|
|
|
|
# ... and a filter that means we only return 2 events, represented by the dashed
|
|
|
|
# horizontal line. Assuming S2 was *not* included in the previous sync, we need to
|
|
|
|
# include it in the `state` section.
|
|
|
|
#
|
|
|
|
# Note that the state at the start of the timeline (E3) does not include S2. So,
|
|
|
|
# to make sure it gets included in the calculation here, we actually look at
|
|
|
|
# the state at the *end* of the timeline, and subtract any events that are present
|
|
|
|
# in the timeline.
|
|
|
|
#
|
|
|
|
# ----------
|
|
|
|
#
|
|
|
|
# Aside 1: You may then wonder if we need to include `timeline_start` in the
|
|
|
|
# calculation. Consider a linear DAG:
|
|
|
|
#
|
|
|
|
# E1
|
|
|
|
# ↑
|
|
|
|
# S2
|
|
|
|
# ↑
|
|
|
|
# ----|------
|
|
|
|
# |
|
|
|
|
# E3
|
|
|
|
# ↑
|
|
|
|
# S4
|
|
|
|
# ↑
|
|
|
|
# E5
|
|
|
|
#
|
|
|
|
# ... where S2 and S4 change the same piece of state; and where we have a filter
|
|
|
|
# that returns 3 events (E3, S4, E5). We still need to tell the client about S2,
|
|
|
|
# because it might affect the display of E3. However, the state at the end of the
|
|
|
|
# timeline only tells us about S4; if we don't inspect `timeline_start` we won't
|
|
|
|
# find out about S2.
|
|
|
|
#
|
|
|
|
# (There are yet more complicated cases in which a state event is excluded from the
|
|
|
|
# timeline, but whose effect actually lands in the DAG in the *middle* of the
|
|
|
|
# timeline. We have no way to represent that in the /sync response, and we don't
|
|
|
|
# even try; it is ether omitted or plonked into `state` as if it were at the start
|
|
|
|
# of the timeline, depending on what else is in the timeline.)
|
|
|
|
|
2022-08-10 07:58:20 -04:00
|
|
|
state_ids = (
|
|
|
|
(timeline_end_ids | timeline_start_ids)
|
|
|
|
- previous_timeline_end_ids
|
|
|
|
- timeline_contains_ids
|
|
|
|
)
|
2016-02-01 10:59:40 -05:00
|
|
|
|
2022-08-10 07:58:20 -04:00
|
|
|
return {event_id_to_state_key[e]: e for e in state_ids}
|
2016-05-23 12:37:01 -04:00
|
|
|
|
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2020-02-03 13:05:44 -05:00
|
|
|
class SyncResultBuilder:
|
2019-03-06 12:21:08 -05:00
|
|
|
"""Used to help build up a new SyncResult for a user
|
|
|
|
|
|
|
|
Attributes:
|
2020-02-03 13:05:44 -05:00
|
|
|
sync_config
|
|
|
|
full_state: The full_state flag as specified by user
|
|
|
|
since_token: The token supplied by user, or None.
|
|
|
|
now_token: The token to sync up to.
|
|
|
|
joined_room_ids: List of rooms the user is joined to
|
2023-01-23 10:44:39 -05:00
|
|
|
excluded_room_ids: Set of room ids we should omit from the /sync response.
|
|
|
|
forced_newly_joined_room_ids:
|
|
|
|
Rooms that should be presented in the /sync response as if they were
|
|
|
|
newly joined during the sync period, even if that's not the case.
|
|
|
|
(This is useful if the room was previously excluded from a /sync response,
|
|
|
|
and now the client should be made aware of it.)
|
|
|
|
Only used by incremental syncs.
|
2019-03-06 12:21:08 -05:00
|
|
|
|
|
|
|
# The following mirror the fields in a sync response
|
2021-08-23 08:14:42 -04:00
|
|
|
presence
|
|
|
|
account_data
|
|
|
|
joined
|
|
|
|
invited
|
|
|
|
knocked
|
|
|
|
archived
|
|
|
|
to_device
|
2019-03-06 12:21:08 -05:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
sync_config: SyncConfig
|
|
|
|
full_state: bool
|
|
|
|
since_token: Optional[StreamToken]
|
|
|
|
now_token: StreamToken
|
|
|
|
joined_room_ids: FrozenSet[str]
|
2023-01-23 10:44:39 -05:00
|
|
|
excluded_room_ids: FrozenSet[str]
|
|
|
|
forced_newly_joined_room_ids: FrozenSet[str]
|
2022-10-04 11:42:59 -04:00
|
|
|
membership_change_events: List[EventBase]
|
2020-02-03 13:05:44 -05:00
|
|
|
|
2021-08-23 08:14:42 -04:00
|
|
|
presence: List[UserPresenceState] = attr.Factory(list)
|
2021-08-18 07:27:32 -04:00
|
|
|
account_data: List[JsonDict] = attr.Factory(list)
|
|
|
|
joined: List[JoinedSyncResult] = attr.Factory(list)
|
|
|
|
invited: List[InvitedSyncResult] = attr.Factory(list)
|
|
|
|
knocked: List[KnockedSyncResult] = attr.Factory(list)
|
|
|
|
archived: List[ArchivedSyncResult] = attr.Factory(list)
|
|
|
|
to_device: List[JsonDict] = attr.Factory(list)
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2022-09-08 15:13:39 -04:00
|
|
|
def calculate_user_changes(self) -> Tuple[AbstractSet[str], AbstractSet[str]]:
|
2021-12-07 07:42:05 -05:00
|
|
|
"""Work out which other users have joined or left rooms we are joined to.
|
|
|
|
|
|
|
|
This data only is only useful for an incremental sync.
|
|
|
|
|
|
|
|
The SyncResultBuilder is not modified by this function.
|
|
|
|
"""
|
|
|
|
newly_joined_or_invited_or_knocked_users = set()
|
|
|
|
newly_left_users = set()
|
|
|
|
if self.since_token:
|
|
|
|
for joined_sync in self.joined:
|
|
|
|
it = itertools.chain(
|
2024-03-14 13:34:19 -04:00
|
|
|
joined_sync.state.values(), joined_sync.timeline.events
|
2021-12-07 07:42:05 -05:00
|
|
|
)
|
|
|
|
for event in it:
|
|
|
|
if event.type == EventTypes.Member:
|
|
|
|
if (
|
|
|
|
event.membership == Membership.JOIN
|
|
|
|
or event.membership == Membership.INVITE
|
|
|
|
or event.membership == Membership.KNOCK
|
|
|
|
):
|
|
|
|
newly_joined_or_invited_or_knocked_users.add(
|
|
|
|
event.state_key
|
|
|
|
)
|
2024-03-14 13:34:19 -04:00
|
|
|
# If the user left and rejoined in the same batch, they
|
|
|
|
# count as a newly-joined user, *not* a newly-left user.
|
|
|
|
newly_left_users.discard(event.state_key)
|
2021-12-07 07:42:05 -05:00
|
|
|
else:
|
|
|
|
prev_content = event.unsigned.get("prev_content", {})
|
|
|
|
prev_membership = prev_content.get("membership", None)
|
|
|
|
if prev_membership == Membership.JOIN:
|
|
|
|
newly_left_users.add(event.state_key)
|
2024-03-14 13:34:19 -04:00
|
|
|
# If the user joined and left in the same batch, they
|
|
|
|
# count as a newly-left user, not a newly-joined user.
|
|
|
|
newly_joined_or_invited_or_knocked_users.discard(
|
|
|
|
event.state_key
|
|
|
|
)
|
2021-12-07 07:42:05 -05:00
|
|
|
|
|
|
|
return newly_joined_or_invited_or_knocked_users, newly_left_users
|
|
|
|
|
2016-05-23 12:37:01 -04:00
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2020-09-04 06:54:56 -04:00
|
|
|
class RoomSyncResultBuilder:
|
2016-05-24 05:14:53 -04:00
|
|
|
"""Stores information needed to create either a `JoinedSyncResult` or
|
|
|
|
`ArchivedSyncResult`.
|
2020-02-03 13:05:44 -05:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
room_id
|
2024-04-04 07:47:59 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
rtype: One of `"joined"` or `"archived"`
|
2024-04-04 07:47:59 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
events: List of events to include in the room (more events may be added
|
|
|
|
when generating result).
|
2024-04-04 07:47:59 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
newly_joined: If the user has newly joined the room
|
2024-04-04 07:47:59 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
full_state: Whether the full state should be sent in result
|
2024-04-04 07:47:59 -04:00
|
|
|
|
2020-02-03 13:05:44 -05:00
|
|
|
since_token: Earliest point to return events from, or None
|
2024-04-04 07:47:59 -04:00
|
|
|
|
|
|
|
upto_token: Latest point to return events from. If `events` is populated,
|
|
|
|
this is set to the token at the start of `events`
|
|
|
|
|
|
|
|
end_token: The last point in the timeline that the client should see events
|
|
|
|
from. Normally this will be the same as the global `now_token`, but in
|
|
|
|
the case of rooms where the user has left the room, this will be the point
|
|
|
|
just after their leave event.
|
|
|
|
|
|
|
|
This is used in the calculation of the state which is returned in `state`:
|
|
|
|
any state changes *up to* `end_token` (and not beyond!) which are not
|
|
|
|
reflected in the timeline need to be returned in `state`.
|
|
|
|
|
2022-04-07 10:54:16 -04:00
|
|
|
out_of_band: whether the events in the room are "out of band" events
|
|
|
|
and the server isn't in the room.
|
2016-05-24 05:14:53 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2021-08-18 07:27:32 -04:00
|
|
|
room_id: str
|
|
|
|
rtype: str
|
|
|
|
events: Optional[List[EventBase]]
|
|
|
|
newly_joined: bool
|
|
|
|
full_state: bool
|
|
|
|
since_token: Optional[StreamToken]
|
|
|
|
upto_token: StreamToken
|
2024-04-04 07:47:59 -04:00
|
|
|
end_token: StreamToken
|
2022-04-07 10:54:16 -04:00
|
|
|
out_of_band: bool = False
|