Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Experimental support for MSC3266 Room Summary API #10394

Merged
merged 41 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7d9c34d
Extract ResolveRoomIdMixin and reuse it for /_matrix/client/r0/join/{…
t3chguy Jul 14, 2021
ef295d1
First cut of a local-only poc for MSC3266
t3chguy Jul 14, 2021
85abffc
Update allowed_spaces to allowed_room_ids to match Space Summary API
t3chguy Jul 15, 2021
b0c8e02
Extract _is_room_accessible into synapse.api.auth.Auth
t3chguy Jul 15, 2021
d169cd8
Extract build_room_entry into a mixin, updates Space Summary API allo…
t3chguy Jul 15, 2021
01b222a
remove unused variables
t3chguy Jul 15, 2021
9227efc
delint
t3chguy Jul 15, 2021
c02485c
fix types
t3chguy Jul 15, 2021
0fa04fe
Fix typo in the msc3266 experimental config flag
t3chguy Jul 15, 2021
3982736
Merge branch 'develop' into msc3266
t3chguy Jul 19, 2021
957cb4f
Merge branch 'develop' of https://github.com/matrix-org/synapse into …
t3chguy Aug 4, 2021
9994f12
extract requester_can_see_room_entry to reuse in both space and room …
t3chguy Aug 5, 2021
b2c8a7c
Add newsfragment
t3chguy Aug 5, 2021
e6e7ecf
Add tests for the room_summary handler and make remote_room_hosts opt…
t3chguy Aug 5, 2021
85c166e
Merge branch 'msc3266' of github.com:t3chguy/synapse into msc3266
t3chguy Aug 5, 2021
cc6c271
fix mypy lint and missing await
t3chguy Aug 5, 2021
0c5952b
delint s'more
t3chguy Aug 5, 2021
9e113aa
fix test_state by stubbing get_event_auth_handler
t3chguy Aug 5, 2021
faa8bf1
Merge branch 'develop' of https://github.com/matrix-org/synapse into …
t3chguy Aug 12, 2021
caa8015
re-apply consolidation between room_summary and space_summary
t3chguy Aug 12, 2021
f5c6740
delint
t3chguy Aug 12, 2021
2a9b961
Make requester optional to be more generically re-usable
t3chguy Aug 12, 2021
b277031
update naming and comments
t3chguy Aug 12, 2021
61d9312
Apply suggestions from code review
t3chguy Aug 13, 2021
dfa8247
remove references to summary
t3chguy Aug 13, 2021
9dbe660
Consolidate the two handlers into 1
t3chguy Aug 13, 2021
b7ba82a
Fix mocks
t3chguy Aug 13, 2021
7a57bcc
fix arg order
t3chguy Aug 13, 2021
8371e26
minimise diff
t3chguy Aug 13, 2021
d4020d8
delint
t3chguy Aug 13, 2021
3fbf621
revert bits of the PR
t3chguy Aug 13, 2021
e99ca29
revert s'more
t3chguy Aug 13, 2021
2823459
minimise diff
t3chguy Aug 13, 2021
82c6f40
Fix ordering of mypy.
clokep Aug 13, 2021
82ed39c
Update comments.
clokep Aug 13, 2021
0a74384
Rollback an unused change.
clokep Aug 13, 2021
5dcbf1a
Strip out code for federation, which is not yet supported.
clokep Aug 13, 2021
a168618
Pull in changes from #10569 to simplify code.
clokep Aug 13, 2021
d9c4e0c
Merge remote-tracking branch 'origin/develop' into msc3266
clokep Aug 16, 2021
4e23960
Inline a variable.
clokep Aug 16, 2021
a1f45e8
Merge remote-tracking branch 'origin/develop' into msc3266
clokep Aug 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ def read_config(self, config: JsonDict, **kwargs):

# MSC2716 (backfill existing history)
self.msc2716_enabled = experimental.get("msc2716_enabled", False) # type: bool

# MSC3266 (room summary api)
self.mxc3266_enabled = experimental.get("mxc3266_enabled", False) # type: bool
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
285 changes: 285 additions & 0 deletions synapse/handlers/room_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
# 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 logging
from typing import TYPE_CHECKING, List, Optional

from synapse.api.constants import (
EventContentFields,
EventTypes,
HistoryVisibility,
JoinRules,
Membership,
)
from synapse.types import JsonDict

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)


class RoomSummaryHandler:
def __init__(self, hs: "HomeServer"):
self._clock = hs.get_clock()
self._auth = hs.get_auth()
self._event_auth_handler = hs.get_event_auth_handler()
self._store = hs.get_datastore()
self._event_serializer = hs.get_event_client_serializer()
self._server_name = hs.hostname
self._federation_client = hs.get_federation_client()

async def get_room_summary(
self,
requester: Optional[str],
room_id: str,
remote_room_hosts: List[str],
) -> JsonDict:
"""
Implementation of the room summary C-S API MSC3244

Args:
requester: user id of the user making this request,
can be None for unauthenticated requests

room_id: room id to start the summary at

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_summary = await self._summarize_local_room(requester, None, room_id)

if requester:
membership, _ = await self._store.get_local_current_membership_for_user_in_room(
requester, room_id
)

room_summary["membership"] = membership or "leave"
else:
room_summary = await self._summarize_remote_room(room_id, remote_room_hosts)

# TODO validate that the requester has permission to see this room
# https://github.com/matrix-org/matrix-doc/pull/3266/files#diff-97aeb566f3ce4bd6ec3b98e71ecbca3d6e86c0407e6a82afbc57e86bf0316607R106-R108

return room_summary

async def _summarize_local_room(
self,
requester: Optional[str],
origin: Optional[str],
room_id: str,
) -> JsonDict:
"""
Generate a room entry and a list of event entries for a given room.

Args:
requester:
The user requesting the summary, if it is a local request. None
if this is a federation request.
origin:
The server requesting the summary, if it is a federation request.
None if this is a local request.
room_id: The room ID to summarize.

Returns:
summary dict to return
"""
if not await self._is_room_accessible(room_id, requester, origin):
return None

return await self._build_room_entry(room_id)

async def _summarize_remote_room(
self,
room_id: str,
remote_room_hosts: List[str],
) -> JsonDict:
"""
Request room entries and a list of event entries for a given room by querying a remote server.

Args:
room_id: The room to summarize.
remote_room_hosts: List of homeservers to attempt to fetch the data from.

Returns:
summary dict to return
"""
logger.info("Requesting summary for %s via %s", room_id, remote_room_hosts)

return None # TODO federation API

# TODO extract into mixin/helper method
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
async def _is_room_accessible(
self, room_id: str, requester: Optional[str], origin: Optional[str]
) -> bool:
"""
Calculate whether the room should be shown to the requester.

It should be included if:

* The requester is joined or can join the room (per MSC3173).
* The origin server has any user that is joined or can join the room.
* The history visibility is set to world readable.

Args:
room_id: The room ID to summarize.
requester:
The user requesting the summary, if it is a local request. None
if this is a federation request.
origin:
The server requesting the summary, if it is a federation request.
None if this is a local request.

