clientformat.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import copy
  16. from synapse.push.rulekinds import PRIORITY_CLASS_INVERSE_MAP, PRIORITY_CLASS_MAP
  17. def format_push_rules_for_user(user, ruleslist):
  18. """Converts a list of rawrules and a enabled map into nested dictionaries
  19. to match the Matrix client-server format for push rules"""
  20. # We're going to be mutating this a lot, so do a deep copy
  21. ruleslist = copy.deepcopy(ruleslist)
  22. rules = {"global": {}, "device": {}}
  23. rules["global"] = _add_empty_priority_class_arrays(rules["global"])
  24. for r in ruleslist:
  25. rulearray = None
  26. template_name = _priority_class_to_template_name(r["priority_class"])
  27. # Remove internal stuff.
  28. for c in r["conditions"]:
  29. c.pop("_id", None)
  30. pattern_type = c.pop("pattern_type", None)
  31. if pattern_type == "user_id":
  32. c["pattern"] = user.to_string()
  33. elif pattern_type == "user_localpart":
  34. c["pattern"] = user.localpart
  35. rulearray = rules["global"][template_name]
  36. template_rule = _rule_to_template(r)
  37. if template_rule:
  38. if "enabled" in r:
  39. template_rule["enabled"] = r["enabled"]
  40. else:
  41. template_rule["enabled"] = True
  42. rulearray.append(template_rule)
  43. return rules
  44. def _add_empty_priority_class_arrays(d):
  45. for pc in PRIORITY_CLASS_MAP.keys():
  46. d[pc] = []
  47. return d
  48. def _rule_to_template(rule):
  49. unscoped_rule_id = None
  50. if "rule_id" in rule:
  51. unscoped_rule_id = _rule_id_from_namespaced(rule["rule_id"])
  52. template_name = _priority_class_to_template_name(rule["priority_class"])
  53. if template_name in ["override", "underride"]:
  54. templaterule = {k: rule[k] for k in ["conditions", "actions"]}
  55. elif template_name in ["sender", "room"]:
  56. templaterule = {"actions": rule["actions"]}
  57. unscoped_rule_id = rule["conditions"][0]["pattern"]
  58. elif template_name == "content":
  59. if len(rule["conditions"]) != 1:
  60. return None
  61. thecond = rule["conditions"][0]
  62. if "pattern" not in thecond:
  63. return None
  64. templaterule = {"actions": rule["actions"]}
  65. templaterule["pattern"] = thecond["pattern"]
  66. if unscoped_rule_id:
  67. templaterule["rule_id"] = unscoped_rule_id
  68. if "default" in rule:
  69. templaterule["default"] = rule["default"]
  70. return templaterule
  71. def _rule_id_from_namespaced(in_rule_id):
  72. return in_rule_id.split("/")[-1]
  73. def _priority_class_to_template_name(pc):
  74. return PRIORITY_CLASS_INVERSE_MAP[pc]