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

Commit e5b4d93

Browse files
Update Mutual Rooms (MSC2666) implementation (#15621)
To track changes in MSC2666: - The change from `/mutual_rooms/{user_id}` to `/mutual_rooms?user_id={user_id}`. - The addition of `next_batch_token` (and logic). - Unstable flag now being `uk.half-shot.msc2666.query_mutual_rooms`. - The error code when your own user is requested.
1 parent 5dc1f25 commit e5b4d93

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

changelog.d/15621.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update Mutual Rooms (MSC2666) implementation to match new proposal text.

synapse/rest/client/mutual_rooms.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15-
from typing import TYPE_CHECKING, Tuple
15+
from http import HTTPStatus
16+
from typing import TYPE_CHECKING, Dict, List, Tuple
1617

1718
from synapse.api.errors import Codes, SynapseError
1819
from synapse.http.server import HttpServer
19-
from synapse.http.servlet import RestServlet
20+
from synapse.http.servlet import RestServlet, parse_strings_from_args
2021
from synapse.http.site import SynapseRequest
21-
from synapse.types import JsonDict, UserID
22+
from synapse.types import JsonDict
2223

2324
from ._base import client_patterns
2425

@@ -30,11 +31,11 @@
3031

3132
class UserMutualRoomsServlet(RestServlet):
3233
"""
33-
GET /uk.half-shot.msc2666/user/mutual_rooms/{user_id} HTTP/1.1
34+
GET /uk.half-shot.msc2666/user/mutual_rooms?user_id={user_id} HTTP/1.1
3435
"""
3536

3637
PATTERNS = client_patterns(
37-
"/uk.half-shot.msc2666/user/mutual_rooms/(?P<user_id>[^/]*)",
38+
"/uk.half-shot.msc2666/user/mutual_rooms$",
3839
releases=(), # This is an unstable feature
3940
)
4041

@@ -43,17 +44,35 @@ def __init__(self, hs: "HomeServer"):
4344
self.auth = hs.get_auth()
4445
self.store = hs.get_datastores().main
4546

46-
async def on_GET(
47-
self, request: SynapseRequest, user_id: str
48-
) -> Tuple[int, JsonDict]:
49-
UserID.from_string(user_id)
47+
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
48+
# twisted.web.server.Request.args is incorrectly defined as Optional[Any]
49+
args: Dict[bytes, List[bytes]] = request.args # type: ignore
50+
51+
user_ids = parse_strings_from_args(args, "user_id", required=True)
52+
53+
if len(user_ids) > 1:
54+
raise SynapseError(
55+
HTTPStatus.BAD_REQUEST,
56+
"Duplicate user_id query parameter",
57+
errcode=Codes.INVALID_PARAM,
58+
)
59+
60+
# We don't do batching, so a batch token is illegal by default
61+
if b"batch_token" in args:
62+
raise SynapseError(
63+
HTTPStatus.BAD_REQUEST,
64+
"Unknown batch_token",
65+
errcode=Codes.INVALID_PARAM,
66+
)
67+
68+
user_id = user_ids[0]
5069

5170
requester = await self.auth.get_user_by_req(request)
5271
if user_id == requester.user.to_string():
5372
raise SynapseError(
54-
code=400,
55-
msg="You cannot request a list of shared rooms with yourself",
56-
errcode=Codes.FORBIDDEN,
73+
HTTPStatus.UNPROCESSABLE_ENTITY,
74+
"You cannot request a list of shared rooms with yourself",
75+
errcode=Codes.INVALID_PARAM,
5776
)
5877

5978
rooms = await self.store.get_mutual_rooms_between_users(

synapse/rest/client/versions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def on_GET(self, request: Request) -> Tuple[int, JsonDict]:
9191
# Implements additional endpoints as described in MSC2432
9292
"org.matrix.msc2432": True,
9393
# Implements additional endpoints as described in MSC2666
94-
"uk.half-shot.msc2666.mutual_rooms": True,
94+
"uk.half-shot.msc2666.query_mutual_rooms": True,
9595
# Whether new rooms will be set to encrypted or not (based on presets).
9696
"io.element.e2ee_forced.public": self.e2ee_forced_public,
9797
"io.element.e2ee_forced.private": self.e2ee_forced_private,

tests/rest/client/test_mutual_rooms.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from urllib.parse import quote
15+
1416
from twisted.test.proto_helpers import MemoryReactor
1517

1618
import synapse.rest.admin
@@ -44,8 +46,8 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
4446
def _get_mutual_rooms(self, token: str, other_user: str) -> FakeChannel:
4547
return self.make_request(
4648
"GET",
47-
"/_matrix/client/unstable/uk.half-shot.msc2666/user/mutual_rooms/%s"
48-
% other_user,
49+
"/_matrix/client/unstable/uk.half-shot.msc2666/user/mutual_rooms"
50+
f"?user_id={quote(other_user)}",
4951
access_token=token,
5052
)
5153

0 commit comments

Comments
 (0)