clientformat.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. from synapse.push.rulekinds import (
  16. PRIORITY_CLASS_MAP, PRIORITY_CLASS_INVERSE_MAP
  17. )
  18. import copy
  19. def format_push_rules_for_user(user, ruleslist):
  20. """Converts a list of rawrules and a enabled map into nested dictionaries
  21. to match the Matrix client-server format for push rules"""
  22. # We're going to be mutating this a lot, so do a deep copy
  23. ruleslist = copy.deepcopy(ruleslist)
  24. rules = {'global': {}, 'device': {}}
  25. rules['global'] = _add_empty_priority_class_arrays(rules['global'])
  26. for r in ruleslist:
  27. rulearray = None
  28. template_name = _priority_class_to_template_name(r['priority_class'])
  29. # Remove internal stuff.
  30. for c in r["conditions"]:
  31. c.pop("_id", None)
  32. pattern_type = c.pop("pattern_type", None)
  33. if pattern_type == "user_id":
  34. c["pattern"] = user.to_string()
  35. elif pattern_type == "user_localpart":
  36. c["pattern"] = user.localpart
  37. rulearray = rules['global'][template_name]
  38. template_rule = _rule_to_template(r)
  39. if template_rule:
  40. if 'enabled' in r:
  41. template_rule['enabled'] = r['enabled']
  42. else:
  43. template_rule['enabled'] = True
  44. rulearray.append(template_rule)
  45. return rules
  46. def _add_empty_priority_class_arrays(d):
  47. for pc in PRIORITY_CLASS_MAP.keys():
  48. d[pc] = []
  49. return d
  50. def _rule_to_template(rule):
  51. unscoped_rule_id = None
  52. if 'rule_id' in rule:
  53. unscoped_rule_id = _rule_id_from_namespaced(rule['rule_id'])
  54. template_name = _priority_class_to_template_name(rule['priority_class'])
  55. if template_name in ['override', 'underride']:
  56. templaterule = {k: rule[k] for k in ["conditions", "actions"]}
  57. elif template_name in ["sender", "room"]:
  58. templaterule = {'actions': rule['actions']}
  59. unscoped_rule_id = rule['conditions'][0]['pattern']
  60. elif template_name == 'content':
  61. if len(rule["conditions"]) != 1:
  62. return None
  63. thecond = rule["conditions"][0]
  64. if "pattern" not in thecond:
  65. return None
  66. templaterule = {'actions': rule['actions']}
  67. templaterule["pattern"] = thecond["pattern"]
  68. if unscoped_rule_id:
  69. templaterule['rule_id'] = unscoped_rule_id
  70. if 'default' in rule:
  71. templaterule['default'] = rule['default']
  72. return templaterule
  73. def _rule_id_from_namespaced(in_rule_id):
  74. return in_rule_id.split('/')[-1]
  75. def _priority_class_to_template_name(pc):
  76. return PRIORITY_CLASS_INVERSE_MAP[pc]