summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuke Barnard <lukebarnard1@users.noreply.github.com>2017-10-16 17:33:33 +0200
committerGitHub <noreply@github.com>2017-10-16 17:33:33 +0200
commite0a75e0c253321bca6f00f79815eac11ce003988 (patch)
tree68e666ef8c0c3f478aba9dab51c1d7c66cad9f5b
parentMerge pull request #2543 from matrix-org/luke/fix-on-group-invite-no-profile (diff)
parentDelint (diff)
downloadsynapse-e0a75e0c253321bca6f00f79815eac11ce003988.tar.xz
Merge pull request #2544 from matrix-org/luke/groups-invited-users
 Implement GET /groups/$groupId/invited_users
-rw-r--r--synapse/federation/transport/client.py13
-rw-r--r--synapse/federation/transport/server.py20
-rw-r--r--synapse/groups/groups_server.py34
-rw-r--r--synapse/handlers/groups_local.py2
-rw-r--r--synapse/rest/client/v2_alpha/groups.py22
-rw-r--r--synapse/storage/group_server.py12
6 files changed, 102 insertions, 1 deletions
diff --git a/synapse/federation/transport/client.py b/synapse/federation/transport/client.py
index f96561c1fe..125d8f3598 100644
--- a/synapse/federation/transport/client.py
+++ b/synapse/federation/transport/client.py
@@ -551,6 +551,19 @@ class TransportLayerClient(object):
         )
 
     @log_function
+    def get_invited_users_in_group(self, destination, group_id, requester_user_id):
+        """Get users that have been invited to a group
+        """
+        path = PREFIX + "/groups/%s/invited_users" % (group_id,)
+
+        return self.client.get_json(
+            destination=destination,
+            path=path,
+            args={"requester_user_id": requester_user_id},
+            ignore_backoff=True,
+        )
+
+    @log_function
     def accept_group_invite(self, destination, group_id, user_id, content):
         """Accept a group invite
         """
diff --git a/synapse/federation/transport/server.py b/synapse/federation/transport/server.py
index c7565e0737..6a0bd8d222 100644
--- a/synapse/federation/transport/server.py
+++ b/synapse/federation/transport/server.py
@@ -721,6 +721,24 @@ class FederationGroupsUsersServlet(BaseFederationServlet):
         defer.returnValue((200, new_content))
 
 
+class FederationGroupsInvitedUsersServlet(BaseFederationServlet):
+    """Get the users that have been invited to a group
+    """
+    PATH = "/groups/(?P<group_id>[^/]*)/invited_users$"
+
+    @defer.inlineCallbacks
+    def on_GET(self, origin, content, query, group_id):
+        requester_user_id = parse_string_from_args(query, "requester_user_id")
+        if get_domain_from_id(requester_user_id) != origin:
+            raise SynapseError(403, "requester_user_id doesn't match origin")
+
+        new_content = yield self.handler.get_invited_users_in_group(
+            group_id, requester_user_id
+        )
+
+        defer.returnValue((200, new_content))
+
+
 class FederationGroupsInviteServlet(BaseFederationServlet):
     """Ask a group server to invite someone to the group
     """
@@ -1109,12 +1127,12 @@ ROOM_LIST_CLASSES = (
     PublicRoomList,
 )
 
-
 GROUP_SERVER_SERVLET_CLASSES = (
     FederationGroupsProfileServlet,
     FederationGroupsSummaryServlet,
     FederationGroupsRoomsServlet,
     FederationGroupsUsersServlet,
+    FederationGroupsInvitedUsersServlet,
     FederationGroupsInviteServlet,
     FederationGroupsAcceptInviteServlet,
     FederationGroupsRemoveUserServlet,
diff --git a/synapse/groups/groups_server.py b/synapse/groups/groups_server.py
index 1083bc2990..a3a500b9d6 100644
--- a/synapse/groups/groups_server.py
+++ b/synapse/groups/groups_server.py
@@ -421,6 +421,40 @@ class GroupsServerHandler(object):
         })
 
     @defer.inlineCallbacks
