2017-07-10 09:52:27 -04:00
|
|
|
# Copyright 2017 Vector Creations Ltd
|
2018-03-28 09:03:37 -04:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2017-07-10 09:52:27 -04:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2020-12-30 08:39:59 -05:00
|
|
|
from functools import wraps
|
2021-09-03 09:22:22 -04:00
|
|
|
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional, Tuple
|
2021-02-08 13:59:54 -05:00
|
|
|
|
2021-03-01 12:23:46 -05:00
|
|
|
from twisted.web.server import Request
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
from synapse.api.constants import (
|
|
|
|
MAX_GROUP_CATEGORYID_LENGTH,
|
|
|
|
MAX_GROUP_ROLEID_LENGTH,
|
|
|
|
MAX_GROUPID_LENGTH,
|
|
|
|
)
|
|
|
|
from synapse.api.errors import Codes, SynapseError
|
2021-02-08 13:59:54 -05:00
|
|
|
from synapse.handlers.groups_local import GroupsLocalHandler
|
2021-08-26 07:53:52 -04:00
|
|
|
from synapse.http.server import HttpServer
|
2021-02-08 13:59:54 -05:00
|
|
|
from synapse.http.servlet import (
|
|
|
|
RestServlet,
|
|
|
|
assert_params_in_dict,
|
|
|
|
parse_json_object_from_request,
|
|
|
|
)
|
2021-03-12 11:37:57 -05:00
|
|
|
from synapse.http.site import SynapseRequest
|
2021-02-08 13:59:54 -05:00
|
|
|
from synapse.types import GroupID, JsonDict
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
from ._base import client_patterns
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
if TYPE_CHECKING:
|
2021-03-23 07:12:48 -04:00
|
|
|
from synapse.server import HomeServer
|
2021-02-08 13:59:54 -05:00
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-09-03 09:22:22 -04:00
|
|
|
def _validate_group_id(
|
|
|
|
f: Callable[..., Awaitable[Tuple[int, JsonDict]]]
|
|
|
|
) -> Callable[..., Awaitable[Tuple[int, JsonDict]]]:
|
2020-12-30 08:39:59 -05:00
|
|
|
"""Wrapper to validate the form of the group ID.
|
|
|
|
|
|
|
|
Can be applied to any on_FOO methods that accepts a group ID as a URL parameter.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@wraps(f)
|
2021-09-03 09:22:22 -04:00
|
|
|
def wrapper(
|
|
|
|
self: RestServlet, request: Request, group_id: str, *args: Any, **kwargs: Any
|
|
|
|
) -> Awaitable[Tuple[int, JsonDict]]:
|
2020-12-30 08:39:59 -05:00
|
|
|
if not GroupID.is_valid(group_id):
|
|
|
|
raise SynapseError(400, "%s is not a legal group ID" % (group_id,))
|
|
|
|
|
|
|
|
return f(self, request, group_id, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
class GroupServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get the group profile"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/profile$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
group_description = await self.groups_handler.get_group_profile(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id
|
2017-10-26 12:20:24 -04:00
|
|
|
)
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, group_description
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-20 04:46:33 -04:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-08 13:59:54 -05:00
|
|
|
assert_params_in_dict(
|
|
|
|
content, ("name", "avatar_url", "short_description", "long_description")
|
|
|
|
)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot create group profiles."
|
2019-12-05 11:46:37 -05:00
|
|
|
await self.groups_handler.update_group_profile(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, content
|
2017-07-20 04:46:33 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, {}
|
2017-07-20 04:46:33 -04:00
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
class GroupSummaryServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get the full group summary"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/summary$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
get_group_summary = await self.groups_handler.get_group_summary(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id
|
2017-10-26 12:20:24 -04:00
|
|
|
)
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, get_group_summary
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupSummaryRoomsCatServlet(RestServlet):
|
2017-07-18 12:19:39 -04:00
|
|
|
"""Update/delete a rooms entry in the summary.
|
|
|
|
|
|
|
|
Matches both:
|
|
|
|
- /groups/:group/summary/rooms/:room_id
|
|
|
|
- /groups/:group/summary/categories/:category/rooms/:room_id
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2017-07-10 09:52:27 -04:00
|
|
|
"/groups/(?P<group_id>[^/]*)/summary"
|
2017-07-18 12:19:39 -04:00
|
|
|
"(/categories/(?P<category_id>[^/]+))?"
|
|
|
|
"/rooms/(?P<room_id>[^/]*)$"
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_PUT(
|
2021-03-12 11:37:57 -05:00
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
group_id: str,
|
|
|
|
category_id: Optional[str],
|
|
|
|
room_id: str,
|
2021-08-31 13:22:29 -04:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
if category_id == "":
|
|
|
|
raise SynapseError(400, "category_id cannot be empty", Codes.INVALID_PARAM)
|
|
|
|
|
|
|
|
if category_id and len(category_id) > MAX_GROUP_CATEGORYID_LENGTH:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"category_id may not be longer than %s characters"
|
|
|
|
% (MAX_GROUP_CATEGORYID_LENGTH,),
|
|
|
|
Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group summaries."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.update_group_summary_room(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id,
|
|
|
|
requester_user_id,
|
2017-07-10 09:52:27 -04:00
|
|
|
room_id=room_id,
|
|
|
|
category_id=category_id,
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_DELETE(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, category_id: str, room_id: str
|
2021-08-31 13:22:29 -04:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group profiles."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.delete_group_summary_room(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, room_id=room_id, category_id=category_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupCategoryServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get/add/update/delete a group category"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2017-07-10 09:52:27 -04:00
|
|
|
"/groups/(?P<group_id>[^/]*)/categories/(?P<category_id>[^/]+)$"
|
|
|
|
)
|
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_GET(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, category_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
category = await self.groups_handler.get_group_category(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, category_id=category_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, category
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_PUT(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, category_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
if not category_id:
|
|
|
|
raise SynapseError(400, "category_id cannot be empty", Codes.INVALID_PARAM)
|
|
|
|
|
|
|
|
if len(category_id) > MAX_GROUP_CATEGORYID_LENGTH:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"category_id may not be longer than %s characters"
|
|
|
|
% (MAX_GROUP_CATEGORYID_LENGTH,),
|
|
|
|
Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group categories."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.update_group_category(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, category_id=category_id, content=content
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_DELETE(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, category_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group categories."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.delete_group_category(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, category_id=category_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupCategoriesServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get all group categories"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/categories/$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
category = await self.groups_handler.get_group_categories(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, category
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupRoleServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get/add/update/delete a group role"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/roles/(?P<role_id>[^/]+)$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_GET(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, role_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
category = await self.groups_handler.get_group_role(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, role_id=role_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, category
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_PUT(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, role_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
if not role_id:
|
|
|
|
raise SynapseError(400, "role_id cannot be empty", Codes.INVALID_PARAM)
|
|
|
|
|
|
|
|
if len(role_id) > MAX_GROUP_ROLEID_LENGTH:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"role_id may not be longer than %s characters"
|
|
|
|
% (MAX_GROUP_ROLEID_LENGTH,),
|
|
|
|
Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group roles."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.update_group_role(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, role_id=role_id, content=content
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_DELETE(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, role_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group roles."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.delete_group_role(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, role_id=role_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupRolesServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get all group roles"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/roles/$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
category = await self.groups_handler.get_group_roles(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, category
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupSummaryUsersRoleServlet(RestServlet):
|
2017-07-18 12:19:39 -04:00
|
|
|
"""Update/delete a user's entry in the summary.
|
|
|
|
|
|
|
|
Matches both:
|
|
|
|
- /groups/:group/summary/users/:room_id
|
|
|
|
- /groups/:group/summary/roles/:role/users/:user_id
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2017-07-10 09:52:27 -04:00
|
|
|
"/groups/(?P<group_id>[^/]*)/summary"
|
2017-07-18 12:19:39 -04:00
|
|
|
"(/roles/(?P<role_id>[^/]+))?"
|
|
|
|
"/users/(?P<user_id>[^/]*)$"
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_PUT(
|
2021-03-12 11:37:57 -05:00
|
|
|
self,
|
|
|
|
request: SynapseRequest,
|
|
|
|
group_id: str,
|
|
|
|
role_id: Optional[str],
|
|
|
|
user_id: str,
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-07-10 09:52:27 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
if role_id == "":
|
|
|
|
raise SynapseError(400, "role_id cannot be empty", Codes.INVALID_PARAM)
|
|
|
|
|
|
|
|
if role_id and len(role_id) > MAX_GROUP_ROLEID_LENGTH:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"role_id may not be longer than %s characters"
|
|
|
|
% (MAX_GROUP_ROLEID_LENGTH,),
|
|
|
|
Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group summaries."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.update_group_summary_user(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id,
|
|
|
|
requester_user_id,
|
2017-07-10 09:52:27 -04:00
|
|
|
user_id=user_id,
|
|
|
|
role_id=role_id,
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_DELETE(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, role_id: str, user_id: str
|
2021-08-31 13:22:29 -04:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-07-10 09:52:27 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group summaries."
|
2019-12-05 11:46:37 -05:00
|
|
|
resp = await self.groups_handler.delete_group_summary_user(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, user_id=user_id, role_id=role_id
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, resp
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupRoomServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get all rooms in a group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/rooms$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.get_rooms_in_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id
|
|
|
|
)
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupUsersServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get all users in a group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/users$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.get_users_in_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id
|
|
|
|
)
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2017-10-16 10:52:12 -04:00
|
|
|
|
2017-10-16 10:31:11 -04:00
|
|
|
class GroupInvitedUsersServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get users invited to a group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/invited_users$")
|
2017-10-16 10:31:11 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-10-16 10:31:11 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-10-16 10:31:11 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.get_invited_users_in_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id
|
2017-10-26 12:20:24 -04:00
|
|
|
)
|
2017-10-16 10:31:11 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-10-16 10:31:11 -04:00
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2018-04-03 10:40:43 -04:00
|
|
|
class GroupSettingJoinPolicyServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Set group join policy"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/settings/m.join_policy$")
|
2018-03-28 09:03:37 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2018-03-28 09:03:37 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_PUT(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2018-03-28 09:03:37 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group join policy."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.set_group_join_policy(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, content
|
2018-03-28 09:03:37 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2018-03-28 09:03:37 -04:00
|
|
|
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
class GroupCreateServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Create a group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns("/create_group$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
# TODO: Create group on remote server
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
localpart = content.pop("localpart")
|
2017-10-20 11:33:15 -04:00
|
|
|
group_id = GroupID(localpart, self.server_name).to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
if not localpart:
|
|
|
|
raise SynapseError(400, "Group ID cannot be empty", Codes.INVALID_PARAM)
|
|
|
|
|
|
|
|
if len(group_id) > MAX_GROUPID_LENGTH:
|
|
|
|
raise SynapseError(
|
|
|
|
400,
|
|
|
|
"Group ID may not be longer than %s characters" % (MAX_GROUPID_LENGTH,),
|
|
|
|
Codes.INVALID_PARAM,
|
|
|
|
)
|
|
|
|
|
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot create groups."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.create_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, content
|
2017-10-26 12:20:24 -04:00
|
|
|
)
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupAdminRoomsServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Add a room to the group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2017-07-10 09:52:27 -04:00
|
|
|
"/groups/(?P<group_id>[^/]*)/admin/rooms/(?P<room_id>[^/]*)$"
|
|
|
|
)
|
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_PUT(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, room_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify rooms in a group."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.add_room_to_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, room_id, content
|
2017-07-18 12:28:42 -04:00
|
|
|
)
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_DELETE(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, room_id: str
|
2021-02-08 13:59:54 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-09-26 10:52:41 -04:00
|
|
|
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group categories."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.remove_room_from_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, room_id
|
2017-09-26 10:52:41 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-09-26 10:52:41 -04:00
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2017-11-08 11:13:27 -05:00
|
|
|
class GroupAdminRoomsConfigServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Update the config of a room in a group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2017-11-08 11:13:27 -05:00
|
|
|
"/groups/(?P<group_id>[^/]*)/admin/rooms/(?P<room_id>[^/]*)"
|
|
|
|
"/config/(?P<config_key>[^/]*)$"
|
|
|
|
)
|
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-11-08 11:13:27 -05:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-02-08 13:59:54 -05:00
|
|
|
async def on_PUT(
|
2021-03-12 11:37:57 -05:00
|
|
|
self, request: SynapseRequest, group_id: str, room_id: str, config_key: str
|
2021-08-31 13:22:29 -04:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-11-08 11:13:27 -05:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot modify group categories."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.update_room_in_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, room_id, config_key, content
|
2017-11-08 11:13:27 -05:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-11-08 11:13:27 -05:00
|
|
|
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
class GroupAdminUsersInviteServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Invite a user to the group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2017-07-10 09:52:27 -04:00
|
|
|
"/groups/(?P<group_id>[^/]*)/admin/users/invite/(?P<user_id>[^/]*)$"
|
|
|
|
)
|
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2017-07-10 09:52:27 -04:00
|
|
|
self.is_mine_id = hs.is_mine_id
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_PUT(
|
2021-08-31 13:22:29 -04:00
|
|
|
self, request: SynapseRequest, group_id: str, user_id: str
|
2021-03-12 11:37:57 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-07-10 09:52:27 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
config = content.get("config", {})
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot invite users to a group."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.invite(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, user_id, requester_user_id, config
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupAdminUsersKickServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Kick a user from the group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(
|
2017-07-10 09:52:27 -04:00
|
|
|
"/groups/(?P<group_id>[^/]*)/admin/users/remove/(?P<user_id>[^/]*)$"
|
|
|
|
)
|
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_PUT(
|
2021-08-31 13:22:29 -04:00
|
|
|
self, request: SynapseRequest, group_id: str, user_id: str
|
2021-03-12 11:37:57 -05:00
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-07-10 09:52:27 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot kick users from a group."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.remove_user_from_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, user_id, requester_user_id, content
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupSelfLeaveServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Leave a joined group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/self/leave$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_PUT(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-07-10 09:52:27 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot leave a group for a users."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.remove_user_from_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, requester_user_id, content
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupSelfJoinServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Attempt to join a group, or knock"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/self/join$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_PUT(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-07-10 09:52:27 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot join a user to a group."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.join_group(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, content
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
class GroupSelfAcceptInviteServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Accept a group invite"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/self/accept_invite$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_PUT(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-07-10 09:52:27 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
2021-02-17 08:41:47 -05:00
|
|
|
assert isinstance(
|
|
|
|
self.groups_handler, GroupsLocalHandler
|
|
|
|
), "Workers cannot accept an invite to a group."
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.accept_invite(
|
2019-06-20 05:32:02 -04:00
|
|
|
group_id, requester_user_id, content
|
2017-07-10 09:52:27 -04:00
|
|
|
)
|
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
2017-08-08 09:19:07 -04:00
|
|
|
class GroupSelfUpdatePublicityServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Update whether we publicise a users membership of a group"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/groups/(?P<group_id>[^/]*)/self/update_publicity$")
|
2017-08-08 09:19:07 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-08-08 09:19:07 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2017-08-08 09:19:07 -04:00
|
|
|
|
2020-12-30 08:39:59 -05:00
|
|
|
@_validate_group_id
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_PUT(
|
|
|
|
self, request: SynapseRequest, group_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request)
|
2017-08-08 09:19:07 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
publicise = content["publicise"]
|
2019-12-05 11:46:37 -05:00
|
|
|
await self.store.update_group_publicity(group_id, requester_user_id, publicise)
|
2017-08-08 09:19:07 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, {}
|
2017-08-08 09:19:07 -04:00
|
|
|
|
|
|
|
|
2017-08-09 08:36:22 -04:00
|
|
|
class PublicisedGroupsForUserServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get the list of groups a user is advertising"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/publicised_groups/(?P<user_id>[^/]*)$")
|
2017-08-09 08:36:22 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-08-09 08:36:22 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2017-08-09 08:36:22 -04:00
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(
|
|
|
|
self, request: SynapseRequest, user_id: str
|
|
|
|
) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-08-09 08:36:22 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.get_publicised_groups_for_user(user_id)
|
2017-08-09 08:36:22 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-08-09 08:36:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PublicisedGroupsForUsersServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get the list of groups a user is advertising"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/publicised_groups$")
|
2017-08-09 08:36:22 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-08-09 08:36:22 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
2022-02-23 06:04:02 -05:00
|
|
|
self.store = hs.get_datastores().main
|
2017-08-09 08:36:22 -04:00
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-08-09 08:36:22 -04:00
|
|
|
|
|
|
|
content = parse_json_object_from_request(request)
|
|
|
|
user_ids = content["user_ids"]
|
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.bulk_get_publicised_groups(user_ids)
|
2017-08-09 08:36:22 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-08-09 08:36:22 -04:00
|
|
|
|
|
|
|
|
2017-07-10 09:52:27 -04:00
|
|
|
class GroupsForUserServlet(RestServlet):
|
2021-02-16 17:32:34 -05:00
|
|
|
"""Get all groups the logged in user is joined to"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
|
|
|
PATTERNS = client_patterns("/joined_groups$")
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2021-02-08 13:59:54 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2017-07-10 09:52:27 -04:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.groups_handler = hs.get_groups_local_handler()
|
|
|
|
|
2021-03-12 11:37:57 -05:00
|
|
|
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
|
2019-12-05 11:46:37 -05:00
|
|
|
requester = await self.auth.get_user_by_req(request, allow_guest=True)
|
2017-10-26 11:51:32 -04:00
|
|
|
requester_user_id = requester.user.to_string()
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-12-05 11:46:37 -05:00
|
|
|
result = await self.groups_handler.get_joined_groups(requester_user_id)
|
2017-07-10 09:52:27 -04:00
|
|
|
|
2019-08-30 11:28:26 -04:00
|
|
|
return 200, result
|
2017-07-10 09:52:27 -04:00
|
|
|
|
|
|
|
|
2021-08-26 07:53:52 -04:00
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
2017-07-10 09:52:27 -04:00
|
|
|
GroupServlet(hs).register(http_server)
|
|
|
|
GroupSummaryServlet(hs).register(http_server)
|
2017-10-16 10:31:11 -04:00
|
|
|
GroupInvitedUsersServlet(hs).register(http_server)
|
2017-07-10 09:52:27 -04:00
|
|
|
GroupUsersServlet(hs).register(http_server)
|
|
|
|
GroupRoomServlet(hs).register(http_server)
|
2018-04-03 10:40:43 -04:00
|
|
|
GroupSettingJoinPolicyServlet(hs).register(http_server)
|
2017-07-10 09:52:27 -04:00
|
|
|
GroupCreateServlet(hs).register(http_server)
|
|
|
|
GroupAdminRoomsServlet(hs).register(http_server)
|
2017-11-09 10:49:42 -05:00
|
|
|
GroupAdminRoomsConfigServlet(hs).register(http_server)
|
2017-07-10 09:52:27 -04:00
|
|
|
GroupAdminUsersInviteServlet(hs).register(http_server)
|
|
|
|
GroupAdminUsersKickServlet(hs).register(http_server)
|
|
|
|
GroupSelfLeaveServlet(hs).register(http_server)
|
|
|
|
GroupSelfJoinServlet(hs).register(http_server)
|
|
|
|
GroupSelfAcceptInviteServlet(hs).register(http_server)
|
|
|
|
GroupsForUserServlet(hs).register(http_server)
|
|
|
|
GroupCategoryServlet(hs).register(http_server)
|
|
|
|
GroupCategoriesServlet(hs).register(http_server)
|
|
|
|
GroupSummaryRoomsCatServlet(hs).register(http_server)
|
|
|
|
GroupRoleServlet(hs).register(http_server)
|
|
|
|
GroupRolesServlet(hs).register(http_server)
|
2017-08-08 09:19:07 -04:00
|
|
|
GroupSelfUpdatePublicityServlet(hs).register(http_server)
|
2017-07-10 09:52:27 -04:00
|
|
|
GroupSummaryUsersRoleServlet(hs).register(http_server)
|
2017-08-09 08:36:22 -04:00
|
|
|
PublicisedGroupsForUserServlet(hs).register(http_server)
|
2017-08-11 08:42:42 -04:00
|
|
|
PublicisedGroupsForUsersServlet(hs).register(http_server)
|