2016-09-14 09:07:37 -04:00
|
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
|
# Copyright 2014 - 2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
|
#
|
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
|
#
|
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2016-09-14 09:07:37 -04:00
|
|
|
|
#
|
|
|
|
|
#
|
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
|
import logging
|
2024-01-08 12:24:20 -05:00
|
|
|
|
from typing import TYPE_CHECKING, Any, List, Optional, Tuple
|
2016-09-14 09:07:37 -04:00
|
|
|
|
|
2021-12-30 13:47:12 -05:00
|
|
|
|
import attr
|
2018-07-09 02:09:20 -04:00
|
|
|
|
import msgpack
|
|
|
|
|
from unpaddedbase64 import decode_base64, encode_base64
|
|
|
|
|
|
2021-09-06 07:17:16 -04:00
|
|
|
|
from synapse.api.constants import (
|
|
|
|
|
EventContentFields,
|
|
|
|
|
EventTypes,
|
|
|
|
|
GuestAccess,
|
|
|
|
|
HistoryVisibility,
|
|
|
|
|
JoinRules,
|
2022-06-29 13:12:45 -04:00
|
|
|
|
PublicRoomsFilterFields,
|
2021-09-06 07:17:16 -04:00
|
|
|
|
)
|
2021-07-15 05:35:46 -04:00
|
|
|
|
from synapse.api.errors import (
|
|
|
|
|
Codes,
|
|
|
|
|
HttpResponseException,
|
|
|
|
|
RequestSendFailed,
|
|
|
|
|
SynapseError,
|
|
|
|
|
)
|
2023-10-31 13:13:28 -04:00
|
|
|
|
from synapse.storage.databases.main.room import LargestRoomStats
|
2023-10-02 10:22:36 -04:00
|
|
|
|
from synapse.types import JsonDict, JsonMapping, ThirdPartyInstanceID
|
2021-09-20 08:56:23 -04:00
|
|
|
|
from synapse.util.caches.descriptors import _CacheContext, cached
|
2016-09-14 09:07:37 -04:00
|
|
|
|
from synapse.util.caches.response_cache import ResponseCache
|
2016-09-15 04:08:57 -04:00
|
|
|
|
|
2020-12-29 17:42:10 -05:00
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
|
from synapse.server import HomeServer
|
2020-12-29 17:42:10 -05:00
|
|
|
|
|
2016-09-14 09:07:37 -04:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
REMOTE_ROOM_LIST_POLL_INTERVAL = 60 * 1000
|
|
|
|
|
|
2016-12-06 05:43:48 -05:00
|
|
|
|
# This is used to indicate we should only return rooms published to the main list.
|
2018-07-13 07:03:34 -04:00
|
|
|
|
EMPTY_THIRD_PARTY_ID = ThirdPartyInstanceID(None, None)
|
2016-12-06 05:43:48 -05:00
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
# Maximum number of local public rooms returned over the CS or SS API
|
|
|
|
|
MAX_PUBLIC_ROOMS_IN_RESPONSE = 100
|
|
|
|
|
|
2016-12-06 05:43:48 -05:00
|
|
|
|
|
2021-10-08 07:44:43 -04:00
|
|
|
|
class RoomListHandler:
|
2020-12-29 17:42:10 -05:00
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2022-02-23 06:04:02 -05:00
|
|
|
|
self.store = hs.get_datastores().main
|
2022-06-01 11:02:53 -04:00
|
|
|
|
self._storage_controllers = hs.get_storage_controllers()
|
2021-10-08 07:44:43 -04:00
|
|
|
|
self.hs = hs
|
2021-09-24 07:25:21 -04:00
|
|
|
|
self.enable_room_list_search = hs.config.roomdirectory.enable_room_list_search
|
2021-07-16 13:22:36 -04:00
|
|
|
|
self.response_cache: ResponseCache[
|
|
|
|
|
Tuple[Optional[int], Optional[str], Optional[ThirdPartyInstanceID]]
|
|
|
|
|
] = ResponseCache(hs.get_clock(), "room_list")
|
|
|
|
|
self.remote_response_cache: ResponseCache[
|
|
|
|
|
Tuple[str, Optional[int], Optional[str], bool, Optional[str]]
|
|
|
|
|
] = ResponseCache(hs.get_clock(), "remote_room_list", timeout_ms=30 * 1000)
|
2016-09-14 09:07:37 -04:00
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
async def get_local_public_room_list(
|
2016-09-15 12:35:20 -04:00
|
|
|
|
self,
|
2020-12-29 17:42:10 -05:00
|
|
|
|
limit: Optional[int] = None,
|
|
|
|
|
since_token: Optional[str] = None,
|
|
|
|
|
search_filter: Optional[dict] = None,
|
2021-06-08 11:19:25 -04:00
|
|
|
|
network_tuple: Optional[ThirdPartyInstanceID] = EMPTY_THIRD_PARTY_ID,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
from_federation_origin: Optional[str] = None,
|
2020-12-29 17:42:10 -05:00
|
|
|
|
) -> JsonDict:
|
2016-12-06 05:43:48 -05:00
|
|
|
|
"""Generate a local public room list.
|
|
|
|
|
|
|
|
|
|
There are multiple different lists: the main one plus one per third
|
|
|
|
|
party network. A client can ask for a specific list or to return all.
|
|
|
|
|
|
|
|
|
|
Args:
|
2020-12-29 17:42:10 -05:00
|
|
|
|
limit
|
|
|
|
|
since_token
|
|
|
|
|
search_filter
|
|
|
|
|
network_tuple: Which public list to use.
|
2016-12-06 05:43:48 -05:00
|
|
|
|
This can be (None, None) to indicate the main list, or a particular
|
|
|
|
|
appservice and network id to use an appservice specific one.
|
|
|
|
|
Setting to None returns all public rooms across all lists.
|
2024-01-08 12:24:20 -05:00
|
|
|
|
from_federation_origin: the server name of the requester, or None
|
|
|
|
|
if the request is not from federation.
|
2016-12-06 05:43:48 -05:00
|
|
|
|
"""
|
2019-03-20 10:25:58 -04:00
|
|
|
|
if not self.enable_room_list_search:
|
2020-07-21 07:51:48 -04:00
|
|
|
|
return {"chunk": [], "total_room_count_estimate": 0}
|
2019-03-19 12:50:51 -04:00
|
|
|
|
|
2019-03-20 10:25:28 -04:00
|
|
|
|
logger.info(
|
|
|
|
|
"Getting public room list: limit=%r, since=%r, search=%r, network=%r",
|
|
|
|
|
limit,
|
|
|
|
|
since_token,
|
|
|
|
|
bool(search_filter),
|
|
|
|
|
network_tuple,
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
capped_limit: int = (
|
|
|
|
|
MAX_PUBLIC_ROOMS_IN_RESPONSE
|
|
|
|
|
if limit is None or limit > MAX_PUBLIC_ROOMS_IN_RESPONSE
|
|
|
|
|
else limit
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if search_filter or from_federation_origin is not None:
|
2016-12-07 04:58:33 -05:00
|
|
|
|
# We explicitly don't bother caching searches or requests for
|
|
|
|
|
# appservice specific lists.
|
2024-01-08 12:24:20 -05:00
|
|
|
|
# We also don't bother caching requests from federated homeservers.
|
|
|
|
|
logger.debug("Bypassing cache as search or federation request.")
|
2019-01-24 07:44:27 -05:00
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
return await self._get_public_room_list(
|
2024-01-08 12:24:20 -05:00
|
|
|
|
capped_limit,
|
2020-04-30 06:38:07 -04:00
|
|
|
|
since_token,
|
|
|
|
|
search_filter,
|
|
|
|
|
network_tuple=network_tuple,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
from_federation_origin=from_federation_origin,
|
2016-12-06 05:43:48 -05:00
|
|
|
|
)
|
2016-09-16 04:05:11 -04:00
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
key = (capped_limit, since_token, network_tuple)
|
2020-07-21 07:51:48 -04:00
|
|
|
|
return await self.response_cache.wrap(
|
2018-04-12 07:08:59 -04:00
|
|
|
|
key,
|
|
|
|
|
self._get_public_room_list,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
capped_limit,
|
2019-02-22 07:11:43 -05:00
|
|
|
|
since_token,
|
|
|
|
|
network_tuple=network_tuple,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
from_federation_origin=from_federation_origin,
|
2018-04-12 07:08:59 -04:00
|
|
|
|
)
|
2016-09-14 09:07:37 -04:00
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
async def _get_public_room_list(
|
2016-09-15 12:35:20 -04:00
|
|
|
|
self,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
limit: int,
|
2020-04-06 07:35:30 -04:00
|
|
|
|
since_token: Optional[str] = None,
|
2020-12-29 17:42:10 -05:00
|
|
|
|
search_filter: Optional[dict] = None,
|
2021-06-08 11:19:25 -04:00
|
|
|
|
network_tuple: Optional[ThirdPartyInstanceID] = EMPTY_THIRD_PARTY_ID,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
from_federation_origin: Optional[str] = None,
|
2020-12-29 17:42:10 -05:00
|
|
|
|
) -> JsonDict:
|
2019-02-26 09:23:40 -05:00
|
|
|
|
"""Generate a public room list.
|
|
|
|
|
Args:
|
2020-04-06 07:35:30 -04:00
|
|
|
|
limit: Maximum amount of rooms to return.
|
|
|
|
|
since_token:
|
|
|
|
|
search_filter: Dictionary to filter rooms by.
|
|
|
|
|
network_tuple: Which public list to use.
|
2019-02-26 09:23:40 -05:00
|
|
|
|
This can be (None, None) to indicate the main list, or a particular
|
|
|
|
|
appservice and network id to use an appservice specific one.
|
|
|
|
|
Setting to None returns all public rooms across all lists.
|
2024-01-08 12:24:20 -05:00
|
|
|
|
from_federation_origin: the server name of the requester, or None
|
|
|
|
|
if the request is not from federation.
|
2019-02-26 09:23:40 -05:00
|
|
|
|
"""
|
2016-09-15 04:08:57 -04:00
|
|
|
|
|
2019-10-02 09:08:35 -04:00
|
|
|
|
# Pagination tokens work by storing the room ID sent in the last batch,
|
|
|
|
|
# plus the direction (forwards or backwards). Next batch tokens always
|
|
|
|
|
# go forwards, prev batch tokens always go backwards.
|
2016-09-14 12:28:52 -04:00
|
|
|
|
|
2016-09-15 05:36:19 -04:00
|
|
|
|
if since_token:
|
2019-10-02 09:08:35 -04:00
|
|
|
|
batch_token = RoomListNextBatch.from_token(since_token)
|
2017-03-13 05:50:10 -04:00
|
|
|
|
|
2021-07-16 13:22:36 -04:00
|
|
|
|
bounds: Optional[Tuple[int, str]] = (
|
2020-12-29 17:42:10 -05:00
|
|
|
|
batch_token.last_joined_members,
|
|
|
|
|
batch_token.last_room_id,
|
2021-07-16 13:22:36 -04:00
|
|
|
|
)
|
2019-10-02 09:08:35 -04:00
|
|
|
|
forwards = batch_token.direction_is_forward
|
2020-12-29 17:42:10 -05:00
|
|
|
|
has_batch_token = True
|
2019-10-02 09:08:35 -04:00
|
|
|
|
else:
|
2019-10-02 10:09:10 -04:00
|
|
|
|
bounds = None
|
2016-09-14 12:28:52 -04:00
|
|
|
|
|
2019-10-02 09:08:35 -04:00
|
|
|
|
forwards = True
|
2020-12-29 17:42:10 -05:00
|
|
|
|
has_batch_token = False
|
2016-09-14 12:28:52 -04:00
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
if from_federation_origin is None:
|
|
|
|
|
# Client-Server API:
|
|
|
|
|
# we request one more than wanted to see if there are more pages to come
|
|
|
|
|
probing_limit = limit + 1
|
|
|
|
|
else:
|
|
|
|
|
# Federation API:
|
|
|
|
|
# we request a handful more in case any get filtered out by ACLs
|
|
|
|
|
# as a best easy effort attempt to return the full number of entries
|
|
|
|
|
# specified by `limit`.
|
|
|
|
|
probing_limit = limit + 10
|
2016-09-14 12:28:52 -04:00
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
results = await self.store.get_largest_public_rooms(
|
2019-10-02 09:08:35 -04:00
|
|
|
|
network_tuple,
|
|
|
|
|
search_filter,
|
|
|
|
|
probing_limit,
|
2019-10-02 10:09:10 -04:00
|
|
|
|
bounds=bounds,
|
2019-10-02 09:08:35 -04:00
|
|
|
|
forwards=forwards,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
ignore_non_federatable=from_federation_origin is not None,
|
2017-11-14 04:23:56 -05:00
|
|
|
|
)
|
2016-09-14 12:28:52 -04:00
|
|
|
|
|
2023-10-31 13:13:28 -04:00
|
|
|
|
def build_room_entry(room: LargestRoomStats) -> JsonDict:
|
2019-10-02 09:08:35 -04:00
|
|
|
|
entry = {
|
2023-10-31 13:13:28 -04:00
|
|
|
|
"room_id": room.room_id,
|
|
|
|
|
"name": room.name,
|
|
|
|
|
"topic": room.topic,
|
|
|
|
|
"canonical_alias": room.canonical_alias,
|
|
|
|
|
"num_joined_members": room.joined_members,
|
|
|
|
|
"avatar_url": room.avatar,
|
|
|
|
|
"world_readable": room.history_visibility
|
2020-12-16 08:46:37 -05:00
|
|
|
|
== HistoryVisibility.WORLD_READABLE,
|
2023-10-31 13:13:28 -04:00
|
|
|
|
"guest_can_join": room.guest_access == "can_join",
|
|
|
|
|
"join_rule": room.join_rules,
|
|
|
|
|
"room_type": room.room_type,
|
2019-10-02 09:08:35 -04:00
|
|
|
|
}
|
2016-09-14 12:28:52 -04:00
|
|
|
|
|
2019-10-02 09:08:35 -04:00
|
|
|
|
# Filter out Nones – rather omit the field altogether
|
|
|
|
|
return {k: v for k, v in entry.items() if v is not None}
|
2016-09-17 09:40:28 -04:00
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
# Build a list of up to `limit` entries.
|
|
|
|
|
room_entries: List[JsonDict] = []
|
|
|
|
|
rooms_iterator = results if forwards else reversed(results)
|
|
|
|
|
|
|
|
|
|
# Track the first and last 'considered' rooms so that we can provide correct
|
|
|
|
|
# next_batch/prev_batch tokens.
|
|
|
|
|
# This is needed because the loop might finish early when
|
|
|
|
|
# `len(room_entries) >= limit` and we might be left with rooms we didn't
|
|
|
|
|
# 'consider' (iterate over) and we should save those rooms for the next
|
|
|
|
|
# batch.
|
|
|
|
|
first_considered_room: Optional[LargestRoomStats] = None
|
|
|
|
|
last_considered_room: Optional[LargestRoomStats] = None
|
|
|
|
|
cut_off_due_to_limit: bool = False
|
|
|
|
|
|
|
|
|
|
for room_result in rooms_iterator:
|
|
|
|
|
if len(room_entries) >= limit:
|
|
|
|
|
cut_off_due_to_limit = True
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if first_considered_room is None:
|
|
|
|
|
first_considered_room = room_result
|
|
|
|
|
last_considered_room = room_result
|
|
|
|
|
|
|
|
|
|
if from_federation_origin is not None:
|
|
|
|
|
# If this is a federated request, apply server ACLs if the room has any set
|
|
|
|
|
acl_evaluator = (
|
|
|
|
|
await self._storage_controllers.state.get_server_acl_for_room(
|
|
|
|
|
room_result.room_id
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if acl_evaluator is not None:
|
|
|
|
|
if not acl_evaluator.server_matches_acl_event(
|
|
|
|
|
from_federation_origin
|
|
|
|
|
):
|
|
|
|
|
# the requesting server is ACL blocked by the room,
|
|
|
|
|
# don't show in directory
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
room_entries.append(build_room_entry(room_result))
|
|
|
|
|
|
|
|
|
|
if not forwards:
|
|
|
|
|
# If we are paginating backwards, we still return the chunk in
|
|
|
|
|
# biggest-first order, so reverse again.
|
|
|
|
|
room_entries.reverse()
|
|
|
|
|
# Swap the order of first/last considered rooms.
|
|
|
|
|
first_considered_room, last_considered_room = (
|
|
|
|
|
last_considered_room,
|
|
|
|
|
first_considered_room,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
response: JsonDict = {
|
|
|
|
|
"chunk": room_entries,
|
|
|
|
|
}
|
2019-10-02 09:08:35 -04:00
|
|
|
|
num_results = len(results)
|
2016-09-21 08:17:08 -04:00
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
more_to_come_from_database = num_results == probing_limit
|
|
|
|
|
|
|
|
|
|
if forwards and has_batch_token:
|
|
|
|
|
# If there was a token given then we assume that there
|
|
|
|
|
# must be previous results, even if there were no results in this batch.
|
|
|
|
|
if first_considered_room is not None:
|
|
|
|
|
response["prev_batch"] = RoomListNextBatch(
|
|
|
|
|
last_joined_members=first_considered_room.joined_members,
|
|
|
|
|
last_room_id=first_considered_room.room_id,
|
|
|
|
|
direction_is_forward=False,
|
|
|
|
|
).to_token()
|
2016-09-15 05:15:37 -04:00
|
|
|
|
else:
|
2024-01-08 12:24:20 -05:00
|
|
|
|
# If we didn't find any results this time,
|
|
|
|
|
# we don't have an actual room ID to put in the token.
|
|
|
|
|
# But since `first_considered_room` is None, we know that we have
|
|
|
|
|
# reached the end of the results.
|
|
|
|
|
# So we can use a token of (0, empty room ID) to paginate from the end
|
|
|
|
|
# next time.
|
|
|
|
|
response["prev_batch"] = RoomListNextBatch(
|
|
|
|
|
last_joined_members=0,
|
|
|
|
|
last_room_id="",
|
|
|
|
|
direction_is_forward=False,
|
|
|
|
|
).to_token()
|
2019-10-02 09:08:35 -04:00
|
|
|
|
|
|
|
|
|
if num_results > 0:
|
2024-01-08 12:24:20 -05:00
|
|
|
|
assert first_considered_room is not None
|
|
|
|
|
assert last_considered_room is not None
|
2019-10-02 09:08:35 -04:00
|
|
|
|
if forwards:
|
2024-01-08 12:24:20 -05:00
|
|
|
|
if more_to_come_from_database or cut_off_due_to_limit:
|
2019-10-02 09:08:35 -04:00
|
|
|
|
response["next_batch"] = RoomListNextBatch(
|
2024-01-08 12:24:20 -05:00
|
|
|
|
last_joined_members=last_considered_room.joined_members,
|
|
|
|
|
last_room_id=last_considered_room.room_id,
|
2019-10-02 10:09:10 -04:00
|
|
|
|
direction_is_forward=True,
|
2019-10-02 09:08:35 -04:00
|
|
|
|
).to_token()
|
2024-01-08 12:24:20 -05:00
|
|
|
|
else: # backwards
|
2020-12-29 17:42:10 -05:00
|
|
|
|
if has_batch_token:
|
2019-10-02 09:08:35 -04:00
|
|
|
|
response["next_batch"] = RoomListNextBatch(
|
2024-01-08 12:24:20 -05:00
|
|
|
|
last_joined_members=last_considered_room.joined_members,
|
|
|
|
|
last_room_id=last_considered_room.room_id,
|
2019-10-02 10:09:10 -04:00
|
|
|
|
direction_is_forward=True,
|
2019-10-02 09:08:35 -04:00
|
|
|
|
).to_token()
|
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
if more_to_come_from_database or cut_off_due_to_limit:
|
2019-10-02 09:08:35 -04:00
|
|
|
|
response["prev_batch"] = RoomListNextBatch(
|
2024-01-08 12:24:20 -05:00
|
|
|
|
last_joined_members=first_considered_room.joined_members,
|
|
|
|
|
last_room_id=first_considered_room.room_id,
|
2019-10-02 10:09:10 -04:00
|
|
|
|
direction_is_forward=False,
|
2019-10-02 09:08:35 -04:00
|
|
|
|
).to_token()
|
|
|
|
|
|
2024-01-08 12:24:20 -05:00
|
|
|
|
# We can't efficiently count the total number of rooms that are not
|
|
|
|
|
# blocked by ACLs, but this is just an estimate so that should be
|
|
|
|
|
# good enough.
|
2020-07-21 07:51:48 -04:00
|
|
|
|
response["total_room_count_estimate"] = await self.store.count_public_rooms(
|
2022-06-29 13:12:45 -04:00
|
|
|
|
network_tuple,
|
2024-01-08 12:24:20 -05:00
|
|
|
|
ignore_non_federatable=from_federation_origin is not None,
|
2022-06-29 13:12:45 -04:00
|
|
|
|
search_filter=search_filter,
|
2019-10-02 09:08:35 -04:00
|
|
|
|
)
|
2017-03-10 12:39:35 -05:00
|
|
|
|
|
2019-10-02 09:08:35 -04:00
|
|
|
|
return response
|
2017-03-10 12:39:35 -05:00
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
@cached(num_args=1, cache_context=True)
|
|
|
|
|
async def generate_room_entry(
|
2019-02-26 09:13:38 -05:00
|
|
|
|
self,
|
2020-07-21 07:51:48 -04:00
|
|
|
|
room_id: str,
|
|
|
|
|
num_joined_users: int,
|
2021-09-20 08:56:23 -04:00
|
|
|
|
cache_context: _CacheContext,
|
2020-07-21 07:51:48 -04:00
|
|
|
|
with_alias: bool = True,
|
|
|
|
|
allow_private: bool = False,
|
2023-10-02 10:22:36 -04:00
|
|
|
|
) -> Optional[JsonMapping]:
|
2017-03-13 07:53:26 -04:00
|
|
|
|
"""Returns the entry for a room
|
2019-02-22 07:11:43 -05:00
|
|
|
|
|
|
|
|
|
Args:
|
2020-07-21 07:51:48 -04:00
|
|
|
|
room_id: The room's ID.
|
|
|
|
|
num_joined_users: Number of users in the room.
|
2019-02-22 07:11:43 -05:00
|
|
|
|
cache_context: Information for cached responses.
|
2020-07-21 07:51:48 -04:00
|
|
|
|
with_alias: Whether to return the room's aliases in the result.
|
|
|
|
|
allow_private: Whether invite-only rooms should be shown.
|
2019-02-22 07:11:43 -05:00
|
|
|
|
|
|
|
|
|
Returns:
|
2020-07-21 07:51:48 -04:00
|
|
|
|
Returns a room entry as a dictionary, or None if this
|
2019-02-22 07:11:43 -05:00
|
|
|
|
room was determined not to be shown publicly.
|
2017-03-13 07:53:26 -04:00
|
|
|
|
"""
|
2016-09-17 09:40:28 -04:00
|
|
|
|
result = {"room_id": room_id, "num_joined_members": num_joined_users}
|
|
|
|
|
|
2020-05-29 12:49:47 -04:00
|
|
|
|
if with_alias:
|
2020-07-21 07:51:48 -04:00
|
|
|
|
aliases = await self.store.get_aliases_for_room(
|
2020-05-29 12:49:47 -04:00
|
|
|
|
room_id, on_invalidate=cache_context.invalidate
|
|
|
|
|
)
|
|
|
|
|
if aliases:
|
|
|
|
|
result["aliases"] = aliases
|
|
|
|
|
|
2022-06-01 11:02:53 -04:00
|
|
|
|
current_state_ids = await self._storage_controllers.state.get_current_state_ids(
|
2017-03-10 12:39:35 -05:00
|
|
|
|
room_id, on_invalidate=cache_context.invalidate
|
|
|
|
|
)
|
2016-09-17 09:40:28 -04:00
|
|
|
|
|
2020-05-29 12:49:47 -04:00
|
|
|
|
if not current_state_ids:
|
|
|
|
|
# We're not in the room, so may as well bail out here.
|
|
|
|
|
return result
|
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
event_map = await self.store.get_events(
|
2016-09-17 09:40:28 -04:00
|
|
|
|
[
|
2018-05-31 05:03:47 -04:00
|
|
|
|
event_id
|
2020-06-15 07:03:36 -04:00
|
|
|
|
for key, event_id in current_state_ids.items()
|
2016-09-17 09:40:28 -04:00
|
|
|
|
if key[0]
|
|
|
|
|
in (
|
2019-02-22 07:11:43 -05:00
|
|
|
|
EventTypes.Create,
|
2016-09-17 09:40:28 -04:00
|
|
|
|
EventTypes.JoinRules,
|
|
|
|
|
EventTypes.Name,
|
|
|
|
|
EventTypes.Topic,
|
|
|
|
|
EventTypes.CanonicalAlias,
|
|
|
|
|
EventTypes.RoomHistoryVisibility,
|
|
|
|
|
EventTypes.GuestAccess,
|
|
|
|
|
"m.room.avatar",
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
current_state = {(ev.type, ev.state_key): ev for ev in event_map.values()}
|
|
|
|
|
|
|
|
|
|
# Double check that this is actually a public room.
|
2019-02-22 07:11:43 -05:00
|
|
|
|
|
2016-09-17 09:40:28 -04:00
|
|
|
|
join_rules_event = current_state.get((EventTypes.JoinRules, ""))
|
|
|
|
|
if join_rules_event:
|
|
|
|
|
join_rule = join_rules_event.content.get("join_rule", None)
|
2017-07-10 10:44:15 -04:00
|
|
|
|
if not allow_private and join_rule and join_rule != JoinRules.PUBLIC:
|
2019-07-23 09:00:55 -04:00
|
|
|
|
return None
|
2016-09-17 09:40:28 -04:00
|
|
|
|
|
2019-02-22 07:11:43 -05:00
|
|
|
|
# Return whether this room is open to federation users or not
|
2020-12-29 17:42:10 -05:00
|
|
|
|
create_event = current_state[EventTypes.Create, ""]
|
2021-09-08 10:00:43 -04:00
|
|
|
|
result["m.federate"] = create_event.content.get(
|
|
|
|
|
EventContentFields.FEDERATE, True
|
|
|
|
|
)
|
2019-02-22 07:11:43 -05:00
|
|
|
|
|
2020-05-29 12:49:47 -04:00
|
|
|
|
name_event = current_state.get((EventTypes.Name, ""))
|
2016-09-17 09:40:28 -04:00
|
|
|
|
if name_event:
|
|
|
|
|
name = name_event.content.get("name", None)
|
|
|
|
|
if name:
|
|
|
|
|
result["name"] = name
|
|
|
|
|
|
|
|
|
|
topic_event = current_state.get((EventTypes.Topic, ""))
|
|
|
|
|
if topic_event:
|
|
|
|
|
topic = topic_event.content.get("topic", None)
|
|
|
|
|
if topic:
|
|
|
|
|
result["topic"] = topic
|
|
|
|
|
|
|
|
|
|
canonical_event = current_state.get((EventTypes.CanonicalAlias, ""))
|
|
|
|
|
if canonical_event:
|
|
|
|
|
canonical_alias = canonical_event.content.get("alias", None)
|
|
|
|
|
if canonical_alias:
|
|
|
|
|
result["canonical_alias"] = canonical_alias
|
|
|
|
|
|
|
|
|
|
visibility_event = current_state.get((EventTypes.RoomHistoryVisibility, ""))
|
|
|
|
|
visibility = None
|
|
|
|
|
if visibility_event:
|
|
|
|
|
visibility = visibility_event.content.get("history_visibility", None)
|
2020-12-16 08:46:37 -05:00
|
|
|
|
result["world_readable"] = visibility == HistoryVisibility.WORLD_READABLE
|
2016-09-17 09:40:28 -04:00
|
|
|
|
|
|
|
|
|
guest_event = current_state.get((EventTypes.GuestAccess, ""))
|
|
|
|
|
guest = None
|
|
|
|
|
if guest_event:
|
2021-09-06 07:17:16 -04:00
|
|
|
|
guest = guest_event.content.get(EventContentFields.GUEST_ACCESS)
|
|
|
|
|
result["guest_can_join"] = guest == GuestAccess.CAN_JOIN
|
2016-09-17 09:40:28 -04:00
|
|
|
|
|
|
|
|
|
avatar_event = current_state.get(("m.room.avatar", ""))
|
|
|
|
|
if avatar_event:
|
|
|
|
|
avatar_url = avatar_event.content.get("url", None)
|
|
|
|
|
if avatar_url:
|
|
|
|
|
result["avatar_url"] = avatar_url
|
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
|
return result
|
2016-09-17 09:40:28 -04:00
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
async def get_remote_public_room_list(
|
2016-09-15 12:35:20 -04:00
|
|
|
|
self,
|
2020-12-29 17:42:10 -05:00
|
|
|
|
server_name: str,
|
|
|
|
|
limit: Optional[int] = None,
|
|
|
|
|
since_token: Optional[str] = None,
|
|
|
|
|
search_filter: Optional[dict] = None,
|
|
|
|
|
include_all_networks: bool = False,
|
|
|
|
|
third_party_instance_id: Optional[str] = None,
|
|
|
|
|
) -> JsonDict:
|
2021-08-06 09:05:41 -04:00
|
|
|
|
"""Get the public room list from remote server
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
SynapseError
|
|
|
|
|
"""
|
|
|
|
|
|
2019-03-20 10:25:58 -04:00
|
|
|
|
if not self.enable_room_list_search:
|
2019-07-23 09:00:55 -04:00
|
|
|
|
return {"chunk": [], "total_room_count_estimate": 0}
|
2019-03-19 12:50:51 -04:00
|
|
|
|
|
2016-09-16 05:24:15 -04:00
|
|
|
|
if search_filter:
|
2019-08-14 08:30:36 -04:00
|
|
|
|
# Searching across federation is defined in MSC2197.
|
|
|
|
|
# However, the remote homeserver may or may not actually support it.
|
|
|
|
|
# So we first try an MSC2197 remote-filtered search, then fall back
|
|
|
|
|
# to a locally-filtered search if we must.
|
|
|
|
|
|
|
|
|
|
try:
|
2020-07-21 07:51:48 -04:00
|
|
|
|
res = await self._get_remote_list_cached(
|
2019-08-14 08:30:36 -04:00
|
|
|
|
server_name,
|
|
|
|
|
limit=limit,
|
|
|
|
|
since_token=since_token,
|
|
|
|
|
include_all_networks=include_all_networks,
|
|
|
|
|
third_party_instance_id=third_party_instance_id,
|
|
|
|
|
search_filter=search_filter,
|
|
|
|
|
)
|
|
|
|
|
return res
|
|
|
|
|
except HttpResponseException as hre:
|
|
|
|
|
syn_err = hre.to_synapse_error()
|
|
|
|
|
if hre.code in (404, 405) or syn_err.errcode in (
|
|
|
|
|
Codes.UNRECOGNIZED,
|
|
|
|
|
Codes.NOT_FOUND,
|
|
|
|
|
):
|
|
|
|
|
logger.debug("Falling back to locally-filtered /publicRooms")
|
|
|
|
|
else:
|
2021-07-20 06:35:23 -04:00
|
|
|
|
# Not an error that should trigger a fallback.
|
|
|
|
|
raise SynapseError(502, "Failed to fetch room list")
|
|
|
|
|
except RequestSendFailed:
|
|
|
|
|
# Not an error that should trigger a fallback.
|
|
|
|
|
raise SynapseError(502, "Failed to fetch room list")
|
2019-08-14 08:30:36 -04:00
|
|
|
|
|
|
|
|
|
# if we reach this point, then we fall back to the situation where
|
|
|
|
|
# we currently don't support searching across federation, so we have
|
2016-09-16 05:24:15 -04:00
|
|
|
|
# to do it manually without pagination
|
|
|
|
|
limit = None
|
|
|
|
|
since_token = None
|
|
|
|
|
|
2021-08-06 09:05:41 -04:00
|
|
|
|
try:
|
|
|
|
|
res = await self._get_remote_list_cached(
|
|
|
|
|
server_name,
|
|
|
|
|
limit=limit,
|
|
|
|
|
since_token=since_token,
|
|
|
|
|
include_all_networks=include_all_networks,
|
|
|
|
|
third_party_instance_id=third_party_instance_id,
|
|
|
|
|
)
|
|
|
|
|
except (RequestSendFailed, HttpResponseException):
|
|
|
|
|
raise SynapseError(502, "Failed to fetch room list")
|
2016-09-14 09:07:37 -04:00
|
|
|
|
|
2016-09-16 05:19:32 -04:00
|
|
|
|
if search_filter:
|
2016-09-16 05:24:15 -04:00
|
|
|
|
res = {
|
|
|
|
|
"chunk": [
|
2016-09-16 05:19:32 -04:00
|
|
|
|
entry
|
2016-09-16 05:24:15 -04:00
|
|
|
|
for entry in list(res.get("chunk", []))
|
2016-09-16 05:19:32 -04:00
|
|
|
|
if _matches_room_entry(entry, search_filter)
|
2016-09-16 05:24:15 -04:00
|
|
|
|
]
|
|
|
|
|
}
|
2016-09-16 05:19:32 -04:00
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
|
return res
|
2016-09-14 09:07:37 -04:00
|
|
|
|
|
2020-07-21 07:51:48 -04:00
|
|
|
|
async def _get_remote_list_cached(
|
2016-09-16 05:31:59 -04:00
|
|
|
|
self,
|
2020-12-29 17:42:10 -05:00
|
|
|
|
server_name: str,
|
|
|
|
|
limit: Optional[int] = None,
|
|
|
|
|
since_token: Optional[str] = None,
|
|
|
|
|
search_filter: Optional[dict] = None,
|
|
|
|
|
include_all_networks: bool = False,
|
|
|
|
|
third_party_instance_id: Optional[str] = None,
|
|
|
|
|
) -> JsonDict:
|
2021-08-06 09:05:41 -04:00
|
|
|
|
"""Wrapper around FederationClient.get_public_rooms that caches the
|
|
|
|
|
result.
|
|
|
|
|
"""
|
|
|
|
|
|
2018-03-13 09:26:52 -04:00
|
|
|
|
repl_layer = self.hs.get_federation_client()
|
2016-09-16 05:31:59 -04:00
|
|
|
|
if search_filter:
|
|
|
|
|
# We can't cache when asking for search
|
2021-08-06 09:05:41 -04:00
|
|
|
|
return await repl_layer.get_public_rooms(
|
|
|
|
|
server_name,
|
|
|
|
|
limit=limit,
|
|
|
|
|
since_token=since_token,
|
|
|
|
|
search_filter=search_filter,
|
|
|
|
|
include_all_networks=include_all_networks,
|
|
|
|
|
third_party_instance_id=third_party_instance_id,
|
|
|
|
|
)
|
2016-09-16 05:31:59 -04:00
|
|
|
|
|
2016-12-06 05:43:48 -05:00
|
|
|
|
key = (
|
|
|
|
|
server_name,
|
|
|
|
|
limit,
|
|
|
|
|
since_token,
|
|
|
|
|
include_all_networks,
|
|
|
|
|
third_party_instance_id,
|
|
|
|
|
)
|
2020-07-21 07:51:48 -04:00
|
|
|
|
return await self.remote_response_cache.wrap(
|
2018-04-12 07:08:59 -04:00
|
|
|
|
key,
|
|
|
|
|
repl_layer.get_public_rooms,
|
|
|
|
|
server_name,
|
|
|
|
|
limit=limit,
|
|
|
|
|
since_token=since_token,
|
|
|
|
|
search_filter=search_filter,
|
|
|
|
|
include_all_networks=include_all_networks,
|
|
|
|
|
third_party_instance_id=third_party_instance_id,
|
|
|
|
|
)
|
2016-09-16 05:31:59 -04:00
|
|
|
|
|
2016-09-15 04:08:57 -04:00
|
|
|
|
|
2021-12-30 13:47:12 -05:00
|
|
|
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
|
|
|
|
class RoomListNextBatch:
|
|
|
|
|
last_joined_members: int # The count to get rooms after/before
|
|
|
|
|
last_room_id: str # The room_id to get rooms after/before
|
|
|
|
|
direction_is_forward: bool # True if this is a next_batch, false if prev_batch
|
|
|
|
|
|
2019-10-02 10:09:10 -04:00
|
|
|
|
KEY_DICT = {
|
|
|
|
|
"last_joined_members": "m",
|
|
|
|
|
"last_room_id": "r",
|
|
|
|
|
"direction_is_forward": "d",
|
|
|
|
|
}
|
2016-09-15 04:08:57 -04:00
|
|
|
|
|
|
|
|
|
REVERSE_KEY_DICT = {v: k for k, v in KEY_DICT.items()}
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2020-12-29 17:42:10 -05:00
|
|
|
|
def from_token(cls, token: str) -> "RoomListNextBatch":
|
2019-10-02 09:08:35 -04:00
|
|
|
|
decoded = msgpack.loads(decode_base64(token), raw=False)
|
2016-09-15 04:08:57 -04:00
|
|
|
|
return RoomListNextBatch(
|
2018-10-17 08:04:55 -04:00
|
|
|
|
**{cls.REVERSE_KEY_DICT[key]: val for key, val in decoded.items()}
|
2016-09-15 04:08:57 -04:00
|
|
|
|
)
|
|
|
|
|
|
2020-12-29 17:42:10 -05:00
|
|
|
|
def to_token(self) -> str:
|
2016-09-15 04:08:57 -04:00
|
|
|
|
return encode_base64(
|
|
|
|
|
msgpack.dumps(
|
2021-12-30 13:47:12 -05:00
|
|
|
|
{self.KEY_DICT[key]: val for key, val in attr.asdict(self).items()}
|
2016-09-15 04:08:57 -04:00
|
|
|
|
)
|
|
|
|
|
)
|
2016-09-15 05:15:37 -04:00
|
|
|
|
|
2021-09-20 08:56:23 -04:00
|
|
|
|
def copy_and_replace(self, **kwds: Any) -> "RoomListNextBatch":
|
2021-12-30 13:47:12 -05:00
|
|
|
|
return attr.evolve(self, **kwds)
|
2016-09-16 05:19:32 -04:00
|
|
|
|
|
|
|
|
|
|
2020-12-29 17:42:10 -05:00
|
|
|
|
def _matches_room_entry(room_entry: JsonDict, search_filter: dict) -> bool:
|
2022-06-29 13:12:45 -04:00
|
|
|
|
"""Determines whether the given search filter matches a room entry returned over
|
|
|
|
|
federation.
|
|
|
|
|
|
|
|
|
|
Only used if the remote server does not support MSC2197 remote-filtered search, and
|
|
|
|
|
hence does not support MSC3827 filtering of `/publicRooms` by room type either.
|
|
|
|
|
|
|
|
|
|
In this case, we cannot apply the `room_type` filter since no `room_type` field is
|
|
|
|
|
returned.
|
|
|
|
|
"""
|
|
|
|
|
if search_filter and search_filter.get(
|
|
|
|
|
PublicRoomsFilterFields.GENERIC_SEARCH_TERM, None
|
|
|
|
|
):
|
|
|
|
|
generic_search_term = search_filter[
|
|
|
|
|
PublicRoomsFilterFields.GENERIC_SEARCH_TERM
|
|
|
|
|
].upper()
|
2016-09-16 14:02:42 -04:00
|
|
|
|
if generic_search_term in room_entry.get("name", "").upper():
|
2016-09-16 05:19:32 -04:00
|
|
|
|
return True
|
2016-09-16 14:02:42 -04:00
|
|
|
|
elif generic_search_term in room_entry.get("topic", "").upper():
|
2016-09-16 05:19:32 -04:00
|
|
|
|
return True
|
2016-09-16 14:02:42 -04:00
|
|
|
|
elif generic_search_term in room_entry.get("canonical_alias", "").upper():
|
2016-09-16 05:19:32 -04:00
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return False
|