2016-09-21 04:46:28 -06: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 00:09:20 -06:00
|
|
|
import logging
|
2023-09-05 05:14:14 -06:00
|
|
|
from typing import TYPE_CHECKING, List, Optional, Tuple
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2023-01-27 05:27:55 -07:00
|
|
|
from synapse.api.constants import (
|
|
|
|
AccountDataTypes,
|
|
|
|
Direction,
|
|
|
|
EduTypes,
|
|
|
|
EventTypes,
|
|
|
|
Membership,
|
|
|
|
)
|
2020-02-18 16:13:29 -07:00
|
|
|
from synapse.api.errors import SynapseError
|
2022-03-03 08:43:06 -07:00
|
|
|
from synapse.events.utils import SerializeEventConfig
|
2016-09-21 04:46:28 -06:00
|
|
|
from synapse.events.validator import EventValidator
|
2017-03-15 08:27:34 -06:00
|
|
|
from synapse.handlers.presence import format_user_presence_state
|
2021-07-28 02:05:11 -06:00
|
|
|
from synapse.handlers.receipts import ReceiptEventSource
|
2019-07-03 08:07:04 -06:00
|
|
|
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
2020-09-03 15:02:29 -06:00
|
|
|
from synapse.storage.roommember import RoomsForUser
|
2016-09-21 04:46:28 -06:00
|
|
|
from synapse.streams.config import PaginationConfig
|
2021-12-14 10:35:28 -07:00
|
|
|
from synapse.types import (
|
|
|
|
JsonDict,
|
|
|
|
Requester,
|
|
|
|
RoomStreamToken,
|
2022-05-16 09:35:31 -06:00
|
|
|
StreamKeyType,
|
2021-12-14 10:35:28 -07:00
|
|
|
StreamToken,
|
|
|
|
UserID,
|
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
from synapse.util import unwrapFirstError
|
2021-12-14 10:35:28 -07:00
|
|
|
from synapse.util.async_helpers import concurrently_execute, gather_results
|
2019-12-09 06:42:49 -07:00
|
|
|
from synapse.util.caches.response_cache import ResponseCache
|
2016-09-21 04:46:28 -06:00
|
|
|
from synapse.visibility import filter_events_for_client
|
|
|
|
|
2020-09-03 15:02:29 -06:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
|
2016-09-21 04:46:28 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-10-08 05:44:43 -06:00
|
|
|
class InitialSyncHandler:
|
2020-09-03 15:02:29 -06:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 04:04:02 -07:00
|
|
|
self.store = hs.get_datastores().main
|
2021-10-08 05:44:43 -06:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.state_handler = hs.get_state_handler()
|
2016-09-21 04:46:28 -06:00
|
|
|
self.hs = hs
|
|
|
|
self.state = hs.get_state_handler()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.validator = EventValidator()
|
2021-07-16 11:22:36 -06:00
|
|
|
self.snapshot_cache: ResponseCache[
|
|
|
|
Tuple[
|
2023-01-27 05:27:55 -07:00
|
|
|
str,
|
|
|
|
Optional[StreamToken],
|
|
|
|
Optional[StreamToken],
|
|
|
|
Direction,
|
|
|
|
int,
|
|
|
|
bool,
|
|
|
|
bool,
|
2021-07-16 11:22:36 -06:00
|
|
|
]
|
|
|
|
] = ResponseCache(hs.get_clock(), "initial_sync_cache")
|
2019-05-09 06:21:57 -06:00
|
|
|
self._event_serializer = hs.get_event_client_serializer()
|
2022-05-31 06:17:50 -06:00
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
|
|
|
self._state_storage_controller = self._storage_controllers.state
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2020-10-09 09:35:11 -06:00
|
|
|
async def snapshot_all_rooms(
|
2016-09-21 04:46:28 -06:00
|
|
|
self,
|
2020-09-03 15:02:29 -06:00
|
|
|
user_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
|
|
|
as_client_event: bool = True,
|
|
|
|
include_archived: bool = False,
|
|
|
|
) -> JsonDict:
|
2016-09-21 04:46:28 -06: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 15:02:29 -06: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 04:46:28 -06:00
|
|
|
Returns:
|
2020-09-03 15:02:29 -06:00
|
|
|
A JsonDict with the same format as the response to `/intialSync`
|
|
|
|
API
|
2016-09-21 04:46:28 -06: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 09:35:11 -06:00
|
|
|
return await self.snapshot_cache.wrap(
|
2016-09-21 04:46:28 -06:00
|
|
|
key,
|
2019-12-09 06:42:49 -07:00
|
|
|
self._snapshot_all_rooms,
|
|
|
|
user_id,
|
|
|
|
pagin_config,
|
|
|
|
as_client_event,
|
|
|
|
include_archived,
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
async def _snapshot_all_rooms(
|
2016-09-21 04:46:28 -06:00
|
|
|
self,
|
2020-09-03 15:02:29 -06:00
|
|
|
user_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
|
|
|
as_client_event: bool = True,
|
|
|
|
include_archived: bool = False,
|
|
|
|
) -> JsonDict:
|
2016-09-21 04:46:28 -06:00
|
|
|
memberships = [Membership.INVITE, Membership.JOIN]
|
|
|
|
if include_archived:
|
|
|
|
memberships.append(Membership.LEAVE)
|
|
|
|
|
2020-01-15 07:59:33 -07:00
|
|
|
room_list = await self.store.get_rooms_for_local_user_where_membership_is(
|
2016-09-21 04:46:28 -06:00
|
|
|
user_id=user_id, membership_list=memberships
|
|
|
|
)
|
|
|
|
|
|
|
|
user = UserID.from_string(user_id)
|
|
|
|
|
|
|
|
rooms_ret = []
|
|
|
|
|
2020-08-04 05:21:47 -06:00
|
|
|
now_token = self.hs.get_event_sources().get_current_token()
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2021-09-21 11:34:26 -06:00
|
|
|
presence_stream = self.hs.get_event_sources().sources.presence
|
2020-09-08 08:00:17 -06:00
|
|
|
presence, _ = await presence_stream.get_new_events(
|
|
|
|
user, from_key=None, include_offline=False
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
2020-09-08 08:00:17 -06: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(
|
|
|
|
joined_rooms,
|
|
|
|
to_key=int(now_token.receipt_key),
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
2022-08-05 09:09:33 -06:00
|
|
|
|
|
|
|
receipt = ReceiptEventSource.filter_out_private_receipts(receipt, user_id)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
tags_by_room = await self.store.get_tags_for_user(user_id)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2023-02-10 07:22:16 -07:00
|
|
|
account_data = await self.store.get_global_account_data_for_user(user_id)
|
|
|
|
account_data_by_room = await self.store.get_room_account_data_for_user(user_id)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
public_room_ids = await self.store.get_public_room_ids()
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2022-03-03 08:43:06 -07:00
|
|
|
serializer_options = SerializeEventConfig(as_client_event=as_client_event)
|
|
|
|
|
2021-09-20 06:56:23 -06:00
|
|
|
async def handle_room(event: RoomsForUser) -> None:
|
2021-08-18 07:22:07 -06:00
|
|
|
d: JsonDict = {
|
2016-09-21 04:46:28 -06:00
|
|
|
"room_id": event.room_id,
|
|
|
|
"membership": event.membership,
|
|
|
|
"visibility": (
|
|
|
|
"public" if event.room_id in public_room_ids else "private"
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.membership == Membership.INVITE:
|
|
|
|
time_now = self.clock.time_msec()
|
|
|
|
d["inviter"] = event.sender
|
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
invite_event = await self.store.get_event(event.event_id)
|
2022-01-07 07:10:46 -07:00
|
|
|
d["invite"] = self._event_serializer.serialize_event(
|
2021-12-06 08:51:15 -07:00
|
|
|
invite_event,
|
|
|
|
time_now,
|
2022-03-03 08:43:06 -07:00
|
|
|
config=serializer_options,
|
2019-05-09 06:21:57 -06:00
|
|
|
)
|
2016-09-21 04:46:28 -06: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 17:57:48 -06:00
|
|
|
deferred_room_state = run_in_background(
|
2022-06-06 02:24:12 -06:00
|
|
|
self._state_storage_controller.get_current_state, event.room_id
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
elif event.membership == Membership.LEAVE:
|
2020-09-11 05:22:55 -06:00
|
|
|
room_end_token = RoomStreamToken(
|
|
|
|
None,
|
|
|
|
event.stream_ordering,
|
|
|
|
)
|
2018-07-23 17:57:48 -06:00
|
|
|
deferred_room_state = run_in_background(
|
2022-05-31 06:17:50 -06:00
|
|
|
self._state_storage_controller.get_state_for_events,
|
|
|
|
[event.event_id],
|
2023-09-05 05:14:14 -06:00
|
|
|
).addCallback(lambda states: states[event.event_id])
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
(messages, token), current_state = await make_deferred_yieldable(
|
2021-12-14 10:35:28 -07:00
|
|
|
gather_results(
|
|
|
|
(
|
2018-04-27 04:29:27 -06:00
|
|
|
run_in_background(
|
|
|
|
self.store.get_recent_events_for_room,
|
2016-09-21 04:46:28 -06:00
|
|
|
event.room_id,
|
2022-10-14 06:30:05 -06:00
|
|
|
limit=pagin_config.limit,
|
2016-09-21 04:46:28 -06:00
|
|
|
end_token=room_end_token,
|
|
|
|
),
|
|
|
|
deferred_room_state,
|
2021-12-14 10:35:28 -07:00
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
).addErrback(unwrapFirstError)
|
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
messages = await filter_events_for_client(
|
2022-05-31 06:17:50 -06:00
|
|
|
self._storage_controllers, user_id, messages
|
2019-10-23 10:25:54 -06:00
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2022-05-16 09:35:31 -06: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 04:46:28 -06:00
|
|
|
time_now = self.clock.time_msec()
|
|
|
|
|
|
|
|
d["messages"] = {
|
2019-05-09 06:21:57 -06:00
|
|
|
"chunk": (
|
2022-01-07 07:10:46 -07:00
|
|
|
self._event_serializer.serialize_events(
|
2021-12-06 08:51:15 -07:00
|
|
|
messages,
|
|
|
|
time_now=time_now,
|
2022-03-03 08:43:06 -07:00
|
|
|
config=serializer_options,
|
2019-05-09 06:21:57 -06:00
|
|
|
)
|
|
|
|
),
|
2020-09-30 13:29:19 -06:00
|
|
|
"start": await start_token.to_string(self.store),
|
|
|
|
"end": await end_token.to_string(self.store),
|
2016-09-21 04:46:28 -06:00
|
|
|
}
|
|
|
|
|
2022-01-07 07:10:46 -07:00
|
|
|
d["state"] = self._event_serializer.serialize_events(
|
2019-05-09 06:21:57 -06:00
|
|
|
current_state.values(),
|
|
|
|
time_now=time_now,
|
2022-03-03 08:43:06 -07:00
|
|
|
config=serializer_options,
|
2019-05-09 06:21:57 -06:00
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
|
|
|
account_data_events = []
|
|
|
|
tags = tags_by_room.get(event.room_id)
|
|
|
|
if tags:
|
|
|
|
account_data_events.append(
|
2023-01-13 07:57:43 -07:00
|
|
|
{"type": AccountDataTypes.TAG, "content": {"tags": tags}}
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
account_data = account_data_by_room.get(event.room_id, {})
|
|
|
|
for account_data_type, content in account_data.items():
|
|
|
|
account_data_events.append(
|
|
|
|
{"type": account_data_type, "content": content}
|
|
|
|
)
|
|
|
|
|
|
|
|
d["account_data"] = account_data_events
|
2017-10-23 08:52:32 -06:00
|
|
|
except Exception:
|
2016-09-21 04:46:28 -06:00
|
|
|
logger.exception("Failed to get snapshot")
|
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
await concurrently_execute(handle_room, room_list, 10)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
|
|
|
account_data_events = []
|
|
|
|
for account_data_type, content in account_data.items():
|
|
|
|
account_data_events.append({"type": account_data_type, "content": content})
|
|
|
|
|
2017-03-15 08:27:34 -06:00
|
|
|
now = self.clock.time_msec()
|
|
|
|
|
2016-09-21 04:46:28 -06:00
|
|
|
ret = {
|
|
|
|
"rooms": rooms_ret,
|
2017-03-15 08:27:34 -06:00
|
|
|
"presence": [
|
|
|
|
{
|
2022-05-27 05:14:36 -06:00
|
|
|
"type": EduTypes.PRESENCE,
|
2017-03-15 08:27:34 -06:00
|
|
|
"content": format_user_presence_state(event, now),
|
|
|
|
}
|
|
|
|
for event in presence
|
|
|
|
],
|
2016-09-21 04:46:28 -06:00
|
|
|
"account_data": account_data_events,
|
|
|
|
"receipts": receipt,
|
2020-09-30 13:29:19 -06:00
|
|
|
"end": await now_token.to_string(self.store),
|
2016-09-21 04:46:28 -06:00
|
|
|
}
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return ret
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2020-09-03 15:02:29 -06:00
|
|
|
async def room_initial_sync(
|
|
|
|
self, requester: Requester, room_id: str, pagin_config: PaginationConfig
|
|
|
|
) -> JsonDict:
|
2016-09-21 04:46:28 -06: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 15:02:29 -06: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 04:46:28 -06: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 06:46:45 -07:00
|
|
|
blocked = await self.store.is_room_blocked(room_id)
|
2019-03-21 05:20:13 -06:00
|
|
|
if blocked:
|
|
|
|
raise SynapseError(403, "This room has been blocked on this server")
|
|
|
|
|
2020-02-18 16:13:29 -07:00
|
|
|
(
|
|
|
|
membership,
|
|
|
|
member_event_id,
|
2020-02-18 16:14:57 -07:00
|
|
|
) = await self.auth.check_user_in_room_or_world_readable(
|
|
|
|
room_id,
|
2022-08-22 07:17:59 -06:00
|
|
|
requester,
|
2020-02-18 16:14:57 -07:00
|
|
|
allow_departed_users=True,
|
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
is_peeking = member_event_id is None
|
|
|
|
|
|
|
|
if membership == Membership.JOIN:
|
2019-12-09 06:46:45 -07:00
|
|
|
result = await self._room_initial_sync_joined(
|
2023-03-06 09:08:39 -07:00
|
|
|
requester, room_id, pagin_config, membership, is_peeking
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
elif membership == Membership.LEAVE:
|
2020-10-21 04:44:31 -06:00
|
|
|
# The member_event_id will always be available if membership is set
|
|
|
|
# to leave.
|
|
|
|
assert member_event_id
|
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
result = await self._room_initial_sync_parted(
|
2023-03-06 09:08:39 -07:00
|
|
|
requester,
|
|
|
|
room_id,
|
|
|
|
pagin_config,
|
|
|
|
membership,
|
|
|
|
member_event_id,
|
|
|
|
is_peeking,
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
account_data_events = []
|
2023-03-06 09:08:39 -07:00
|
|
|
user_id = requester.user.to_string()
|
2019-12-09 06:46:45 -07:00
|
|
|
tags = await self.store.get_tags_for_room(user_id, room_id)
|
2016-09-21 04:46:28 -06:00
|
|
|
if tags:
|
2023-01-13 07:57:43 -07:00
|
|
|
account_data_events.append(
|
|
|
|
{"type": AccountDataTypes.TAG, "content": {"tags": tags}}
|
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
account_data = await self.store.get_account_data_for_room(user_id, room_id)
|
2016-09-21 04:46:28 -06:00
|
|
|
for account_data_type, content in account_data.items():
|
|
|
|
account_data_events.append({"type": account_data_type, "content": content})
|
|
|
|
|
|
|
|
result["account_data"] = account_data_events
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return result
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
async def _room_initial_sync_parted(
|
2020-09-03 15:02:29 -06:00
|
|
|
self,
|
2023-03-06 09:08:39 -07:00
|
|
|
requester: Requester,
|
2020-09-03 15:02:29 -06:00
|
|
|
room_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
2020-10-21 04:44:31 -06:00
|
|
|
membership: str,
|
2020-09-03 15:02:29 -06:00
|
|
|
member_event_id: str,
|
|
|
|
is_peeking: bool,
|
|
|
|
) -> JsonDict:
|
2022-05-31 06:17:50 -06:00
|
|
|
room_state = await self._state_storage_controller.get_state_for_event(
|
|
|
|
member_event_id
|
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2020-09-29 14:48:33 -06:00
|
|
|
leave_position = await self.store.get_position_for_event(member_event_id)
|
|
|
|
stream_token = leave_position.to_room_stream_token()
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
messages, token = await self.store.get_recent_events_for_room(
|
2022-10-14 06:30:05 -06:00
|
|
|
room_id, limit=pagin_config.limit, end_token=stream_token
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
messages = await filter_events_for_client(
|
2023-03-06 09:08:39 -07:00
|
|
|
self._storage_controllers,
|
|
|
|
requester.user.to_string(),
|
|
|
|
messages,
|
|
|
|
is_peeking=is_peeking,
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
2022-05-16 09:35:31 -06: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 04:46:28 -06:00
|
|
|
|
|
|
|
time_now = self.clock.time_msec()
|
2023-03-06 09:08:39 -07:00
|
|
|
serialize_options = SerializeEventConfig(requester=requester)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {
|
|
|
|
"membership": membership,
|
|
|
|
"room_id": room_id,
|
|
|
|
"messages": {
|
|
|
|
"chunk": (
|
2021-12-06 08:51:15 -07:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2023-03-06 09:08:39 -07:00
|
|
|
self._event_serializer.serialize_events(
|
|
|
|
messages, time_now, config=serialize_options
|
|
|
|
)
|
2019-05-09 06:21:57 -06:00
|
|
|
),
|
2020-09-30 13:29:19 -06:00
|
|
|
"start": await start_token.to_string(self.store),
|
|
|
|
"end": await end_token.to_string(self.store),
|
2019-07-23 07:00:55 -06:00
|
|
|
},
|
|
|
|
"state": (
|
2021-12-06 08:51:15 -07:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2023-03-06 09:08:39 -07:00
|
|
|
self._event_serializer.serialize_events(
|
|
|
|
room_state.values(), time_now, config=serialize_options
|
|
|
|
)
|
2019-07-23 07:00:55 -06:00
|
|
|
),
|
|
|
|
"presence": [],
|
|
|
|
"receipts": [],
|
|
|
|
}
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
async def _room_initial_sync_joined(
|
2020-09-03 15:02:29 -06:00
|
|
|
self,
|
2023-03-06 09:08:39 -07:00
|
|
|
requester: Requester,
|
2020-09-03 15:02:29 -06:00
|
|
|
room_id: str,
|
|
|
|
pagin_config: PaginationConfig,
|
2020-10-21 04:44:31 -06:00
|
|
|
membership: str,
|
2020-09-03 15:02:29 -06:00
|
|
|
is_peeking: bool,
|
|
|
|
) -> JsonDict:
|
2022-06-06 02:24:12 -06:00
|
|
|
current_state = await self._storage_controllers.state.get_current_state(
|
|
|
|
room_id=room_id
|
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
|
|
|
# TODO: These concurrently
|
|
|
|
time_now = self.clock.time_msec()
|
2023-03-06 09:08:39 -07:00
|
|
|
serialize_options = SerializeEventConfig(requester=requester)
|
2021-12-06 08:51:15 -07:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2022-01-07 07:10:46 -07:00
|
|
|
state = self._event_serializer.serialize_events(
|
2023-03-06 09:08:39 -07:00
|
|
|
current_state.values(),
|
|
|
|
time_now,
|
|
|
|
config=serialize_options,
|
2019-05-09 06:21:57 -06:00
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2020-08-04 05:21:47 -06:00
|
|
|
now_token = self.hs.get_event_sources().get_current_token()
|
2016-09-21 04:46:28 -06:00
|
|
|
|
|
|
|
room_members = [
|
|
|
|
m
|
|
|
|
for m in current_state.values()
|
|
|
|
if m.type == EventTypes.Member
|
|
|
|
and m.content["membership"] == Membership.JOIN
|
|
|
|
]
|
|
|
|
|
|
|
|
presence_handler = self.hs.get_presence_handler()
|
|
|
|
|
2021-09-20 06:56:23 -06:00
|
|
|
async def get_presence() -> List[JsonDict]:
|
2018-08-17 09:08:45 -06:00
|
|
|
# If presence is disabled, return an empty list
|
2021-09-15 06:34:52 -06:00
|
|
|
if not self.hs.config.server.use_presence:
|
2019-07-23 07:00:55 -06:00
|
|
|
return []
|
2018-08-17 09:08:45 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
states = await presence_handler.get_states(
|
2020-04-22 15:39:04 -06:00
|
|
|
[m.user_id for m in room_members]
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
2020-04-22 15:39:04 -06:00
|
|
|
return [
|
|
|
|
{
|
2022-05-27 05:14:36 -06:00
|
|
|
"type": EduTypes.PRESENCE,
|
2020-04-22 15:39:04 -06:00
|
|
|
"content": format_user_presence_state(s, time_now),
|
|
|
|
}
|
|
|
|
for s in states
|
|
|
|
]
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2021-09-20 06:56:23 -06:00
|
|
|
async def get_receipts() -> List[JsonDict]:
|
2019-12-09 06:46:45 -07:00
|
|
|
receipts = await self.store.get_linearized_receipts_for_room(
|
2016-11-23 08:14:24 -07:00
|
|
|
room_id, to_key=now_token.receipt_key
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
2016-11-23 08:14:24 -07:00
|
|
|
if not receipts:
|
2021-07-28 02:05:11 -06:00
|
|
|
return []
|
2022-08-05 09:09:33 -06:00
|
|
|
|
2023-03-06 09:08:39 -07:00
|
|
|
return ReceiptEventSource.filter_out_private_receipts(
|
|
|
|
receipts,
|
|
|
|
requester.user.to_string(),
|
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
presence, receipts, (messages, token) = await make_deferred_yieldable(
|
2021-12-14 10:35:28 -07:00
|
|
|
gather_results(
|
|
|
|
(
|
2018-07-23 17:37:17 -06:00
|
|
|
run_in_background(get_presence),
|
|
|
|
run_in_background(get_receipts),
|
|
|
|
run_in_background(
|
|
|
|
self.store.get_recent_events_for_room,
|
|
|
|
room_id,
|
2022-10-14 06:30:05 -06:00
|
|
|
limit=pagin_config.limit,
|
2018-07-23 17:37:17 -06:00
|
|
|
end_token=now_token.room_key,
|
|
|
|
),
|
2021-12-14 10:35:28 -07:00
|
|
|
),
|
2018-07-23 17:37:17 -06:00
|
|
|
consumeErrors=True,
|
|
|
|
).addErrback(unwrapFirstError)
|
|
|
|
)
|
2016-09-21 04:46:28 -06:00
|
|
|
|
2019-12-09 06:46:45 -07:00
|
|
|
messages = await filter_events_for_client(
|
2023-03-06 09:08:39 -07:00
|
|
|
self._storage_controllers,
|
|
|
|
requester.user.to_string(),
|
|
|
|
messages,
|
|
|
|
is_peeking=is_peeking,
|
2016-09-21 04:46:28 -06:00
|
|
|
)
|
|
|
|
|
2022-05-16 09:35:31 -06:00
|
|
|
start_token = now_token.copy_and_replace(StreamKeyType.ROOM, token)
|
2018-05-09 04:55:34 -06:00
|
|
|
end_token = now_token
|
2016-09-21 04:46:28 -06:00
|
|
|
|
|
|
|
ret = {
|
|
|
|
"room_id": room_id,
|
|
|
|
"messages": {
|
2019-05-09 06:21:57 -06:00
|
|
|
"chunk": (
|
2021-12-06 08:51:15 -07:00
|
|
|
# Don't bundle aggregations as this is a deprecated API.
|
2023-03-06 09:08:39 -07:00
|
|
|
self._event_serializer.serialize_events(
|
|
|
|
messages, time_now, config=serialize_options
|
|
|
|
)
|
2019-05-09 06:21:57 -06:00
|
|
|
),
|
2020-09-30 13:29:19 -06:00
|
|
|
"start": await start_token.to_string(self.store),
|
|
|
|
"end": await end_token.to_string(self.store),
|
2016-09-21 04:46:28 -06:00
|
|
|
},
|
|
|
|
"state": state,
|
|
|
|
"presence": presence,
|
|
|
|
"receipts": receipts,
|
|
|
|
}
|
|
|
|
if not is_peeking:
|
|
|
|
ret["membership"] = membership
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return ret
|