mirror of
https://github.com/element-hq/synapse.git
synced 2025-03-15 12:17:48 +00:00
Support & use stable endpoints for MSC4151 (#17374)
https://github.com/matrix-org/matrix-spec-proposals/pull/4151 has finished FCP. See https://github.com/element-hq/synapse/issues/17373 for unstable endpoint removal --------- Co-authored-by: Andrew Morgan <andrew@amorgan.xyz>
This commit is contained in:
parent
47fe6df013
commit
c705beebf7
3 changed files with 25 additions and 42 deletions
1
changelog.d/17374.feature
Normal file
1
changelog.d/17374.feature
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Support [MSC4151](https://github.com/matrix-org/matrix-spec-proposals/pull/4151)'s stable report room API.
|
|
@ -20,11 +20,13 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
from typing import TYPE_CHECKING, Tuple
|
from typing import TYPE_CHECKING, Tuple
|
||||||
|
|
||||||
from synapse._pydantic_compat import StrictStr
|
from synapse._pydantic_compat import StrictStr
|
||||||
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
|
from synapse.api.errors import AuthError, Codes, NotFoundError, SynapseError
|
||||||
|
from synapse.api.urls import CLIENT_API_PREFIX
|
||||||
from synapse.http.server import HttpServer
|
from synapse.http.server import HttpServer
|
||||||
from synapse.http.servlet import (
|
from synapse.http.servlet import (
|
||||||
RestServlet,
|
RestServlet,
|
||||||
|
@ -105,18 +107,17 @@ class ReportEventRestServlet(RestServlet):
|
||||||
class ReportRoomRestServlet(RestServlet):
|
class ReportRoomRestServlet(RestServlet):
|
||||||
"""This endpoint lets clients report a room for abuse.
|
"""This endpoint lets clients report a room for abuse.
|
||||||
|
|
||||||
Whilst MSC4151 is not yet merged, this unstable endpoint is enabled on matrix.org
|
Introduced by MSC4151: https://github.com/matrix-org/matrix-spec-proposals/pull/4151
|
||||||
for content moderation purposes, and therefore backwards compatibility should be
|
|
||||||
carefully considered when changing anything on this endpoint.
|
|
||||||
|
|
||||||
More details on the MSC: https://github.com/matrix-org/matrix-spec-proposals/pull/4151
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
PATTERNS = client_patterns(
|
# Cast the Iterable to a list so that we can `append` below.
|
||||||
"/org.matrix.msc4151/rooms/(?P<room_id>[^/]*)/report$",
|
PATTERNS = list(
|
||||||
releases=[],
|
client_patterns(
|
||||||
v1=False,
|
"/rooms/(?P<room_id>[^/]*)/report$",
|
||||||
unstable=True,
|
releases=("v3",),
|
||||||
|
unstable=False,
|
||||||
|
v1=False,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, hs: "HomeServer"):
|
def __init__(self, hs: "HomeServer"):
|
||||||
|
@ -126,6 +127,16 @@ class ReportRoomRestServlet(RestServlet):
|
||||||
self.clock = hs.get_clock()
|
self.clock = hs.get_clock()
|
||||||
self.store = hs.get_datastores().main
|
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$"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
class PostBody(RequestBodyModel):
|
class PostBody(RequestBodyModel):
|
||||||
reason: StrictStr
|
reason: StrictStr
|
||||||
|
|
||||||
|
@ -153,6 +164,4 @@ class ReportRoomRestServlet(RestServlet):
|
||||||
|
|
||||||
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
||||||
ReportEventRestServlet(hs).register(http_server)
|
ReportEventRestServlet(hs).register(http_server)
|
||||||
|
ReportRoomRestServlet(hs).register(http_server)
|
||||||
if hs.config.experimental.msc4151_enabled:
|
|
||||||
ReportRoomRestServlet(hs).register(http_server)
|
|
||||||
|
|
|
@ -156,58 +156,31 @@ class ReportRoomTestCase(unittest.HomeserverTestCase):
|
||||||
self.room_id = self.helper.create_room_as(
|
self.room_id = self.helper.create_room_as(
|
||||||
self.other_user, tok=self.other_user_tok, is_public=True
|
self.other_user, tok=self.other_user_tok, is_public=True
|
||||||
)
|
)
|
||||||
self.report_path = (
|
self.report_path = f"/_matrix/client/v3/rooms/{self.room_id}/report"
|
||||||
f"/_matrix/client/unstable/org.matrix.msc4151/rooms/{self.room_id}/report"
|
|
||||||
)
|
|
||||||
|
|
||||||
@unittest.override_config(
|
|
||||||
{
|
|
||||||
"experimental_features": {"msc4151_enabled": True},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
def test_reason_str(self) -> None:
|
def test_reason_str(self) -> None:
|
||||||
data = {"reason": "this makes me sad"}
|
data = {"reason": "this makes me sad"}
|
||||||
self._assert_status(200, data)
|
self._assert_status(200, data)
|
||||||
|
|
||||||
@unittest.override_config(
|
|
||||||
{
|
|
||||||
"experimental_features": {"msc4151_enabled": True},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
def test_no_reason(self) -> None:
|
def test_no_reason(self) -> None:
|
||||||
data = {"not_reason": "for typechecking"}
|
data = {"not_reason": "for typechecking"}
|
||||||
self._assert_status(400, data)
|
self._assert_status(400, data)
|
||||||
|
|
||||||
@unittest.override_config(
|
|
||||||
{
|
|
||||||
"experimental_features": {"msc4151_enabled": True},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
def test_reason_nonstring(self) -> None:
|
def test_reason_nonstring(self) -> None:
|
||||||
data = {"reason": 42}
|
data = {"reason": 42}
|
||||||
self._assert_status(400, data)
|
self._assert_status(400, data)
|
||||||
|
|
||||||
@unittest.override_config(
|
|
||||||
{
|
|
||||||
"experimental_features": {"msc4151_enabled": True},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
def test_reason_null(self) -> None:
|
def test_reason_null(self) -> None:
|
||||||
data = {"reason": None}
|
data = {"reason": None}
|
||||||
self._assert_status(400, data)
|
self._assert_status(400, data)
|
||||||
|
|
||||||
@unittest.override_config(
|
|
||||||
{
|
|
||||||
"experimental_features": {"msc4151_enabled": True},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
def test_cannot_report_nonexistent_room(self) -> None:
|
def test_cannot_report_nonexistent_room(self) -> None:
|
||||||
"""
|
"""
|
||||||
Tests that we don't accept event reports for rooms which do not exist.
|
Tests that we don't accept event reports for rooms which do not exist.
|
||||||
"""
|
"""
|
||||||
channel = self.make_request(
|
channel = self.make_request(
|
||||||
"POST",
|
"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"},
|
{"reason": "i am very sad"},
|
||||||
access_token=self.other_user_tok,
|
access_token=self.other_user_tok,
|
||||||
shorthand=False,
|
shorthand=False,
|
||||||
|
|
Loading…
Add table
Reference in a new issue