Skip to content
Snippets Groups Projects
Unverified Commit 6af9cdca authored by Patrick Cloke's avatar Patrick Cloke Committed by GitHub
Browse files

Convert groups local and server to async/await. (#7600)

parent c1bdd4fa
No related branches found
No related tags found
No related merge requests found
Convert groups handlers to async/await.
This diff is collapsed.
...@@ -18,8 +18,6 @@ import logging ...@@ -18,8 +18,6 @@ import logging
from six import iteritems from six import iteritems
from twisted.internet import defer
from synapse.api.errors import HttpResponseException, RequestSendFailed, SynapseError from synapse.api.errors import HttpResponseException, RequestSendFailed, SynapseError
from synapse.types import get_domain_from_id from synapse.types import get_domain_from_id
...@@ -92,19 +90,18 @@ class GroupsLocalWorkerHandler(object): ...@@ -92,19 +90,18 @@ class GroupsLocalWorkerHandler(object):
get_group_role = _create_rerouter("get_group_role") get_group_role = _create_rerouter("get_group_role")
get_group_roles = _create_rerouter("get_group_roles") get_group_roles = _create_rerouter("get_group_roles")
@defer.inlineCallbacks async def get_group_summary(self, group_id, requester_user_id):
def get_group_summary(self, group_id, requester_user_id):
"""Get the group summary for a group. """Get the group summary for a group.
If the group is remote we check that the users have valid attestations. If the group is remote we check that the users have valid attestations.
""" """
if self.is_mine_id(group_id): if self.is_mine_id(group_id):
res = yield self.groups_server_handler.get_group_summary( res = await self.groups_server_handler.get_group_summary(
group_id, requester_user_id group_id, requester_user_id
) )
else: else:
try: try:
res = yield self.transport_client.get_group_summary( res = await self.transport_client.get_group_summary(
get_domain_from_id(group_id), group_id, requester_user_id get_domain_from_id(group_id), group_id, requester_user_id
) )
except HttpResponseException as e: except HttpResponseException as e:
...@@ -122,7 +119,7 @@ class GroupsLocalWorkerHandler(object): ...@@ -122,7 +119,7 @@ class GroupsLocalWorkerHandler(object):
attestation = entry.pop("attestation", {}) attestation = entry.pop("attestation", {})
try: try:
if get_domain_from_id(g_user_id) != group_server_name: if get_domain_from_id(g_user_id) != group_server_name:
yield self.attestations.verify_attestation( await self.attestations.verify_attestation(
attestation, attestation,
group_id=group_id, group_id=group_id,
user_id=g_user_id, user_id=g_user_id,
...@@ -139,19 +136,18 @@ class GroupsLocalWorkerHandler(object): ...@@ -139,19 +136,18 @@ class GroupsLocalWorkerHandler(object):
# Add `is_publicised` flag to indicate whether the user has publicised their # Add `is_publicised` flag to indicate whether the user has publicised their
# membership of the group on their profile # membership of the group on their profile
result = yield self.store.get_publicised_groups_for_user(requester_user_id) result = await self.store.get_publicised_groups_for_user(requester_user_id)
is_publicised = group_id in result is_publicised = group_id in result
res.setdefault("user", {})["is_publicised"] = is_publicised res.setdefault("user", {})["is_publicised"] = is_publicised
return res return res
@defer.inlineCallbacks async def get_users_in_group(self, group_id, requester_user_id):
def get_users_in_group(self, group_id, requester_user_id):
"""Get users in a group """Get users in a group
""" """
if self.is_mine_id(group_id): if self.is_mine_id(group_id):
res = yield self.groups_server_handler.get_users_in_group( res = await self.groups_server_handler.get_users_in_group(
group_id, requester_user_id group_id, requester_user_id
) )
return res return res
...@@ -159,7 +155,7 @@ class GroupsLocalWorkerHandler(object): ...@@ -159,7 +155,7 @@ class GroupsLocalWorkerHandler(object):
group_server_name = get_domain_from_id(group_id) group_server_name = get_domain_from_id(group_id)
try: try:
res = yield self.transport_client.get_users_in_group( res = await self.transport_client.get_users_in_group(
get_domain_from_id(group_id), group_id, requester_user_id get_domain_from_id(group_id), group_id, requester_user_id
) )
except HttpResponseException as e: except HttpResponseException as e:
...@@ -174,7 +170,7 @@ class GroupsLocalWorkerHandler(object): ...@@ -174,7 +170,7 @@ class GroupsLocalWorkerHandler(object):
attestation = entry.pop("attestation", {}) attestation = entry.pop("attestation", {})
try: try:
if get_domain_from_id(g_user_id) != group_server_name: if get_domain_from_id(g_user_id) != group_server_name:
yield self.attestations.verify_attestation( await self.attestations.verify_attestation(
attestation, attestation,
group_id=group_id, group_id=group_id,
user_id=g_user_id, user_id=g_user_id,
...@@ -188,15 +184,13 @@ class GroupsLocalWorkerHandler(object): ...@@ -188,15 +184,13 @@ class GroupsLocalWorkerHandler(object):
return res return res
@defer.inlineCallbacks async def get_joined_groups(self, user_id):
def get_joined_groups(self, user_id): group_ids = await self.store.get_joined_groups(user_id)
group_ids = yield self.store.get_joined_groups(user_id)
return {"groups": group_ids} return {"groups": group_ids}
@defer.inlineCallbacks async def get_publicised_groups_for_user(self, user_id):
def get_publicised_groups_for_user(self, user_id):
if self.hs.is_mine_id(user_id): if self.hs.is_mine_id(user_id):
result = yield self.store.get_publicised_groups_for_user(user_id) result = await self.store.get_publicised_groups_for_user(user_id)
# Check AS associated groups for this user - this depends on the # Check AS associated groups for this user - this depends on the
# RegExps in the AS registration file (under `users`) # RegExps in the AS registration file (under `users`)
...@@ -206,7 +200,7 @@ class GroupsLocalWorkerHandler(object): ...@@ -206,7 +200,7 @@ class GroupsLocalWorkerHandler(object):
return {"groups": result} return {"groups": result}
else: else:
try: try:
bulk_result = yield self.transport_client.bulk_get_publicised_groups( bulk_result = await self.transport_client.bulk_get_publicised_groups(
get_domain_from_id(user_id), [user_id] get_domain_from_id(user_id), [user_id]
) )
except HttpResponseException as e: except HttpResponseException as e:
...@@ -218,8 +212,7 @@ class GroupsLocalWorkerHandler(object): ...@@ -218,8 +212,7 @@ class GroupsLocalWorkerHandler(object):
# TODO: Verify attestations # TODO: Verify attestations
return {"groups": result} return {"groups": result}
@defer.inlineCallbacks async def bulk_get_publicised_groups(self, user_ids, proxy=True):
def bulk_get_publicised_groups(self, user_ids, proxy=True):
destinations = {} destinations = {}
local_users = set() local_users = set()
...@@ -236,7 +229,7 @@ class GroupsLocalWorkerHandler(object): ...@@ -236,7 +229,7 @@ class GroupsLocalWorkerHandler(object):
failed_results = [] failed_results = []
for destination, dest_user_ids in iteritems(destinations): for destination, dest_user_ids in iteritems(destinations):
try: try:
r = yield self.transport_client.bulk_get_publicised_groups( r = await self.transport_client.bulk_get_publicised_groups(
destination, list(dest_user_ids) destination, list(dest_user_ids)
) )
results.update(r["users"]) results.update(r["users"])
...@@ -244,7 +237,7 @@ class GroupsLocalWorkerHandler(object): ...@@ -244,7 +237,7 @@ class GroupsLocalWorkerHandler(object):
failed_results.extend(dest_user_ids) failed_results.extend(dest_user_ids)
for uid in local_users: for uid in local_users:
results[uid] = yield self.store.get_publicised_groups_for_user(uid) results[uid] = await self.store.get_publicised_groups_for_user(uid)
# Check AS associated groups for this user - this depends on the # Check AS associated groups for this user - this depends on the
# RegExps in the AS registration file (under `users`) # RegExps in the AS registration file (under `users`)
...@@ -333,12 +326,11 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -333,12 +326,11 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
return res return res
@defer.inlineCallbacks async def join_group(self, group_id, user_id, content):
def join_group(self, group_id, user_id, content):
"""Request to join a group """Request to join a group
""" """
if self.is_mine_id(group_id): if self.is_mine_id(group_id):
yield self.groups_server_handler.join_group(group_id, user_id, content) await self.groups_server_handler.join_group(group_id, user_id, content)
local_attestation = None local_attestation = None
remote_attestation = None remote_attestation = None
else: else:
...@@ -346,7 +338,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -346,7 +338,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
content["attestation"] = local_attestation content["attestation"] = local_attestation
try: try:
res = yield self.transport_client.join_group( res = await self.transport_client.join_group(
get_domain_from_id(group_id), group_id, user_id, content get_domain_from_id(group_id), group_id, user_id, content
) )
except HttpResponseException as e: except HttpResponseException as e:
...@@ -356,7 +348,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -356,7 +348,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
remote_attestation = res["attestation"] remote_attestation = res["attestation"]
yield self.attestations.verify_attestation( await self.attestations.verify_attestation(
remote_attestation, remote_attestation,
group_id=group_id, group_id=group_id,
user_id=user_id, user_id=user_id,
...@@ -366,7 +358,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -366,7 +358,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
# TODO: Check that the group is public and we're being added publically # TODO: Check that the group is public and we're being added publically
is_publicised = content.get("publicise", False) is_publicised = content.get("publicise", False)
token = yield self.store.register_user_group_membership( token = await self.store.register_user_group_membership(
group_id, group_id,
user_id, user_id,
membership="join", membership="join",
...@@ -379,12 +371,11 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -379,12 +371,11 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
return {} return {}
@defer.inlineCallbacks async def accept_invite(self, group_id, user_id, content):
def accept_invite(self, group_id, user_id, content):
"""Accept an invite to a group """Accept an invite to a group
""" """
if self.is_mine_id(group_id): if self.is_mine_id(group_id):
yield self.groups_server_handler.accept_invite(group_id, user_id, content) await self.groups_server_handler.accept_invite(group_id, user_id, content)
local_attestation = None local_attestation = None
remote_attestation = None remote_attestation = None
else: else:
...@@ -392,7 +383,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -392,7 +383,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
content["attestation"] = local_attestation content["attestation"] = local_attestation
try: try:
res = yield self.transport_client.accept_group_invite( res = await self.transport_client.accept_group_invite(
get_domain_from_id(group_id), group_id, user_id, content get_domain_from_id(group_id), group_id, user_id, content
) )
except HttpResponseException as e: except HttpResponseException as e:
...@@ -402,7 +393,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -402,7 +393,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
remote_attestation = res["attestation"] remote_attestation = res["attestation"]
yield self.attestations.verify_attestation( await self.attestations.verify_attestation(
remote_attestation, remote_attestation,
group_id=group_id, group_id=group_id,
user_id=user_id, user_id=user_id,
...@@ -412,7 +403,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -412,7 +403,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
# TODO: Check that the group is public and we're being added publically # TODO: Check that the group is public and we're being added publically
is_publicised = content.get("publicise", False) is_publicised = content.get("publicise", False)
token = yield self.store.register_user_group_membership( token = await self.store.register_user_group_membership(
group_id, group_id,
user_id, user_id,
membership="join", membership="join",
...@@ -425,18 +416,17 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -425,18 +416,17 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
return {} return {}
@defer.inlineCallbacks async def invite(self, group_id, user_id, requester_user_id, config):
def invite(self, group_id, user_id, requester_user_id, config):
"""Invite a user to a group """Invite a user to a group
""" """
content = {"requester_user_id": requester_user_id, "config": config} content = {"requester_user_id": requester_user_id, "config": config}
if self.is_mine_id(group_id): if self.is_mine_id(group_id):
res = yield self.groups_server_handler.invite_to_group( res = await self.groups_server_handler.invite_to_group(
group_id, user_id, requester_user_id, content group_id, user_id, requester_user_id, content
) )
else: else:
try: try:
res = yield self.transport_client.invite_to_group( res = await self.transport_client.invite_to_group(
get_domain_from_id(group_id), get_domain_from_id(group_id),
group_id, group_id,
user_id, user_id,
...@@ -450,8 +440,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -450,8 +440,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
return res return res
@defer.inlineCallbacks async def on_invite(self, group_id, user_id, content):
def on_invite(self, group_id, user_id, content):
"""One of our users were invited to a group """One of our users were invited to a group
""" """
# TODO: Support auto join and rejection # TODO: Support auto join and rejection
...@@ -466,7 +455,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -466,7 +455,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
if "avatar_url" in content["profile"]: if "avatar_url" in content["profile"]:
local_profile["avatar_url"] = content["profile"]["avatar_url"] local_profile["avatar_url"] = content["profile"]["avatar_url"]
token = yield self.store.register_user_group_membership( token = await self.store.register_user_group_membership(
group_id, group_id,
user_id, user_id,
membership="invite", membership="invite",
...@@ -474,7 +463,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -474,7 +463,7 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
) )
self.notifier.on_new_event("groups_key", token, users=[user_id]) self.notifier.on_new_event("groups_key", token, users=[user_id])
try: try:
user_profile = yield self.profile_handler.get_profile(user_id) user_profile = await self.profile_handler.get_profile(user_id)
except Exception as e: except Exception as e:
logger.warning("No profile for user %s: %s", user_id, e) logger.warning("No profile for user %s: %s", user_id, e)
user_profile = {} user_profile = {}
...@@ -516,12 +505,11 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler): ...@@ -516,12 +505,11 @@ class GroupsLocalHandler(GroupsLocalWorkerHandler):
return res return res
@defer.inlineCallbacks async def user_removed_from_group(self, group_id, user_id, content):
def user_removed_from_group(self, group_id, user_id, content):
"""One of our users was removed/kicked from a group """One of our users was removed/kicked from a group
""" """
# TODO: Check if user in group # TODO: Check if user in group
token = yield self.store.register_user_group_membership( token = await self.store.register_user_group_membership(
group_id, user_id, membership="leave" group_id, user_id, membership="leave"
) )
self.notifier.on_new_event("groups_key", token, users=[user_id]) self.notifier.on_new_event("groups_key", token, users=[user_id])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment