Various fixups

anoa/public_rooms_module_api
Mathieu Velten 2023-05-19 11:23:55 +02:00
parent 738b372379
commit 79923666c5
6 changed files with 68 additions and 55 deletions

View File

@ -149,7 +149,10 @@ class PublicRoomList(BaseFederationServlet):
limit = None limit = None
data = await self.handler.get_local_public_room_list( data = await self.handler.get_local_public_room_list(
limit, since_token, network_tuple=network_tuple, from_federation=True limit,
since_token,
network_tuple=network_tuple,
from_remote_server_name=origin,
) )
return 200, data return 200, data
@ -190,7 +193,7 @@ class PublicRoomList(BaseFederationServlet):
since_token=since_token, since_token=since_token,
search_filter=search_filter, search_filter=search_filter,
network_tuple=network_tuple, network_tuple=network_tuple,
from_federation=True, from_remote_server_name=origin,
) )
return 200, data return 200, data

View File

@ -34,7 +34,7 @@ from synapse.api.errors import (
RequestSendFailed, RequestSendFailed,
SynapseError, SynapseError,
) )
from synapse.types import JsonDict, ThirdPartyInstanceID from synapse.types import JsonDict, PublicRoom, ThirdPartyInstanceID
from synapse.util.caches.descriptors import _CacheContext, cached from synapse.util.caches.descriptors import _CacheContext, cached
from synapse.util.caches.response_cache import ResponseCache from synapse.util.caches.response_cache import ResponseCache
@ -166,30 +166,47 @@ class RoomListHandler:
# we request one more than wanted to see if there are more pages to come # 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 probing_limit = limit + 1 if limit is not None else None
public_rooms = await self.store.get_largest_public_rooms( results = await self.store.get_largest_public_rooms(
network_tuple, network_tuple,
search_filter, search_filter,
probing_limit, probing_limit,
bounds=( bounds=(batch_token.last_joined_members, batch_token.last_room_id)
[batch_token.last_joined_members, batch_token.last_room_id] if batch_token
if batch_token else None else None,
),
forwards=forwards, forwards=forwards,
ignore_non_federatable=bool(from_remote_server_name), ignore_non_federatable=bool(from_remote_server_name),
) )
for fetch_public_rooms in self._module_api_callbacks.fetch_public_rooms_callbacks: for (
fetch_public_rooms
) in self._module_api_callbacks.fetch_public_rooms_callbacks:
# Ask each module for a list of public rooms given the last_joined_members # Ask each module for a list of public rooms given the last_joined_members
# value from the since token and the probing limit. # value from the since token and the probing limit.
module_public_rooms = await fetch_public_rooms( module_public_rooms = await fetch_public_rooms(
limit=probing_limit, forwards,
max_member_count=( probing_limit,
batch_token.last_joined_members batch_token.last_joined_members if batch_token else None,
if batch_token else None
),
) )
# Insert the module's reported public rooms into the list # Insert the module's reported public rooms into the list
for new_room in module_public_rooms:
inserted = False
for i in range(len(results)):
r = results[i]
if (
forwards
and new_room["num_joined_members"] >= r["num_joined_members"]
) or (
not forwards
and new_room["num_joined_members"] <= r["num_joined_members"]
):
results.insert(i, new_room)
break
if not inserted:
if forwards:
results.append(new_room)
else:
results.insert(0, new_room)
response: JsonDict = {} response: JsonDict = {}
num_results = len(results) num_results = len(results)

View File

