2016-09-21 06:46:28 -04:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2021-12-14 12:35:28 -05:00
|
|
|
from typing import TYPE_CHECKING, List, Optional, Tuple, cast
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2021-02-19 13:20:34 -05:00
|
|
|
from synapse.api.constants import EduTypes, EventTypes, Membership
|
2020-02-18 18:13:29 -05:00
|
|
|
from synapse.api.errors import SynapseError
|
2021-12-14 12:35:28 -05:00
|
|
|
from synapse.events import EventBase
|
2022-03-03 10:43:06 -05:00
|
|
|
from synapse.events.utils import SerializeEventConfig
|
2016-09-21 06:46:28 -04:00
|
|
|
from synapse.events.validator import EventValidator
|
2017-03-15 10:27:34 -04:00
|
|
|
from synapse.handlers.presence import format_user_presence_state
|
2021-07-28 04:05:11 -04:00
|
|
|
from synapse.handlers.receipts import ReceiptEventSource
|
2019-07-03 10:07:04 -04:00
|
|
|
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
2020-09-03 17:02:29 -04:00
|
|
|
from synapse.storage.roommember import RoomsForUser
|
2016-09-21 06:46:28 -04:00
|
|
|
from synapse.streams.config import PaginationConfig
|
2021-12-14 12:35:28 -05:00
|
|
|
from synapse.types import (
|
|
|
|
JsonDict,
|
|
|
|
Requester,
|
|
|
|
RoomStreamToken,
|
|
|
|
StateMap,
|
2022-05-16 11:35:31 -04:00
|
|
|
StreamKeyType,
|
2021-12-14 12:35:28 -05:00
|
|
|
StreamToken,
|
|
|
|
UserID,
|
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
from synapse.util import unwrapFirstError
|
2021-12-14 12:35:28 -05:00
|
|
|
from synapse.util.async_helpers import concurrently_execute, gather_results
|
2019-12-09 08:42:49 -05:00
|
|
|
from synapse.util.caches.response_cache import ResponseCache
|
2016-09-21 06:46:28 -04:00
|
|
|
from synapse.visibility import filter_events_for_client
|
|
|
|
|
2020-09-03 17:02:29 -04:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
|
2016-09-21 06:46:28 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-10-08 07:44:43 -04:00
|
|
|
class InitialSyncHandler:
|
2020-09-03 17:02:29 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2021-10-08 07:44:43 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.state_handler = hs.get_state_handler()
|
2016-09-21 06:46:28 -04:00
|
|
|
self.hs = hs
|
|
|
|
self.state = hs.get_state_handler()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.validator = EventValidator()
|
2021-07-16 13:22:36 -04:00
|
|
|
self.snapshot_cache: ResponseCache[
|
|
|
|
Tuple[
|
|
|
|
str,
|
|
|
|
Optional[StreamToken],
|
|
|
|
Optional[StreamToken],
|
|
|
|
str,
|
|
|
|
Optional[int],
|
|
|
|
bool,
|
|
|
|
bool,
|
|
|
|
]
|
|
|
|
] = ResponseCache(hs.get_clock(), "initial_sync_cache")
|
2019-05-09 08:21:57 -04:00
|
|
|
self._event_serializer = hs.get_event_client_serializer()
|
2022-05-31 08:17:50 -04:00
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
|
|
|
self._state_storage_controller = self._storage_controllers.state
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2020-10-09 11:35:11 -04:00
|
|
|
async def snapshot_all_rooms(
|
2019-06-20 05:32:02 -04:00
|
|
|
self,
|
2020-09-03 17:02:29 -04:00
|
|
|
user_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
|
|
|
as_client_event: bool = True,
|
|
|
|
include_archived: bool = False,
|
|
|
|
) -> JsonDict:
|
2016-09-21 06:46:28 -04:00
|
|
|
"""Retrieve a snapshot of all rooms the user is invited or has joined.
|
|
|
|
|
|
|
|
This snapshot may include messages for all rooms where the user is
|
|
|
|
joined, depending on the pagination config.
|
|
|
|
|
|
|
|
Args:
|
2020-09-03 17:02:29 -04:00
|
|
|
user_id: The ID of the user making the request.
|
|
|
|
pagin_config: The pagination config used to determine how many
|
|
|
|
messages *PER ROOM* to return.
|
|
|
|
as_client_event: True to get events in client-server format.
|
|
|
|
include_archived: True to get rooms that the user has left
|
2016-09-21 06:46:28 -04:00
|
|
|
Returns:
|
2020-09-03 17:02:29 -04:00
|
|
|
A JsonDict with the same format as the response to `/intialSync`
|
|
|
|
API
|
2016-09-21 06:46:28 -04:00
|
|
|
"""
|
|
|
|
key = (
|
|
|
|
user_id,
|
|
|
|
pagin_config.from_token,
|
|
|
|
pagin_config.to_token,
|
|
|
|
pagin_config.direction,
|
|
|
|
pagin_config.limit,
|
|
|
|
as_client_event,
|
|
|
|
include_archived,
|
|
|
|
)
|
|
|
|
|
2020-10-09 11:35:11 -04:00
|
|
|
return await self.snapshot_cache.wrap(
|
2019-06-20 05:32:02 -04:00
|
|
|
key,
|
2019-12-09 08:42:49 -05:00
|
|
|
self._snapshot_all_rooms,
|
|
|
|
user_id,
|
|
|
|
pagin_config,
|
|
|
|
as_client_event,
|
|
|
|
include_archived,
|
2019-06-20 05:32:02 -04:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
async def _snapshot_all_rooms(
|
2019-06-20 05:32:02 -04:00
|
|
|
self,
|
2020-09-03 17:02:29 -04:00
|
|
|
user_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
|
|
|
as_client_event: bool = True,
|
|
|
|
include_archived: bool = False,
|
|
|
|
) -> JsonDict:
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
memberships = [Membership.INVITE, Membership.JOIN]
|
|
|
|
if include_archived:
|
|
|
|
memberships.append(Membership.LEAVE)
|
|
|
|
|
2020-01-15 09:59:33 -05:00
|
|
|
room_list = await self.store.get_rooms_for_local_user_where_membership_is(
|
2016-09-21 06:46:28 -04:00
|
|
|
user_id=user_id, membership_list=memberships
|
|
|
|
)
|
|
|
|
|
|
|
|
user = UserID.from_string(user_id)
|
|
|
|
|
|
|
|
rooms_ret = []
|
|
|
|
|
2020-08-04 07:21:47 -04:00
|
|
|
now_token = self.hs.get_event_sources().get_current_token()
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2021-09-21 13:34:26 -04:00
|
|
|
presence_stream = self.hs.get_event_sources().sources.presence
|
2020-09-08 10:00:17 -04:00
|
|
|
presence, _ = await presence_stream.get_new_events(
|
|
|
|
user, from_key=None, include_offline=False
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2020-09-08 10:00:17 -04:00
|
|
|
joined_rooms = [r.room_id for r in room_list if r.membership == Membership.JOIN]
|
|
|
|
receipt = await self.store.get_linearized_receipts_for_rooms(
|
2021-02-16 17:32:34 -05:00
|
|
|
joined_rooms,
|
|
|
|
to_key=int(now_token.receipt_key),
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
2021-07-28 04:05:11 -04:00
|
|
|
if self.hs.config.experimental.msc2285_enabled:
|
2022-05-16 11:06:23 -04:00
|
|
|
receipt = ReceiptEventSource.filter_out_private_receipts(receipt, user_id)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
tags_by_room = await self.store.get_tags_for_user(user_id)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
account_data, account_data_by_room = await self.store.get_account_data_for_user(
|
2019-10-31 11:43:24 -04:00
|
|
|
user_id
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
public_room_ids = await self.store.get_public_room_ids()
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2022-03-08 10:58:14 -05:00
|
|
|
if pagin_config.limit is not None:
|
|
|
|
limit = pagin_config.limit
|
|
|
|
else:
|
2016-09-21 06:46:28 -04:00
|
|
|
limit = 10
|
|
|
|
|
2022-03-03 10:43:06 -05:00
|
|
|
serializer_options = SerializeEventConfig(as_client_event=as_client_event)
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def handle_room(event: RoomsForUser) -> None:
|
2021-08-18 09:22:07 -04:00
|
|
|
d: JsonDict = {
|
2016-09-21 06:46:28 -04:00
|
|
|
"room_id": event.room_id,
|
|
|
|
"membership": event.membership,
|
|
|
|
"visibility": (
|
2019-06-20 05:32:02 -04:00
|
|
|
"public" if event.room_id in public_room_ids else "private"
|
2016-09-21 06:46:28 -04:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.membership == Membership.INVITE:
|
|
|
|
time_now = self.clock.time_msec()
|
|
|
|
d["inviter"] = event.sender
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
invite_event = await self.store.get_event(event.event_id)
|
2022-01-07 09:10:46 -05:00
|
|
|
d["invite"] = self._event_serializer.serialize_event(
|
2021-12-06 10:51:15 -05:00
|
|
|
invite_event,
|
|
|
|
time_now,
|
2022-03-03 10:43:06 -05:00
|
|
|
config=serializer_options,
|
2019-05-09 08:21:57 -04:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
rooms_ret.append(d)
|
|
|
|
|
|
|
|
if event.membership not in (Membership.JOIN, Membership.LEAVE):
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
if event.membership == Membership.JOIN:
|
|
|
|
room_end_token = now_token.room_key
|
2018-07-23 19:57:48 -04:00
|
|
|
deferred_room_state = run_in_background(
|
2022-06-06 04:24:12 -04:00
|
|
|
self._state_storage_controller.get_current_state, event.room_id
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
elif event.membership == Membership.LEAVE:
|
2021-02-16 17:32:34 -05:00
|
|
|
room_end_token = RoomStreamToken(
|
|
|
|
None,
|
|
|
|
event.stream_ordering,
|
|
|
|
)
|
2018-07-23 19:57:48 -04:00
|
|
|
deferred_room_state = run_in_background(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._state_storage_controller.get_state_for_events,
|
|
|
|
[event.event_id],
|
2021-12-14 12:35:28 -05:00
|
|
|
).addCallback(
|
|
|
|
lambda states: cast(StateMap[EventBase], states[event.event_id])
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
(messages, token), current_state = await make_deferred_yieldable(
|
2021-12-14 12:35:28 -05:00
|
|
|
gather_results(
|
|
|
|
(
|
2018-04-27 06:29:27 -04:00
|
|
|
run_in_background(
|
|
|
|
self.store.get_recent_events_for_room,
|
2016-09-21 06:46:28 -04:00
|
|
|
event.room_id,
|
|
|
|
limit=limit,
|
|
|
|
end_token=room_end_token,
|
|
|
|
),
|
|
|
|
deferred_room_state,
|
2021-12-14 12:35:28 -05:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
).addErrback(unwrapFirstError)
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
messages = await filter_events_for_client(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._storage_controllers, user_id, messages
|
2019-10-23 12:25:54 -04:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2022-05-16 11:35:31 -04:00
|
|
|
start_token = now_token.copy_and_replace(StreamKeyType.ROOM, token)
|
|
|
|
end_token = now_token.copy_and_replace(
|
|
|
|
StreamKeyType.ROOM, room_end_token
|
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
time_now = self.clock.time_msec()
|
|
|
|
|
|
|
|
d["messages"] = {
|
2019-05-09 08:21:57 -04:00
|
|
|
"chunk": (
|
2022-01-07 09:10:46 -05:00
|
|
|
self._event_serializer.serialize_events(
|
2021-12-06 10:51:15 -05:00
|
|
|
messages,
|
|
|
|
time_now=time_now,
|
2022-03-03 10:43:06 -05:00
|
|
|
config=serializer_options,
|
2019-05-09 08:21:57 -04:00
|
|
|
)
|
|
|
|
),
|
2020-09-30 15:29:19 -04:00
|
|
|
"start": await start_token.to_string(self.store),
|
|
|
|
"end": await end_token.to_string(self.store),
|
2016-09-21 06:46:28 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 09:10:46 -05:00
|
|
|
d["state"] = self._event_serializer.serialize_events(
|
2019-05-09 08:21:57 -04:00
|
|
|
current_state.values(),
|
|
|
|
time_now=time_now,
|
2022-03-03 10:43:06 -05:00
|
|
|
config=serializer_options,
|
2019-05-09 08:21:57 -04:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
account_data_events = []
|
|
|
|
tags = tags_by_room.get(event.room_id)
|
|
|
|
if tags:
|
2019-06-20 05:32:02 -04:00
|
|
|
account_data_events.append(
|
|
|
|
{"type": "m.tag", "content": {"tags": tags}}
|
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
account_data = account_data_by_room.get(event.room_id, {})
|
|
|
|
for account_data_type, content in account_data.items():
|
2019-06-20 05:32:02 -04:00
|
|
|
account_data_events.append(
|
|
|
|
{"type": account_data_type, "content": content}
|
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
d["account_data"] = account_data_events
|
2017-10-23 10:52:32 -04:00
|
|
|
except Exception:
|
2016-09-21 06:46:28 -04:00
|
|
|
logger.exception("Failed to get snapshot")
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
await concurrently_execute(handle_room, room_list, 10)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
account_data_events = []
|
|
|
|
for account_data_type, content in account_data.items():
|
2019-06-20 05:32:02 -04:00
|
|
|
account_data_events.append({"type": account_data_type, "content": content})
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2017-03-15 10:27:34 -04:00
|
|
|
now = self.clock.time_msec()
|
|
|
|
|
2016-09-21 06:46:28 -04:00
|
|
|
ret = {
|
|
|
|
"rooms": rooms_ret,
|
2017-03-15 10:27:34 -04:00
|
|
|
"presence": [
|
|
|
|
{
|
2022-05-27 07:14:36 -04:00
|
|
|
"type": EduTypes.PRESENCE,
|
2017-03-15 10:27:34 -04:00
|
|
|
"content": format_user_presence_state(event, now),
|
|
|
|
}
|
|
|
|
for event in presence
|
|
|
|
],
|
2016-09-21 06:46:28 -04:00
|
|
|
"account_data": account_data_events,
|
|
|
|
"receipts": receipt,
|
2020-09-30 15:29:19 -04:00
|
|
|
"end": await now_token.to_string(self.store),
|
2016-09-21 06:46:28 -04:00
|
|
|
}
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return ret
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2020-09-03 17:02:29 -04:00
|
|
|
async def room_initial_sync(
|
|
|
|
self, requester: Requester, room_id: str, pagin_config: PaginationConfig
|
|
|
|
) -> JsonDict:
|
2016-09-21 06:46:28 -04:00
|
|
|
"""Capture the a snapshot of a room. If user is currently a member of
|
|
|
|
the room this will be what is currently in the room. If the user left
|
|
|
|
the room this will be what was in the room when they left.
|
|
|
|
|
|
|
|
Args:
|
2020-09-03 17:02:29 -04:00
|
|
|
requester: The user to get a snapshot for.
|
|
|
|
room_id: The room to get a snapshot of.
|
|
|
|
pagin_config: The pagination config used to determine how many
|
|
|
|
messages to return.
|
2016-09-21 06:46:28 -04:00
|
|
|
Raises:
|
|
|
|
AuthError if the user wasn't in the room.
|
|
|
|
Returns:
|
|
|
|
A JSON serialisable dict with the snapshot of the room.
|
|
|
|
"""
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
blocked = await self.store.is_room_blocked(room_id)
|
2019-03-21 07:20:13 -04:00
|
|
|
if blocked:
|
|
|
|
raise SynapseError(403, "This room has been blocked on this server")
|
|
|
|
|
2016-09-21 06:46:28 -04:00
|
|
|
user_id = requester.user.to_string()
|
|
|
|
|
2020-02-18 18:13:29 -05:00
|
|
|
(
|
|
|
|
membership,
|
|
|
|
member_event_id,
|
2020-02-18 18:14:57 -05:00
|
|
|
) = await self.auth.check_user_in_room_or_world_readable(
|
2021-02-16 17:32:34 -05:00
|
|
|
room_id,
|
|
|
|
user_id,
|
|
|
|
allow_departed_users=True,
|
2020-02-18 18:14:57 -05:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
is_peeking = member_event_id is None
|
|
|
|
|
|
|
|
if membership == Membership.JOIN:
|
2019-12-09 08:46:45 -05:00
|
|
|
result = await self._room_initial_sync_joined(
|
2016-09-21 06:46:28 -04:00
|
|
|
user_id, room_id, pagin_config, membership, is_peeking
|
|
|
|
)
|
|
|
|
elif membership == Membership.LEAVE:
|
2020-10-21 06:44:31 -04:00
|
|
|
# The member_event_id will always be available if membership is set
|
|
|
|
# to leave.
|
|
|
|
assert member_event_id
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
result = await self._room_initial_sync_parted(
|
2016-09-21 06:46:28 -04:00
|
|
|
user_id, room_id, pagin_config, membership, member_event_id, is_peeking
|
|
|
|
)
|
|
|
|
|
|
|
|
account_data_events = []
|
2019-12-09 08:46:45 -05:00
|
|
|
tags = await self.store.get_tags_for_room(user_id, room_id)
|
2016-09-21 06:46:28 -04:00
|
|
|
if tags:
|
2019-06-20 05:32:02 -04:00
|
|
|
account_data_events.append({"type": "m.tag", "content": {"tags": tags}})
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
account_data = await self.store.get_account_data_for_room(user_id, room_id)
|
2016-09-21 06:46:28 -04:00
|
|
|
for account_data_type, content in account_data.items():
|
2019-06-20 05:32:02 -04:00
|
|
|
account_data_events.append({"type": account_data_type, "content": content})
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
result["account_data"] = account_data_events
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return result
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
async def _room_initial_sync_parted(
|
2020-09-03 17:02:29 -04:00
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
room_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
2020-10-21 06:44:31 -04:00
|
|
|
membership: str,
|
2020-09-03 17:02:29 -04:00
|
|
|
member_event_id: str,
|
|
|
|
is_peeking: bool,
|
|
|
|
) -> JsonDict:
|
2022-05-31 08:17:50 -04:00
|
|
|
room_state = await self._state_storage_controller.get_state_for_event(
|
|
|
|
member_event_id
|
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
limit = pagin_config.limit if pagin_config else None
|
|
|
|
if limit is None:
|
|
|
|
limit = 10
|
|
|
|
|
2020-09-29 16:48:33 -04:00
|
|
|
leave_position = await self.store.get_position_for_event(member_event_id)
|
|
|
|
stream_token = leave_position.to_room_stream_token()
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
messages, token = await self.store.get_recent_events_for_room(
|
2019-06-20 05:32:02 -04:00
|
|
|
room_id, limit=limit, end_token=stream_token
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
messages = await filter_events_for_client(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._storage_controllers, user_id, messages, is_peeking=is_peeking
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2022-05-16 11:35:31 -04:00
|
|
|
start_token = StreamToken.START.copy_and_replace(StreamKeyType.ROOM, token)
|
|
|
|
end_token = StreamToken.START.copy_and_replace(StreamKeyType.ROOM, stream_token)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
time_now = self.clock.time_msec()
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return {
|
|
|
|
"membership": membership,
|
|
|
|
"room_id": room_id,
|
|
|
|
"messages": {
|
|
|
|
"chunk": (
|
2021-12-06 10:51:15 -05:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2022-01-07 09:10:46 -05:00
|
|
|
self._event_serializer.serialize_events(messages, time_now)
|
2019-06-20 05:32:02 -04:00
|
|
|
),
|
2020-09-30 15:29:19 -04:00
|
|
|
"start": await start_token.to_string(self.store),
|
|
|
|
"end": await end_token.to_string(self.store),
|
2019-07-23 09:00:55 -04:00
|
|
|
},
|
|
|
|
"state": (
|
2021-12-06 10:51:15 -05:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2022-01-07 09:10:46 -05:00
|
|
|
self._event_serializer.serialize_events(room_state.values(), time_now)
|
2019-07-23 09:00:55 -04:00
|
|
|
),
|
|
|
|
"presence": [],
|
|
|
|
"receipts": [],
|
|
|
|
}
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
async def _room_initial_sync_joined(
|
2020-09-03 17:02:29 -04:00
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
room_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
2020-10-21 06:44:31 -04:00
|
|
|
membership: str,
|
2020-09-03 17:02:29 -04:00
|
|
|
is_peeking: bool,
|
|
|
|
) -> JsonDict:
|
2022-06-06 04:24:12 -04:00
|
|
|
current_state = await self._storage_controllers.state.get_current_state(
|
|
|
|
room_id=room_id
|
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
# TODO: These concurrently
|
|
|
|
time_now = self.clock.time_msec()
|
2021-12-06 10:51:15 -05:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2022-01-07 09:10:46 -05:00
|
|
|
state = self._event_serializer.serialize_events(
|
2021-12-20 14:14:38 -05:00
|
|
|
current_state.values(), time_now
|
2019-05-09 08:21:57 -04:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2020-08-04 07:21:47 -04:00
|
|
|
now_token = self.hs.get_event_sources().get_current_token()
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
limit = pagin_config.limit if pagin_config else None
|
|
|
|
if limit is None:
|
|
|
|
limit = 10
|
|
|
|
|
|
|
|
room_members = [
|
2019-06-20 05:32:02 -04:00
|
|
|
m
|
|
|
|
for m in current_state.values()
|
2016-09-21 06:46:28 -04:00
|
|
|
if m.type == EventTypes.Member
|
|
|
|
and m.content["membership"] == Membership.JOIN
|
|
|
|
]
|
|
|
|
|
|
|
|
presence_handler = self.hs.get_presence_handler()
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def get_presence() -> List[JsonDict]:
|
2018-08-17 11:08:45 -04:00
|
|
|
# If presence is disabled, return an empty list
|
2021-09-15 08:34:52 -04:00
|
|
|
if not self.hs.config.server.use_presence:
|
2019-07-23 09:00:55 -04:00
|
|
|
return []
|
2018-08-17 11:08:45 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
states = await presence_handler.get_states(
|
2020-04-22 17:39:04 -04:00
|
|
|
[m.user_id for m in room_members]
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2020-04-22 17:39:04 -04:00
|
|
|
return [
|
|
|
|
{
|
2022-05-27 07:14:36 -04:00
|
|
|
"type": EduTypes.PRESENCE,
|
2020-04-22 17:39:04 -04:00
|
|
|
"content": format_user_presence_state(s, time_now),
|
|
|
|
}
|
|
|
|
for s in states
|
|
|
|
]
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
async def get_receipts() -> List[JsonDict]:
|
2019-12-09 08:46:45 -05:00
|
|
|
receipts = await self.store.get_linearized_receipts_for_room(
|
2019-06-20 05:32:02 -04:00
|
|
|
room_id, to_key=now_token.receipt_key
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
2016-11-23 10:14:24 -05:00
|
|
|
if not receipts:
|
2021-07-28 04:05:11 -04:00
|
|
|
return []
|
|
|
|
if self.hs.config.experimental.msc2285_enabled:
|
2022-05-16 11:06:23 -04:00
|
|
|
receipts = ReceiptEventSource.filter_out_private_receipts(
|
|
|
|
receipts, user_id
|
|
|
|
)
|
2019-07-23 09:00:55 -04:00
|
|
|
return receipts
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
presence, receipts, (messages, token) = await make_deferred_yieldable(
|
2021-12-14 12:35:28 -05:00
|
|
|
gather_results(
|
|
|
|
(
|
2018-07-23 19:37:17 -04:00
|
|
|
run_in_background(get_presence),
|
|
|
|
run_in_background(get_receipts),
|
|
|
|
run_in_background(
|
|
|
|
self.store.get_recent_events_for_room,
|
|
|
|
room_id,
|
|
|
|
limit=limit,
|
|
|
|
end_token=now_token.room_key,
|
2019-06-20 05:32:02 -04:00
|
|
|
),
|
2021-12-14 12:35:28 -05:00
|
|
|
),
|
2018-07-23 19:37:17 -04:00
|
|
|
consumeErrors=True,
|
2019-06-20 05:32:02 -04:00
|
|
|
).addErrback(unwrapFirstError)
|
2018-07-23 19:37:17 -04:00
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
messages = await filter_events_for_client(
|
2022-05-31 08:17:50 -04:00
|
|
|
self._storage_controllers, user_id, messages, is_peeking=is_peeking
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2022-05-16 11:35:31 -04:00
|
|
|
start_token = now_token.copy_and_replace(StreamKeyType.ROOM, token)
|
2018-05-09 06:55:34 -04:00
|
|
|
end_token = now_token
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
time_now = self.clock.time_msec()
|
|
|
|
|
|
|
|
ret = {
|
|
|
|
"room_id": room_id,
|
|
|
|
"messages": {
|
2019-06-20 05:32:02 -04:00
|
|
|
"chunk": (
|
2021-12-06 10:51:15 -05:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2022-01-07 09:10:46 -05:00
|
|
|
self._event_serializer.serialize_events(messages, time_now)
|
2019-06-20 05:32:02 -04:00
|
|
|
),
|
2020-09-30 15:29:19 -04:00
|
|
|
"start": await start_token.to_string(self.store),
|
|
|
|
"end": await end_token.to_string(self.store),
|
2016-09-21 06:46:28 -04:00
|
|
|
},
|
|
|
|
"state": state,
|
|
|
|
"presence": presence,
|
|
|
|
"receipts": receipts,
|
|
|
|
}
|
|
|
|
if not is_peeking:
|
|
|
|
ret["membership"] = membership
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return ret
|