summary refs log tree commit diff
path: root/synapse/storage
diff options
context:
space:
mode:
authorBrendan Abolivier <babolivier@matrix.org>2020-07-28 19:20:55 +0100
committerBrendan Abolivier <babolivier@matrix.org>2020-07-28 19:20:55 +0100
commit9725c59247131d243316ff299e6864098d9bdc58 (patch)
tree797d59129c8d005426ee5db615e0e7d4e698743f /synapse/storage
parentSkip serializing /sync response if client has disconnected (#7927) (diff)
downloadsynapse-9725c59247131d243316ff299e6864098d9bdc58.tar.xz
Implement new experimental push rules with a database hack to enable them
Diffstat (limited to 'synapse/storage')
-rw-r--r--synapse/storage/data_stores/main/push_rule.py35
-rw-r--r--synapse/storage/data_stores/main/schema/delta/58/13new_push_rules_tmp.sql21
2 files changed, 51 insertions, 5 deletions
diff --git a/synapse/storage/data_stores/main/push_rule.py b/synapse/storage/data_stores/main/push_rule.py
index d181488db7..c10da245d2 100644
--- a/synapse/storage/data_stores/main/push_rule.py
+++ b/synapse/storage/data_stores/main/push_rule.py
@@ -39,7 +39,7 @@ from synapse.util.caches.stream_change_cache import StreamChangeCache
 logger = logging.getLogger(__name__)
 
 
-def _load_rules(rawrules, enabled_map):
+def _load_rules(rawrules, enabled_map, use_new_defaults=False):
     ruleslist = []
     for rawrule in rawrules:
         rule = dict(rawrule)
@@ -49,7 +49,7 @@ def _load_rules(rawrules, enabled_map):
         ruleslist.append(rule)
 
     # We're going to be mutating this a lot, so do a deep copy
-    rules = list(list_with_base_rules(ruleslist))
+    rules = list(list_with_base_rules(ruleslist, use_new_defaults))
 
     for i, rule in enumerate(rules):
         rule_id = rule["rule_id"]
@@ -115,7 +115,7 @@ class PushRulesWorkerStore(
         raise NotImplementedError()
 
     @cachedInlineCallbacks(max_entries=5000)
-    def get_push_rules_for_user(self, user_id):
+    def _get_push_rules_for_user(self, user_id, use_new_defaults=False):
         rows = yield self.db.simple_select_list(
             table="push_rules",
             keyvalues={"user_name": user_id},
@@ -134,8 +134,22 @@ class PushRulesWorkerStore(
 
         enabled_map = yield self.get_push_rules_enabled_for_user(user_id)
 
-        rules = _load_rules(rows, enabled_map)
+        rules = _load_rules(rows, enabled_map, use_new_defaults)
+
+        return rules
+
+    @defer.inlineCallbacks
+    def get_push_rules_for_user(self, user_id):
+        # Temporary hack so we can use the new experimental default push rules to some
+        # users without impacting others.
+        use_new_defaults = yield self.db.simple_select_list(
+            table="new_push_rules_users_tmp",
+            keyvalues={"user_id": user_id},
+            retcols=("user_id",),
+            desc="get_user_new_default_push_rules",
+        )
 
+        rules = yield self._get_push_rules_for_user(user_id, bool(use_new_defaults))
         return rules
 
     @cachedInlineCallbacks(max_entries=5000)
@@ -194,7 +208,18 @@ class PushRulesWorkerStore(
         enabled_map_by_user = yield self.bulk_get_push_rules_enabled(user_ids)
 
         for user_id, rules in results.items():
-            results[user_id] = _load_rules(rules, enabled_map_by_user.get(user_id, {}))
+            # Temporary hack so we can use the new experimental default push rules to some
+            # users without impacting others.
+            use_new_defaults = yield self.db.simple_select_list(
+                table="new_push_rules_users_tmp",
+                keyvalues={"user_id": user_id},
+                retcols=("user_id",),
+                desc="get_user_new_default_push_rules",
+            )
+
+            results[user_id] = _load_rules(
+                rules, enabled_map_by_user.get(user_id, {}), bool(use_new_defaults),
+            )
 
         return results
 
diff --git a/synapse/storage/data_stores/main/schema/delta/58/13new_push_rules_tmp.sql b/synapse/storage/data_stores/main/schema/delta/58/13new_push_rules_tmp.sql
new file mode 100644
index 0000000000..b7daf1c67b
--- /dev/null
+++ b/synapse/storage/data_stores/main/schema/delta/58/13new_push_rules_tmp.sql
@@ -0,0 +1,21 @@
+/* Copyright 2020 The Matrix.org Foundation C.I.C
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+-- This is a temporary table in which we store the IDs of the users for which we need to
+-- serve the new experimental default push rules. The purpose of this table is to help
+-- test these new defaults, so it shall be dropped when the experimentation is done.
+CREATE TABLE IF NOT EXISTS new_push_rules_users_tmp (
+    user_id TEXT PRIMARY KEY
+);
\ No newline at end of file