summary refs log tree commit diff
path: root/synapse/rest
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/rest')
-rw-r--r--synapse/rest/client/v1/push_rule.py36
-rw-r--r--synapse/rest/client/v1/pusher.py4
-rw-r--r--synapse/rest/media/v1/identicon_resource.py51
-rw-r--r--synapse/rest/media/v1/media_repository.py2
4 files changed, 73 insertions, 20 deletions
diff --git a/synapse/rest/client/v1/push_rule.py b/synapse/rest/client/v1/push_rule.py
index faa7919fbb..5582f33c8e 100644
--- a/synapse/rest/client/v1/push_rule.py
+++ b/synapse/rest/client/v1/push_rule.py
@@ -73,7 +73,7 @@ class PushRuleRestServlet(ClientV1RestServlet):
             'rule_id': rule_id
         }
         if device:
-            spec['device'] = device
+            spec['profile_tag'] = device
         return spec
 
     def rule_tuple_from_request_object(self, rule_template, rule_id, req_obj, device=None):
@@ -112,7 +112,7 @@ class PushRuleRestServlet(ClientV1RestServlet):
         if device:
             conditions.append({
                 'kind': 'device',
-                'instance_handle': device
+                'profile_tag': device
             })
 
         if 'actions' not in req_obj:
@@ -188,15 +188,15 @@ class PushRuleRestServlet(ClientV1RestServlet):
 
         user, _ = yield self.auth.get_user_by_req(request)
 
-        if 'device' in spec:
+        if 'profile_tag' in spec:
             rules = yield self.hs.get_datastore().get_push_rules_for_user_name(
                 user.to_string()
             )
 
             for r in rules:
                 conditions = json.loads(r['conditions'])
-                ih = _instance_handle_from_conditions(conditions)
-                if ih == spec['device'] and r['priority_class'] == priority_class:
+                pt = _profile_tag_from_conditions(conditions)
+                if pt == spec['profile_tag'] and r['priority_class'] == priority_class:
                     yield self.hs.get_datastore().delete_push_rule(
                         user.to_string(), spec['rule_id']
                     )
@@ -239,19 +239,19 @@ class PushRuleRestServlet(ClientV1RestServlet):
 
             if r['priority_class'] > PushRuleRestServlet.PRIORITY_CLASS_MAP['override']:
                 # per-device rule
-                instance_handle = _instance_handle_from_conditions(r["conditions"])
+                profile_tag = _profile_tag_from_conditions(r["conditions"])
                 r = _strip_device_condition(r)
-                if not instance_handle:
+                if not profile_tag:
                     continue
-                if instance_handle not in rules['device']:
-                    rules['device'][instance_handle] = {}
-                    rules['device'][instance_handle] = (
+                if profile_tag not in rules['device']:
+                    rules['device'][profile_tag] = {}
+                    rules['device'][profile_tag] = (
                         _add_empty_priority_class_arrays(
-                            rules['device'][instance_handle]
+                            rules['device'][profile_tag]
                         )
                     )
 
-                rulearray = rules['device'][instance_handle][template_name]
+                rulearray = rules['device'][profile_tag][template_name]
             else:
                 rulearray = rules['global'][template_name]
 
@@ -282,13 +282,13 @@ class PushRuleRestServlet(ClientV1RestServlet):
             if path[0] == '':
                 defer.returnValue((200, rules['device']))
 
-            instance_handle = path[0]
+            profile_tag = path[0]
             path = path[1:]
-            if instance_handle not in rules['device']:
+            if profile_tag not in rules['device']:
                 ret = {}
                 ret = _add_empty_priority_class_arrays(ret)
                 defer.returnValue((200, ret))
-            ruleset = rules['device'][instance_handle]
+            ruleset = rules['device'][profile_tag]
             result = _filter_ruleset_with_path(ruleset, path)
             defer.returnValue((200, result))
         else:
@@ -304,14 +304,14 @@ def _add_empty_priority_class_arrays(d):
     return d
 
 
-def _instance_handle_from_conditions(conditions):
+def _profile_tag_from_conditions(conditions):
     """
-    Given a list of conditions, return the instance handle of the
+    Given a list of conditions, return the profile tag of the
     device rule if there is one
     """
     for c in conditions:
         if c['kind'] == 'device':
-            return c['instance_handle']
+            return c['profile_tag']
     return None
 
 
diff --git a/synapse/rest/client/v1/pusher.py b/synapse/rest/client/v1/pusher.py
index 353a4a6589..e10d2576d2 100644
--- a/synapse/rest/client/v1/pusher.py
+++ b/synapse/rest/client/v1/pusher.py
@@ -41,7 +41,7 @@ class PusherRestServlet(ClientV1RestServlet):
             )
             defer.returnValue((200, {}))
 