+    def get_invited_users_in_group(self, group_id, requester_user_id):
+        """Get the users that have been invited to a group as seen by requester_user_id.
+
+        The ordering is arbitrary at the moment
+        """
+
+        yield self.check_group_is_ours(group_id, and_exists=True)
+
+        is_user_in_group = yield self.store.is_user_in_group(requester_user_id, group_id)
+
+        if not is_user_in_group:
+            raise SynapseError(403, "User not in group")
+
+        invited_users = yield self.store.get_invited_users_in_group(group_id)
+
+        user_profiles = []
+
+        for user_id in invited_users:
+            user_profile = {
+                "user_id": user_id
+            }
+            try:
+                profile = yield self.profile_handler.get_profile_from_cache(user_id)
+                user_profile.update(profile)
+            except Exception as e:
+                logger.warn("Error getting profile for %s: %s", user_id, e)
+            user_profiles.append(user_profile)
+
+        defer.returnValue({
+            "chunk": user_profiles,
+            "total_user_count_estimate": len(invited_users),
+        })
+
+    @defer.inlineCallbacks
     def get_rooms_in_group(self, group_id, requester_user_id):
         """Get the rooms in group as seen by requester_user_id
 
diff --git a/synapse/handlers/groups_local.py b/synapse/handlers/groups_local.py
index 97a20f2b04..6699d0888f 100644
--- a/synapse/handlers/groups_local.py
+++ b/synapse/handlers/groups_local.py
@@ -68,6 +68,8 @@ class GroupsLocalHandler(object):
     update_group_profile = _create_rerouter("update_group_profile")
     get_rooms_in_group = _create_rerouter("get_rooms_in_group")
 
+    get_invited_users_in_group = _create_rerouter("get_invited_users_in_group")
+
     add_room_to_group = _create_rerouter("add_room_to_group")
     remove_room_from_group = _create_rerouter("remove_room_from_group")
 
diff --git a/synapse/rest/client/v2_alpha/groups.py b/synapse/rest/client/v2_alpha/groups.py
index 8f3ce15b02..d11bccc1da 100644
--- a/synapse/rest/client/v2_alpha/groups.py
+++ b/synapse/rest/client/v2_alpha/groups.py
@@ -371,6 +371,27 @@ class GroupUsersServlet(RestServlet):
         defer.returnValue((200, result))
 
 
+class GroupInvitedUsersServlet(RestServlet):
+    """Get users invited to a group
+    """
+    PATTERNS = client_v2_patterns("/groups/(?P<group_id>[^/]*)/invited_users$")
+
+    def __init__(self, hs):
+        super(GroupInvitedUsersServlet, self).__init__()
+        self.auth = hs.get_auth()
+        self.clock = hs.get_clock()
+        self.groups_handler = hs.get_groups_local_handler()
+
+    @defer.inlineCallbacks
+    def on_GET(self, request, group_id):
+        requester = yield self.auth.get_user_by_req(request)
+        user_id = requester.user.to_string()
+
+        result = yield self.groups_handler.get_invited_users_in_group(group_id, user_id)
+
+        defer.returnValue((200, result))
+
+
 class GroupCreateServlet(RestServlet):
     """Create a group
     """
@@ -674,6 +695,7 @@ class GroupsForUserServlet(RestServlet):
 def register_servlets(hs, http_server):
     GroupServlet(hs).register(http_server)
     GroupSummaryServlet(hs).register(http_server)
+    GroupInvitedUsersServlet(hs).register(http_server)
     GroupUsersServlet(hs).register(http_server)
     GroupRoomServlet(hs).register(http_server)
     GroupCreateServlet(hs).register(http_server)
diff --git a/synapse/storage/group_server.py b/synapse/storage/group_server.py
index 3af372de59..9e63db5c6c 100644
--- a/synapse/storage/group_server.py
+++ b/synapse/storage/group_server.py
@@ -56,6 +56,18 @@ class GroupServerStore(SQLBaseStore):
             desc="get_users_in_group",
         )
 
+    def get_invited_users_in_group(self, group_id):
+        # TODO: Pagination
+
+        return self._simple_select_onecol(
+            table="group_invites",
+            keyvalues={
+                "group_id": group_id,
+            },
+            retcol="user_id",
+            desc="get_invited_users_in_group",
+        )
+
     def get_rooms_in_group(self, group_id, include_private=False):
         # TODO: Pagination