Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MSC4185: Event Visibility API #17973

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.d/17973.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement MSC4185: Event Visibility API.
3 changes: 3 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
# MSC4151: Report room API (Client-Server API)
self.msc4151_enabled: bool = experimental.get("msc4151_enabled", False)

# MSC4185: Event Visibility API
self.msc4185_enabled: bool = experimental.get("msc4185_enabled", False)

# MSC4210: Remove legacy mentions
self.msc4210_enabled: bool = experimental.get("msc4210_enabled", False)

Expand Down
2 changes: 2 additions & 0 deletions synapse/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
account,
account_data,
account_validity,
appservice_event_visibility,
appservice_ping,
auth,
auth_issuer,
Expand Down Expand Up @@ -117,6 +118,7 @@
password_policy.register_servlets,
knock.register_servlets,
appservice_ping.register_servlets,
appservice_event_visibility.register_servlets,
admin.register_servlets_for_client_rest_resource,
mutual_rooms.register_servlets,
login_token_request.register_servlets,
Expand Down
75 changes: 75 additions & 0 deletions synapse/rest/client/appservice_event_visibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2024 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 http import HTTPStatus
from typing import TYPE_CHECKING, Tuple

from synapse.api.errors import (
Codes,
SynapseError,
)
from synapse.http.server import HttpServer
from synapse.http.servlet import RestServlet
from synapse.http.site import SynapseRequest
from synapse.visibility import filter_events_for_client

from ._base import client_patterns

if TYPE_CHECKING:
from synapse.server import HomeServer

logger = logging.getLogger(__name__)


class AppserviceEventVisibilityrestServlet(RestServlet):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the alternative mentioned in matrix-org/matrix-spec-proposals#4185 (comment) cover your use case? Feels like a new endpoint isn't necessary

PATTERNS = client_patterns(
"/net.tadzik/appservice/(?P<appservice_id>[^/]*)/can_user_see_event/(?P<room_id>[^/]*)/(?P<user_id>[^/]*)/(?P<event_id>[^/]*)$",
unstable=True,
releases=(),
)

def __init__(self, hs: "HomeServer"):
super().__init__()
self.auth = hs.get_auth()
self.store = hs.get_datastores().main
self._storage_controllers = hs.get_storage_controllers()

async def on_GET(
self, request: SynapseRequest, appservice_id: str, room_id: str, user_id: str, event_id: str
) -> Tuple[int, bool]:
logger.info('on_GET')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover debug log

requester = await self.auth.get_user_by_req(request)

if not requester.app_service:
raise SynapseError(
HTTPStatus.FORBIDDEN,
"Only application services can use the /can_user_see_event endpoint",
Codes.FORBIDDEN,
)
elif requester.app_service.id != appservice_id:
raise SynapseError(
HTTPStatus.FORBIDDEN,
"Mismatching application service ID in path",
Codes.FORBIDDEN,
)

event = await self.store.get_event(event_id, check_room_id=room_id)
filtered = await filter_events_for_client(self._storage_controllers, user_id, [event])
Comment on lines +68 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have some sanity check test cases. We don't need to re-test every scenario that filter_events_for_client(...) covers but would be good to have a true and false test case.


return HTTPStatus.OK, bool(filtered)

def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
if hs.config.experimental.msc4185_enabled:
AppserviceEventVisibilityrestServlet(hs).register(http_server)
2 changes: 2 additions & 0 deletions synapse/rest/client/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
"fi.mau.msc2815": self.config.experimental.msc2815_enabled,
# Adds a ping endpoint for appservices to check HS->AS connection
"fi.mau.msc2659.stable": True, # TODO: remove when "v1.7" is added above
# Adds Event visibility API
"net.tadzik.msc4185": self.config.experimental.msc4185_enabled,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the org.matrix.msc4185 unstable identifier (same question for the endpoint)? Is there some prior art somewhere else?

# TODO: this is no longer needed once unstable MSC3882 does not need to be supported:
"org.matrix.msc3882": self.config.auth.login_via_existing_enabled,
# Adds support for remotely enabling/disabling pushers, as per MSC3881
Expand Down
Loading