summary refs log tree commit diff
diff options
context:
space:
mode:
authorLuke Barnard <luke@matrix.org>2017-11-16 16:32:58 +0000
committerLuke Barnard <luke@matrix.org>2017-11-16 16:44:55 +0000
commit97bd18af4ee368a5fe8bf8fb06d0299f6b2c1cfd (patch)
treee96e70c383fbc34b0cb6a12bb6208c6268816235
parentMerge pull request #2661 from matrix-org/rav/statereadstore (diff)
downloadsynapse-97bd18af4ee368a5fe8bf8fb06d0299f6b2c1cfd.tar.xz
Add automagical AS Publicised Group(s)
via registration file "users" namespace:

```YAML
...
namespaces:
  users:
    - exclusive: true
      regex: '.*luke.*'
      group_id: '+all_the_lukes:hsdomain'
...
```

This is part of giving App Services their own groups for matching users. With this, ghost users will be given the appeareance that they are in a group and that they have publicised the fact, but _only_ from the perspective of the `get_publicised_groups_for_user` API.
Diffstat (limited to '')
-rw-r--r--synapse/appservice/__init__.py22
-rw-r--r--synapse/handlers/groups_local.py6
2 files changed, 28 insertions, 0 deletions
diff --git a/synapse/appservice/__init__.py b/synapse/appservice/__init__.py
index b989007314..5c6c724fae 100644
--- a/synapse/appservice/__init__.py
+++ b/synapse/appservice/__init__.py
@@ -81,6 +81,8 @@ class ApplicationService(object):
     # values.
     NS_LIST = [NS_USERS, NS_ALIASES, NS_ROOMS]
 
+    GROUP_ID_REGEX = re.compile('\+.*:.+')
+
     def __init__(self, token, url=None, namespaces=None, hs_token=None,
                  sender=None, id=None, protocols=None, rate_limited=True):
         self.token = token
@@ -125,6 +127,17 @@ class ApplicationService(object):
                     raise ValueError(
                         "Expected bool for 'exclusive' in ns '%s'" % ns
                     )
+                if regex_obj.get("group_id"):
+                    if not isinstance(regex_obj.get("group_id"), str):
+                        raise ValueError(
+                            "Expected string for 'group_id' in ns '%s'" % ns
+                        )
+                    if not ApplicationService.GROUP_ID_REGEX.match(
+                            regex_obj.get("group_id")):
+                        raise ValueError(
+                            "Expected valid group ID for 'group_id' in ns '%s'" % ns
+                        )
+
                 regex = regex_obj.get("regex")
                 if isinstance(regex, basestring):
                     regex_obj["regex"] = re.compile(regex)  # Pre-compile regex
@@ -251,6 +264,15 @@ class ApplicationService(object):
             if regex_obj["exclusive"]
         ]
 
+    def get_groups_for_user(self, user_id):
+        """Get the groups that this user is associated with by this AS
+        """
+        return [
+            regex_obj["group_id"]
+            for regex_obj in self.namespaces[ApplicationService.NS_USERS]
+            if "group_id" in regex_obj and regex_obj["regex"].match(user_id)
+        ]
+
     def is_rate_limited(self):
         return self.rate_limited
 
diff --git a/synapse/handlers/groups_local.py b/synapse/handlers/groups_local.py
index da00aeb0f4..5cc4b86afd 100644
--- a/synapse/handlers/groups_local.py
+++ b/synapse/handlers/groups_local.py
@@ -375,6 +375,12 @@ class GroupsLocalHandler(object):
     def get_publicised_groups_for_user(self, user_id):
         if self.hs.is_mine_id(user_id):
             result = yield self.store.get_publicised_groups_for_user(user_id)
+
+            # 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))
+
             defer.returnValue({"groups": result})
         else:
             result = yield self.transport_client.get_publicised_groups_for_user(