Skip to content

Commit c705bee

Browse files
Support & use stable endpoints for MSC4151 (#17374)
matrix-org/matrix-spec-proposals#4151 has finished FCP. See #17373 for unstable endpoint removal --------- Co-authored-by: Andrew Morgan <[email protected]>
1 parent 47fe6df commit c705bee

File tree

3 files changed

+25
-42
lines changed

3 files changed

+25
-42
lines changed

changelog.d/17374.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Support [MSC4151](https://github.com/matrix-org/matrix-spec-proposals/pull/4151)'s stable report room API.

synapse/rest/client/reporting.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#
2121

2222
import logging
23+
import re
2324
from http import HTTPStatus
2425
from typing import TYPE_CHECKING, Tuple
2526

2627
from synapse._pydantic_compat import StrictStr
2728
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
29+
from synapse.api.urls import CLIENT_API_PREFIX
2830
from synapse.http.server import HttpServer
2931
from synapse.http.servlet import (
3032
RestServlet,
@@ -105,18 +107,17 @@ async def on_POST(
105107
class ReportRoomRestServlet(RestServlet):
106108
"""This endpoint lets clients report a room for abuse.
107109
108-
Whilst MSC4151 is not yet merged, this unstable endpoint is enabled on matrix.org
109-
for content moderation purposes, and therefore backwards compatibility should be
110-
carefully considered when changing anything on this endpoint.
111-
112-
More details on the MSC: https://github.com/matrix-org/matrix-spec-proposals/pull/4151
110+
Introduced by MSC4151: https://github.com/matrix-org/matrix-spec-proposals/pull/4151
113111
"""
114112

115-
PATTERNS = client_patterns(
116-
"/org.matrix.msc4151/rooms/(?P<room_id>[^/]*)/report$",
117-
releases=[],
118-
v1=False,
119-
unstable=True,
113+
# Cast the Iterable to a list so that we can `append` below.
114+
PATTERNS = list(
115+
client_patterns(
116+
"/rooms/(?P<room_id>[^/]*)/report$",
117+
releases=("v3",),
118+
unstable=False,
119+
v1=False,
120+
)
120121
)
121122

122123
def __init__(self, hs: "HomeServer"):
@@ -126,6 +127,16 @@ def __init__(self, hs: "HomeServer"):
126127
self.clock = hs.get_clock()
127128
self.store = hs.get_datastores().main
128129

130+
# TODO: Remove the unstable variant after 2-3 releases
131+
# https://github.com/element-hq/synapse/issues/17373
132+
if hs.config.experimental.msc4151_enabled:
133+
self.PATTERNS.append(
134+
re.compile(
135+
f"^{CLIENT_API_PREFIX}/unstable/org.matrix.msc4151"
136+
"/rooms/(?P<room_id>[^/]*)/report$"
137+
)
138+
)
139+
129140
class PostBody(RequestBodyModel):
130141
reason: StrictStr
131142

@@ -153,6 +164,4 @@ async def on_POST(
153164

154165
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
155166
ReportEventRestServlet(hs).register(http_server)
156-
157-
if hs.config.experimental.msc4151_enabled:
158-
ReportRoomRestServlet(hs).register(http_server)
167+
ReportRoomRestServlet(hs).register(http_server)

tests/rest/client/test_reporting.py

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -156,58 +156,31 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
156156
self.room_id = self.helper.create_room_as(
157157
self.other_user, tok=self.other_user_tok, is_public=True
158158
)
159-
self.report_path = (
160-
f"/_matrix/client/unstable/org.matrix.msc4151/rooms/{self.room_id}/report"
161-
)
159+
self.report_path = f"/_matrix/client/v3/rooms/{self.room_id}/report"
162160

163-
@unittest.override_config(
164-
{
165-
"experimental_features": {"msc4151_enabled": True},
166-
}
167-
)
168161
def test_reason_str(self) -> None:
169162
data = {"reason": "this makes me sad"}
170163
self._assert_status(200, data)
171164

172-
@unittest.override_config(
173-
{
174-
"experimental_features": {"msc4151_enabled": True},
175-
}
176-
)
177165
def test_no_reason(self) -> None:
178166
data = {"not_reason": "for typechecking"}
179167
self._assert_status(400, data)
180168

181-
@unittest.override_config(
182-
{
183-
"experimental_features": {"msc4151_enabled": True},
184-
}
185-
)
186169
def test_reason_nonstring(self) -> None:
187170
data = {"reason": 42}
188171
self._assert_status(400, data)
189172

190-
@unittest.override_config(
191-
{
192-
"experimental_features": {"msc4151_enabled": True},
193-
}
194-
)
195173
def test_reason_null(self) -> None:
196174
data = {"reason": None}
197175
self._assert_status(400, data)
198176

199-
@unittest.override_config(
200-
{
201-
"experimental_features": {"msc4151_enabled": True},
202-
}
203-
)
204177
def test_cannot_report_nonexistent_room(self) -> None:
205178
"""
206179
Tests that we don't accept event reports for rooms which do not exist.
207180
"""
208181
channel = self.make_request(
209182
"POST",
210-
"/_matrix/client/unstable/org.matrix.msc4151/rooms/!bloop:example.org/report",
183+
"/_matrix/client/v3/rooms/!bloop:example.org/report",
211184
{"reason": "i am very sad"},
212185
access_token=self.other_user_tok,
213186
shorthand=False,

0 commit comments

Comments
 (0)