2016-09-14 13:07:37 +00:00
|
|
|
|
# Copyright 2014 - 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 06:09:20 +00:00
|
|
|
|
import logging
|
|
|
|
|
from collections import namedtuple
|
2020-12-29 22:42:10 +00:00
|
|
|
|
from typing import TYPE_CHECKING, Optional, Tuple
|
2016-09-14 13:07:37 +00:00
|
|
|
|
|
2018-07-09 06:09:20 +00:00
|
|
|
|
import msgpack
|
|
|
|
|
from unpaddedbase64 import decode_base64, encode_base64
|
|
|
|
|
|
2020-12-16 13:46:37 +00:00
|
|
|
|
from synapse.api.constants import EventTypes, HistoryVisibility, JoinRules
|
2019-08-14 12:30:36 +00:00
|
|
|
|
from synapse.api.errors import Codes, HttpResponseException
|
2020-12-29 22:42:10 +00:00
|
|
|
|
from synapse.types import JsonDict, ThirdPartyInstanceID
|
2020-07-21 11:51:48 +00:00
|
|
|
|
from synapse.util.caches.descriptors import cached
|
2016-09-14 13:07:37 +00:00
|
|
|
|
from synapse.util.caches.response_cache import ResponseCache
|
2016-09-15 08:08:57 +00:00
|
|
|
|
|
2018-07-09 06:09:20 +00:00
|
|
|
|
from ._base import BaseHandler
|
2016-09-14 13:07:37 +00:00
|
|
|
|
|
2020-12-29 22:42:10 +00:00
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 11:12:48 +00:00
|
|
|
|
from synapse.server import HomeServer
|
2020-12-29 22:42:10 +00:00
|
|
|
|
|
2016-09-14 13:07:37 +00:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
REMOTE_ROOM_LIST_POLL_INTERVAL = 60 * 1000
|
|
|
|
|
|
2016-12-06 10:43:48 +00:00
|
|
|
|
# This is used to indicate we should only return rooms published to the main list.
|
2018-07-13 11:03:34 +00:00
|
|
|
|
EMPTY_THIRD_PARTY_ID = ThirdPartyInstanceID(None, None)
|
2016-12-06 10:43:48 +00:00
|
|
|
|
|
|
|
|
|
|
2016-09-14 13:07:37 +00:00
|
|
|
|
class RoomListHandler(BaseHandler):
|
2020-12-29 22:42:10 +00:00
|
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 13:56:44 +00:00
|
|
|
|
super().__init__(hs)
|
2019-03-20 14:25:58 +00:00
|
|
|
|
self.enable_room_list_search = hs.config.enable_room_list_search
|
2020-12-29 22:42:10 +00:00
|
|
|
|
self.response_cache = ResponseCache(
|
2021-03-08 19:00:07 +00:00
|
|
|
|
hs.get_clock(), "room_list"
|
2021-06-08 15:19:25 +00:00
|
|
|
|
) # type: ResponseCache[Tuple[Optional[int], Optional[str], Optional[ThirdPartyInstanceID]]]
|
2018-04-10 22:14:47 +00:00
|
|
|
|
self.remote_response_cache = ResponseCache(
|
2021-03-08 19:00:07 +00:00
|
|
|
|
hs.get_clock(), "remote_room_list", timeout_ms=30 * 1000
|
2020-12-29 22:42:10 +00:00
|
|
|
|
) # type: ResponseCache[Tuple[str, Optional[int], Optional[str], bool, Optional[str]]]
|
2016-09-14 13:07:37 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
async def get_local_public_room_list(
|
2016-09-15 16:35:20 +00:00
|
|
|
|
self,
|
2020-12-29 22:42:10 +00:00
|
|
|
|
limit: Optional[int] = None,
|
|
|
|
|
since_token: Optional[str] = None,
|
|
|
|
|
search_filter: Optional[dict] = None,
|
2021-06-08 15:19:25 +00:00
|
|
|
|
network_tuple: Optional[ThirdPartyInstanceID] = EMPTY_THIRD_PARTY_ID,
|
2020-12-29 22:42:10 +00:00
|
|
|
|
from_federation: bool = False,
|
|
|
|
|
) -> JsonDict:
|
2016-12-06 10:43:48 +00: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 22:42:10 +00:00
|
|
|
|
limit
|
|
|
|
|
since_token
|
|
|
|
|
search_filter
|
|
|
|
|
network_tuple: Which public list to use.
|
2016-12-06 10:43:48 +00: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.
|
2020-12-29 22:42:10 +00:00
|
|
|
|
from_federation: true iff the request comes from the federation API
|
2016-12-06 10:43:48 +00:00
|
|
|
|
"""
|
2019-03-20 14:25:58 +00:00
|
|
|
|
if not self.enable_room_list_search:
|
2020-07-21 11:51:48 +00:00
|
|
|
|
return {"chunk": [], "total_room_count_estimate": 0}
|
2019-03-19 16:50:51 +00:00
|
|
|
|
|
2019-03-20 14:25:28 +00:00
|
|
|
|
logger.info(
|
|
|
|
|
"Getting public room list: limit=%r, since=%r, search=%r, network=%r",
|
|
|
|
|
limit,
|
|
|
|
|
since_token,
|
|
|
|
|
bool(search_filter),
|
|
|
|
|
network_tuple,
|
|
|
|
|
)
|
|
|
|
|
|
2016-12-16 16:11:43 +00:00
|
|
|
|
if search_filter:
|
2016-12-07 09:58:33 +00:00
|
|
|
|
# We explicitly don't bother caching searches or requests for
|
|
|
|
|
# appservice specific lists.
|
2017-10-25 09:26:06 +00:00
|
|
|
|
logger.info("Bypassing cache as search request.")
|
2019-01-24 12:44:27 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
return await self._get_public_room_list(
|
2020-04-30 10:38:07 +00:00
|
|
|
|
limit,
|
|
|
|
|
since_token,
|
|
|
|
|
search_filter,
|
|
|
|
|
network_tuple=network_tuple,
|
|
|
|
|
from_federation=from_federation,
|
2016-12-06 10:43:48 +00:00
|
|
|
|
)
|
2016-09-16 08:05:11 +00:00
|
|
|
|
|
2016-12-16 16:11:43 +00:00
|
|
|
|
key = (limit, since_token, network_tuple)
|
2020-07-21 11:51:48 +00:00
|
|
|
|
return await self.response_cache.wrap(
|
2018-04-12 11:08:59 +00:00
|
|
|
|
key,
|
|
|
|
|
self._get_public_room_list,
|
2019-02-22 12:11:43 +00:00
|
|
|
|
limit,
|
|
|
|
|
since_token,
|
|
|
|
|
network_tuple=network_tuple,
|
|
|
|
|
from_federation=from_federation,
|
2018-04-12 11:08:59 +00:00
|
|
|
|
)
|
2016-09-14 13:07:37 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
async def _get_public_room_list(
|
2016-09-15 16:35:20 +00:00
|
|
|
|
self,
|
2020-04-06 11:35:30 +00:00
|
|
|
|
limit: Optional[int] = None,
|
|
|
|
|
since_token: Optional[str] = None,
|
2020-12-29 22:42:10 +00:00
|
|
|
|
search_filter: Optional[dict] = None,
|
2021-06-08 15:19:25 +00:00
|
|
|
|
network_tuple: Optional[ThirdPartyInstanceID] = EMPTY_THIRD_PARTY_ID,
|
2020-04-06 11:35:30 +00:00
|
|
|
|
from_federation: bool = False,
|
2020-12-29 22:42:10 +00:00
|
|
|
|
) -> JsonDict:
|
2019-02-26 14:23:40 +00:00
|
|
|
|
"""Generate a public room list.
|
|
|
|
|
Args:
|
2020-04-06 11:35:30 +00: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 14:23:40 +00: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.
|
2020-04-06 11:35:30 +00:00
|
|
|
|
from_federation: Whether this request originated from a
|
2019-02-26 14:23:40 +00:00
|
|
|
|
federating server or a client. Used for room filtering.
|
|
|
|
|
"""
|
2016-09-15 08:08:57 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00: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 16:28:52 +00:00
|
|
|
|
|
2016-09-15 09:36:19 +00:00
|
|
|
|
if since_token:
|
2019-10-02 13:08:35 +00:00
|
|
|
|
batch_token = RoomListNextBatch.from_token(since_token)
|
2017-03-13 09:50:10 +00:00
|
|
|
|
|
2020-12-29 22:42:10 +00:00
|
|
|
|
bounds = (
|
|
|
|
|
batch_token.last_joined_members,
|
|
|
|
|
batch_token.last_room_id,
|
|
|
|
|
) # type: Optional[Tuple[int, str]]
|
2019-10-02 13:08:35 +00:00
|
|
|
|
forwards = batch_token.direction_is_forward
|
2020-12-29 22:42:10 +00:00
|
|
|
|
has_batch_token = True
|
2019-10-02 13:08:35 +00:00
|
|
|
|
else:
|
2019-10-02 14:09:10 +00:00
|
|
|
|
bounds = None
|
2016-09-14 16:28:52 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00:00
|
|
|
|
forwards = True
|
2020-12-29 22:42:10 +00:00
|
|
|
|
has_batch_token = False
|
2016-09-14 16:28:52 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00:00
|
|
|
|
# we request one more than wanted to see if there are more pages to come
|
|
|
|
|
probing_limit = limit + 1 if limit is not None else None
|
2016-09-14 16:28:52 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
results = await self.store.get_largest_public_rooms(
|
2019-10-02 13:08:35 +00:00
|
|
|
|
network_tuple,
|
|
|
|
|
search_filter,
|
|
|
|
|
probing_limit,
|
2019-10-02 14:09:10 +00:00
|
|
|
|
bounds=bounds,
|
2019-10-02 13:08:35 +00:00
|
|
|
|
forwards=forwards,
|
|
|
|
|
ignore_non_federatable=from_federation,
|
2017-11-14 09:23:56 +00:00
|
|
|
|
)
|
2016-09-14 16:28:52 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00:00
|
|
|
|
def build_room_entry(room):
|
|
|
|
|
entry = {
|
|
|
|
|
"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"],
|
2020-12-16 13:46:37 +00:00
|
|
|
|
"world_readable": room["history_visibility"]
|
|
|
|
|
== HistoryVisibility.WORLD_READABLE,
|
2019-10-02 13:08:35 +00:00
|
|
|
|
"guest_can_join": room["guest_access"] == "can_join",
|
|
|
|
|
}
|
2016-09-14 16:28:52 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00: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 13:40:28 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00:00
|
|
|
|
results = [build_room_entry(r) for r in results]
|
2016-09-17 13:40:28 +00:00
|
|
|
|
|
2020-12-29 22:42:10 +00:00
|
|
|
|
response = {} # type: JsonDict
|
2019-10-02 13:08:35 +00:00
|
|
|
|
num_results = len(results)
|
|
|
|
|
if limit is not None:
|
|
|
|
|
more_to_come = num_results == probing_limit
|
2016-09-21 12:17:08 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00:00
|
|
|
|
# Depending on direction we trim either the front or back.
|
|
|
|
|
if forwards:
|
|
|
|
|
results = results[:limit]
|
2016-09-15 09:15:37 +00:00
|
|
|
|
else:
|
2019-10-02 13:08:35 +00:00
|
|
|
|
results = results[-limit:]
|
2016-09-17 17:01:54 +00:00
|
|
|
|
else:
|
2019-10-02 13:08:35 +00:00
|
|
|
|
more_to_come = False
|
|
|
|
|
|
|
|
|
|
if num_results > 0:
|
2019-10-02 14:09:10 +00:00
|
|
|
|
final_entry = results[-1]
|
|
|
|
|
initial_entry = results[0]
|
2019-10-02 13:08:35 +00:00
|
|
|
|
|
|
|
|
|
if forwards:
|
2020-12-29 22:42:10 +00:00
|
|
|
|
if has_batch_token:
|
2019-10-02 13:08:35 +00:00
|
|
|
|
# If there was a token given then we assume that there
|
|
|
|
|
# must be previous results.
|
|
|
|
|
response["prev_batch"] = RoomListNextBatch(
|
2019-10-02 14:09:10 +00:00
|
|
|
|
last_joined_members=initial_entry["num_joined_members"],
|
|
|
|
|
last_room_id=initial_entry["room_id"],
|
|
|
|
|
direction_is_forward=False,
|
2019-10-02 13:08:35 +00:00
|
|
|
|
).to_token()
|
|
|
|
|
|
|
|
|
|
if more_to_come:
|
|
|
|
|
response["next_batch"] = RoomListNextBatch(
|
2019-10-02 14:09:10 +00:00
|
|
|
|
last_joined_members=final_entry["num_joined_members"],
|
|
|
|
|
last_room_id=final_entry["room_id"],
|
|
|
|
|
direction_is_forward=True,
|
2019-10-02 13:08:35 +00:00
|
|
|
|
).to_token()
|
2016-09-15 16:35:20 +00:00
|
|
|
|
else:
|
2020-12-29 22:42:10 +00:00
|
|
|
|
if has_batch_token:
|
2019-10-02 13:08:35 +00:00
|
|
|
|
response["next_batch"] = RoomListNextBatch(
|
2019-10-02 14:09:10 +00:00
|
|
|
|
last_joined_members=final_entry["num_joined_members"],
|
|
|
|
|
last_room_id=final_entry["room_id"],
|
|
|
|
|
direction_is_forward=True,
|
2019-10-02 13:08:35 +00:00
|
|
|
|
).to_token()
|
|
|
|
|
|
|
|
|
|
if more_to_come:
|
|
|
|
|
response["prev_batch"] = RoomListNextBatch(
|
2019-10-02 14:09:10 +00:00
|
|
|
|
last_joined_members=initial_entry["num_joined_members"],
|
|
|
|
|
last_room_id=initial_entry["room_id"],
|
|
|
|
|
direction_is_forward=False,
|
2019-10-02 13:08:35 +00:00
|
|
|
|
).to_token()
|
|
|
|
|
|
|
|
|
|
response["chunk"] = results
|
2019-02-22 12:11:43 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
response["total_room_count_estimate"] = await self.store.count_public_rooms(
|
2019-10-02 13:08:35 +00:00
|
|
|
|
network_tuple, ignore_non_federatable=from_federation
|
|
|
|
|
)
|
2017-03-10 17:39:35 +00:00
|
|
|
|
|
2019-10-02 13:08:35 +00:00
|
|
|
|
return response
|
2017-03-10 17:39:35 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
@cached(num_args=1, cache_context=True)
|
|
|
|
|
async def generate_room_entry(
|
2019-02-26 14:13:38 +00:00
|
|
|
|
self,
|
2020-07-21 11:51:48 +00:00
|
|
|
|
room_id: str,
|
|
|
|
|
num_joined_users: int,
|
2019-02-26 14:13:38 +00:00
|
|
|
|
cache_context,
|
2020-07-21 11:51:48 +00:00
|
|
|
|
with_alias: bool = True,
|
|
|
|
|
allow_private: bool = False,
|
|
|
|
|
) -> Optional[dict]:
|
2017-03-13 11:53:26 +00:00
|
|
|
|
"""Returns the entry for a room
|
2019-02-22 12:11:43 +00:00
|
|
|
|
|
|
|
|
|
Args:
|
2020-07-21 11:51:48 +00:00
|
|
|
|
room_id: The room's ID.
|
|
|
|
|
num_joined_users: Number of users in the room.
|
2019-02-22 12:11:43 +00:00
|
|
|
|
cache_context: Information for cached responses.
|
2020-07-21 11:51:48 +00: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 12:11:43 +00:00
|
|
|
|
|
|
|
|
|
Returns:
|
2020-07-21 11:51:48 +00:00
|
|
|
|
Returns a room entry as a dictionary, or None if this
|
2019-02-22 12:11:43 +00:00
|
|
|
|
room was determined not to be shown publicly.
|
2017-03-13 11:53:26 +00:00
|
|
|
|
"""
|
2016-09-17 13:40:28 +00:00
|
|
|
|
result = {"room_id": room_id, "num_joined_members": num_joined_users}
|
|
|
|
|
|
2020-05-29 16:49:47 +00:00
|
|
|
|
if with_alias:
|
2020-07-21 11:51:48 +00:00
|
|
|
|
aliases = await self.store.get_aliases_for_room(
|
2020-05-29 16:49:47 +00:00
|
|
|
|
room_id, on_invalidate=cache_context.invalidate
|
|
|
|
|
)
|
|
|
|
|
if aliases:
|
|
|
|
|
result["aliases"] = aliases
|
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
current_state_ids = await self.store.get_current_state_ids(
|
2017-03-10 17:39:35 +00:00
|
|
|
|
room_id, on_invalidate=cache_context.invalidate
|
|
|
|
|
)
|
2016-09-17 13:40:28 +00:00
|
|
|
|
|
2020-05-29 16:49:47 +00:00
|
|
|
|
if not current_state_ids:
|
|
|
|
|
# We're not in the room, so may as well bail out here.
|
|
|
|
|
return result
|
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
event_map = await self.store.get_events(
|
2016-09-17 13:40:28 +00:00
|
|
|
|
[
|
2018-05-31 09:03:47 +00:00
|
|
|
|
event_id
|
2020-06-15 11:03:36 +00:00
|
|
|
|
for key, event_id in current_state_ids.items()
|
2016-09-17 13:40:28 +00:00
|
|
|
|
if key[0]
|
|
|
|
|
in (
|
2019-02-22 12:11:43 +00:00
|
|
|
|
EventTypes.Create,
|
2016-09-17 13:40:28 +00: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 12:11:43 +00:00
|
|
|
|
|
2016-09-17 13:40:28 +00: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 14:44:15 +00:00
|
|
|
|
if not allow_private and join_rule and join_rule != JoinRules.PUBLIC:
|
2019-07-23 13:00:55 +00:00
|
|
|
|
return None
|
2016-09-17 13:40:28 +00:00
|
|
|
|
|
2019-02-22 12:11:43 +00:00
|
|
|
|
# Return whether this room is open to federation users or not
|
2020-12-29 22:42:10 +00:00
|
|
|
|
create_event = current_state[EventTypes.Create, ""]
|
2019-02-22 12:11:43 +00:00
|
|
|
|
result["m.federate"] = create_event.content.get("m.federate", True)
|
|
|
|
|
|
2020-05-29 16:49:47 +00:00
|
|
|
|
name_event = current_state.get((EventTypes.Name, ""))
|
2016-09-17 13:40:28 +00: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 13:46:37 +00:00
|
|
|
|
result["world_readable"] = visibility == HistoryVisibility.WORLD_READABLE
|
2016-09-17 13:40:28 +00:00
|
|
|
|
|
|
|
|
|
guest_event = current_state.get((EventTypes.GuestAccess, ""))
|
|
|
|
|
guest = None
|
|
|
|
|
if guest_event:
|
|
|
|
|
guest = guest_event.content.get("guest_access", None)
|
|
|
|
|
result["guest_can_join"] = guest == "can_join"
|
|
|
|
|
|
|
|
|
|
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 13:00:55 +00:00
|
|
|
|
return result
|
2016-09-17 13:40:28 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
async def get_remote_public_room_list(
|
2016-09-15 16:35:20 +00:00
|
|
|
|
self,
|
2020-12-29 22:42:10 +00: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:
|
2019-03-20 14:25:58 +00:00
|
|
|
|
if not self.enable_room_list_search:
|
2019-07-23 13:00:55 +00:00
|
|
|
|
return {"chunk": [], "total_room_count_estimate": 0}
|
2019-03-19 16:50:51 +00:00
|
|
|
|
|
2016-09-16 09:24:15 +00:00
|
|
|
|
if search_filter:
|
2019-08-14 12:30:36 +00: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 11:51:48 +00:00
|
|
|
|
res = await self._get_remote_list_cached(
|
2019-08-14 12:30:36 +00: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:
|
|
|
|
|
raise # Not an error that should trigger a fallback.
|
|
|
|
|
|
|
|
|
|
# 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 09:24:15 +00:00
|
|
|
|
# to do it manually without pagination
|
|
|
|
|
limit = None
|
|
|
|
|
since_token = None
|
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
res = await self._get_remote_list_cached(
|
2016-09-15 09:36:19 +00:00
|
|
|
|
server_name,
|
|
|
|
|
limit=limit,
|
|
|
|
|
since_token=since_token,
|
2016-12-06 10:43:48 +00:00
|
|
|
|
include_all_networks=include_all_networks,
|
|
|
|
|
third_party_instance_id=third_party_instance_id,
|
2016-09-14 13:07:37 +00:00
|
|
|
|
)
|
|
|
|
|
|
2016-09-16 09:19:32 +00:00
|
|
|
|
if search_filter:
|
2016-09-16 09:24:15 +00:00
|
|
|
|
res = {
|
|
|
|
|
"chunk": [
|
2016-09-16 09:19:32 +00:00
|
|
|
|
entry
|
2016-09-16 09:24:15 +00:00
|
|
|
|
for entry in list(res.get("chunk", []))
|
2016-09-16 09:19:32 +00:00
|
|
|
|
if _matches_room_entry(entry, search_filter)
|
2016-09-16 09:24:15 +00:00
|
|
|
|
]
|
|
|
|
|
}
|
2016-09-16 09:19:32 +00:00
|
|
|
|
|
2019-07-23 13:00:55 +00:00
|
|
|
|
return res
|
2016-09-14 13:07:37 +00:00
|
|
|
|
|
2020-07-21 11:51:48 +00:00
|
|
|
|
async def _get_remote_list_cached(
|
2016-09-16 09:31:59 +00:00
|
|
|
|
self,
|
2020-12-29 22:42:10 +00: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:
|
2018-03-13 13:26:52 +00:00
|
|
|
|
repl_layer = self.hs.get_federation_client()
|
2016-09-16 09:31:59 +00:00
|
|
|
|
if search_filter:
|
|
|
|
|
# We can't cache when asking for search
|
2020-07-21 11:51:48 +00:00
|
|
|
|
return await repl_layer.get_public_rooms(
|
2016-09-16 09:31:59 +00:00
|
|
|
|
server_name,
|
|
|
|
|
limit=limit,
|
|
|
|
|
since_token=since_token,
|
2016-12-06 10:43:48 +00:00
|
|
|
|
search_filter=search_filter,
|
|
|
|
|
include_all_networks=include_all_networks,
|
|
|
|
|
third_party_instance_id=third_party_instance_id,
|
2016-09-16 09:31:59 +00:00
|
|
|
|
)
|
|
|
|
|
|
2016-12-06 10:43:48 +00:00
|
|
|
|
key = (
|
|
|
|
|
server_name,
|
|
|
|
|
limit,
|
|
|
|
|
since_token,
|
|
|
|
|
include_all_networks,
|
|
|
|
|
third_party_instance_id,
|
|
|
|
|
)
|
2020-07-21 11:51:48 +00:00
|
|
|
|
return await self.remote_response_cache.wrap(
|
2018-04-12 11:08:59 +00: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 09:31:59 +00:00
|
|
|
|
|
2016-09-15 08:08:57 +00:00
|
|
|
|
|
|
|
|
|
class RoomListNextBatch(
|
|
|
|
|
namedtuple(
|
|
|
|
|
"RoomListNextBatch",
|
|
|
|
|
(
|
2019-10-02 14:09:10 +00:00
|
|
|
|
"last_joined_members", # The count to get rooms after/before
|
2019-10-02 13:08:35 +00:00
|
|
|
|
"last_room_id", # The room_id to get rooms after/before
|
2016-09-15 09:15:37 +00:00
|
|
|
|
"direction_is_forward", # Bool if this is a next_batch, false if prev_batch
|
2019-06-20 09:32:02 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
2016-09-15 08:08:57 +00:00
|
|
|
|
):
|
2019-10-02 14:09:10 +00:00
|
|
|
|
KEY_DICT = {
|
|
|
|
|
"last_joined_members": "m",
|
|
|
|
|
"last_room_id": "r",
|
|
|
|
|
"direction_is_forward": "d",
|
|
|
|
|
}
|
2016-09-15 08:08:57 +00:00
|
|
|
|
|
|
|
|
|
REVERSE_KEY_DICT = {v: k for k, v in KEY_DICT.items()}
|
|
|
|
|
|
|
|
|
|
@classmethod
|
2020-12-29 22:42:10 +00:00
|
|
|
|
def from_token(cls, token: str) -> "RoomListNextBatch":
|
2019-10-02 13:08:35 +00:00
|
|
|
|
decoded = msgpack.loads(decode_base64(token), raw=False)
|
2016-09-15 08:08:57 +00:00
|
|
|
|
return RoomListNextBatch(
|
2018-10-17 12:04:55 +00:00
|
|
|
|
**{cls.REVERSE_KEY_DICT[key]: val for key, val in decoded.items()}
|
2016-09-15 08:08:57 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-12-29 22:42:10 +00:00
|
|
|
|
def to_token(self) -> str:
|
2016-09-15 08:08:57 +00:00
|
|
|
|
return encode_base64(
|
|
|
|
|
msgpack.dumps(
|
|
|
|
|
{self.KEY_DICT[key]: val for key, val in self._asdict().items()}
|
|
|
|
|
)
|
|
|
|
|
)
|
2016-09-15 09:15:37 +00:00
|
|
|
|
|
2020-12-29 22:42:10 +00:00
|
|
|
|
def copy_and_replace(self, **kwds) -> "RoomListNextBatch":
|
2016-09-15 09:15:37 +00:00
|
|
|
|
return self._replace(**kwds)
|
2016-09-16 09:19:32 +00:00
|
|
|
|
|
|
|
|
|
|
2020-12-29 22:42:10 +00:00
|
|
|
|
def _matches_room_entry(room_entry: JsonDict, search_filter: dict) -> bool:
|
2016-09-16 09:19:32 +00:00
|
|
|
|
if search_filter and search_filter.get("generic_search_term", None):
|
2016-09-16 18:02:42 +00:00
|
|
|
|
generic_search_term = search_filter["generic_search_term"].upper()
|
|
|
|
|
if generic_search_term in room_entry.get("name", "").upper():
|
2016-09-16 09:19:32 +00:00
|
|
|
|
return True
|
2016-09-16 18:02:42 +00:00
|
|
|
|
elif generic_search_term in room_entry.get("topic", "").upper():
|
2016-09-16 09:19:32 +00:00
|
|
|
|
return True
|
2016-09-16 18:02:42 +00:00
|
|
|
|
elif generic_search_term in room_entry.get("canonical_alias", "").upper():
|
2016-09-16 09:19:32 +00:00
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return False
|