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

Support & use stable endpoints for MSC4151 #17374

Open
wants to merge 3 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/17374.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support [MSC4151](https://github.com/matrix-org/matrix-spec-proposals/pull/4151)'s stable report room API.
47 changes: 45 additions & 2 deletions synapse/rest/client/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ async def on_POST(
return 200, {}


class ReportRoomRestServlet(RestServlet):
# TODO: Delete UnstableReportRoomRestServlet after 2-3 releases
# https://github.com/element-hq/synapse/issues/17373
class UnstableReportRoomRestServlet(RestServlet):
"""This endpoint lets clients report a room for abuse.

Whilst MSC4151 is not yet merged, this unstable endpoint is enabled on matrix.org
Expand Down Expand Up @@ -155,8 +157,49 @@ async def on_POST(
return 200, {}


class ReportRoomRestServlet(RestServlet):
"""This endpoint lets clients report a room for abuse.

Introduced by MSC4151: https://github.com/matrix-org/matrix-spec-proposals/pull/4151
"""

PATTERNS = client_patterns("/rooms/(?P<room_id>[^/]*)/report$")
Copy link
Member

Choose a reason for hiding this comment

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

Note that the default value for the releases argument is (r0, v3). I assume we only want to make this available on v3?

Note the unstable endpoint was available on r0 as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

For cleanliness it should be v3 only - is there a common way to declare that? It's not great that the endpoint was on r0 all this time too :(

Copy link
Member

@anoadragon453 anoadragon453 Jul 11, 2024

Choose a reason for hiding this comment

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

The usual method is releases=("v3",).


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

class PostBody(RequestBodyModel):
reason: StrictStr

async def on_POST(
self, request: SynapseRequest, room_id: str
) -> Tuple[int, JsonDict]:
requester = await self.auth.get_user_by_req(request)
user_id = requester.user.to_string()

body = parse_and_validate_json_object_from_request(request, self.PostBody)

room = await self.store.get_room(room_id)
if room is None:
raise NotFoundError("Room does not exist")

await self.store.add_room_report(
room_id=room_id,
user_id=user_id,
reason=body.reason,
received_ts=self.clock.time_msec(),
)

return 200, {}


def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
ReportEventRestServlet(hs).register(http_server)
ReportRoomRestServlet(hs).register(http_server)

if hs.config.experimental.msc4151_enabled:
ReportRoomRestServlet(hs).register(http_server)
UnstableReportRoomRestServlet(hs).register(http_server)
Comment on lines +202 to +205
Copy link
Member

Choose a reason for hiding this comment

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

Given we don't expect the functionality to differ between the stable and unstable versions of this endpoint, you can avoid the code duplication by setting PATTERNS from within the __init__ method of ReportRoomRestServlet based on the experimental config value:

class ReportRoomRestServlet(RestServlet): 
   # Note: Top-level PATTERNS definition removed
 
    def __init__(self, hs: "HomeServer"):
        super().__init__()
        self.hs = hs
        self.auth = hs.get_auth()
        self.clock = hs.get_clock()
        self.store = hs.get_datastores().main

        self.PATTERNS = client_patterns(
	        "/rooms/(?P<room_id>[^/]*)/report$",
	        releases=("v3",),
            # TODO: Remove the unstable variant after 2-3 releases
            # https://github.com/element-hq/synapse/issues/17373
	        unstable=hs.config.experimental.msc4151_enabled,
            v1=False,
	    )

Copy link
Member Author

Choose a reason for hiding this comment

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

actually, how would this know to use unstable/org.matrix.msc4151 as its version?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, true. In that case you could instead do:

class ReportRoomRestServlet(RestServlet): 
    PATTERNS = client_patterns(
        "/rooms/(?P<room_id>[^/]*)/report$"),
        releases=("v3",),
        unstable=False,
        v1=False,
    )
 
    def __init__(self, hs: "HomeServer"):
        super().__init__()
        self.hs = hs
        self.auth = hs.get_auth()
        self.clock = hs.get_clock()
        self.store = hs.get_datastores().main

        # TODO: Remove the unstable variant after 2-3 releases
        # https://github.com/element-hq/synapse/issues/17373
        if hs.config.experimental.msc4151_enabled:
            self.PATTERNS.append(
                re.compile(
                    f"^{CLIENT_API_PREFIX}/unstable/org.matrix.msc4151"
                    "/rooms/(?P<room_id>[^/]*)/report$"
                )
            )

31 changes: 2 additions & 29 deletions tests/rest/client/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,58 +156,31 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.room_id = self.helper.create_room_as(
self.other_user, tok=self.other_user_tok, is_public=True
)
self.report_path = (
f"/_matrix/client/unstable/org.matrix.msc4151/rooms/{self.room_id}/report"
)
self.report_path = f"/_matrix/client/v3/rooms/{self.room_id}/report"

@unittest.override_config(
{
"experimental_features": {"msc4151_enabled": True},
}
)
def test_reason_str(self) -> None:
data = {"reason": "this makes me sad"}
self._assert_status(200, data)

@unittest.override_config(
{
"experimental_features": {"msc4151_enabled": True},
}
)
def test_no_reason(self) -> None:
data = {"not_reason": "for typechecking"}
self._assert_status(400, data)

@unittest.override_config(
{
"experimental_features": {"msc4151_enabled": True},
}
)
def test_reason_nonstring(self) -> None:
data = {"reason": 42}
self._assert_status(400, data)

@unittest.override_config(
{
"experimental_features": {"msc4151_enabled": True},
}
)
def test_reason_null(self) -> None:
data = {"reason": None}
self._assert_status(400, data)

@unittest.override_config(
{
"experimental_features": {"msc4151_enabled": True},
}
)
def test_cannot_report_nonexistent_room(self) -> None:
"""
Tests that we don't accept event reports for rooms which do not exist.
"""
channel = self.make_request(
"POST",
"/_matrix/client/unstable/org.matrix.msc4151/rooms/!bloop:example.org/report",
"/_matrix/client/v3/rooms/!bloop:example.org/report",
{"reason": "i am very sad"},
access_token=self.other_user_tok,
shorthand=False,
Expand Down
Loading