@ -79,7 +79,6 @@ from synapse.module_api.callbacks.account_validity_callbacks import (
) )
from synapse.module_api.callbacks.public_rooms_callbacks import ( from synapse.module_api.callbacks.public_rooms_callbacks import (
FETCH_PUBLIC_ROOMS_CALLBACK, FETCH_PUBLIC_ROOMS_CALLBACK,
PublicRoomChunk,
) )
from synapse.module_api.callbacks.spamchecker_callbacks import ( from synapse.module_api.callbacks.spamchecker_callbacks import (
CHECK_EVENT_FOR_SPAM_CALLBACK, CHECK_EVENT_FOR_SPAM_CALLBACK,
@ -123,6 +122,7 @@ from synapse.types import (
DomainSpecificString, DomainSpecificString,
JsonDict, JsonDict,
JsonMapping, JsonMapping,
PublicRoom,
Requester, Requester,
RoomAlias, RoomAlias,
StateMap, StateMap,

View File

@ -14,30 +14,17 @@
import logging import logging
from typing import Awaitable, Callable, Iterable, List, Optional, Tuple from typing import Awaitable, Callable, Iterable, List, Optional, Tuple
from synapse.types import PublicRoom
import attr import attr
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@attr.s(auto_attribs=True)
class PublicRoom:
room_id: str
name: str
topic: str
num_joined_members: int
canonical_alias: str
avatar_url: str
world_readable: bool
guest_can_join: bool
join_rule: str
room_type: str
# Types for callbacks to be registered via the module api # Types for callbacks to be registered via the module api
FETCH_PUBLIC_ROOMS_CALLBACK = Callable[ FETCH_PUBLIC_ROOMS_CALLBACK = Callable[
[int, Optional[Tuple[int, bool]], Optional[dict], Optional[str], Optional[str]], [bool, Optional[int], Optional[int]],
Awaitable[Tuple[Iterable[PublicRoom], bool]], Awaitable[Iterable[PublicRoom]],
] ]

View File

@ -62,7 +62,13 @@ from synapse.storage.util.id_generators import (
MultiWriterIdGenerator, MultiWriterIdGenerator,
StreamIdGenerator, StreamIdGenerator,
) )
from synapse.types import JsonDict, RetentionPolicy, StrCollection, ThirdPartyInstanceID from synapse.types import (
JsonDict,
PublicRoom,
RetentionPolicy,
StrCollection,
ThirdPartyInstanceID,
)
from synapse.util import json_encoder from synapse.util import json_encoder
from synapse.util.caches.descriptors import cached, cachedList from synapse.util.caches.descriptors import cached, cachedList
from synapse.util.stringutils import MXC_REGEX from synapse.util.stringutils import MXC_REGEX
@ -369,7 +375,7 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
bounds: Optional[Tuple[int, str]], bounds: Optional[Tuple[int, str]],
forwards: bool, forwards: bool,
ignore_non_federatable: bool = False, ignore_non_federatable: bool = False,
) -> List[Dict[str, Any]]: ) -> List[PublicRoom]:
"""Gets the largest public rooms (where largest is in terms of joined """Gets the largest public rooms (where largest is in terms of joined
members, as tracked in the statistics table). members, as tracked in the statistics table).
@ -520,23 +526,23 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
"get_largest_public_rooms", _get_largest_public_rooms_txn "get_largest_public_rooms", _get_largest_public_rooms_txn
) )
def build_room_entry(room: JsonDict) -> JsonDict: def build_room_entry(room: JsonDict) -> PublicRoom:
entry = { return PublicRoom(
"room_id": room["room_id"], room_id=room["room_id"],
"name": room["name"], name=room["name"],
"topic": room["topic"], topic=room["topic"],
"canonical_alias": room["canonical_alias"], canonical_alias=room["canonical_alias"],
"num_joined_members": room["joined_members"], num_joined_members=room["joined_members"],
"avatar_url": room["avatar"], avatar_url=room["avatar"],
"world_readable": room["history_visibility"] world_readable=room["history_visibility"]
== HistoryVisibility.WORLD_READABLE, == HistoryVisibility.WORLD_READABLE,
"guest_can_join": room["guest_access"] == "can_join", guest_can_join=room["guest_access"] == "can_join",
"join_rule": room["join_rules"], join_rule=room["join_rules"],
"room_type": room["room_type"], room_type=room["room_type"],
} )
# Filter out Nones rather omit the field altogether # Filter out Nones rather omit the field altogether
return {k: v for k, v in entry.items() if v is not None} # return {k: v for k, v in entry.items() if v is not None}
return [build_room_entry(r) for r in ret_val] return [build_room_entry(r) for r in ret_val]

View File

@ -953,17 +953,17 @@ class UserInfo:
is_shadow_banned: bool is_shadow_banned: bool
class PublicRoomsChunk: class PublicRoom(TypedDict):
room_id: str room_id: str
name: str name: Optional[str]
topic: str topic: Optional[str]
num_joined_members: int num_joined_members: int
canonical_alias: str canonical_alias: Optional[str]
avatar_url: str avatar_url: Optional[str]
world_readable: bool world_readable: bool
guest_can_join: bool guest_can_join: bool
join_rule: str join_rule: Optional[str]
room_type: str room_type: Optional[str]
class UserProfile(TypedDict): class UserProfile(TypedDict):