-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add /user/{user_id}/shared_rooms/ api #7785
Changes from 25 commits
26634cb
0f6800f
984c52b
f43bf0c
9c1e934
208d3cf
90a5975
3fa92f5
6bd34c7
09e09a3
c143e62
676edea
c399292
dde46cf
256e017
9cd5116
72e90a2
ebfe6c4
a281322
ce49d19
42a09b9
cc4fb16
7905978
130d2f3
c120fd3
e135cd5
e785cc6
8a4c5ab
49f14c0
e86f406
7554836
1fa8b84
783f8f5
a444ab0
837cf31
023e21f
679810a
0578bb9
5acf573
078b3e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +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). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright 2020 Half-Shot | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import logging | ||
|
|
||
| from synapse.api.errors import Codes, SynapseError | ||
| from synapse.http.servlet import RestServlet | ||
|
|
||
| from ._base import client_patterns | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class UserSharedRoomsServlet(RestServlet): | ||
| """ | ||
| GET /uk.half-shot.msc2666/user/shared_rooms/{user_id} HTTP/1.1 | ||
| """ | ||
|
|
||
| PATTERNS = client_patterns( | ||
| "/uk.half-shot.msc2666/user/shared_rooms/(?P<user_id>[^/]*)", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, just noticed this and your MSC don't match up. You'll want to put You can do so by adding
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't need to.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! My bad. Just a couple doc updates then and we're good to go! |
||
| releases=(), # This is an unstable feature | ||
| ) | ||
|
|
||
| def __init__(self, hs): | ||
| super(UserSharedRoomsServlet, self).__init__() | ||
| self.auth = hs.get_auth() | ||
| self.store = hs.get_datastore() | ||
|
|
||
| async def on_GET(self, request, user_id): | ||
| requester = await self.auth.get_user_by_req(request) | ||
| if user_id == requester.user.to_string(): | ||
| raise SynapseError( | ||
| code=400, | ||
| msg="'user_id' must not be the authenticated user", | ||
Half-Shot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| errcode=Codes.FORBIDDEN, | ||
| ) | ||
Half-Shot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| rooms = await self.store.get_shared_rooms_for_users( | ||
| requester.user.to_string(), user_id | ||
| ) | ||
|
|
||
| return 200, {"joined": rooms} | ||
|
|
||
|
|
||
| def register_servlets(hs, http_server): | ||
| UserSharedRoomsServlet(hs).register(http_server) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -663,6 +663,42 @@ async def get_user_dir_rooms_user_is_in(self, user_id): | |
| users.update(rows) | ||
| return list(users) | ||
|
|
||
| @cached() | ||
| async def get_shared_rooms_for_users(self, user_id, other_user_id): | ||
Half-Shot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Returns the rooms that a user is in. | ||
Half-Shot marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Args: | ||
| user_id(str): Must be a local user | ||
Half-Shot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
Half-Shot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Returns: | ||
| list: user_id | ||
Half-Shot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| SQL = """ | ||
| SELECT p1.room_id | ||
| FROM users_in_public_rooms as p1 | ||
|
||
| INNER JOIN users_in_public_rooms as p2 | ||
| ON p1.room_id = p2.room_id | ||
| AND p1.user_id = ? | ||
| AND p2.user_id = ? | ||
| UNION | ||
| SELECT room_id | ||
| FROM users_who_share_private_rooms | ||
| WHERE | ||
| user_id = ? | ||
| AND other_user_id = ?; | ||
| """ | ||
| rows = await self.db_pool.execute( | ||
Half-Shot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "get_shared_rooms_for_users", | ||
| None, | ||
| SQL, | ||
| user_id, | ||
| other_user_id, | ||
| user_id, | ||
| other_user_id, | ||
| ) | ||
| return list({row[0] for row in rows}) | ||
Half-Shot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def get_user_directory_stream_pos(self): | ||
| return self.db_pool.simple_select_one_onecol( | ||
| table="user_directory_stream_pos", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.