-        reqd = ['instance_handle', 'kind', 'app_id', 'app_display_name',
+        reqd = ['profile_tag', 'kind', 'app_id', 'app_display_name',
                 'device_display_name', 'pushkey', 'lang', 'data']
         missing = []
         for i in reqd:
@@ -54,7 +54,7 @@ class PusherRestServlet(ClientV1RestServlet):
         try:
             yield pusher_pool.add_pusher(
                 user_name=user.to_string(),
-                instance_handle=content['instance_handle'],
+                profile_tag=content['profile_tag'],
                 kind=content['kind'],
                 app_id=content['app_id'],
                 app_display_name=content['app_display_name'],
diff --git a/synapse/rest/media/v1/identicon_resource.py b/synapse/rest/media/v1/identicon_resource.py
new file mode 100644
index 0000000000..912856386a
--- /dev/null
+++ b/synapse/rest/media/v1/identicon_resource.py
@@ -0,0 +1,51 @@
+from pydenticon import Generator
+from twisted.web.resource import Resource
+
+FOREGROUND = [
+    "rgb(45,79,255)",
+    "rgb(254,180,44)",
+    "rgb(226,121,234)",
+    "rgb(30,179,253)",
+    "rgb(232,77,65)",
+    "rgb(49,203,115)",
+    "rgb(141,69,170)"
+]
+
+BACKGROUND = "rgb(224,224,224)"
+SIZE = 5
+
+
+class IdenticonResource(Resource):
+    isLeaf = True
+
+    def __init__(self):
+        Resource.__init__(self)
+        self.generator = Generator(
+            SIZE, SIZE, foreground=FOREGROUND, background=BACKGROUND,
+        )
+
+    def generate_identicon(self, name, width, height):
+        v_padding = width % SIZE
+        h_padding = height % SIZE
+        top_padding = v_padding // 2
+        left_padding = h_padding // 2
+        bottom_padding = v_padding - top_padding
+        right_padding = h_padding - left_padding
+        width -= v_padding
+        height -= h_padding
+        padding = (top_padding, bottom_padding, left_padding, right_padding)
+        identicon = self.generator.generate(
+            name, width, height, padding=padding
+        )
+        return identicon
+
+    def render_GET(self, request):
+        name = "/".join(request.postpath)
+        width = int(request.args.get("width", [96])[0])
+        height = int(request.args.get("height", [96])[0])
+        identicon_bytes = self.generate_identicon(name, width, height)
+        request.setHeader(b"Content-Type", b"image/png")
+        request.setHeader(
+            b"Cache-Control", b"public,max-age=86400,s-maxage=86400"
+        )
+        return identicon_bytes
diff --git a/synapse/rest/media/v1/media_repository.py b/synapse/rest/media/v1/media_repository.py
index 461cc001f1..61ed90f39f 100644
--- a/synapse/rest/media/v1/media_repository.py
+++ b/synapse/rest/media/v1/media_repository.py
@@ -16,6 +16,7 @@
 from .upload_resource import UploadResource
 from .download_resource import DownloadResource
 from .thumbnail_resource import ThumbnailResource
+from .identicon_resource import IdenticonResource
 from .filepath import MediaFilePaths
 
 from twisted.web.resource import Resource
@@ -75,3 +76,4 @@ class MediaRepositoryResource(Resource):
         self.putChild("upload", UploadResource(hs, filepaths))
         self.putChild("download", DownloadResource(hs, filepaths))
         self.putChild("thumbnail", ThumbnailResource(hs, filepaths))
+        self.putChild("identicon", IdenticonResource())