2017-07-10 07:52:27 -06:00
|
|
|
# Copyright 2017 Vector Creations Ltd
|
2018-03-28 07:03:37 -06:00
|
|
|
# Copyright 2018 New Vector Ltd
|
2017-07-10 07:52:27 -06: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 00:09:20 -06:00
|
|
|
import logging
|
2021-01-26 08:50:21 -07:00
|
|
|
from typing import TYPE_CHECKING, Dict, Iterable, List, Set
|
2018-07-09 00:09:20 -06:00
|
|
|
|
2019-02-14 06:58:52 -07:00
|
|
|
from synapse.api.errors import HttpResponseException, RequestSendFailed, SynapseError
|
2021-01-26 08:50:21 -07:00
|
|
|
from synapse.types import GroupID, JsonDict, get_domain_from_id
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
2021-03-23 05:12:48 -06:00
|
|
|
from synapse.server import HomeServer
|
2017-07-10 07:52:27 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-07-18 03:29:57 -06:00
|
|
|
def _create_rerouter(func_name):
|
2020-07-30 06:01:33 -06:00
|
|
|
"""Returns an async function that looks at the group id and calls the function
|
2017-07-18 03:29:57 -06:00
|
|
|
on federation or the local group server if the group is local
|
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2020-07-30 06:01:33 -06:00
|
|
|
async def f(self, group_id, *args, **kwargs):
|
2020-10-22 06:19:06 -06:00
|
|
|
if not GroupID.is_valid(group_id):
|
2020-12-30 06:39:59 -07:00
|
|
|
raise SynapseError(400, "%s is not a legal group ID" % (group_id,))
|
2020-10-22 06:19:06 -06:00
|
|
|
|
2017-07-10 07:52:27 -06:00
|
|
|
if self.is_mine_id(group_id):
|
2020-07-30 06:01:33 -06:00
|
|
|
return await getattr(self.groups_server_handler, func_name)(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id, *args, **kwargs
|
|
|
|
)
|
2017-07-18 07:45:37 -06:00
|
|
|
else:
|
|
|
|
destination = get_domain_from_id(group_id)
|
2018-10-12 14:53:30 -06:00
|
|
|
|
2020-07-30 06:01:33 -06:00
|
|
|
try:
|
|
|
|
return await getattr(self.transport_client, func_name)(
|
|
|
|
destination, group_id, *args, **kwargs
|
|
|
|
)
|
|
|
|
except HttpResponseException as e:
|
|
|
|
# Capture errors returned by the remote homeserver and
|
|
|
|
# re-throw specific errors as SynapseErrors. This is so
|
|
|
|
# when the remote end responds with things like 403 Not
|
|
|
|
# In Group, we can communicate that to the client instead
|
|
|
|
# of a 500.
|
2019-06-07 03:29:35 -06:00
|
|
|
raise e.to_synapse_error()
|
2020-07-30 06:01:33 -06:00
|
|
|
except RequestSendFailed:
|
2019-02-14 06:58:52 -07:00
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
|
|
|
|
2017-07-10 07:52:27 -06:00
|
|
|
return f
|
|
|
|
|
|
|
|
|
2020-09-04 04:54:56 -06:00
|
|
|
class GroupsLocalWorkerHandler:
|
2021-01-26 08:50:21 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2017-07-10 07:52:27 -06:00
|
|
|
self.hs = hs
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.room_list_handler = hs.get_room_list_handler()
|
|
|
|
self.groups_server_handler = hs.get_groups_server_handler()
|
2017-07-18 03:29:57 -06:00
|
|
|
self.transport_client = hs.get_federation_transport_client()
|
2017-07-10 07:52:27 -06:00
|
|
|
self.auth = hs.get_auth()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.keyring = hs.get_keyring()
|
|
|
|
self.is_mine_id = hs.is_mine_id
|
2020-07-08 10:51:56 -06:00
|
|
|
self.signing_key = hs.signing_key
|
2017-07-10 07:52:27 -06:00
|
|
|
self.server_name = hs.hostname
|
2017-07-20 10:14:44 -06:00
|
|
|
self.notifier = hs.get_notifier()
|
2017-07-10 07:52:27 -06:00
|
|
|
self.attestations = hs.get_groups_attestation_signing()
|
|
|
|
|
2017-08-25 07:34:56 -06:00
|
|
|
self.profile_handler = hs.get_profile_handler()
|
2017-08-25 04:21:34 -06:00
|
|
|
|
2017-07-18 03:31:59 -06:00
|
|
|
# The following functions merely route the query to the local groups server
|
|
|
|
# or federation depending on if the group is local or remote
|
|
|
|
|
2017-07-10 07:52:27 -06:00
|
|
|
get_group_profile = _create_rerouter("get_group_profile")
|
|
|
|
get_rooms_in_group = _create_rerouter("get_rooms_in_group")
|
2017-10-16 08:41:03 -06:00
|
|
|
get_invited_users_in_group = _create_rerouter("get_invited_users_in_group")
|
2017-07-10 07:52:27 -06:00
|
|
|
get_group_category = _create_rerouter("get_group_category")
|
|
|
|
get_group_categories = _create_rerouter("get_group_categories")
|
|
|
|
get_group_role = _create_rerouter("get_group_role")
|
|
|
|
get_group_roles = _create_rerouter("get_group_roles")
|
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def get_group_summary(
|
|
|
|
self, group_id: str, requester_user_id: str
|
|
|
|
) -> JsonDict:
|
2017-07-18 03:29:57 -06:00
|
|
|
"""Get the group summary for a group.
|
|
|
|
|
|
|
|
If the group is remote we check that the users have valid attestations.
|
|
|
|
"""
|
2017-07-10 07:52:27 -06:00
|
|
|
if self.is_mine_id(group_id):
|
2020-06-01 05:28:43 -06:00
|
|
|
res = await self.groups_server_handler.get_group_summary(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id, requester_user_id
|
|
|
|
)
|
2017-09-26 04:04:37 -06:00
|
|
|
else:
|
2019-07-29 10:21:57 -06:00
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
res = await self.transport_client.get_group_summary(
|
2019-07-29 10:21:57 -06:00
|
|
|
get_domain_from_id(group_id), group_id, requester_user_id
|
|
|
|
)
|
2020-01-07 08:36:41 -07:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2019-07-29 10:21:57 -06:00
|
|
|
except RequestSendFailed:
|
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
2017-09-26 04:04:37 -06:00
|
|
|
|
2017-10-11 07:11:43 -06:00
|
|
|
group_server_name = get_domain_from_id(group_id)
|
|
|
|
|
2017-09-26 04:04:37 -06:00
|
|
|
# Loop through the users and validate the attestations.
|
|
|
|
chunk = res["users_section"]["users"]
|
|
|
|
valid_users = []
|
|
|
|
for entry in chunk:
|
|
|
|
g_user_id = entry["user_id"]
|
2017-10-11 09:54:36 -06:00
|
|
|
attestation = entry.pop("attestation", {})
|
2017-09-26 04:04:37 -06:00
|
|
|
try:
|
2017-10-11 07:11:43 -06:00
|
|
|
if get_domain_from_id(g_user_id) != group_server_name:
|
2020-06-01 05:28:43 -06:00
|
|
|
await self.attestations.verify_attestation(
|
2017-10-11 07:11:43 -06:00
|
|
|
attestation,
|
|
|
|
group_id=group_id,
|
|
|
|
user_id=g_user_id,
|
|
|
|
server_name=get_domain_from_id(g_user_id),
|
|
|
|
)
|
2017-09-26 04:04:37 -06:00
|
|
|
valid_users.append(entry)
|
|
|
|
except Exception as e:
|
|
|
|
logger.info("Failed to verify user is in group: %s", e)
|
|
|
|
|
|
|
|
res["users_section"]["users"] = valid_users
|
|
|
|
|
|
|
|
res["users_section"]["users"].sort(key=lambda e: e.get("order", 0))
|
|
|
|
res["rooms_section"]["rooms"].sort(key=lambda e: e.get("order", 0))
|
|
|
|
|
|
|
|
# Add `is_publicised` flag to indicate whether the user has publicised their
|
|
|
|
# membership of the group on their profile
|
2020-06-01 05:28:43 -06:00
|
|
|
result = await self.store.get_publicised_groups_for_user(requester_user_id)
|
2017-09-26 04:04:37 -06:00
|
|
|
is_publicised = group_id in result
|
|
|
|
|
|
|
|
res.setdefault("user", {})["is_publicised"] = is_publicised
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return res
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def get_users_in_group(
|
|
|
|
self, group_id: str, requester_user_id: str
|
|
|
|
) -> JsonDict:
|
2020-02-07 04:14:19 -07:00
|
|
|
"""Get users in a group"""
|
|
|
|
if self.is_mine_id(group_id):
|
2021-01-26 08:50:21 -07:00
|
|
|
return await self.groups_server_handler.get_users_in_group(
|
2020-02-07 04:14:19 -07:00
|
|
|
group_id, requester_user_id
|
|
|
|
)
|
|
|
|
|
|
|
|
group_server_name = get_domain_from_id(group_id)
|
|
|
|
|
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
res = await self.transport_client.get_users_in_group(
|
2020-02-07 04:14:19 -07:00
|
|
|
get_domain_from_id(group_id), group_id, requester_user_id
|
|
|
|
)
|
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
|
|
|
except RequestSendFailed:
|
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
|
|
|
|
|
|
|
chunk = res["chunk"]
|
|
|
|
valid_entries = []
|
|
|
|
for entry in chunk:
|
|
|
|
g_user_id = entry["user_id"]
|
|
|
|
attestation = entry.pop("attestation", {})
|
|
|
|
try:
|
|
|
|
if get_domain_from_id(g_user_id) != group_server_name:
|
2020-06-01 05:28:43 -06:00
|
|
|
await self.attestations.verify_attestation(
|
2020-02-07 04:14:19 -07:00
|
|
|
attestation,
|
|
|
|
group_id=group_id,
|
|
|
|
user_id=g_user_id,
|
|
|
|
server_name=get_domain_from_id(g_user_id),
|
|
|
|
)
|
|
|
|
valid_entries.append(entry)
|
|
|
|
except Exception as e:
|
|
|
|
logger.info("Failed to verify user is in group: %s", e)
|
|
|
|
|
|
|
|
res["chunk"] = valid_entries
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def get_joined_groups(self, user_id: str) -> JsonDict:
|
2020-06-01 05:28:43 -06:00
|
|
|
group_ids = await self.store.get_joined_groups(user_id)
|
2020-02-07 04:14:19 -07:00
|
|
|
return {"groups": group_ids}
|
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def get_publicised_groups_for_user(self, user_id: str) -> JsonDict:
|
2020-02-07 04:14:19 -07:00
|
|
|
if self.hs.is_mine_id(user_id):
|
2020-06-01 05:28:43 -06:00
|
|
|
result = await self.store.get_publicised_groups_for_user(user_id)
|
2020-02-07 04:14:19 -07:00
|
|
|
|
|
|
|
# Check AS associated groups for this user - this depends on the
|
|
|
|
# RegExps in the AS registration file (under `users`)
|
|
|
|
for app_service in self.store.get_app_services():
|
|
|
|
result.extend(app_service.get_groups_for_user(user_id))
|
|
|
|
|
|
|
|
return {"groups": result}
|
|
|
|
else:
|
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
bulk_result = await self.transport_client.bulk_get_publicised_groups(
|
2020-02-07 04:14:19 -07:00
|
|
|
get_domain_from_id(user_id), [user_id]
|
|
|
|
)
|
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
|
|
|
except RequestSendFailed:
|
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
|
|
|
|
|
|
|
result = bulk_result.get("users", {}).get(user_id)
|
|
|
|
# TODO: Verify attestations
|
|
|
|
return {"groups": result}
|
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def bulk_get_publicised_groups(
|
|
|
|
self, user_ids: Iterable[str], proxy: bool = True
|
|
|
|
) -> JsonDict:
|
|
|
|
destinations = {} # type: Dict[str, Set[str]]
|
2020-02-07 04:14:19 -07:00
|
|
|
local_users = set()
|
|
|
|
|
|
|
|
for user_id in user_ids:
|
|
|
|
if self.hs.is_mine_id(user_id):
|
|
|
|
local_users.add(user_id)
|
|
|
|
else:
|
|
|
|
destinations.setdefault(get_domain_from_id(user_id), set()).add(user_id)
|
|
|
|
|
|
|
|
if not proxy and destinations:
|
|
|
|
raise SynapseError(400, "Some user_ids are not local")
|
|
|
|
|
|
|
|
results = {}
|
2021-01-26 08:50:21 -07:00
|
|
|
failed_results = [] # type: List[str]
|
2020-06-15 05:03:36 -06:00
|
|
|
for destination, dest_user_ids in destinations.items():
|
2020-02-07 04:14:19 -07:00
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
r = await self.transport_client.bulk_get_publicised_groups(
|
2020-02-07 04:14:19 -07:00
|
|
|
destination, list(dest_user_ids)
|
|
|
|
)
|
|
|
|
results.update(r["users"])
|
|
|
|
except Exception:
|
|
|
|
failed_results.extend(dest_user_ids)
|
|
|
|
|
|
|
|
for uid in local_users:
|
2020-06-01 05:28:43 -06:00
|
|
|
results[uid] = await self.store.get_publicised_groups_for_user(uid)
|
2020-02-07 04:14:19 -07:00
|
|
|
|
|
|
|
# Check AS associated groups for this user - this depends on the
|
|
|
|
# RegExps in the AS registration file (under `users`)
|
|
|
|
for app_service in self.store.get_app_services():
|
|
|
|
results[uid].extend(app_service.get_groups_for_user(uid))
|
|
|
|
|
|
|
|
return {"users": results}
|
|
|
|
|
|
|
|
|
|
|
|
class GroupsLocalHandler(GroupsLocalWorkerHandler):
|
2021-01-26 08:50:21 -07:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 07:56:44 -06:00
|
|
|
super().__init__(hs)
|
2020-02-07 04:14:19 -07:00
|
|
|
|
|
|
|
# Ensure attestations get renewed
|
|
|
|
hs.get_groups_attestation_renewer()
|
|
|
|
|
|
|
|
# The following functions merely route the query to the local groups server
|
|
|
|
# or federation depending on if the group is local or remote
|
|
|
|
|
|
|
|
update_group_profile = _create_rerouter("update_group_profile")
|
|
|
|
|
|
|
|
add_room_to_group = _create_rerouter("add_room_to_group")
|
|
|
|
update_room_in_group = _create_rerouter("update_room_in_group")
|
|
|
|
remove_room_from_group = _create_rerouter("remove_room_from_group")
|
|
|
|
|
|
|
|
update_group_summary_room = _create_rerouter("update_group_summary_room")
|
|
|
|
delete_group_summary_room = _create_rerouter("delete_group_summary_room")
|
|
|
|
|
|
|
|
update_group_category = _create_rerouter("update_group_category")
|
|
|
|
delete_group_category = _create_rerouter("delete_group_category")
|
|
|
|
|
|
|
|
update_group_summary_user = _create_rerouter("update_group_summary_user")
|
|
|
|
delete_group_summary_user = _create_rerouter("delete_group_summary_user")
|
|
|
|
|
|
|
|
update_group_role = _create_rerouter("update_group_role")
|
|
|
|
delete_group_role = _create_rerouter("delete_group_role")
|
|
|
|
|
|
|
|
set_group_join_policy = _create_rerouter("set_group_join_policy")
|
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def create_group(
|
|
|
|
self, group_id: str, user_id: str, content: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-07-18 10:28:42 -06:00
|
|
|
"""Create a group"""
|
|
|
|
|
2017-07-10 07:52:27 -06:00
|
|
|
logger.info("Asking to create group with ID: %r", group_id)
|
|
|
|
|
|
|
|
if self.is_mine_id(group_id):
|
2020-05-01 08:15:36 -06:00
|
|
|
res = await self.groups_server_handler.create_group(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id, user_id, content
|
|
|
|
)
|
2017-09-19 03:35:35 -06:00
|
|
|
local_attestation = None
|
|
|
|
remote_attestation = None
|
|
|
|
else:
|
2021-01-26 08:50:21 -07:00
|
|
|
raise SynapseError(400, "Unable to create remote groups")
|
2017-09-19 03:35:35 -06:00
|
|
|
|
|
|
|
is_publicised = content.get("publicise", False)
|
2020-05-01 08:15:36 -06:00
|
|
|
token = await self.store.register_user_group_membership(
|
2017-09-19 03:35:35 -06:00
|
|
|
group_id,
|
|
|
|
user_id,
|
|
|
|
membership="join",
|
|
|
|
is_admin=True,
|
|
|
|
local_attestation=local_attestation,
|
|
|
|
remote_attestation=remote_attestation,
|
|
|
|
is_publicised=is_publicised,
|
2017-08-25 04:21:34 -06:00
|
|
|
)
|
2017-09-19 03:35:35 -06:00
|
|
|
self.notifier.on_new_event("groups_key", token, users=[user_id])
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return res
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def join_group(
|
|
|
|
self, group_id: str, user_id: str, content: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-07-18 10:28:42 -06:00
|
|
|
"""Request to join a group"""
|
2018-03-28 10:18:02 -06:00
|
|
|
if self.is_mine_id(group_id):
|
2020-06-01 05:28:43 -06:00
|
|
|
await self.groups_server_handler.join_group(group_id, user_id, content)
|
2018-03-28 10:18:02 -06:00
|
|
|
local_attestation = None
|
|
|
|
remote_attestation = None
|
|
|
|
else:
|
|
|
|
local_attestation = self.attestations.create_attestation(group_id, user_id)
|
|
|
|
content["attestation"] = local_attestation
|
|
|
|
|
2019-07-29 10:21:57 -06:00
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
res = await self.transport_client.join_group(
|
2019-07-29 10:21:57 -06:00
|
|
|
get_domain_from_id(group_id), group_id, user_id, content
|
|
|
|
)
|
2020-01-07 08:36:41 -07:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2019-07-29 10:21:57 -06:00
|
|
|
except RequestSendFailed:
|
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
2018-03-28 10:18:02 -06:00
|
|
|
|
|
|
|
remote_attestation = res["attestation"]
|
|
|
|
|
2020-06-01 05:28:43 -06:00
|
|
|
await self.attestations.verify_attestation(
|
2018-03-28 10:18:02 -06:00
|
|
|
remote_attestation,
|
|
|
|
group_id=group_id,
|
|
|
|
user_id=user_id,
|
|
|
|
server_name=get_domain_from_id(group_id),
|
|
|
|
)
|
|
|
|
|
2020-10-23 10:38:40 -06:00
|
|
|
# TODO: Check that the group is public and we're being added publicly
|
2018-03-28 10:18:02 -06:00
|
|
|
is_publicised = content.get("publicise", False)
|
|
|
|
|
2020-06-01 05:28:43 -06:00
|
|
|
token = await self.store.register_user_group_membership(
|
2018-03-28 10:18:02 -06:00
|
|
|
group_id,
|
|
|
|
user_id,
|
|
|
|
membership="join",
|
|
|
|
is_admin=False,
|
|
|
|
local_attestation=local_attestation,
|
|
|
|
remote_attestation=remote_attestation,
|
|
|
|
is_publicised=is_publicised,
|
|
|
|
)
|
|
|
|
self.notifier.on_new_event("groups_key", token, users=[user_id])
|
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {}
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def accept_invite(
|
|
|
|
self, group_id: str, user_id: str, content: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-07-18 10:28:42 -06:00
|
|
|
"""Accept an invite to a group"""
|
2017-07-10 07:52:27 -06:00
|
|
|
if self.is_mine_id(group_id):
|
2020-06-01 05:28:43 -06:00
|
|
|
await self.groups_server_handler.accept_invite(group_id, user_id, content)
|
2017-07-10 07:52:27 -06:00
|
|
|
local_attestation = None
|
|
|
|
remote_attestation = None
|
|
|
|
else:
|
|
|
|
local_attestation = self.attestations.create_attestation(group_id, user_id)
|
|
|
|
content["attestation"] = local_attestation
|
|
|
|
|
2019-07-29 10:21:57 -06:00
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
res = await self.transport_client.accept_group_invite(
|
2019-07-29 10:21:57 -06:00
|
|
|
get_domain_from_id(group_id), group_id, user_id, content
|
|
|
|
)
|
2020-01-07 08:36:41 -07:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2019-07-29 10:21:57 -06:00
|
|
|
except RequestSendFailed:
|
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
2017-07-10 07:52:27 -06:00
|
|
|
|
|
|
|
remote_attestation = res["attestation"]
|
|
|
|
|
2020-06-01 05:28:43 -06:00
|
|
|
await self.attestations.verify_attestation(
|
2017-07-10 07:52:27 -06:00
|
|
|
remote_attestation,
|
|
|
|
group_id=group_id,
|
|
|
|
user_id=user_id,
|
2017-10-11 07:11:43 -06:00
|
|
|
server_name=get_domain_from_id(group_id),
|
2017-07-10 07:52:27 -06:00
|
|
|
)
|
|
|
|
|
2020-10-23 10:38:40 -06:00
|
|
|
# TODO: Check that the group is public and we're being added publicly
|
2017-08-08 04:50:09 -06:00
|
|
|
is_publicised = content.get("publicise", False)
|
|
|
|
|
2020-06-01 05:28:43 -06:00
|
|
|
token = await self.store.register_user_group_membership(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id,
|
|
|
|
user_id,
|
|
|
|
membership="join",
|
|
|
|
is_admin=False,
|
|
|
|
local_attestation=local_attestation,
|
|
|
|
remote_attestation=remote_attestation,
|
2017-08-08 04:50:09 -06:00
|
|
|
is_publicised=is_publicised,
|
2017-07-10 07:52:27 -06:00
|
|
|
)
|
2017-07-20 10:13:18 -06:00
|
|
|
self.notifier.on_new_event("groups_key", token, users=[user_id])
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {}
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def invite(
|
|
|
|
self, group_id: str, user_id: str, requester_user_id: str, config: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-07-18 10:28:42 -06:00
|
|
|
"""Invite a user to a group"""
|
2017-07-10 07:52:27 -06:00
|
|
|
content = {"requester_user_id": requester_user_id, "config": config}
|
|
|
|
if self.is_mine_id(group_id):
|
2020-06-01 05:28:43 -06:00
|
|
|
res = await self.groups_server_handler.invite_to_group(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id, user_id, requester_user_id, content
|
|
|
|
)
|
|
|
|
else:
|
2019-07-29 10:21:57 -06:00
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
res = await self.transport_client.invite_to_group(
|
2019-07-29 10:21:57 -06:00
|
|
|
get_domain_from_id(group_id),
|
|
|
|
group_id,
|
|
|
|
user_id,
|
|
|
|
requester_user_id,
|
|
|
|
content,
|
|
|
|
)
|
2020-01-07 08:36:41 -07:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2019-07-29 10:21:57 -06:00
|
|
|
except RequestSendFailed:
|
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return res
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def on_invite(
|
|
|
|
self, group_id: str, user_id: str, content: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-07-18 10:28:42 -06:00
|
|
|
"""One of our users were invited to a group"""
|
2017-07-10 07:52:27 -06:00
|
|
|
# TODO: Support auto join and rejection
|
|
|
|
|
|
|
|
if not self.is_mine_id(user_id):
|
|
|
|
raise SynapseError(400, "User not on this server")
|
|
|
|
|
|
|
|
local_profile = {}
|
|
|
|
if "profile" in content:
|
|
|
|
if "name" in content["profile"]:
|
|
|
|
local_profile["name"] = content["profile"]["name"]
|
|
|
|
if "avatar_url" in content["profile"]:
|
|
|
|
local_profile["avatar_url"] = content["profile"]["avatar_url"]
|
|
|
|
|
2020-06-01 05:28:43 -06:00
|
|
|
token = await self.store.register_user_group_membership(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id,
|
|
|
|
user_id,
|
|
|
|
membership="invite",
|
|
|
|
content={"profile": local_profile, "inviter": content["inviter"]},
|
|
|
|
)
|
2017-07-20 10:13:18 -06:00
|
|
|
self.notifier.on_new_event("groups_key", token, users=[user_id])
|
2017-10-16 07:20:45 -06:00
|
|
|
try:
|
2020-06-01 05:28:43 -06:00
|
|
|
user_profile = await self.profile_handler.get_profile(user_id)
|
2017-10-16 07:20:45 -06:00
|
|
|
except Exception as e:
|
2019-10-31 04:23:24 -06:00
|
|
|
logger.warning("No profile for user %s: %s", user_id, e)
|
2017-10-16 07:20:45 -06:00
|
|
|
user_profile = {}
|
2017-08-25 04:21:34 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return {"state": "invite", "user_profile": user_profile}
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2020-05-01 08:15:36 -06:00
|
|
|
async def remove_user_from_group(
|
2021-01-26 08:50:21 -07:00
|
|
|
self, group_id: str, user_id: str, requester_user_id: str, content: JsonDict
|
|
|
|
) -> JsonDict:
|
2017-07-18 10:28:42 -06:00
|
|
|
"""Remove a user from a group"""
|
2017-07-10 07:52:27 -06:00
|
|
|
if user_id == requester_user_id:
|
2020-05-01 08:15:36 -06:00
|
|
|
token = await self.store.register_user_group_membership(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id, user_id, membership="leave"
|
2017-07-20 10:13:18 -06:00
|
|
|
)
|
|
|
|
self.notifier.on_new_event("groups_key", token, users=[user_id])
|
2017-07-10 07:52:27 -06:00
|
|
|
|
|
|
|
# TODO: Should probably remember that we tried to leave so that we can
|
|
|
|
# retry if the group server is currently down.
|
|
|
|
|
|
|
|
if self.is_mine_id(group_id):
|
2020-05-01 08:15:36 -06:00
|
|
|
res = await self.groups_server_handler.remove_user_from_group(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id, user_id, requester_user_id, content
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
content["requester_user_id"] = requester_user_id
|
2019-07-29 10:21:57 -06:00
|
|
|
try:
|
2020-05-01 08:15:36 -06:00
|
|
|
res = await self.transport_client.remove_user_from_group(
|
2019-07-29 10:21:57 -06:00
|
|
|
get_domain_from_id(group_id),
|
|
|
|
group_id,
|
|
|
|
requester_user_id,
|
|
|
|
user_id,
|
|
|
|
content,
|
|
|
|
)
|
2020-01-07 08:36:41 -07:00
|
|
|
except HttpResponseException as e:
|
|
|
|
raise e.to_synapse_error()
|
2019-07-29 10:21:57 -06:00
|
|
|
except RequestSendFailed:
|
|
|
|
raise SynapseError(502, "Failed to contact group server")
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return res
|
2017-07-10 07:52:27 -06:00
|
|
|
|
2021-01-26 08:50:21 -07:00
|
|
|
async def user_removed_from_group(
|
|
|
|
self, group_id: str, user_id: str, content: JsonDict
|
|
|
|
) -> None:
|
2017-07-18 10:28:42 -06:00
|
|
|
"""One of our users was removed/kicked from a group"""
|
2017-07-10 07:52:27 -06:00
|
|
|
# TODO: Check if user in group
|
2020-06-01 05:28:43 -06:00
|
|
|
token = await self.store.register_user_group_membership(
|
2017-07-10 07:52:27 -06:00
|
|
|
group_id, user_id, membership="leave"
|
2017-07-20 10:13:18 -06:00
|
|
|
)
|
|
|
|
self.notifier.on_new_event("groups_key", token, users=[user_id])
|