2021-03-18 19:24:16 +01:00
|
|
|
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
import itertools
|
|
|
|
|
import logging
|
2021-05-11 18:57:39 +02:00
|
|
|
|
import re
|
2021-08-16 14:06:17 +02:00
|
|
|
|
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Sequence, Set, Tuple
|
2021-03-23 12:51:12 +01:00
|
|
|
|
|
|
|
|
|
import attr
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-05-20 17:10:36 +02:00
|
|
|
|
from synapse.api.constants import (
|
|
|
|
|
EventTypes,
|
|
|
|
|
HistoryVisibility,
|
2021-07-13 14:59:27 +02:00
|
|
|
|
JoinRules,
|
2021-05-20 17:10:36 +02:00
|
|
|
|
Membership,
|
2021-06-29 18:00:04 +02:00
|
|
|
|
RoomTypes,
|
2021-05-20 17:10:36 +02:00
|
|
|
|
)
|
2021-09-01 19:01:08 +02:00
|
|
|
|
from synapse.api.errors import (
|
|
|
|
|
Codes,
|
|
|
|
|
NotFoundError,
|
|
|
|
|
StoreError,
|
|
|
|
|
SynapseError,
|
2022-07-27 14:44:40 +02:00
|
|
|
|
UnstableSpecAuthError,
|
2021-09-01 19:01:08 +02:00
|
|
|
|
UnsupportedRoomVersionError,
|
|
|
|
|
)
|
2021-11-29 20:32:20 +01:00
|
|
|
|
from synapse.api.ratelimiting import Ratelimiter
|
2021-03-18 19:24:16 +01:00
|
|
|
|
from synapse.events import EventBase
|
2023-01-26 18:31:58 +01:00
|
|
|
|
from synapse.types import JsonDict, Requester, StrCollection
|
2021-08-10 19:08:17 +02:00
|
|
|
|
from synapse.util.caches.response_cache import ResponseCache
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
# number of rooms to return. We'll stop once we hit this limit.
|
|
|
|
|
MAX_ROOMS = 50
|
|
|
|
|
|
|
|
|
|
# max number of events to return per room.
|
|
|
|
|
MAX_ROOMS_PER_SPACE = 50
|
|
|
|
|
|
2021-03-24 13:45:39 +01:00
|
|
|
|
# max number of federation servers to hit per room
|
|
|
|
|
MAX_SERVERS_PER_SPACE = 3
|
|
|
|
|
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-08-10 19:08:17 +02:00
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
|
class _PaginationKey:
|
|
|
|
|
"""The key used to find unique pagination session."""
|
|
|
|
|
|
|
|
|
|
# The first three entries match the request parameters (and cannot change
|
|
|
|
|
# during a pagination session).
|
|
|
|
|
room_id: str
|
|
|
|
|
suggested_only: bool
|
|
|
|
|
max_depth: Optional[int]
|
|
|
|
|
# The randomly generated token.
|
|
|
|
|
token: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
|
class _PaginationSession:
|
|
|
|
|
"""The information that is stored for pagination."""
|
|
|
|
|
|
2021-08-11 20:52:09 +02:00
|
|
|
|
# The time the pagination session was created, in milliseconds.
|
|
|
|
|
creation_time_ms: int
|
2021-08-10 19:08:17 +02:00
|
|
|
|
# The queue of rooms which are still to process.
|
2021-08-16 14:06:17 +02:00
|
|
|
|
room_queue: List["_RoomQueueEntry"]
|
2021-08-10 19:08:17 +02:00
|
|
|
|
# A set of rooms which have been processed.
|
|
|
|
|
processed_rooms: Set[str]
|
|
|
|
|
|
|
|
|
|
|
2021-08-16 16:49:12 +02:00
|
|
|
|
class RoomSummaryHandler:
|
2021-08-24 14:14:03 +02:00
|
|
|
|
# A unique key used for pagination sessions for the room hierarchy endpoint.
|
|
|
|
|
_PAGINATION_SESSION_TYPE = "room_hierarchy_pagination"
|
|
|
|
|
|
2021-08-11 20:52:09 +02:00
|
|
|
|
# The time a pagination session remains valid for.
|
|
|
|
|
_PAGINATION_SESSION_VALIDITY_PERIOD_MS = 5 * 60 * 1000
|
|
|
|
|
|
2021-03-18 19:24:16 +01:00
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2021-05-20 17:10:36 +02:00
|
|
|
|
self._event_auth_handler = hs.get_event_auth_handler()
|
2022-02-23 12:04:02 +01:00
|
|
|
|
self._store = hs.get_datastores().main
|
2022-06-01 17:02:53 +02:00
|
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2021-03-18 19:24:16 +01:00
|
|
|
|
self._event_serializer = hs.get_event_client_serializer()
|
2021-03-24 13:45:39 +01:00
|
|
|
|
self._server_name = hs.hostname
|
|
|
|
|
self._federation_client = hs.get_federation_client()
|
2021-11-29 20:32:20 +01:00
|
|
|
|
self._ratelimiter = Ratelimiter(
|
|
|
|
|
store=self._store, clock=hs.get_clock(), rate_hz=5, burst_count=10
|
|
|
|
|
)
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-08-10 19:08:17 +02:00
|
|
|
|
# If a user tries to fetch the same page multiple times in quick succession,
|
|
|
|
|
# only process the first attempt and return its result to subsequent requests.
|
|
|
|
|
self._pagination_response_cache: ResponseCache[
|
2021-11-16 16:40:47 +01:00
|
|
|
|
Tuple[str, str, bool, Optional[int], Optional[int], Optional[str]]
|
2021-08-10 19:08:17 +02:00
|
|
|
|
] = ResponseCache(
|
|
|
|
|
hs.get_clock(),
|
|
|
|
|
"get_room_hierarchy",
|
|
|
|
|
)
|
2022-05-05 16:25:00 +02:00
|
|
|
|
self._msc3266_enabled = hs.config.experimental.msc3266_enabled
|
2021-08-10 19:08:17 +02:00
|
|
|
|
|
|
|
|
|
async def get_room_hierarchy(
|
|
|
|
|
self,
|
2021-11-29 20:32:20 +01:00
|
|
|
|
requester: Requester,
|
2021-08-10 19:08:17 +02:00
|
|
|
|
requested_room_id: str,
|
|
|
|
|
suggested_only: bool = False,
|
|
|
|
|
max_depth: Optional[int] = None,
|
|
|
|
|
limit: Optional[int] = None,
|
|
|
|
|
from_token: Optional[str] = None,
|
|
|
|
|
) -> JsonDict:
|
|
|
|
|
"""
|
|
|
|
|
Implementation of the room hierarchy C-S API.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
requester: The user ID of the user making this request.
|
|
|
|
|
requested_room_id: The room ID to start the hierarchy at (the "root" room).
|
|
|
|
|
suggested_only: Whether we should only return children with the "suggested"
|
|
|
|
|
flag set.
|
|
|
|
|
max_depth: The maximum depth in the tree to explore, must be a
|
|
|
|
|
non-negative integer.
|
|
|
|
|
|
|
|
|
|
0 would correspond to just the root room, 1 would include just
|
|
|
|
|
the root room's children, etc.
|
|
|
|
|
limit: An optional limit on the number of rooms to return per
|
|
|
|
|
page. Must be a positive integer.
|
|
|
|
|
from_token: An optional pagination token.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The JSON hierarchy dictionary.
|
|
|
|
|
"""
|
2021-11-29 20:32:20 +01:00
|
|
|
|
await self._ratelimiter.ratelimit(requester)
|
|
|
|
|
|
2021-08-10 19:08:17 +02:00
|
|
|
|
# If a user tries to fetch the same page multiple times in quick succession,
|
|
|
|
|
# only process the first attempt and return its result to subsequent requests.
|
|
|
|
|
#
|
|
|
|
|
# This is due to the pagination process mutating internal state, attempting
|
|
|
|
|
# to process multiple requests for the same page will result in errors.
|
|
|
|
|
return await self._pagination_response_cache.wrap(
|
2021-11-16 16:40:47 +01:00
|
|
|
|
(
|
2021-11-29 20:32:20 +01:00
|
|
|
|
requester.user.to_string(),
|
2021-11-16 16:40:47 +01:00
|
|
|
|
requested_room_id,
|
|
|
|
|
suggested_only,
|
|
|
|
|
max_depth,
|
|
|
|
|
limit,
|
|
|
|
|
from_token,
|
|
|
|
|
),
|
2021-08-10 19:08:17 +02:00
|
|
|
|
self._get_room_hierarchy,
|
2021-11-29 20:32:20 +01:00
|
|
|
|
requester.user.to_string(),
|
2021-08-10 19:08:17 +02:00
|
|
|
|
requested_room_id,
|
|
|
|
|
suggested_only,
|
|
|
|
|
max_depth,
|
|
|
|
|
limit,
|
|
|
|
|
from_token,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def _get_room_hierarchy(
|
|
|
|
|
self,
|
|
|
|
|
requester: str,
|
|
|
|
|
requested_room_id: str,
|
|
|
|
|
suggested_only: bool = False,
|
|
|
|
|
max_depth: Optional[int] = None,
|
|
|
|
|
limit: Optional[int] = None,
|
|
|
|
|
from_token: Optional[str] = None,
|
|
|
|
|
) -> JsonDict:
|
|
|
|
|
"""See docstring for SpaceSummaryHandler.get_room_hierarchy."""
|
|
|
|
|
|
2021-08-11 21:04:51 +02:00
|
|
|
|
# First of all, check that the room is accessible.
|
|
|
|
|
if not await self._is_local_room_accessible(requested_room_id, requester):
|
2022-07-27 14:44:40 +02:00
|
|
|
|
raise UnstableSpecAuthError(
|
2021-08-11 21:04:51 +02:00
|
|
|
|
403,
|
|
|
|
|
"User %s not in room %s, and room previews are disabled"
|
|
|
|
|
% (requester, requested_room_id),
|
2022-07-27 14:44:40 +02:00
|
|
|
|
errcode=Codes.NOT_JOINED,
|
2021-08-11 21:04:51 +02:00
|
|
|
|
)
|
2021-08-10 19:08:17 +02:00
|
|
|
|
|
|
|
|
|
# If this is continuing a previous session, pull the persisted data.
|
|
|
|
|
if from_token:
|
2021-08-24 14:14:03 +02:00
|
|
|
|
try:
|
|
|
|
|
pagination_session = await self._store.get_session(
|
|
|
|
|
session_type=self._PAGINATION_SESSION_TYPE,
|
|
|
|
|
session_id=from_token,
|
|
|
|
|
)
|
|
|
|
|
except StoreError:
|
|
|
|
|
raise SynapseError(400, "Unknown pagination token", Codes.INVALID_PARAM)
|
2021-08-11 20:52:09 +02:00
|
|
|
|
|
2021-08-24 14:14:03 +02:00
|
|
|
|
# If the requester, room ID, suggested-only, or max depth were modified
|
|
|
|
|
# the session is invalid.
|
|
|
|
|
if (
|
|
|
|
|
requester != pagination_session["requester"]
|
|
|
|
|
or requested_room_id != pagination_session["room_id"]
|
|
|
|
|
or suggested_only != pagination_session["suggested_only"]
|
|
|
|
|
or max_depth != pagination_session["max_depth"]
|
|
|
|
|
):
|
2021-08-10 19:08:17 +02:00
|
|
|
|
raise SynapseError(400, "Unknown pagination token", Codes.INVALID_PARAM)
|
|
|
|
|
|
|
|
|
|
# Load the previous state.
|
2021-08-24 14:14:03 +02:00
|
|
|
|
room_queue = [
|
|
|
|
|
_RoomQueueEntry(*fields) for fields in pagination_session["room_queue"]
|
|
|
|
|
]
|
|
|
|
|
processed_rooms = set(pagination_session["processed_rooms"])
|
2021-08-10 19:08:17 +02:00
|
|
|
|
else:
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# The queue of rooms to process, the next room is last on the stack.
|
|
|
|
|
room_queue = [_RoomQueueEntry(requested_room_id, ())]
|
2021-08-10 19:08:17 +02:00
|
|
|
|
|
|
|
|
|
# Rooms we have already processed.
|
|
|
|
|
processed_rooms = set()
|
|
|
|
|
|
|
|
|
|
rooms_result: List[JsonDict] = []
|
|
|
|
|
|
|
|
|
|
# Cap the limit to a server-side maximum.
|
|
|
|
|
if limit is None:
|
|
|
|
|
limit = MAX_ROOMS
|
|
|
|
|
else:
|
|
|
|
|
limit = min(limit, MAX_ROOMS)
|
|
|
|
|
|
|
|
|
|
# Iterate through the queue until we reach the limit or run out of
|
|
|
|
|
# rooms to include.
|
|
|
|
|
while room_queue and len(rooms_result) < limit:
|
2021-08-16 14:06:17 +02:00
|
|
|
|
queue_entry = room_queue.pop()
|
2021-08-10 19:08:17 +02:00
|
|
|
|
room_id = queue_entry.room_id
|
|
|
|
|
current_depth = queue_entry.depth
|
|
|
|
|
if room_id in processed_rooms:
|
|
|
|
|
# already done this room
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
logger.debug("Processing room %s", room_id)
|
|
|
|
|
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# A map of summaries for children rooms that might be returned over
|
|
|
|
|
# federation. The rationale for caching these and *maybe* using them
|
|
|
|
|
# is to prefer any information local to the homeserver before trusting
|
|
|
|
|
# data received over federation.
|
|
|
|
|
children_room_entries: Dict[str, JsonDict] = {}
|
|
|
|
|
# A set of room IDs which are children that did not have information
|
|
|
|
|
# returned over federation and are known to be inaccessible to the
|
|
|
|
|
# current server. We should not reach out over federation to try to
|
|
|
|
|
# summarise these rooms.
|
|
|
|
|
inaccessible_children: Set[str] = set()
|
|
|
|
|
|
|
|
|
|
# If the room is known locally, summarise it!
|
2021-08-10 19:08:17 +02:00
|
|
|
|
is_in_room = await self._store.is_host_joined(room_id, self._server_name)
|
|
|
|
|
if is_in_room:
|
|
|
|
|
room_entry = await self._summarize_local_room(
|
|
|
|
|
requester,
|
|
|
|
|
None,
|
|
|
|
|
room_id,
|
|
|
|
|
suggested_only,
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# Otherwise, attempt to use information for federation.
|
2021-08-10 19:08:17 +02:00
|
|
|
|
else:
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# A previous call might have included information for this room.
|
|
|
|
|
# It can be used if either:
|
|
|
|
|
#
|
|
|
|
|
# 1. The room is not a space.
|
|
|
|
|
# 2. The maximum depth has been achieved (since no children
|
|
|
|
|
# information is needed).
|
|
|
|
|
if queue_entry.remote_room and (
|
|
|
|
|
queue_entry.remote_room.get("room_type") != RoomTypes.SPACE
|
|
|
|
|
or (max_depth is not None and current_depth >= max_depth)
|
|
|
|
|
):
|
|
|
|
|
room_entry = _RoomEntry(
|
|
|
|
|
queue_entry.room_id, queue_entry.remote_room
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# If the above isn't true, attempt to fetch the room
|
|
|
|
|
# information over federation.
|
|
|
|
|
else:
|
|
|
|
|
(
|
|
|
|
|
room_entry,
|
|
|
|
|
children_room_entries,
|
|
|
|
|
inaccessible_children,
|
2021-08-16 16:49:12 +02:00
|
|
|
|
) = await self._summarize_remote_room_hierarchy(
|
2021-08-16 14:06:17 +02:00
|
|
|
|
queue_entry,
|
|
|
|
|
suggested_only,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Ensure this room is accessible to the requester (and not just
|
|
|
|
|
# the homeserver).
|
|
|
|
|
if room_entry and not await self._is_remote_room_accessible(
|
|
|
|
|
requester, queue_entry.room_id, room_entry.room
|
|
|
|
|
):
|
|
|
|
|
room_entry = None
|
|
|
|
|
|
|
|
|
|
# This room has been processed and should be ignored if it appears
|
|
|
|
|
# elsewhere in the hierarchy.
|
|
|
|
|
processed_rooms.add(room_id)
|
|
|
|
|
|
|
|
|
|
# There may or may not be a room entry based on whether it is
|
|
|
|
|
# inaccessible to the requesting user.
|
|
|
|
|
if room_entry:
|
|
|
|
|
# Add the room (including the stripped m.space.child events).
|
2022-03-08 14:09:11 +01:00
|
|
|
|
rooms_result.append(room_entry.as_json(for_client=True))
|
2021-08-16 14:06:17 +02:00
|
|
|
|
|
|
|
|
|
# If this room is not at the max-depth, check if there are any
|
|
|
|
|
# children to process.
|
|
|
|
|
if max_depth is None or current_depth < max_depth:
|
|
|
|
|
# The children get added in reverse order so that the next
|
|
|
|
|
# room to process, according to the ordering, is the last
|
|
|
|
|
# item in the list.
|
|
|
|
|
room_queue.extend(
|
|
|
|
|
_RoomQueueEntry(
|
|
|
|
|
ev["state_key"],
|
|
|
|
|
ev["content"]["via"],
|
|
|
|
|
current_depth + 1,
|
|
|
|
|
children_room_entries.get(ev["state_key"]),
|
|
|
|
|
)
|
|
|
|
|
for ev in reversed(room_entry.children_state_events)
|
|
|
|
|
if ev["type"] == EventTypes.SpaceChild
|
|
|
|
|
and ev["state_key"] not in inaccessible_children
|
|
|
|
|
)
|
2021-08-10 19:08:17 +02:00
|
|
|
|
|
|
|
|
|
result: JsonDict = {"rooms": rooms_result}
|
|
|
|
|
|
|
|
|
|
# If there's additional data, generate a pagination token (and persist state).
|
|
|
|
|
if room_queue:
|
2021-08-24 14:14:03 +02:00
|
|
|
|
result["next_batch"] = await self._store.create_session(
|
|
|
|
|
session_type=self._PAGINATION_SESSION_TYPE,
|
|
|
|
|
value={
|
|
|
|
|
# Information which must be identical across pagination.
|
|
|
|
|
"requester": requester,
|
|
|
|
|
"room_id": requested_room_id,
|
|
|
|
|
"suggested_only": suggested_only,
|
|
|
|
|
"max_depth": max_depth,
|
|
|
|
|
# The stored state.
|
|
|
|
|
"room_queue": [
|
|
|
|
|
attr.astuple(room_entry) for room_entry in room_queue
|
|
|
|
|
],
|
|
|
|
|
"processed_rooms": list(processed_rooms),
|
|
|
|
|
},
|
|
|
|
|
expiry_ms=self._PAGINATION_SESSION_VALIDITY_PERIOD_MS,
|
2021-08-10 19:08:17 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
2021-08-16 14:06:17 +02:00
|
|
|
|
async def get_federation_hierarchy(
|
|
|
|
|
self,
|
|
|
|
|
origin: str,
|
|
|
|
|
requested_room_id: str,
|
|
|
|
|
suggested_only: bool,
|
2021-09-20 14:56:23 +02:00
|
|
|
|
) -> JsonDict:
|
2021-08-16 14:06:17 +02:00
|
|
|
|
"""
|
|
|
|
|
Implementation of the room hierarchy Federation API.
|
|
|
|
|
|
|
|
|
|
This is similar to get_room_hierarchy, but does not recurse into the space.
|
|
|
|
|
It also considers whether anyone on the server may be able to access the
|
|
|
|
|
room, as opposed to whether a specific user can.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
origin: The server requesting the spaces summary.
|
|
|
|
|
requested_room_id: The room ID to start the hierarchy at (the "root" room).
|
|
|
|
|
suggested_only: whether we should only return children with the "suggested"
|
|
|
|
|
flag set.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The JSON hierarchy dictionary.
|
|
|
|
|
"""
|
|
|
|
|
root_room_entry = await self._summarize_local_room(
|
2022-02-28 19:33:00 +01:00
|
|
|
|
None, origin, requested_room_id, suggested_only
|
2021-08-16 14:06:17 +02:00
|
|
|
|
)
|
|
|
|
|
if root_room_entry is None:
|
|
|
|
|
# Room is inaccessible to the requesting server.
|
|
|
|
|
raise SynapseError(404, "Unknown room: %s" % (requested_room_id,))
|
|
|
|
|
|
|
|
|
|
children_rooms_result: List[JsonDict] = []
|
|
|
|
|
inaccessible_children: List[str] = []
|
|
|
|
|
|
|
|
|
|
# Iterate through each child and potentially add it, but not its children,
|
|
|
|
|
# to the response.
|
2022-01-08 01:27:58 +01:00
|
|
|
|
for child_room in itertools.islice(
|
|
|
|
|
root_room_entry.children_state_events, MAX_ROOMS_PER_SPACE
|
|
|
|
|
):
|
2021-08-16 14:06:17 +02:00
|
|
|
|
room_id = child_room.get("state_key")
|
|
|
|
|
assert isinstance(room_id, str)
|
|
|
|
|
# If the room is unknown, skip it.
|
|
|
|
|
if not await self._store.is_host_joined(room_id, self._server_name):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
room_entry = await self._summarize_local_room(
|
2022-02-28 19:33:00 +01:00
|
|
|
|
None, origin, room_id, suggested_only, include_children=False
|
2021-08-16 14:06:17 +02:00
|
|
|
|
)
|
|
|
|
|
# If the room is accessible, include it in the results.
|
|
|
|
|
#
|
|
|
|
|
# Note that only the room summary (without information on children)
|
|
|
|
|
# is included in the summary.
|
|
|
|
|
if room_entry:
|
|
|
|
|
children_rooms_result.append(room_entry.room)
|
|
|
|
|
# Otherwise, note that the requesting server shouldn't bother
|
|
|
|
|
# trying to summarize this room - they do not have access to it.
|
|
|
|
|
else:
|
|
|
|
|
inaccessible_children.append(room_id)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
# Include the requested room (including the stripped children events).
|
|
|
|
|
"room": root_room_entry.as_json(),
|
|
|
|
|
"children": children_rooms_result,
|
|
|
|
|
"inaccessible_children": inaccessible_children,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 12:51:12 +01:00
|
|
|
|
async def _summarize_local_room(
|
|
|
|
|
self,
|
|
|
|
|
requester: Optional[str],
|
2021-05-20 17:10:36 +02:00
|
|
|
|
origin: Optional[str],
|
2021-03-23 12:51:12 +01:00
|
|
|
|
room_id: str,
|
|
|
|
|
suggested_only: bool,
|
2022-02-28 19:33:00 +01:00
|
|
|
|
include_children: bool = True,
|
2021-08-05 14:39:17 +02:00
|
|
|
|
) -> Optional["_RoomEntry"]:
|
2021-05-11 18:57:39 +02:00
|
|
|
|
"""
|
|
|
|
|
Generate a room entry and a list of event entries for a given room.
|
|
|
|
|
|
|
|
|
|
Args:
|
2021-05-17 15:01:19 +02:00
|
|
|
|
requester:
|
|
|
|
|
The user requesting the summary, if it is a local request. None
|
|
|
|
|
if this is a federation request.
|
2021-05-20 17:10:36 +02:00
|
|
|
|
origin:
|
|
|
|
|
The server requesting the summary, if it is a federation request.
|
|
|
|
|
None if this is a local request.
|
2021-05-11 18:57:39 +02:00
|
|
|
|
room_id: The room ID to summarize.
|
|
|
|
|
suggested_only: True if only suggested children should be returned.
|
|
|
|
|
Otherwise, all children are returned.
|
2022-02-28 19:33:00 +01:00
|
|
|
|
include_children:
|
|
|
|
|
Whether to include the events of any children.
|
2021-05-11 18:57:39 +02:00
|
|
|
|
|
|
|
|
|
Returns:
|
2021-08-05 14:39:17 +02:00
|
|
|
|
A room entry if the room should be returned. None, otherwise.
|
2021-05-11 18:57:39 +02:00
|
|
|
|
"""
|
2021-08-10 16:56:54 +02:00
|
|
|
|
if not await self._is_local_room_accessible(room_id, requester, origin):
|
2021-08-05 14:39:17 +02:00
|
|
|
|
return None
|
2021-03-23 12:51:12 +01:00
|
|
|
|
|
2021-08-06 13:40:29 +02:00
|
|
|
|
room_entry = await self._build_room_entry(room_id, for_federation=bool(origin))
|
2021-03-23 12:51:12 +01:00
|
|
|
|
|
2022-02-28 19:33:00 +01:00
|
|
|
|
# If the room is not a space return just the room information.
|
|
|
|
|
if room_entry.get("room_type") != RoomTypes.SPACE or not include_children:
|
2021-08-05 14:39:17 +02:00
|
|
|
|
return _RoomEntry(room_id, room_entry)
|
2021-06-29 18:00:04 +02:00
|
|
|
|
|
|
|
|
|
# Otherwise, look for child rooms/spaces.
|
2021-03-23 12:51:12 +01:00
|
|
|
|
child_events = await self._get_child_events(room_id)
|
|
|
|
|
|
|
|
|
|
if suggested_only:
|
|
|
|
|
# we only care about suggested children
|
|
|
|
|
child_events = filter(_is_suggested_child_event, child_events)
|
|
|
|
|
|
2021-09-07 14:43:54 +02:00
|
|
|
|
stripped_events: List[JsonDict] = [
|
|
|
|
|
{
|
|
|
|
|
"type": e.type,
|
|
|
|
|
"state_key": e.state_key,
|
|
|
|
|
"content": e.content,
|
|
|
|
|
"sender": e.sender,
|
|
|
|
|
"origin_server_ts": e.origin_server_ts,
|
|
|
|
|
}
|
2022-02-28 19:33:00 +01:00
|
|
|
|
for e in child_events
|
2021-09-07 14:43:54 +02:00
|
|
|
|
]
|
|
|
|
|
return _RoomEntry(room_id, room_entry, stripped_events)
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-08-16 16:49:12 +02:00
|
|
|
|
async def _summarize_remote_room_hierarchy(
|
2021-08-16 14:06:17 +02:00
|
|
|
|
self, room: "_RoomQueueEntry", suggested_only: bool
|
|
|
|
|
) -> Tuple[Optional["_RoomEntry"], Dict[str, JsonDict], Set[str]]:
|
|
|
|
|
"""
|
|
|
|
|
Request room entries and a list of event entries for a given room by querying a remote server.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
room: The room to summarize.
|
|
|
|
|
suggested_only: True if only suggested children should be returned.
|
|
|
|
|
Otherwise, all children are returned.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
A tuple of:
|
|
|
|
|
The room entry.
|
|
|
|
|
Partial room data return over federation.
|
|
|
|
|
A set of inaccessible children room IDs.
|
|
|
|
|
"""
|
|
|
|
|
room_id = room.room_id
|
|
|
|
|
logger.info("Requesting summary for %s via %s", room_id, room.via)
|
|
|
|
|
|
|
|
|
|
via = itertools.islice(room.via, MAX_SERVERS_PER_SPACE)
|
|
|
|
|
try:
|
|
|
|
|
(
|
|
|
|
|
room_response,
|
2022-01-20 12:03:42 +01:00
|
|
|
|
children_state_events,
|
2021-08-16 14:06:17 +02:00
|
|
|
|
children,
|
|
|
|
|
inaccessible_children,
|
|
|
|
|
) = await self._federation_client.get_room_hierarchy(
|
|
|
|
|
via,
|
|
|
|
|
room_id,
|
|
|
|
|
suggested_only=suggested_only,
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"Unable to get hierarchy of %s via federation: %s",
|
|
|
|
|
room_id,
|
|
|
|
|
e,
|
|
|
|
|
exc_info=logger.isEnabledFor(logging.DEBUG),
|
|
|
|
|
)
|
|
|
|
|
return None, {}, set()
|
|
|
|
|
|
|
|
|
|
# Map the children to their room ID.
|
|
|
|
|
children_by_room_id = {
|
|
|
|
|
c["room_id"]: c
|
|
|
|
|
for c in children
|
|
|
|
|
if "room_id" in c and isinstance(c["room_id"], str)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2022-01-20 12:03:42 +01:00
|
|
|
|
_RoomEntry(room_id, room_response, children_state_events),
|
2021-08-16 14:06:17 +02:00
|
|
|
|
children_by_room_id,
|
|
|
|
|
set(inaccessible_children),
|
|
|
|
|
)
|
|
|
|
|
|
2021-08-10 16:56:54 +02:00
|
|
|
|
async def _is_local_room_accessible(
|
2021-08-11 21:04:51 +02:00
|
|
|
|
self, room_id: str, requester: Optional[str], origin: Optional[str] = None
|
2021-05-20 17:10:36 +02:00
|
|
|
|
) -> bool:
|
2021-05-17 15:01:19 +02:00
|
|
|
|
"""
|
2021-08-16 16:49:12 +02:00
|
|
|
|
Calculate whether the room should be shown to the requester.
|
2021-05-17 15:01:19 +02:00
|
|
|
|
|
2021-08-16 16:49:12 +02:00
|
|
|
|
It should return true if:
|
2021-05-17 15:01:19 +02:00
|
|
|
|
|
2023-02-13 17:30:58 +01:00
|
|
|
|
* The requesting user is joined or can join the room (per MSC3173); or
|
|
|
|
|
* The origin server has any user that is joined or can join the room; or
|
2021-05-17 15:01:19 +02:00
|
|
|
|
* The history visibility is set to world readable.
|
|
|
|
|
|
|
|
|
|
Args:
|
2021-08-16 16:49:12 +02:00
|
|
|
|
room_id: The room ID to check accessibility of.
|
2021-05-17 15:01:19 +02:00
|
|
|
|
requester:
|
2021-08-16 16:49:12 +02:00
|
|
|
|
The user making the request, if it is a local request.
|
|
|
|
|
None if this is a federation request.
|
2021-05-20 17:10:36 +02:00
|
|
|
|
origin:
|
2021-08-16 16:49:12 +02:00
|
|
|
|
The server making the request, if it is a federation request.
|
2021-05-20 17:10:36 +02:00
|
|
|
|
None if this is a local request.
|
2021-05-17 15:01:19 +02:00
|
|
|
|
|
|
|
|
|
Returns:
|
2021-08-16 16:49:12 +02:00
|
|
|
|
True if the room is accessible to the requesting user or server.
|
2021-05-17 15:01:19 +02:00
|
|
|
|
"""
|
2022-06-01 17:02:53 +02:00
|
|
|
|
state_ids = await self._storage_controllers.state.get_current_state_ids(room_id)
|
2021-05-20 17:10:36 +02:00
|
|
|
|
|
|
|
|
|
# If there's no state for the room, it isn't known.
|
|
|
|
|
if not state_ids:
|
2021-07-13 14:59:27 +02:00
|
|
|
|
# The user might have a pending invite for the room.
|
|
|
|
|
if requester and await self._store.get_invite_for_local_user_in_room(
|
|
|
|
|
requester, room_id
|
|
|
|
|
):
|
|
|
|
|
return True
|
|
|
|
|
|
2021-05-20 17:10:36 +02:00
|
|
|
|
logger.info("room %s is unknown, omitting from summary", room_id)
|
|
|
|
|
return False
|
|
|
|
|
|
2021-09-01 19:01:08 +02:00
|
|
|
|
try:
|
|
|
|
|
room_version = await self._store.get_room_version(room_id)
|
|
|
|
|
except UnsupportedRoomVersionError:
|
|
|
|
|
# If a room with an unsupported room version is encountered, ignore
|
|
|
|
|
# it to avoid breaking the entire summary response.
|
|
|
|
|
return False
|
2021-05-17 15:01:19 +02:00
|
|
|
|
|
2021-07-13 14:59:27 +02:00
|
|
|
|
# Include the room if it has join rules of public or knock.
|
|
|
|
|
join_rules_event_id = state_ids.get((EventTypes.JoinRules, ""))
|
|
|
|
|
if join_rules_event_id:
|
|
|
|
|
join_rules_event = await self._store.get_event(join_rules_event_id)
|
|
|
|
|
join_rule = join_rules_event.content.get("join_rule")
|
2022-05-17 12:41:39 +02:00
|
|
|
|
if (
|
|
|
|
|
join_rule == JoinRules.PUBLIC
|
|
|
|
|
or (room_version.msc2403_knocking and join_rule == JoinRules.KNOCK)
|
|
|
|
|
or (
|
|
|
|
|
room_version.msc3787_knock_restricted_join_rule
|
|
|
|
|
and join_rule == JoinRules.KNOCK_RESTRICTED
|
|
|
|
|
)
|
2021-07-13 14:59:27 +02:00
|
|
|
|
):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
# Include the room if it is peekable.
|
|
|
|
|
hist_vis_event_id = state_ids.get((EventTypes.RoomHistoryVisibility, ""))
|
|
|
|
|
if hist_vis_event_id:
|
|
|
|
|
hist_vis_ev = await self._store.get_event(hist_vis_event_id)
|
|
|
|
|
hist_vis = hist_vis_ev.content.get("history_visibility")
|
|
|
|
|
if hist_vis == HistoryVisibility.WORLD_READABLE:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
# Otherwise we need to check information specific to the user or server.
|
|
|
|
|
|
|
|
|
|
# If we have an authenticated requesting user, check if they are a member
|
|
|
|
|
# of the room (or can join the room).
|
2021-03-23 12:51:12 +01:00
|
|
|
|
if requester:
|
2021-05-20 17:10:36 +02:00
|
|
|
|
member_event_id = state_ids.get((EventTypes.Member, requester), None)
|
|
|
|
|
|
|
|
|
|
# If they're in the room they can see info on it.
|
|
|
|
|
if member_event_id:
|
|
|
|
|
member_event = await self._store.get_event(member_event_id)
|
|
|
|
|
if member_event.membership in (Membership.JOIN, Membership.INVITE):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
# Otherwise, check if they should be allowed access via membership in a space.
|
2021-06-18 19:41:33 +02:00
|
|
|
|
if await self._event_auth_handler.has_restricted_join_rules(
|
2021-06-02 17:31:41 +02:00
|
|
|
|
state_ids, room_version
|
|
|
|
|
):
|
2021-06-17 18:53:27 +02:00
|
|
|
|
allowed_rooms = (
|
|
|
|
|
await self._event_auth_handler.get_rooms_that_allow_join(state_ids)
|
2021-05-20 17:10:36 +02:00
|
|
|
|
)
|
2021-06-02 17:31:41 +02:00
|
|
|
|
if await self._event_auth_handler.is_user_in_rooms(
|
2021-06-17 18:53:27 +02:00
|
|
|
|
allowed_rooms, requester
|
2021-06-02 17:31:41 +02:00
|
|
|
|
):
|
|
|
|
|
return True
|
2021-05-20 17:10:36 +02:00
|
|
|
|
|
|
|
|
|
# If this is a request over federation, check if the host is in the room or
|
2021-07-13 14:59:27 +02:00
|
|
|
|
# has a user who could join the room.
|
2021-05-20 17:10:36 +02:00
|
|
|
|
elif origin:
|
2022-09-23 12:47:16 +02:00
|
|
|
|
if await self._event_auth_handler.is_host_in_room(
|
2021-07-13 14:59:27 +02:00
|
|
|
|
room_id, origin
|
|
|
|
|
) or await self._store.is_host_invited(room_id, origin):
|
2021-05-20 17:10:36 +02:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
# Alternately, if the host has a user in any of the spaces specified
|
|
|
|
|
# for access, then the host can see this room (and should do filtering
|
|
|
|
|
# if the requester cannot see it).
|
|
|
|
|
if await self._event_auth_handler.has_restricted_join_rules(
|
|
|
|
|
state_ids, room_version
|
|
|
|
|
):
|
2021-06-17 18:53:27 +02:00
|
|
|
|
allowed_rooms = (
|
|
|
|
|
await self._event_auth_handler.get_rooms_that_allow_join(state_ids)
|
2021-05-20 17:10:36 +02:00
|
|
|
|
)
|
2021-06-17 18:53:27 +02:00
|
|
|
|
for space_id in allowed_rooms:
|
2022-09-23 12:47:16 +02:00
|
|
|
|
if await self._event_auth_handler.is_host_in_room(space_id, origin):
|
2021-05-20 17:10:36 +02:00
|
|
|
|
return True
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-03-23 12:51:12 +01:00
|
|
|
|
logger.info(
|
2021-07-13 14:59:27 +02:00
|
|
|
|
"room %s is unpeekable and requester %s is not a member / not allowed to join, omitting from summary",
|
2021-03-23 12:51:12 +01:00
|
|
|
|
room_id,
|
2021-07-13 14:59:27 +02:00
|
|
|
|
requester or origin,
|
2021-03-23 12:51:12 +01:00
|
|
|
|
)
|
|
|
|
|
return False
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-08-10 16:56:54 +02:00
|
|
|
|
async def _is_remote_room_accessible(
|
2022-05-05 16:25:00 +02:00
|
|
|
|
self, requester: Optional[str], room_id: str, room: JsonDict
|
2021-08-10 16:56:54 +02:00
|
|
|
|
) -> bool:
|
|
|
|
|
"""
|
2021-08-16 16:49:12 +02:00
|
|
|
|
Calculate whether the room received over federation should be shown to the requester.
|
2021-08-10 16:56:54 +02:00
|
|
|
|
|
2021-08-16 16:49:12 +02:00
|
|
|
|
It should return true if:
|
2021-08-10 16:56:54 +02:00
|
|
|
|
|
|
|
|
|
* The requester is joined or can join the room (per MSC3173).
|
|
|
|
|
* The history visibility is set to world readable.
|
|
|
|
|
|
|
|
|
|
Note that the local server is not in the requested room (which is why the
|
|
|
|
|
remote call was made in the first place), but the user could have access
|
|
|
|
|
due to an invite, etc.
|
|
|
|
|
|
|
|
|
|
Args:
|
2022-05-05 16:25:00 +02:00
|
|
|
|
requester: The user requesting the summary. If not passed only world
|
|
|
|
|
readability is checked.
|
2021-08-10 16:56:54 +02:00
|
|
|
|
room_id: The room ID returned over federation.
|
2021-08-16 16:49:12 +02:00
|
|
|
|
room: The summary of the room returned over federation.
|
2021-08-10 16:56:54 +02:00
|
|
|
|
|
|
|
|
|
Returns:
|
2021-08-16 16:49:12 +02:00
|
|
|
|
True if the room is accessible to the requesting user.
|
2021-08-10 16:56:54 +02:00
|
|
|
|
"""
|
|
|
|
|
# The API doesn't return the room version so assume that a
|
|
|
|
|
# join rule of knock is valid.
|
|
|
|
|
if (
|
2022-05-26 13:10:28 +02:00
|
|
|
|
room.get("join_rule")
|
2022-05-24 18:50:50 +02:00
|
|
|
|
in (JoinRules.PUBLIC, JoinRules.KNOCK, JoinRules.KNOCK_RESTRICTED)
|
2021-08-10 16:56:54 +02:00
|
|
|
|
or room.get("world_readable") is True
|
|
|
|
|
):
|
|
|
|
|
return True
|
2022-05-05 16:25:00 +02:00
|
|
|
|
elif not requester:
|
|
|
|
|
return False
|
2021-08-10 16:56:54 +02:00
|
|
|
|
|
2022-02-28 19:33:00 +01:00
|
|
|
|
# Check if the user is a member of any of the allowed rooms from the response.
|
|
|
|
|
allowed_rooms = room.get("allowed_room_ids")
|
2021-08-10 16:56:54 +02:00
|
|
|
|
if allowed_rooms and isinstance(allowed_rooms, list):
|
|
|
|
|
if await self._event_auth_handler.is_user_in_rooms(
|
|
|
|
|
allowed_rooms, requester
|
|
|
|
|
):
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
# Finally, check locally if we can access the room. The user might
|
|
|
|
|
# already be in the room (if it was a child room), or there might be a
|
|
|
|
|
# pending invite, etc.
|
2021-08-11 21:04:51 +02:00
|
|
|
|
return await self._is_local_room_accessible(room_id, requester)
|
2021-08-10 16:56:54 +02:00
|
|
|
|
|
2021-08-06 13:40:29 +02:00
|
|
|
|
async def _build_room_entry(self, room_id: str, for_federation: bool) -> JsonDict:
|
|
|
|
|
"""
|
2021-08-16 16:49:12 +02:00
|
|
|
|
Generate en entry summarising a single room.
|
2021-08-06 13:40:29 +02:00
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
room_id: The room ID to summarize.
|
|
|
|
|
for_federation: True if this is a summary requested over federation
|
|
|
|
|
(which includes additional fields).
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The JSON dictionary for the room.
|
|
|
|
|
"""
|
2021-03-18 19:24:16 +01:00
|
|
|
|
stats = await self._store.get_room_with_stats(room_id)
|
|
|
|
|
|
|
|
|
|
# currently this should be impossible because we call
|
2021-08-11 21:04:51 +02:00
|
|
|
|
# _is_local_room_accessible on the room before we get here, so
|
2021-03-18 19:24:16 +01:00
|
|
|
|
# there should always be an entry
|
|
|
|
|
assert stats is not None, "unable to retrieve stats for %s" % (room_id,)
|
|
|
|
|
|
|
|
|
|
entry = {
|
|
|
|
|
"room_id": stats["room_id"],
|
|
|
|
|
"name": stats["name"],
|
|
|
|
|
"topic": stats["topic"],
|
|
|
|
|
"canonical_alias": stats["canonical_alias"],
|
|
|
|
|
"num_joined_members": stats["joined_members"],
|
|
|
|
|
"avatar_url": stats["avatar"],
|
2022-01-05 21:33:43 +01:00
|
|
|
|
"join_rule": stats["join_rules"],
|
2021-03-18 19:24:16 +01:00
|
|
|
|
"world_readable": (
|
|
|
|
|
stats["history_visibility"] == HistoryVisibility.WORLD_READABLE
|
|
|
|
|
),
|
|
|
|
|
"guest_can_join": stats["guest_access"] == "can_join",
|
2022-12-13 14:43:53 +01:00
|
|
|
|
"room_type": stats["room_type"],
|
2021-03-18 19:24:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 16:25:00 +02:00
|
|
|
|
if self._msc3266_enabled:
|
|
|
|
|
entry["im.nheko.summary.version"] = stats["version"]
|
|
|
|
|
entry["im.nheko.summary.encryption"] = stats["encryption"]
|
|
|
|
|
|
2021-08-06 13:40:29 +02:00
|
|
|
|
# Federation requests need to provide additional information so the
|
|
|
|
|
# requested server is able to filter the response appropriately.
|
|
|
|
|
if for_federation:
|
2022-12-13 14:43:53 +01:00
|
|
|
|
current_state_ids = (
|
|
|
|
|
await self._storage_controllers.state.get_current_state_ids(room_id)
|
|
|
|
|
)
|
2021-08-06 13:40:29 +02:00
|
|
|
|
room_version = await self._store.get_room_version(room_id)
|
2022-12-13 14:43:53 +01:00
|
|
|
|
|
2021-08-06 13:40:29 +02:00
|
|
|
|
if await self._event_auth_handler.has_restricted_join_rules(
|
|
|
|
|
current_state_ids, room_version
|
|
|
|
|
):
|
|
|
|
|
allowed_rooms = (
|
|
|
|
|
await self._event_auth_handler.get_rooms_that_allow_join(
|
|
|
|
|
current_state_ids
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
if allowed_rooms:
|
|
|
|
|
entry["allowed_room_ids"] = allowed_rooms
|
|
|
|
|
|
2021-03-18 19:24:16 +01:00
|
|
|
|
# Filter out Nones – rather omit the field altogether
|
|
|
|
|
room_entry = {k: v for k, v in entry.items() if v is not None}
|
|
|
|
|
|
|
|
|
|
return room_entry
|
|
|
|
|
|
|
|
|
|
async def _get_child_events(self, room_id: str) -> Iterable[EventBase]:
|
2021-05-11 18:57:39 +02:00
|
|
|
|
"""
|
|
|
|
|
Get the child events for a given room.
|
|
|
|
|
|
|
|
|
|
The returned results are sorted for stability.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
room_id: The room id to get the children of.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
An iterable of sorted child events.
|
|
|
|
|
"""
|
|
|
|
|
|
2021-03-18 19:24:16 +01:00
|
|
|
|
# look for child rooms/spaces.
|
2022-06-01 17:02:53 +02:00
|
|
|
|
current_state_ids = await self._storage_controllers.state.get_current_state_ids(
|
|
|
|
|
room_id
|
|
|
|
|
)
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
|
|
|
|
events = await self._store.get_events_as_list(
|
|
|
|
|
[
|
|
|
|
|
event_id
|
|
|
|
|
for key, event_id in current_state_ids.items()
|
2021-06-15 14:03:17 +02:00
|
|
|
|
if key[0] == EventTypes.SpaceChild
|
2021-03-18 19:24:16 +01:00
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
2021-05-11 18:57:39 +02:00
|
|
|
|
# filter out any events without a "via" (which implies it has been redacted),
|
|
|
|
|
# and order to ensure we return stable results.
|
|
|
|
|
return sorted(filter(_has_valid_via, events), key=_child_events_comparison_key)
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-08-16 16:49:12 +02:00
|
|
|
|
async def get_room_summary(
|
|
|
|
|
self,
|
|
|
|
|
requester: Optional[str],
|
|
|
|
|
room_id: str,
|
|
|
|
|
remote_room_hosts: Optional[List[str]] = None,
|
|
|
|
|
) -> JsonDict:
|
|
|
|
|
"""
|
|
|
|
|
Implementation of the room summary C-S API from MSC3266
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
requester: user id of the user making this request, will be None
|
|
|
|
|
for unauthenticated requests
|
|
|
|
|
|
|
|
|
|
room_id: room id to summarise.
|
|
|
|
|
|
|
|
|
|
remote_room_hosts: a list of homeservers to try fetching data through
|
|
|
|
|
if we don't know it ourselves
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
summary dict to return
|
|
|
|
|
"""
|
|
|
|
|
is_in_room = await self._store.is_host_joined(room_id, self._server_name)
|
|
|
|
|
|
|
|
|
|
if is_in_room:
|
|
|
|
|
room_entry = await self._summarize_local_room(
|
|
|
|
|
requester,
|
|
|
|
|
None,
|
|
|
|
|
room_id,
|
|
|
|
|
# Suggested-only doesn't matter since no children are requested.
|
|
|
|
|
suggested_only=False,
|
2022-02-28 19:33:00 +01:00
|
|
|
|
include_children=False,
|
2021-08-16 16:49:12 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not room_entry:
|
|
|
|
|
raise NotFoundError("Room not found or is not accessible")
|
|
|
|
|
|
|
|
|
|
room_summary = room_entry.room
|
|
|
|
|
|
|
|
|
|
# If there was a requester, add their membership.
|
|
|
|
|
if requester:
|
|
|
|
|
(
|
|
|
|
|
membership,
|
|
|
|
|
_,
|
|
|
|
|
) = await self._store.get_local_current_membership_for_user_in_room(
|
|
|
|
|
requester, room_id
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
room_summary["membership"] = membership or "leave"
|
|
|
|
|
else:
|
2022-05-05 16:25:00 +02:00
|
|
|
|
# Reuse the hierarchy query over federation
|
|
|
|
|
if remote_room_hosts is None:
|
|
|
|
|
raise SynapseError(400, "Missing via to query remote room")
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
room_entry,
|
|
|
|
|
children_room_entries,
|
|
|
|
|
inaccessible_children,
|
|
|
|
|
) = await self._summarize_remote_room_hierarchy(
|
|
|
|
|
_RoomQueueEntry(room_id, remote_room_hosts),
|
|
|
|
|
suggested_only=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# The results over federation might include rooms that we, as the
|
|
|
|
|
# requesting server, are allowed to see, but the requesting user is
|
|
|
|
|
# not permitted to see.
|
|
|
|
|
#
|
|
|
|
|
# Filter the returned results to only what is accessible to the user.
|
|
|
|
|
if not room_entry or not await self._is_remote_room_accessible(
|
|
|
|
|
requester, room_entry.room_id, room_entry.room
|
|
|
|
|
):
|
|
|
|
|
raise NotFoundError("Room not found or is not accessible")
|
|
|
|
|
|
|
|
|
|
room = dict(room_entry.room)
|
|
|
|
|
room.pop("allowed_room_ids", None)
|
|
|
|
|
|
|
|
|
|
# If there was a requester, add their membership.
|
|
|
|
|
# We keep the membership in the local membership table unless the
|
|
|
|
|
# room is purged even for remote rooms.
|
|
|
|
|
if requester:
|
|
|
|
|
(
|
|
|
|
|
membership,
|
|
|
|
|
_,
|
|
|
|
|
) = await self._store.get_local_current_membership_for_user_in_room(
|
|
|
|
|
requester, room_id
|
|
|
|
|
)
|
|
|
|
|
room["membership"] = membership or "leave"
|
|
|
|
|
|
|
|
|
|
return room
|
2021-08-16 16:49:12 +02:00
|
|
|
|
|
|
|
|
|
return room_summary
|
|
|
|
|
|
2021-03-18 19:24:16 +01:00
|
|
|
|
|
2021-08-05 14:39:17 +02:00
|
|
|
|
@attr.s(frozen=True, slots=True, auto_attribs=True)
|
2021-03-23 12:51:12 +01:00
|
|
|
|
class _RoomQueueEntry:
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# The room ID of this entry.
|
2021-08-05 14:39:17 +02:00
|
|
|
|
room_id: str
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# The server to query if the room is not known locally.
|
2023-01-26 18:31:58 +01:00
|
|
|
|
via: StrCollection
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# The minimum number of hops necessary to get to this room (compared to the
|
|
|
|
|
# originally requested room).
|
2021-08-10 19:08:17 +02:00
|
|
|
|
depth: int = 0
|
2021-08-16 14:06:17 +02:00
|
|
|
|
# The room summary for this room returned via federation. This will only be
|
|
|
|
|
# used if the room is not known locally (and is not a space).
|
|
|
|
|
remote_room: Optional[JsonDict] = None
|
2021-08-05 14:39:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(frozen=True, slots=True, auto_attribs=True)
|
|
|
|
|
class _RoomEntry:
|
|
|
|
|
room_id: str
|
|
|
|
|
# The room summary for this room.
|
|
|
|
|
room: JsonDict
|
|
|
|
|
# An iterable of the sorted, stripped children events for children of this room.
|
|
|
|
|
#
|
|
|
|
|
# This may not include all children.
|
2021-08-16 14:06:17 +02:00
|
|
|
|
children_state_events: Sequence[JsonDict] = ()
|
2021-08-10 19:08:17 +02:00
|
|
|
|
|
2022-03-08 14:09:11 +01:00
|
|
|
|
def as_json(self, for_client: bool = False) -> JsonDict:
|
2021-08-16 14:06:17 +02:00
|
|
|
|
"""
|
|
|
|
|
Returns a JSON dictionary suitable for the room hierarchy endpoint.
|
|
|
|
|
|
|
|
|
|
It returns the room summary including the stripped m.space.child events
|
|
|
|
|
as a sub-key.
|
2022-03-08 14:09:11 +01:00
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
for_client: If true, any server-server only fields are stripped from
|
|
|
|
|
the result.
|
|
|
|
|
|
2021-08-16 14:06:17 +02:00
|
|
|
|
"""
|
2021-08-10 19:08:17 +02:00
|
|
|
|
result = dict(self.room)
|
2022-03-08 14:09:11 +01:00
|
|
|
|
|
|
|
|
|
# Before returning to the client, remove the allowed_room_ids key, if it
|
|
|
|
|
# exists.
|
|
|
|
|
if for_client:
|
|
|
|
|
result.pop("allowed_room_ids", False)
|
|
|
|
|
|
2021-08-16 14:06:17 +02:00
|
|
|
|
result["children_state"] = self.children_state_events
|
2021-08-10 19:08:17 +02:00
|
|
|
|
return result
|
2021-03-24 13:45:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _has_valid_via(e: EventBase) -> bool:
|
|
|
|
|
via = e.content.get("via")
|
2022-03-02 14:18:51 +01:00
|
|
|
|
if not via or not isinstance(via, list):
|
2021-03-24 13:45:39 +01:00
|
|
|
|
return False
|
|
|
|
|
for v in via:
|
|
|
|
|
if not isinstance(v, str):
|
|
|
|
|
logger.debug("Ignoring edge event %s with invalid via entry", e.event_id)
|
|
|
|
|
return False
|
|
|
|
|
return True
|
2021-03-23 12:51:12 +01:00
|
|
|
|
|
|
|
|
|
|
2021-03-18 19:24:16 +01:00
|
|
|
|
def _is_suggested_child_event(edge_event: EventBase) -> bool:
|
|
|
|
|
suggested = edge_event.content.get("suggested")
|
|
|
|
|
if isinstance(suggested, bool) and suggested:
|
|
|
|
|
return True
|
|
|
|
|
logger.debug("Ignorning not-suggested child %s", edge_event.state_key)
|
|
|
|
|
return False
|
2021-05-11 18:57:39 +02:00
|
|
|
|
|
|
|
|
|
|
2021-05-17 15:59:17 +02:00
|
|
|
|
# Order may only contain characters in the range of \x20 (space) to \x7E (~) inclusive.
|
|
|
|
|
_INVALID_ORDER_CHARS_RE = re.compile(r"[^\x20-\x7E]")
|
2021-05-11 18:57:39 +02:00
|
|
|
|
|
|
|
|
|
|
2021-09-01 18:59:52 +02:00
|
|
|
|
def _child_events_comparison_key(
|
|
|
|
|
child: EventBase,
|
|
|
|
|
) -> Tuple[bool, Optional[str], int, str]:
|
2021-05-11 18:57:39 +02:00
|
|
|
|
"""
|
|
|
|
|
Generate a value for comparing two child events for ordering.
|
|
|
|
|
|
2021-09-01 18:59:52 +02:00
|
|
|
|
The rules for ordering are:
|
2021-05-11 18:57:39 +02:00
|
|
|
|
|
|
|
|
|
1. The 'order' key, if it is valid.
|
2021-09-01 18:59:52 +02:00
|
|
|
|
2. The 'origin_server_ts' of the 'm.space.child' event.
|
2021-05-11 18:57:39 +02:00
|
|
|
|
3. The 'room_id'.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
child: The event for generating a comparison key.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
The comparison key as a tuple of:
|
|
|
|
|
False if the ordering is valid.
|
2021-09-01 18:59:52 +02:00
|
|
|
|
The 'order' field or None if it is not given or invalid.
|
|
|
|
|
The 'origin_server_ts' field.
|
2021-05-11 18:57:39 +02:00
|
|
|
|
The room ID.
|
|
|
|
|
"""
|
|
|
|
|
order = child.content.get("order")
|
|
|
|
|
# If order is not a string or doesn't meet the requirements, ignore it.
|
|
|
|
|
if not isinstance(order, str):
|
|
|
|
|
order = None
|
|
|
|
|
elif len(order) > 50 or _INVALID_ORDER_CHARS_RE.search(order):
|
|
|
|
|
order = None
|
|
|
|
|
|
|
|
|
|
# Items without an order come last.
|
2021-09-23 12:59:07 +02:00
|
|
|
|
return order is None, order, child.origin_server_ts, child.room_id
|