2016-03-03 17:11:59 +01:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import copy
|
2020-12-11 17:43:53 +01:00
|
|
|
from typing import Any, Dict, List, Optional
|
2016-06-01 15:27:07 +02:00
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from synapse.push.rulekinds import PRIORITY_CLASS_INVERSE_MAP, PRIORITY_CLASS_MAP
|
2020-12-11 17:43:53 +01:00
|
|
|
from synapse.types import UserID
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2016-06-01 15:27:07 +02:00
|
|
|
|
2021-10-11 18:42:10 +02:00
|
|
|
def format_push_rules_for_user(
|
|
|
|
user: UserID, ruleslist: List
|
|
|
|
) -> Dict[str, Dict[str, list]]:
|
2016-06-01 15:27:07 +02:00
|
|
|
"""Converts a list of rawrules and a enabled map into nested dictionaries
|
|
|
|
to match the Matrix client-server format for push rules"""
|
|
|
|
|
|
|
|
# We're going to be mutating this a lot, so do a deep copy
|
|
|
|
ruleslist = copy.deepcopy(ruleslist)
|
2016-03-03 17:11:59 +01:00
|
|
|
|
2021-07-15 12:02:43 +02:00
|
|
|
rules: Dict[str, Dict[str, List[Dict[str, Any]]]] = {
|
2020-12-11 17:43:53 +01:00
|
|
|
"global": {},
|
|
|
|
"device": {},
|
2021-07-15 12:02:43 +02:00
|
|
|
}
|
2016-03-03 17:11:59 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
rules["global"] = _add_empty_priority_class_arrays(rules["global"])
|
2016-03-03 17:11:59 +01:00
|
|
|
|
|
|
|
for r in ruleslist:
|
2019-06-20 11:32:02 +02:00
|
|
|
template_name = _priority_class_to_template_name(r["priority_class"])
|
2016-03-03 17:11:59 +01:00
|
|
|
|
|
|
|
# Remove internal stuff.
|
|
|
|
for c in r["conditions"]:
|
2022-03-11 14:45:26 +01:00
|
|
|
c.pop("_cache_key", None)
|
2016-03-03 17:11:59 +01:00
|
|
|
|
|
|
|
pattern_type = c.pop("pattern_type", None)
|
|
|
|
if pattern_type == "user_id":
|
|
|
|
c["pattern"] = user.to_string()
|
|
|
|
elif pattern_type == "user_localpart":
|
|
|
|
c["pattern"] = user.localpart
|
|
|
|
|
2022-05-24 15:23:23 +02:00
|
|
|
sender_type = c.pop("sender_type", None)
|
|
|
|
if sender_type == "user_id":
|
|
|
|
c["sender"] = user.to_string()
|
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
rulearray = rules["global"][template_name]
|
2016-03-03 17:11:59 +01:00
|
|
|
|
|
|
|
template_rule = _rule_to_template(r)
|
|
|
|
if template_rule:
|
2019-06-20 11:32:02 +02:00
|
|
|
if "enabled" in r:
|
|
|
|
template_rule["enabled"] = r["enabled"]
|
2016-03-03 17:11:59 +01:00
|
|
|
else:
|
2019-06-20 11:32:02 +02:00
|
|
|
template_rule["enabled"] = True
|
2016-03-03 17:11:59 +01:00
|
|
|
rulearray.append(template_rule)
|
|
|
|
|
|
|
|
return rules
|
|
|
|
|
|
|
|
|
2020-12-11 17:43:53 +01:00
|
|
|
def _add_empty_priority_class_arrays(d: Dict[str, list]) -> Dict[str, list]:
|
2016-03-03 17:11:59 +01:00
|
|
|
for pc in PRIORITY_CLASS_MAP.keys():
|
|
|
|
d[pc] = []
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2020-12-11 17:43:53 +01:00
|
|
|
def _rule_to_template(rule: Dict[str, Any]) -> Optional[Dict[str, Any]]:
|
2016-03-03 17:11:59 +01:00
|
|
|
unscoped_rule_id = None
|
2019-06-20 11:32:02 +02:00
|
|
|
if "rule_id" in rule:
|
|
|
|
unscoped_rule_id = _rule_id_from_namespaced(rule["rule_id"])
|
2016-03-03 17:11:59 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
template_name = _priority_class_to_template_name(rule["priority_class"])
|
|
|
|
if template_name in ["override", "underride"]:
|
2016-03-03 17:11:59 +01:00
|
|
|
templaterule = {k: rule[k] for k in ["conditions", "actions"]}
|
|
|
|
elif template_name in ["sender", "room"]:
|
2019-06-20 11:32:02 +02:00
|
|
|
templaterule = {"actions": rule["actions"]}
|
|
|
|
unscoped_rule_id = rule["conditions"][0]["pattern"]
|
|
|
|
elif template_name == "content":
|
2016-03-03 17:11:59 +01:00
|
|
|
if len(rule["conditions"]) != 1:
|
|
|
|
return None
|
|
|
|
thecond = rule["conditions"][0]
|
|
|
|
if "pattern" not in thecond:
|
|
|
|
return None
|
2019-06-20 11:32:02 +02:00
|
|
|
templaterule = {"actions": rule["actions"]}
|
2016-03-03 17:11:59 +01:00
|
|
|
templaterule["pattern"] = thecond["pattern"]
|
2020-12-11 17:43:53 +01:00
|
|
|
else:
|
|
|
|
# This should not be reached unless this function is not kept in sync
|
|
|
|
# with PRIORITY_CLASS_INVERSE_MAP.
|
|
|
|
raise ValueError("Unexpected template_name: %s" % (template_name,))
|
2016-03-03 17:11:59 +01:00
|
|
|
|
|
|
|
if unscoped_rule_id:
|
2019-06-20 11:32:02 +02:00
|
|
|
templaterule["rule_id"] = unscoped_rule_id
|
|
|
|
if "default" in rule:
|
|
|
|
templaterule["default"] = rule["default"]
|
2016-03-03 17:11:59 +01:00
|
|
|
return templaterule
|
|
|
|
|
|
|
|
|
2020-12-11 17:43:53 +01:00
|
|
|
def _rule_id_from_namespaced(in_rule_id: str) -> str:
|
2019-06-20 11:32:02 +02:00
|
|
|
return in_rule_id.split("/")[-1]
|
2016-03-03 17:11:59 +01:00
|
|
|
|
|
|
|
|
2020-12-11 17:43:53 +01:00
|
|
|
def _priority_class_to_template_name(pc: int) -> str:
|
2016-03-03 17:11:59 +01:00
|
|
|
return PRIORITY_CLASS_INVERSE_MAP[pc]
|