2016-03-03 11:11:59 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2016-03-03 11:11:59 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
import copy
|
2020-12-11 11:43:53 -05:00
|
|
|
from typing import Any, Dict, List, Optional
|
2016-06-01 09:27:07 -04:00
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.push.rulekinds import PRIORITY_CLASS_INVERSE_MAP, PRIORITY_CLASS_MAP
|
2022-09-20 07:10:31 -04:00
|
|
|
from synapse.synapse_rust.push import FilteredPushRules, PushRule
|
2020-12-11 11:43:53 -05:00
|
|
|
from synapse.types import UserID
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2016-06-01 09:27:07 -04:00
|
|
|
|
2021-10-11 12:42:10 -04:00
|
|
|
def format_push_rules_for_user(
|
2022-08-16 07:22:17 -04:00
|
|
|
user: UserID, ruleslist: FilteredPushRules
|
2023-05-09 10:34:10 -04:00
|
|
|
) -> Dict[str, Dict[str, List[Dict[str, Any]]]]:
|
2016-06-01 09:27:07 -04:00
|
|
|
"""Converts a list of rawrules and a enabled map into nested dictionaries
|
|
|
|
to match the Matrix client-server format for push rules"""
|
|
|
|
|
2023-01-09 12:17:24 -05:00
|
|
|
rules: Dict[str, Dict[str, List[Dict[str, Any]]]] = {"global": {}}
|
2016-03-03 11:11:59 -05:00
|
|
|
|
2019-06-20 05:32:02 -04:00
|
|
|
rules["global"] = _add_empty_priority_class_arrays(rules["global"])
|
2016-03-03 11:11:59 -05:00
|
|
|
|
2022-09-20 07:10:31 -04:00
|
|
|
for r, enabled in ruleslist.rules():
|
2022-08-16 07:22:17 -04:00
|
|
|
template_name = _priority_class_to_template_name(r.priority_class)
|
|
|
|
|
|
|
|
rulearray = rules["global"][template_name]
|
|
|
|
|
|
|
|
template_rule = _rule_to_template(r)
|
|
|
|
if not template_rule:
|
|
|
|
continue
|
|
|
|
|
|
|
|
rulearray.append(template_rule)
|
|
|
|
|
2023-06-16 08:17:02 -04:00
|
|
|
_convert_type_to_value(template_rule, user)
|
2022-11-15 10:29:30 -05:00
|
|
|
|
2022-08-16 07:22:17 -04:00
|
|
|
template_rule["enabled"] = enabled
|
|
|
|
|
|
|
|
if "conditions" not in template_rule:
|
|
|
|
# Not all formatted rules have explicit conditions, e.g. "room"
|
|
|
|
# rules omit them as they can be derived from the kind and rule ID.
|
|
|
|
#
|
|
|
|
# If the formatted rule has no conditions then we can skip the
|
|
|
|
# formatting of conditions.
|
|
|
|
continue
|
2016-03-03 11:11:59 -05:00
|
|
|
|
|
|
|
# Remove internal stuff.
|
2022-08-16 07:22:17 -04:00
|
|
|
template_rule["conditions"] = copy.deepcopy(template_rule["conditions"])
|
|
|
|
for c in template_rule["conditions"]:
|
2022-03-11 08:45:26 -05:00
|
|
|
c.pop("_cache_key", None)
|
2016-03-03 11:11:59 -05:00
|
|
|
|
2023-06-16 08:17:02 -04:00
|
|
|
_convert_type_to_value(c, user)
|
2022-05-24 09:23:23 -04:00
|
|
|
|
2016-03-03 11:11:59 -05:00
|
|
|
return rules
|
|
|
|
|
|
|
|
|
2023-06-16 08:17:02 -04:00
|
|
|
def _convert_type_to_value(rule_or_cond: Dict[str, Any], user: UserID) -> None:
|
|
|
|
for type_key in ("pattern", "value"):
|
|
|
|
type_value = rule_or_cond.pop(f"{type_key}_type", None)
|
|
|
|
if type_value == "user_id":
|
|
|
|
rule_or_cond[type_key] = user.to_string()
|
|
|
|
elif type_value == "user_localpart":
|
|
|
|
rule_or_cond[type_key] = user.localpart
|
|
|
|
|
|
|
|
|
2020-12-11 11:43:53 -05:00
|
|
|
def _add_empty_priority_class_arrays(d: Dict[str, list]) -> Dict[str, list]:
|
2016-03-03 11:11:59 -05:00
|
|
|
for pc in PRIORITY_CLASS_MAP.keys():
|
|
|
|
d[pc] = []
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
2022-08-16 07:22:17 -04:00
|
|
|
def _rule_to_template(rule: PushRule) -> Optional[Dict[str, Any]]:
|
|
|
|
templaterule: Dict[str, Any]
|
|
|
|
|
|
|
|
unscoped_rule_id = _rule_id_from_namespaced(rule.rule_id)
|
2016-03-03 11:11:59 -05:00
|
|
|
|
2022-08-16 07:22:17 -04:00
|
|
|
template_name = _priority_class_to_template_name(rule.priority_class)
|
2019-06-20 05:32:02 -04:00
|
|
|
if template_name in ["override", "underride"]:
|
2022-08-16 07:22:17 -04:00
|
|
|
templaterule = {"conditions": rule.conditions, "actions": rule.actions}
|
2016-03-03 11:11:59 -05:00
|
|
|
elif template_name in ["sender", "room"]:
|
2022-08-16 07:22:17 -04:00
|
|
|
templaterule = {"actions": rule.actions}
|
|
|
|
unscoped_rule_id = rule.conditions[0]["pattern"]
|
2019-06-20 05:32:02 -04:00
|
|
|
elif template_name == "content":
|
2022-08-16 07:22:17 -04:00
|
|
|
if len(rule.conditions) != 1:
|
2016-03-03 11:11:59 -05:00
|
|
|
return None
|
2022-08-16 07:22:17 -04:00
|
|
|
thecond = rule.conditions[0]
|
2022-11-15 10:29:30 -05:00
|
|
|
|
2022-08-16 07:22:17 -04:00
|
|
|
templaterule = {"actions": rule.actions}
|
2022-11-15 10:29:30 -05:00
|
|
|
if "pattern" in thecond:
|
|
|
|
templaterule["pattern"] = thecond["pattern"]
|
|
|
|
elif "pattern_type" in thecond:
|
|
|
|
templaterule["pattern_type"] = thecond["pattern_type"]
|
|
|
|
else:
|
|
|
|
return None
|
2020-12-11 11:43:53 -05: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 11:11:59 -05:00
|
|
|
|
2022-09-29 06:57:00 -04:00
|
|
|
templaterule["rule_id"] = unscoped_rule_id
|
|
|
|
templaterule["default"] = rule.default
|
2016-03-03 11:11:59 -05:00
|
|
|
return templaterule
|
|
|
|
|
|
|
|
|
2020-12-11 11:43:53 -05:00
|
|
|
def _rule_id_from_namespaced(in_rule_id: str) -> str:
|
2019-06-20 05:32:02 -04:00
|
|
|
return in_rule_id.split("/")[-1]
|
2016-03-03 11:11:59 -05:00
|
|
|
|
|
|
|
|
2020-12-11 11:43:53 -05:00
|
|
|
def _priority_class_to_template_name(pc: int) -> str:
|
2016-03-03 11:11:59 -05:00
|
|
|
return PRIORITY_CLASS_INVERSE_MAP[pc]
|