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.
This commit is contained in:
parent
5dc1f25c53
commit
e5b4d93770
|
@ -0,0 +1 @@
|
||||||
|
Update Mutual Rooms (MSC2666) implementation to match new proposal text.
|
|
@ -12,13 +12,14 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Tuple
|
from http import HTTPStatus
|
||||||
|
from typing import TYPE_CHECKING, Dict, List, Tuple
|
||||||
|
|
||||||
from synapse.api.errors import Codes, SynapseError
|
from synapse.api.errors import Codes, SynapseError
|
||||||
from synapse.http.server import HttpServer
|
from synapse.http.server import HttpServer
|
||||||
from synapse.http.servlet import RestServlet
|
from synapse.http.servlet import RestServlet, parse_strings_from_args
|
||||||
from synapse.http.site import SynapseRequest
|
from synapse.http.site import SynapseRequest
|
||||||
from synapse.types import JsonDict, UserID
|
from synapse.types import JsonDict
|
||||||
|
|
||||||
from ._base import client_patterns
|
from ._base import client_patterns
|
||||||
|
|
||||||
|
@ -30,11 +31,11 @@ logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class UserMutualRoomsServlet(RestServlet):
|
class UserMutualRoomsServlet(RestServlet):
|
||||||
"""
|
"""
|
||||||
GET /uk.half-shot.msc2666/user/mutual_rooms/{user_id} HTTP/1.1
|
GET /uk.half-shot.msc2666/user/mutual_rooms?user_id={user_id} HTTP/1.1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
PATTERNS = client_patterns(
|
PATTERNS = client_patterns(
|
||||||
"/uk.half-shot.msc2666/user/mutual_rooms/(?P<user_id>[^/]*)",
|
"/uk.half-shot.msc2666/user/mutual_rooms$",
|
||||||
releases=(), # This is an unstable feature
|
releases=(), # This is an unstable feature
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,17 +44,35 @@ class UserMutualRoomsServlet(RestServlet):
|
||||||
self.auth = hs.get_auth()
|
self.auth = hs.get_auth()
|
||||||
self.store = hs.get_datastores().main
|
self.store = hs.get_datastores().main
|
||||||
|
|
||||||
async def on_GET(
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
||||||
self, request: SynapseRequest, user_id: str
|
# twisted.web.server.Request.args is incorrectly defined as Optional[Any]
|
||||||
) -> Tuple[int, JsonDict]:
|
args: Dict[bytes, List[bytes]] = request.args # type: ignore
|
||||||
UserID.from_string(user_id)
|
|
||||||
|
user_ids = parse_strings_from_args(args, "user_id", required=True)
|
||||||
|
|
||||||
|
if len(user_ids) > 1:
|
||||||
|
raise SynapseError(
|
||||||
|
HTTPStatus.BAD_REQUEST,
|
||||||
|
"Duplicate user_id query parameter",
|
||||||
|
errcode=Codes.INVALID_PARAM,
|
||||||
|
)
|
||||||
|
|
||||||
|
# We don't do batching, so a batch token is illegal by default
|
||||||
|
if b"batch_token" in args:
|
||||||
|
raise SynapseError(
|
||||||
|
HTTPStatus.BAD_REQUEST,
|
||||||
|
"Unknown batch_token",
|
||||||
|
errcode=Codes.INVALID_PARAM,
|
||||||
|
)
|
||||||
|
|
||||||
|
user_id = user_ids[0]
|
||||||
|
|
||||||
requester = await self.auth.get_user_by_req(request)
|
requester = await self.auth.get_user_by_req(request)
|
||||||
if user_id == requester.user.to_string():
|
if user_id == requester.user.to_string():
|
||||||
raise SynapseError(
|
raise SynapseError(
|
||||||
code=400,
|
HTTPStatus.UNPROCESSABLE_ENTITY,
|
||||||
msg="You cannot request a list of shared rooms with yourself",
|
"You cannot request a list of shared rooms with yourself",
|
||||||
errcode=Codes.FORBIDDEN,
|
errcode=Codes.INVALID_PARAM,
|
||||||
)
|
)
|
||||||
|
|
||||||
rooms = await self.store.get_mutual_rooms_between_users(
|
rooms = await self.store.get_mutual_rooms_between_users(
|
||||||
|
|
|
@ -91,7 +91,7 @@ class VersionsRestServlet(RestServlet):
|
||||||
# Implements additional endpoints as described in MSC2432
|
# Implements additional endpoints as described in MSC2432
|
||||||
"org.matrix.msc2432": True,
|
"org.matrix.msc2432": True,
|
||||||
# Implements additional endpoints as described in MSC2666
|
# Implements additional endpoints as described in MSC2666
|
||||||
"uk.half-shot.msc2666.mutual_rooms": True,
|
"uk.half-shot.msc2666.query_mutual_rooms": True,
|
||||||
# Whether new rooms will be set to encrypted or not (based on presets).
|
# Whether new rooms will be set to encrypted or not (based on presets).
|
||||||
"io.element.e2ee_forced.public": self.e2ee_forced_public,
|
"io.element.e2ee_forced.public": self.e2ee_forced_public,
|
||||||
"io.element.e2ee_forced.private": self.e2ee_forced_private,
|
"io.element.e2ee_forced.private": self.e2ee_forced_private,
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
from urllib.parse import quote
|
||||||
|
|
||||||
from twisted.test.proto_helpers import MemoryReactor
|
from twisted.test.proto_helpers import MemoryReactor
|
||||||
|
|
||||||
import synapse.rest.admin
|
import synapse.rest.admin
|
||||||
|
@ -44,8 +46,8 @@ class UserMutualRoomsTest(unittest.HomeserverTestCase):
|
||||||
def _get_mutual_rooms(self, token: str, other_user: str) -> FakeChannel:
|
def _get_mutual_rooms(self, token: str, other_user: str) -> FakeChannel:
|
||||||
return self.make_request(
|
return self.make_request(
|
||||||
"GET",
|
"GET",
|
||||||
"/_matrix/client/unstable/uk.half-shot.msc2666/user/mutual_rooms/%s"
|
"/_matrix/client/unstable/uk.half-shot.msc2666/user/mutual_rooms"
|
||||||
% other_user,
|
f"?user_id={quote(other_user)}",
|
||||||
access_token=token,
|
access_token=token,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue