push_rule.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-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 twisted.internet import defer
  16. from synapse.api.errors import (
  17. NotFoundError,
  18. StoreError,
  19. SynapseError,
  20. UnrecognizedRequestError,
  21. )
  22. from synapse.http.servlet import (
  23. RestServlet,
  24. parse_json_value_from_request,
  25. parse_string,
  26. )
  27. from synapse.push.baserules import BASE_RULE_IDS
  28. from synapse.push.clientformat import format_push_rules_for_user
  29. from synapse.push.rulekinds import PRIORITY_CLASS_MAP
  30. from synapse.rest.client.v2_alpha._base import client_patterns
  31. from synapse.storage.push_rule import InconsistentRuleException, RuleNotFoundException
  32. class PushRuleRestServlet(RestServlet):
  33. PATTERNS = client_patterns("/(?P<path>pushrules/.*)$", v1=True)
  34. SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR = (
  35. "Unrecognised request: You probably wanted a trailing slash"
  36. )
  37. def __init__(self, hs):
  38. super(PushRuleRestServlet, self).__init__()
  39. self.auth = hs.get_auth()
  40. self.store = hs.get_datastore()
  41. self.notifier = hs.get_notifier()
  42. self._is_worker = hs.config.worker_app is not None
  43. @defer.inlineCallbacks
  44. def on_PUT(self, request, path):
  45. if self._is_worker:
  46. raise Exception("Cannot handle PUT /push_rules on worker")
  47. spec = _rule_spec_from_path([x for x in path.split("/")])
  48. try:
  49. priority_class = _priority_class_from_spec(spec)
  50. except InvalidRuleException as e:
  51. raise SynapseError(400, str(e))
  52. requester = yield self.auth.get_user_by_req(request)
  53. if "/" in spec["rule_id"] or "\\" in spec["rule_id"]:
  54. raise SynapseError(400, "rule_id may not contain slashes")
  55. content = parse_json_value_from_request(request)
  56. user_id = requester.user.to_string()
  57. if "attr" in spec:
  58. yield self.set_rule_attr(user_id, spec, content)
  59. self.notify_user(user_id)
  60. return (200, {})
  61. if spec["rule_id"].startswith("."):
  62. # Rule ids starting with '.' are reserved for server default rules.
  63. raise SynapseError(400, "cannot add new rule_ids that start with '.'")
  64. try:
  65. (conditions, actions) = _rule_tuple_from_request_object(
  66. spec["template"], spec["rule_id"], content
  67. )
  68. except InvalidRuleException as e:
  69. raise SynapseError(400, str(e))
  70. before = parse_string(request, "before")
  71. if before:
  72. before = _namespaced_rule_id(spec, before)
  73. after = parse_string(request, "after")
  74. if after:
  75. after = _namespaced_rule_id(spec, after)
  76. try:
  77. yield self.store.add_push_rule(
  78. user_id=user_id,
  79. rule_id=_namespaced_rule_id_from_spec(spec),
  80. priority_class=priority_class,
  81. conditions=conditions,
  82. actions=actions,
  83. before=before,
  84. after=after,
  85. )
  86. self.notify_user(user_id)
  87. except InconsistentRuleException as e:
  88. raise SynapseError(400, str(e))
  89. except RuleNotFoundException as e:
  90. raise SynapseError(400, str(e))
  91. return (200, {})
  92. @defer.inlineCallbacks
  93. def on_DELETE(self, request, path):
  94. if self._is_worker:
  95. raise Exception("Cannot handle DELETE /push_rules on worker")
  96. spec = _rule_spec_from_path([x for x in path.split("/")])
  97. requester = yield self.auth.get_user_by_req(request)
  98. user_id = requester.user.to_string()
  99. namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
  100. try:
  101. yield self.store.delete_push_rule(user_id, namespaced_rule_id)
  102. self.notify_user(user_id)
  103. return (200, {})
  104. except StoreError as e:
  105. if e.code == 404:
  106. raise NotFoundError()
  107. else:
  108. raise
  109. @defer.inlineCallbacks
  110. def on_GET(self, request, path):
  111. requester = yield self.auth.get_user_by_req(request)
  112. user_id = requester.user.to_string()
  113. # we build up the full structure and then decide which bits of it
  114. # to send which means doing unnecessary work sometimes but is
  115. # is probably not going to make a whole lot of difference
  116. rules = yield self.store.get_push_rules_for_user(user_id)
  117. rules = format_push_rules_for_user(requester.user, rules)
  118. path = [x for x in path.split("/")][1:]
  119. if path == []:
  120. # we're a reference impl: pedantry is our job.
  121. raise UnrecognizedRequestError(
  122. PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
  123. )
  124. if path[0] == "":
  125. return (200, rules)
  126. elif path[0] == "global":
  127. result = _filter_ruleset_with_path(rules["global"], path[1:])
  128. return (200, result)
  129. else:
  130. raise UnrecognizedRequestError()
  131. def on_OPTIONS(self, request, path):
  132. return 200, {}
  133. def notify_user(self, user_id):
  134. stream_id, _ = self.store.get_push_rules_stream_token()
  135. self.notifier.on_new_event("push_rules_key", stream_id, users=[user_id])
  136. def set_rule_attr(self, user_id, spec, val):
  137. if spec["attr"] == "enabled":
  138. if isinstance(val, dict) and "enabled" in val:
  139. val = val["enabled"]
  140. if not isinstance(val, bool):
  141. # Legacy fallback
  142. # This should *actually* take a dict, but many clients pass
  143. # bools directly, so let's not break them.
  144. raise SynapseError(400, "Value for 'enabled' must be boolean")
  145. namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
  146. return self.store.set_push_rule_enabled(user_id, namespaced_rule_id, val)
  147. elif spec["attr"] == "actions":
  148. actions = val.get("actions")
  149. _check_actions(actions)
  150. namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
  151. rule_id = spec["rule_id"]
  152. is_default_rule = rule_id.startswith(".")
  153. if is_default_rule:
  154. if namespaced_rule_id not in BASE_RULE_IDS:
  155. raise SynapseError(404, "Unknown rule %r" % (namespaced_rule_id,))
  156. return self.store.set_push_rule_actions(
  157. user_id, namespaced_rule_id, actions, is_default_rule
  158. )
  159. else:
  160. raise UnrecognizedRequestError()
  161. def _rule_spec_from_path(path):
  162. """Turn a sequence of path components into a rule spec
  163. Args:
  164. path (sequence[unicode]): the URL path components.
  165. Returns:
  166. dict: rule spec dict, containing scope/template/rule_id entries,
  167. and possibly attr.
  168. Raises:
  169. UnrecognizedRequestError if the path components cannot be parsed.
  170. """
  171. if len(path) < 2:
  172. raise UnrecognizedRequestError()
  173. if path[0] != "pushrules":
  174. raise UnrecognizedRequestError()
  175. scope = path[1]
  176. path = path[2:]
  177. if scope != "global":
  178. raise UnrecognizedRequestError()
  179. if len(path) == 0:
  180. raise UnrecognizedRequestError()
  181. template = path[0]
  182. path = path[1:]
  183. if len(path) == 0 or len(path[0]) == 0:
  184. raise UnrecognizedRequestError()
  185. rule_id = path[0]
  186. spec = {"scope": scope, "template": template, "rule_id": rule_id}
  187. path = path[1:]
  188. if len(path) > 0 and len(path[0]) > 0:
  189. spec["attr"] = path[0]
  190. return spec
  191. def _rule_tuple_from_request_object(rule_template, rule_id, req_obj):
  192. if rule_template in ["override", "underride"]:
  193. if "conditions" not in req_obj:
  194. raise InvalidRuleException("Missing 'conditions'")
  195. conditions = req_obj["conditions"]
  196. for c in conditions:
  197. if "kind" not in c:
  198. raise InvalidRuleException("Condition without 'kind'")
  199. elif rule_template == "room":
  200. conditions = [{"kind": "event_match", "key": "room_id", "pattern": rule_id}]
  201. elif rule_template == "sender":
  202. conditions = [{"kind": "event_match", "key": "user_id", "pattern": rule_id}]
  203. elif rule_template == "content":
  204. if "pattern" not in req_obj:
  205. raise InvalidRuleException("Content rule missing 'pattern'")
  206. pat = req_obj["pattern"]
  207. conditions = [{"kind": "event_match", "key": "content.body", "pattern": pat}]
  208. else:
  209. raise InvalidRuleException("Unknown rule template: %s" % (rule_template,))
  210. if "actions" not in req_obj:
  211. raise InvalidRuleException("No actions found")
  212. actions = req_obj["actions"]
  213. _check_actions(actions)
  214. return conditions, actions
  215. def _check_actions(actions):
  216. if not isinstance(actions, list):
  217. raise InvalidRuleException("No actions found")
  218. for a in actions:
  219. if a in ["notify", "dont_notify", "coalesce"]:
  220. pass
  221. elif isinstance(a, dict) and "set_tweak" in a:
  222. pass
  223. else:
  224. raise InvalidRuleException("Unrecognised action")
  225. def _filter_ruleset_with_path(ruleset, path):
  226. if path == []:
  227. raise UnrecognizedRequestError(
  228. PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
  229. )
  230. if path[0] == "":
  231. return ruleset
  232. template_kind = path[0]
  233. if template_kind not in ruleset:
  234. raise UnrecognizedRequestError()
  235. path = path[1:]
  236. if path == []:
  237. raise UnrecognizedRequestError(
  238. PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
  239. )
  240. if path[0] == "":
  241. return ruleset[template_kind]
  242. rule_id = path[0]
  243. the_rule = None
  244. for r in ruleset[template_kind]:
  245. if r["rule_id"] == rule_id:
  246. the_rule = r
  247. if the_rule is None:
  248. raise NotFoundError
  249. path = path[1:]
  250. if len(path) == 0:
  251. return the_rule
  252. attr = path[0]
  253. if attr in the_rule:
  254. # Make sure we return a JSON object as the attribute may be a
  255. # JSON value.
  256. return {attr: the_rule[attr]}
  257. else:
  258. raise UnrecognizedRequestError()
  259. def _priority_class_from_spec(spec):
  260. if spec["template"] not in PRIORITY_CLASS_MAP.keys():
  261. raise InvalidRuleException("Unknown template: %s" % (spec["template"]))
  262. pc = PRIORITY_CLASS_MAP[spec["template"]]
  263. return pc
  264. def _namespaced_rule_id_from_spec(spec):
  265. return _namespaced_rule_id(spec, spec["rule_id"])
  266. def _namespaced_rule_id(spec, rule_id):
  267. return "global/%s/%s" % (spec["template"], rule_id)
  268. class InvalidRuleException(Exception):
  269. pass
  270. def register_servlets(hs, http_server):
  271. PushRuleRestServlet(hs).register(http_server)