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

Commit b257c78

Browse files
Half-Shotanoadragon453clokeptulir
authored
Add /user/{user_id}/shared_rooms/ api (#7785)
* Add shared_rooms api * Add changelog * Add . * Wrap response in {"rooms": } * linting * Add unstable_features key * Remove options from isort that aren't part of 5.x `-y` and `-rc` are now default behaviour and no longer exist. `dont-skip` is no longer required https://timothycrosley.github.io/isort/CHANGELOG/#500-penny-july-4-2020 * Update imports to make isort happy * Add changelog * Update tox.ini file with correct invocation * fix linting again for isort * Vendor prefix unstable API * Fix to match spec * import Codes * import Codes * Use FORBIDDEN * Update changelog.d/7785.feature Co-authored-by: Andrew Morgan <[email protected]> * Implement get_shared_rooms_for_users * a comma * trailing whitespace * Handle the easy feedback * Switch to using runInteraction * Add tests * Feedback * Seperate unstable endpoint from v2 * Add upgrade node * a line * Fix style by adding a blank line at EOF. * Update synapse/storage/databases/main/user_directory.py Co-authored-by: Tulir Asokan <[email protected]> * Update synapse/storage/databases/main/user_directory.py Co-authored-by: Andrew Morgan <[email protected]> * Update UPGRADE.rst Co-authored-by: Andrew Morgan <[email protected]> * Fix UPGRADE/CHANGELOG unstable paths unstable unstable unstable Co-authored-by: Andrew Morgan <[email protected]> Co-authored-by: Tulir Asokan <[email protected]> Co-authored-by: Andrew Morgan <[email protected]> Co-authored-by: Patrick Cloke <[email protected]> Co-authored-by: Tulir Asokan <[email protected]>
1 parent 9356656 commit b257c78

File tree

8 files changed

+270
-1
lines changed

8 files changed

+270
-1
lines changed

UPGRADE.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
Upgrading to v1.20.0
2+
====================
3+
4+
Shared rooms endpoint (MSC2666)
5+
-------------------------------
6+
7+
This release contains a new unstable endpoint `/_matrix/client/unstable/uk.half-shot.msc2666/user/shared_rooms/.*`
8+
for fetching rooms one user has in common with another. This feature requires the
9+
`update_user_directory` config flag to be `True`. If you are you are using a `synapse.app.user_dir`
10+
worker, requests to this endpoint must be handled by that worker.
11+
See `docs/workers.md <docs/workers.md>`_ for more details.
12+
13+
114
Upgrading Synapse
215
=================
316

changelog.d/7785.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add an endpoint to query your shared rooms with another user as an implementation of [MSC2666](https://github.com/matrix-org/matrix-doc/pull/2666).

docs/workers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ Handles searches in the user directory. It can handle REST endpoints matching
380380
the following regular expressions:
381381

382382
^/_matrix/client/(api/v1|r0|unstable)/user_directory/search$
383+
^/_matrix/client/unstable/uk.half-shot.msc2666/user/shared_rooms/.*$
383384

384385
When using this worker you must also set `update_user_directory: False` in the
385386
shared configuration file to stop the main synapse running background

synapse/rest/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
room_keys,
5151
room_upgrade_rest_servlet,
5252
sendtodevice,
53+
shared_rooms,
5354
sync,
5455
tags,
5556
thirdparty,
@@ -125,3 +126,6 @@ def register_servlets(client_resource, hs):
125126
synapse.rest.admin.register_servlets_for_client_rest_resource(
126127
hs, client_resource
127128
)
129+
130+
# unstable
131+
shared_rooms.register_servlets(hs, client_resource)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 Half-Shot
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import logging
16+
17+
from synapse.api.errors import Codes, SynapseError
18+
from synapse.http.servlet import RestServlet
19+
from synapse.types import UserID
20+
21+
from ._base import client_patterns
22+
23+
logger = logging.getLogger(__name__)
24+
25+
26+
class UserSharedRoomsServlet(RestServlet):
27+
"""
28+
GET /uk.half-shot.msc2666/user/shared_rooms/{user_id} HTTP/1.1
29+
"""
30+
31+
PATTERNS = client_patterns(
32+
"/uk.half-shot.msc2666/user/shared_rooms/(?P<user_id>[^/]*)",
33+
releases=(), # This is an unstable feature
34+
)
35+
36+
def __init__(self, hs):
37+
super(UserSharedRoomsServlet, self).__init__()
38+
self.auth = hs.get_auth()
39+
self.store = hs.get_datastore()
40+
self.user_directory_active = hs.config.update_user_directory
41+
42+
async def on_GET(self, request, user_id):
43+
44+
if not self.user_directory_active:
45+
raise SynapseError(
46+
code=400,
47+
msg="The user directory is disabled on this server. Cannot determine shared rooms.",
48+
errcode=Codes.FORBIDDEN,
49+
)
50+
51+
UserID.from_string(user_id)
52+
53+
requester = await self.auth.get_user_by_req(request)
54+
if user_id == requester.user.to_string():
55+
raise SynapseError(
56+
code=400,
57+
msg="You cannot request a list of shared rooms with yourself",
58+
errcode=Codes.FORBIDDEN,
59+
)
60+
rooms = await self.store.get_shared_rooms_for_users(
61+
requester.user.to_string(), user_id
62+
)
63+
64+
return 200, {"joined": list(rooms)}
65+
66+
67+
def register_servlets(hs, http_server):
68+
UserSharedRoomsServlet(hs).register(http_server)

synapse/rest/client/versions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def on_GET(self, request):
6060
"org.matrix.e2e_cross_signing": True,
6161
# Implements additional endpoints as described in MSC2432
6262
"org.matrix.msc2432": True,
63+
# Implements additional endpoints as described in MSC2666
64+
"uk.half-shot.msc2666": True,
6365
},
6466
},
6567
)

