clientformat.py 3.6 KB

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