summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2015-01-30 15:54:29 +0000
committerDavid Baker <dave@matrix.org>2015-01-30 15:54:51 +0000
commitb4b892f4a3f9194289f57ed2d166eca26da41594 (patch)
treed9274d773cbb5fb6a110651d57e1e598f9dc65c3 /synapse
parentMerge pull request #41 from matrix-org/client_v2_sync (diff)
downloadsynapse-b4b892f4a3f9194289f57ed2d166eca26da41594.tar.xz
Spit out server default rules too.
Diffstat (limited to 'synapse')
-rw-r--r--synapse/push/baserules.py11
-rw-r--r--synapse/rest/client/v1/push_rule.py16
2 files changed, 17 insertions, 10 deletions
diff --git a/synapse/push/baserules.py b/synapse/push/baserules.py
index bd162baade..382de118e0 100644
--- a/synapse/push/baserules.py
+++ b/synapse/push/baserules.py
@@ -1,9 +1,5 @@
 def make_base_rules(user_name):
-    """
-    Nominally we reserve priority class 0 for these rules, although
-    in practice we just append them to the end so we don't actually need it.
-    """
-    return [
+    rules = [
         {
             'conditions': [
                 {
@@ -46,4 +42,7 @@ def make_base_rules(user_name):
                 }
             ]
         }
-    ]
\ No newline at end of file
+    ]
+    for r in rules:
+        r['priority_class'] = 0
+    return rules
\ No newline at end of file
diff --git a/synapse/rest/client/v1/push_rule.py b/synapse/rest/client/v1/push_rule.py
index 61e3bc8236..faa7919fbb 100644
--- a/synapse/rest/client/v1/push_rule.py
+++ b/synapse/rest/client/v1/push_rule.py
@@ -19,6 +19,7 @@ from synapse.api.errors import SynapseError, Codes, UnrecognizedRequestError, No
     StoreError
 from .base import ClientV1RestServlet, client_path_pattern
 from synapse.storage.push_rule import InconsistentRuleException, RuleNotFoundException
+import synapse.push.baserules as baserules
 
 import json
 
@@ -26,6 +27,7 @@ import json
 class PushRuleRestServlet(ClientV1RestServlet):
     PATTERN = client_path_pattern("/pushrules/.*$")
     PRIORITY_CLASS_MAP = {
+        'default': 0,
         'underride': 1,
         'sender': 2,
         'room': 3,
@@ -137,6 +139,9 @@ class PushRuleRestServlet(ClientV1RestServlet):
 
         user, _ = yield self.auth.get_user_by_req(request)
 
+        if spec['template'] == 'default':
+            raise SynapseError(403, "The default rules are immutable.")
+
         content = _parse_json(request)
 
         try:
@@ -218,6 +223,10 @@ class PushRuleRestServlet(ClientV1RestServlet):
         # to send which means doing unnecessary work sometimes but is
         # is probably not going to make a whole lot of difference
         rawrules = yield self.hs.get_datastore().get_push_rules_for_user_name(user.to_string())
+        for r in rawrules:
+            r["conditions"] = json.loads(r["conditions"])
+            r["actions"] = json.loads(r["actions"])
+        rawrules.extend(baserules.make_base_rules(user.to_string()))
 
         rules = {'global': {}, 'device': {}}
 
@@ -226,9 +235,6 @@ class PushRuleRestServlet(ClientV1RestServlet):
         for r in rawrules:
             rulearray = None
 
-            r["conditions"] = json.loads(r["conditions"])
-            r["actions"] = json.loads(r["actions"])
-
             template_name = _priority_class_to_template_name(r['priority_class'])
 
             if r['priority_class'] > PushRuleRestServlet.PRIORITY_CLASS_MAP['override']:
@@ -356,7 +362,9 @@ def _priority_class_to_template_name(pc):
 
 def _rule_to_template(rule):
     template_name = _priority_class_to_template_name(rule['priority_class'])
-    if template_name in ['override', 'underride']:
+    if template_name in ['default']:
+        return {k: rule[k] for k in ["conditions", "actions"]}
+    elif template_name in ['override', 'underride']:
         return {k: rule[k] for k in ["rule_id", "conditions", "actions"]}
     elif template_name in ["sender", "room"]:
         return {k: rule[k] for k in ["rule_id", "actions"]}