Returns:
True if the room should be visible to the requester.
"""
state_ids = await self._store.get_current_state_ids(room_id)

# If there's no state for the room, it isn't known.
if not state_ids:
# 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

logger.info("room %s is unknown, omitting from summary", room_id)
return False

room_version = await self._store.get_room_version(room_id)

# 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")
if join_rule == JoinRules.PUBLIC or (
room_version.msc2403_knocking and join_rule == JoinRules.KNOCK
):
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).
if requester:
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.
if await self._event_auth_handler.has_restricted_join_rules(
state_ids, room_version
):
allowed_rooms = (
await self._event_auth_handler.get_rooms_that_allow_join(state_ids)
)
if await self._event_auth_handler.is_user_in_rooms(
allowed_rooms, requester
):
return True

# If this is a request over federation, check if the host is in the room or
# has a user who could join the room.
elif origin:
if await self._event_auth_handler.check_host_in_room(
room_id, origin
) or await self._store.is_host_invited(room_id, origin):
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
):
allowed_rooms = (
await self._event_auth_handler.get_rooms_that_allow_join(state_ids)
)
for space_id in allowed_rooms:
if await self._event_auth_handler.check_host_in_room(
space_id, origin
):
return True

logger.info(
"room %s is unpeekable and requester %s is not a member / not allowed to join, omitting from summary",
room_id,
requester or origin,
)
return False

async def _build_room_entry(self, room_id: str) -> JsonDict:
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
"""Generate en entry suitable for the 'rooms' list in the summary response"""
stats = await self._store.get_room_with_stats(room_id)

# currently this should be impossible because we call
# check_user_in_room_or_world_readable on the room before we get here, so
# there should always be an entry
assert stats is not None, "unable to retrieve stats for %s" % (room_id,)

current_state_ids = await self._store.get_current_state_ids(room_id)
create_event = await self._store.get_event(
current_state_ids[(EventTypes.Create, "")]
)

room_version = await self._store.get_room_version(room_id)
allowed_rooms = None
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
)

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"],
"join_rules": stats["join_rules"],
"world_readable": (
stats["history_visibility"] == HistoryVisibility.WORLD_READABLE
),
"guest_can_join": stats["guest_access"] == "can_join",
"creation_ts": create_event.origin_server_ts,
"room_type": create_event.content.get(EventContentFields.ROOM_TYPE),
"allowed_spaces": allowed_rooms, # TODO unspecced for MSC3266
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
"is_encrypted": (EventTypes.RoomEncryption, "") in current_state_ids,
}

# 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
50 changes: 48 additions & 2 deletions synapse/http/servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@

""" This module contains base REST classes for constructing REST servlets. """
import logging
from typing import Dict, Iterable, List, Optional, overload
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, overload, Tuple

from typing_extensions import Literal

from twisted.web.server import Request

from synapse.api.errors import Codes, SynapseError
from synapse.util import json_decoder
from synapse.types import RoomAlias, RoomID

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)


def parse_integer(request, name, default=None, required=False):
"""Parse an integer parameter from the request string

thank
Args:
request: the twisted HTTP request.
name (bytes/unicode): the name of the query parameter.
Expand Down Expand Up @@ -509,3 +513,45 @@ def register(self, http_server):

else:
raise NotImplementedError("RestServlet must register something.")


class ResolveRoomIdMixin:
def __init__(self, hs: "HomeServer"):
self.room_member_handler = hs.get_room_member_handler()

async def resolve_room_id(
self, room_identifier: str, remote_room_hosts: Optional[List[str]] = None
) -> Tuple[str, Optional[List[str]]]:
"""
Resolve a room identifier to a room ID, if necessary.

This also performanes checks to ensure the room ID is of the proper form.

Args:
room_identifier: The room ID or alias.
remote_room_hosts: The potential remote room hosts to use.

Returns:
The resolved room ID.

Raises:
SynapseError if the room ID is of the wrong form.
"""
if RoomID.is_valid(room_identifier):
resolved_room_id = room_identifier
elif RoomAlias.is_valid(room_identifier):
room_alias = RoomAlias.from_string(room_identifier)
(
room_id,
remote_room_hosts,
) = await self.room_member_handler.lookup_room_alias(room_alias)
resolved_room_id = room_id.to_string()
else:
raise SynapseError(
400, "%s was not legal room ID or room alias" % (room_identifier,)
)
if not resolved_room_id:
raise SynapseError(
400, "Unknown room ID or room alias %s" % room_identifier
)
return resolved_room_id, remote_room_hosts
Loading