summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erikj@jki.re>2017-07-24 18:09:12 +0100
committerGitHub <noreply@github.com>2017-07-24 18:09:12 +0100
commitebbaae5526a0975c0f218a2b9bde52d61c9bbde7 (patch)
tree14f7062f7375b60adc0199d5166665b4a8cfa7dd
parentCorrectly add joins to correct segment (diff)
parentUpdate comment (diff)
downloadsynapse-ebbaae5526a0975c0f218a2b9bde52d61c9bbde7.tar.xz
Merge pull request #2382 from matrix-org/erikj/group_privilege
Include users membership in group in summary API
-rw-r--r--synapse/groups/groups_server.py5
-rw-r--r--synapse/storage/group_server.py81
2 files changed, 85 insertions, 1 deletions
diff --git a/synapse/groups/groups_server.py b/synapse/groups/groups_server.py
index b1ee43ef90..f25f327eb9 100644
--- a/synapse/groups/groups_server.py
+++ b/synapse/groups/groups_server.py
@@ -130,6 +130,10 @@ class GroupsServerHandler(object):
 
         users.sort(key=lambda e: e.get("order", 0))
 
+        membership_info = yield self.store.get_users_membership_info_in_group(
+            group_id, requester_user_id,
+        )
+
         defer.returnValue({
             "profile": profile,
             "users_section": {
@@ -142,6 +146,7 @@ class GroupsServerHandler(object):
                 "categories": categories,
                 "total_room_count_estimate": 0,  # TODO
             },
+            "user": membership_info,
         })
 
     @defer.inlineCallbacks
diff --git a/synapse/storage/group_server.py b/synapse/storage/group_server.py
index d42e215b26..f44e80b514 100644
--- a/synapse/storage/group_server.py
+++ b/synapse/storage/group_server.py
@@ -152,6 +152,18 @@ class GroupServerStore(SQLBaseStore):
                 an order of 1 will put the room first. Otherwise, the room gets
                 added to the end.
         """
+        room_in_group = self._simple_select_one_onecol_txn(
+            txn,
+            table="group_rooms",
+            keyvalues={
+                "group_id": group_id,
+                "room_id": room_id,
+            },
+            retcol="room_id",
+            allow_none=True,
+        )
+        if not room_in_group:
+            raise SynapseError(400, "room not in group")
 
         if category_id is None:
             category_id = _DEFAULT_CATEGORY_ID
@@ -426,6 +438,19 @@ class GroupServerStore(SQLBaseStore):
                 an order of 1 will put the user first. Otherwise, the user gets
                 added to the end.
         """
+        user_in_group = self._simple_select_one_onecol_txn(
+            txn,
+            table="group_users",
+            keyvalues={
+                "group_id": group_id,
+                "user_id": user_id,
+            },
+            retcol="user_id",
+            allow_none=True,
+        )
+        if not user_in_group:
+            raise SynapseError(400, "user not in group")
+
         if role_id is None:
             role_id = _DEFAULT_ROLE_ID
         else:
@@ -618,7 +643,7 @@ class GroupServerStore(SQLBaseStore):
             },
             retcol="is_admin",
             allow_none=True,
-            desc="is_user_adim_in_group",
+            desc="is_user_admin_in_group",
         )
 
     def add_group_invite(self, group_id, user_id):
@@ -647,6 +672,60 @@ class GroupServerStore(SQLBaseStore):
             allow_none=True,
         )
 
+    def get_users_membership_info_in_group(self, group_id, user_id):
+        """Get a dict describing the membership of a user in a group.
+
+        Example if joined:
+
+            {
+                "membership": "join",
+                "is_public": True,
+                "is_privileged": False,
+            }
+
+        Returns an empty dict if the user is not join/invite/etc
+        """
+        def _get_users_membership_in_group_txn(txn):
+            row = self._simple_select_one_txn(
+                txn,
+                table="group_users",
+                keyvalues={
+                    "group_id": group_id,
+                    "user_id": user_id,
+                },
+                retcols=("is_admin", "is_public"),
+                allow_none=True,
+            )
+
+            if row:
+                return {
+                    "membership": "join",
+                    "is_public": row["is_public"],
+                    "is_privileged": row["is_admin"],
+                }
+
+            row = self._simple_select_one_onecol_txn(
+                txn,
+                table="group_invites",
+                keyvalues={
+                    "group_id": group_id,
+                    "user_id": user_id,
+                },
+                retcol="user_id",
+                allow_none=True,
+            )
+
+            if row:
+                return {
+                    "membership": "invite",
+                }
+
+            return {}
+
+        return self.runInteraction(
+            "get_users_membership_info_in_group", _get_users_membership_in_group_txn,
+        )
+
     def add_user_to_group(self, group_id, user_id, is_admin=False, is_public=True,
                           local_attestation=None, remote_attestation=None):
         """Add a user to the group server.