synapse/storage/databases/main/user_directory.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import logging
1717
import re
18-
from typing import Any, Dict, Iterable, Optional, Tuple
18+
from typing import Any, Dict, Iterable, Optional, Set, Tuple
1919

2020
from synapse.api.constants import EventTypes, JoinRules
2121
from synapse.storage.database import DatabasePool
@@ -675,6 +675,48 @@ async def get_user_dir_rooms_user_is_in(self, user_id):
675675
users.update(rows)
676676
return list(users)
677677

678+
@cached()
679+
async def get_shared_rooms_for_users(
680+
self, user_id: str, other_user_id: str
681+
) -> Set[str]:
682+
"""
683+
Returns the rooms that a local user shares with another local or remote user.
684+
685+
Args:
686+
user_id: The MXID of a local user
687+
other_user_id: The MXID of the other user
688+
689+
Returns:
690+
A set of room ID's that the users share.
691+
"""
692+
693+
def _get_shared_rooms_for_users_txn(txn):
694+
txn.execute(
695+
"""
696+
SELECT p1.room_id
697+
FROM users_in_public_rooms as p1
698+
INNER JOIN users_in_public_rooms as p2
699+
ON p1.room_id = p2.room_id
700+
AND p1.user_id = ?
701+
AND p2.user_id = ?
702+
UNION
703+
SELECT room_id
704+
FROM users_who_share_private_rooms
705+
WHERE
706+
user_id = ?
707+
AND other_user_id = ?
708+
""",
709+
(user_id, other_user_id, user_id, other_user_id),
710+
)
711+
rows = self.db_pool.cursor_to_dict(txn)
712+
return rows
713+
714+
rows = await self.db_pool.runInteraction(
715+
"get_shared_rooms_for_users", _get_shared_rooms_for_users_txn
716+
)
717+
718+
return {row["room_id"] for row in rows}
719+
678720
async def get_user_directory_stream_pos(self) -> int:
679721
return await self.db_pool.simple_select_one_onecol(
680722
table="user_directory_stream_pos",
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2020 Half-Shot
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
import synapse.rest.admin
16+
from synapse.rest.client.v1 import login, room
17+
from synapse.rest.client.v2_alpha import shared_rooms
18+
19+
from tests import unittest
20+
21+
22+
class UserSharedRoomsTest(unittest.HomeserverTestCase):
23+
"""
24+
Tests the UserSharedRoomsServlet.
25+
"""
26+
27+
servlets = [
28+
login.register_servlets,
29+
synapse.rest.admin.register_servlets_for_client_rest_resource,
30+
room.register_servlets,
31+
shared_rooms.register_servlets,
32+
]
33+
34+
def make_homeserver(self, reactor, clock):
35+
config = self.default_config()
36+
config["update_user_directory"] = True
37+
return self.setup_test_homeserver(config=config)
38+
39+
def prepare(self, reactor, clock, hs):
40+
self.store = hs.get_datastore()
41+
self.handler = hs.get_user_directory_handler()
42+
43+
def _get_shared_rooms(self, token, other_user):
44+
request, channel = self.make_request(
45+
"GET",
46+
"/_matrix/client/unstable/uk.half-shot.msc2666/user/shared_rooms/%s"
47+
% other_user,
48+
access_token=token,
49+
)
50+
self.render(request)
51+
return request, channel
52+
53+
def test_shared_room_list_public(self):
54+
"""
55+
A room should show up in the shared list of rooms between two users
56+
if it is public.
57+
"""
58+
u1 = self.register_user("user1", "pass")
59+
u1_token = self.login(u1, "pass")
60+
u2 = self.register_user("user2", "pass")
61+
u2_token = self.login(u2, "pass")
62+
63+
room = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
64+
self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
65+
self.helper.join(room, user=u2, tok=u2_token)
66+
67+
request, channel = self._get_shared_rooms(u1_token, u2)
68+
self.assertEquals(200, channel.code, channel.result)
69+
self.assertEquals(len(channel.json_body["joined"]), 1)
70+
self.assertEquals(channel.json_body["joined"][0], room)
71+
72+
def test_shared_room_list_private(self):
73+
"""
74+
A room should show up in the shared list of rooms between two users
75+
if it is private.
76+
"""
77+
u1 = self.register_user("user1", "pass")
78+
u1_token = self.login(u1, "pass")
79+
u2 = self.register_user("user2", "pass")
80+
u2_token = self.login(u2, "pass")
81+
82+
room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
83+
self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
84+
self.helper.join(room, user=u2, tok=u2_token)
85+
86+
request, channel = self._get_shared_rooms(u1_token, u2)
87+
self.assertEquals(200, channel.code, channel.result)
88+
self.assertEquals(len(channel.json_body["joined"]), 1)
89+
self.assertEquals(channel.json_body["joined"][0], room)
90+
91+
def test_shared_room_list_mixed(self):
92+
"""
93+
The shared room list between two users should contain both public and private
94+
rooms.
95+
"""
96+
u1 = self.register_user("user1", "pass")
97+
u1_token = self.login(u1, "pass")
98+
u2 = self.register_user("user2", "pass")
99+
u2_token = self.login(u2, "pass")
100+
101+
room_public = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
102+
room_private = self.helper.create_room_as(u2, is_public=False, tok=u2_token)
103+
self.helper.invite(room_public, src=u1, targ=u2, tok=u1_token)
104+
self.helper.invite(room_private, src=u2, targ=u1, tok=u2_token)
105+
self.helper.join(room_public, user=u2, tok=u2_token)
106+
self.helper.join(room_private, user=u1, tok=u1_token)
107+
108+
request, channel = self._get_shared_rooms(u1_token, u2)
109+
self.assertEquals(200, channel.code, channel.result)
110+
self.assertEquals(len(channel.json_body["joined"]), 2)
111+
self.assertTrue(room_public in channel.json_body["joined"])
112+
self.assertTrue(room_private in channel.json_body["joined"])
113+
114+
def test_shared_room_list_after_leave(self):
115+
"""
116+
A room should no longer be considered shared if the other
117+
user has left it.
118+
"""
119+
u1 = self.register_user("user1", "pass")
120+
u1_token = self.login(u1, "pass")
121+
u2 = self.register_user("user2", "pass")
122+
u2_token = self.login(u2, "pass")
123+
124+
room = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
125+
self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
126+
self.helper.join(room, user=u2, tok=u2_token)
127+
128+
# Assert user directory is not empty
129+
request, channel = self._get_shared_rooms(u1_token, u2)
130+
self.assertEquals(200, channel.code, channel.result)
131+
self.assertEquals(len(channel.json_body["joined"]), 1)
132+
self.assertEquals(channel.json_body["joined"][0], room)
133+
134+
self.helper.leave(room, user=u1, tok=u1_token)
135+
136+
request, channel = self._get_shared_rooms(u2_token, u1)
137+
self.assertEquals(200, channel.code, channel.result)
138+
self.assertEquals(len(channel.json_body["joined"]), 0)

0 commit comments

Comments
 (0)