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