2016-09-21 06:46:28 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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
|
2020-09-03 17:02:29 -04:00
|
|
|
from typing import TYPE_CHECKING
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2016-09-21 06:46:28 -04:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from synapse.api.constants import EventTypes, Membership
|
2020-02-18 18:13:29 -05:00
|
|
|
from synapse.api.errors import SynapseError
|
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
|
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
|
2020-09-03 17:02:29 -04:00
|
|
|
from synapse.types import JsonDict, Requester, StreamToken, UserID
|
2016-09-21 06:46:28 -04:00
|
|
|
from synapse.util import unwrapFirstError
|
2018-08-10 09:50:21 -04:00
|
|
|
from synapse.util.async_helpers import concurrently_execute
|
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
|
|
|
|
|
|
|
|
from ._base import BaseHandler
|
|
|
|
|
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__)
|
|
|
|
|
|
|
|
|
|
|
|
class InitialSyncHandler(BaseHandler):
|
2020-09-03 17:02:29 -04:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2016-09-21 06:46:28 -04:00
|
|
|
super(InitialSyncHandler, self).__init__(hs)
|
|
|
|
self.hs = hs
|
|
|
|
self.state = hs.get_state_handler()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.validator = EventValidator()
|
2019-12-09 08:42:49 -05:00
|
|
|
self.snapshot_cache = ResponseCache(hs, "initial_sync_cache")
|
2019-05-09 08:21:57 -04:00
|
|
|
self._event_serializer = hs.get_event_client_serializer()
|
2019-10-23 12:25:54 -04:00
|
|
|
self.storage = hs.get_storage()
|
|
|
|
self.state_store = self.storage.state
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
def snapshot_all_rooms(
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2019-12-09 08:42:49 -05:00
|
|
|
return 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
|
|
|
|
|
|
|
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(
|
|
|
|
joined_rooms, to_key=int(now_token.receipt_key),
|
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
|
|
|
|
|
|
|
limit = pagin_config.limit
|
|
|
|
if limit is None:
|
|
|
|
limit = 10
|
|
|
|
|
2020-09-03 17:02:29 -04:00
|
|
|
async def handle_room(event: RoomsForUser):
|
2016-09-21 06:46:28 -04:00
|
|
|
d = {
|
|
|
|
"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)
|
|
|
|
d["invite"] = await self._event_serializer.serialize_event(
|
2019-06-20 05:32:02 -04:00
|
|
|
invite_event, time_now, as_client_event
|
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(
|
2019-06-20 05:32:02 -04:00
|
|
|
self.state_handler.get_current_state, event.room_id
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
elif event.membership == Membership.LEAVE:
|
|
|
|
room_end_token = "s%d" % (event.stream_ordering,)
|
2018-07-23 19:57:48 -04:00
|
|
|
deferred_room_state = run_in_background(
|
2019-10-23 12:25:54 -04:00
|
|
|
self.state_store.get_state_for_events, [event.event_id]
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
deferred_room_state.addCallback(
|
|
|
|
lambda states: states[event.event_id]
|
|
|
|
)
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
(messages, token), current_state = await make_deferred_yieldable(
|
2016-09-21 06:46:28 -04:00
|
|
|
defer.gatherResults(
|
|
|
|
[
|
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,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
).addErrback(unwrapFirstError)
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
messages = await filter_events_for_client(
|
2019-10-23 12:25:54 -04:00
|
|
|
self.storage, user_id, messages
|
|
|
|
)
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2018-05-09 06:55:34 -04:00
|
|
|
start_token = now_token.copy_and_replace("room_key", token)
|
|
|
|
end_token = now_token.copy_and_replace("room_key", 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": (
|
2019-12-09 08:46:45 -05:00
|
|
|
await self._event_serializer.serialize_events(
|
2019-06-20 05:32:02 -04:00
|
|
|
messages, time_now=time_now, as_client_event=as_client_event
|
2019-05-09 08:21:57 -04:00
|
|
|
)
|
|
|
|
),
|
2016-09-21 06:46:28 -04:00
|
|
|
"start": start_token.to_string(),
|
|
|
|
"end": end_token.to_string(),
|
|
|
|
}
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
d["state"] = await self._event_serializer.serialize_events(
|
2019-05-09 08:21:57 -04:00
|
|
|
current_state.values(),
|
|
|
|
time_now=time_now,
|
2019-06-20 05:32:02 -04:00
|
|
|
as_client_event=as_client_event,
|
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": [
|
|
|
|
{
|
|
|
|
"type": "m.presence",
|
|
|
|
"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,
|
|
|
|
"end": now_token.to_string(),
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
room_id, user_id, allow_departed_users=True,
|
|
|
|
)
|
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:
|
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,
|
|
|
|
membership: Membership,
|
|
|
|
member_event_id: str,
|
|
|
|
is_peeking: bool,
|
|
|
|
) -> JsonDict:
|
2019-12-09 08:46:45 -05:00
|
|
|
room_state = await self.state_store.get_state_for_events([member_event_id])
|
2016-09-21 06:46:28 -04:00
|
|
|
|
|
|
|
room_state = room_state[member_event_id]
|
|
|
|
|
|
|
|
limit = pagin_config.limit if pagin_config else None
|
|
|
|
if limit is None:
|
|
|
|
limit = 10
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
stream_token = await self.store.get_stream_token_for_event(member_event_id)
|
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(
|
2019-10-23 12:25:54 -04:00
|
|
|
self.storage, user_id, messages, is_peeking=is_peeking
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2018-05-09 06:55:34 -04:00
|
|
|
start_token = StreamToken.START.copy_and_replace("room_key", token)
|
|
|
|
end_token = StreamToken.START.copy_and_replace("room_key", 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": (
|
2019-12-09 08:46:45 -05:00
|
|
|
await self._event_serializer.serialize_events(messages, time_now)
|
2019-06-20 05:32:02 -04:00
|
|
|
),
|
2019-07-23 09:00:55 -04:00
|
|
|
"start": start_token.to_string(),
|
|
|
|
"end": end_token.to_string(),
|
|
|
|
},
|
|
|
|
"state": (
|
2019-12-09 08:46:45 -05:00
|
|
|
await self._event_serializer.serialize_events(
|
2019-07-23 09:00:55 -04:00
|
|
|
room_state.values(), time_now
|
|
|
|
)
|
|
|
|
),
|
|
|
|
"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,
|
|
|
|
membership: Membership,
|
|
|
|
is_peeking: bool,
|
|
|
|
) -> JsonDict:
|
2019-12-09 08:46:45 -05:00
|
|
|
current_state = await self.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()
|
2019-12-09 08:46:45 -05:00
|
|
|
state = await self._event_serializer.serialize_events(
|
2019-06-20 05:32:02 -04: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()
|
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
async def get_presence():
|
2018-08-17 11:08:45 -04:00
|
|
|
# If presence is disabled, return an empty list
|
|
|
|
if not self.hs.config.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 [
|
|
|
|
{
|
|
|
|
"type": EventTypes.Presence,
|
|
|
|
"content": format_user_presence_state(s, time_now),
|
|
|
|
}
|
|
|
|
for s in states
|
|
|
|
]
|
2016-09-21 06:46:28 -04:00
|
|
|
|
2019-12-09 08:46:45 -05:00
|
|
|
async def get_receipts():
|
|
|
|
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:
|
|
|
|
receipts = []
|
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(
|
2018-07-23 19:37:17 -04:00
|
|
|
defer.gatherResults(
|
|
|
|
[
|
|
|
|
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
|
|
|
),
|
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(
|
2019-10-23 12:25:54 -04:00
|
|
|
self.storage, user_id, messages, is_peeking=is_peeking
|
2016-09-21 06:46:28 -04:00
|
|
|
)
|
|
|
|
|
2018-05-09 10:43:00 -04:00
|
|
|
start_token = now_token.copy_and_replace("room_key", 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": (
|
2019-12-09 08:46:45 -05:00
|
|
|
await self._event_serializer.serialize_events(messages, time_now)
|
2019-06-20 05:32:02 -04:00
|
|
|
),
|
2016-09-21 06:46:28 -04:00
|
|
|
"start": start_token.to_string(),
|
|
|
|
"end": end_token.to_string(),
|
|
|
|
},
|
|
|
|
"state": state,
|
|
|
|
"presence": presence,
|
|
|
|
"receipts": receipts,
|
|
|
|
}
|
|
|
|
if not is_peeking:
|
|
|
|
ret["membership"] = membership
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return ret
|