2015-01-22 12:37:12 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 23:26:29 -05:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2015-01-22 12:37:12 -05:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
2015-02-10 11:30:48 -05:00
|
|
|
from synapse.api.errors import (
|
2018-07-09 02:09:20 -04:00
|
|
|
NotFoundError,
|
|
|
|
StoreError,
|
|
|
|
SynapseError,
|
|
|
|
UnrecognizedRequestError,
|
2015-02-10 11:30:48 -05:00
|
|
|
)
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.http.servlet import parse_json_value_from_request
|
2016-03-03 11:11:59 -05:00
|
|
|
from synapse.push.baserules import BASE_RULE_IDS
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.push.clientformat import format_push_rules_for_user
|
2016-03-03 11:11:59 -05:00
|
|
|
from synapse.push.rulekinds import PRIORITY_CLASS_MAP
|
2018-07-09 02:09:20 -04:00
|
|
|
from synapse.storage.push_rule import InconsistentRuleException, RuleNotFoundException
|
|
|
|
|
|
|
|
from .base import ClientV1RestServlet, client_path_patterns
|
2015-01-22 12:37:12 -05:00
|
|
|
|
|
|
|
|
2015-01-28 09:10:46 -05:00
|
|
|
class PushRuleRestServlet(ClientV1RestServlet):
|
2015-12-01 12:34:32 -05:00
|
|
|
PATTERNS = client_path_patterns("/pushrules/.*$")
|
2015-01-28 09:26:03 -05:00
|
|
|
SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR = (
|
|
|
|
"Unrecognised request: You probably wanted a trailing slash")
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2016-03-03 09:57:45 -05:00
|
|
|
def __init__(self, hs):
|
|
|
|
super(PushRuleRestServlet, self).__init__(hs)
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.notifier = hs.get_notifier()
|
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_PUT(self, request):
|
2015-02-05 04:35:21 -05:00
|
|
|
spec = _rule_spec_from_path(request.postpath)
|
2015-01-22 12:37:12 -05:00
|
|
|
try:
|
2015-01-23 12:07:06 -05:00
|
|
|
priority_class = _priority_class_from_spec(spec)
|
2015-01-22 12:37:12 -05:00
|
|
|
except InvalidRuleException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
|
|
|
|
2016-01-11 10:29:57 -05:00
|
|
|
requester = yield self.auth.get_user_by_req(request)
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2015-02-05 09:46:06 -05:00
|
|
|
if '/' in spec['rule_id'] or '\\' in spec['rule_id']:
|
|
|
|
raise SynapseError(400, "rule_id may not contain slashes")
|
2015-02-05 04:35:21 -05:00
|
|
|
|
2016-03-14 10:16:41 -04:00
|
|
|
content = parse_json_value_from_request(request)
|
2015-01-22 12:37:12 -05:00
|
|
|
|
2016-03-03 09:57:45 -05:00
|
|
|
user_id = requester.user.to_string()
|
|
|
|
|
2015-02-26 13:07:44 -05:00
|
|
|
if 'attr' in spec:
|
2016-03-03 09:57:45 -05:00
|
|
|
yield self.set_rule_attr(user_id, spec, content)
|
|
|
|
self.notify_user(user_id)
|
2015-02-26 13:07:44 -05:00
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2016-02-26 09:28:19 -05:00
|
|
|
if spec['rule_id'].startswith('.'):
|
|
|
|
# Rule ids starting with '.' are reserved for server default rules.
|
|
|
|
raise SynapseError(400, "cannot add new rule_ids that start with '.'")
|
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
try:
|
2015-02-05 04:35:21 -05:00
|
|
|
(conditions, actions) = _rule_tuple_from_request_object(
|
2015-01-22 12:37:12 -05:00
|
|
|
spec['template'],
|
|
|
|
spec['rule_id'],
|
2015-01-23 08:23:10 -05:00
|
|
|
content,
|
2015-01-22 12:37:12 -05:00
|
|
|
)
|
|
|
|
except InvalidRuleException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
|
|
|
|
|
|
|
before = request.args.get("before", None)
|
2016-01-27 05:24:20 -05:00
|
|
|
if before:
|
|
|
|
before = _namespaced_rule_id(spec, before[0])
|
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
after = request.args.get("after", None)
|
2016-01-27 05:24:20 -05:00
|
|
|
if after:
|
|
|
|
after = _namespaced_rule_id(spec, after[0])
|
2015-01-22 12:37:12 -05:00
|
|
|
|
|
|
|
try:
|
2016-03-03 09:57:45 -05:00
|
|
|
yield self.store.add_push_rule(
|
|
|
|
user_id=user_id,
|
2015-02-05 04:35:21 -05:00
|
|
|
rule_id=_namespaced_rule_id_from_spec(spec),
|
2015-01-22 12:37:12 -05:00
|
|
|
priority_class=priority_class,
|
|
|
|
conditions=conditions,
|
|
|
|
actions=actions,
|
|
|
|
before=before,
|
|
|
|
after=after
|
|
|
|
)
|
2016-03-03 09:57:45 -05:00
|
|
|
self.notify_user(user_id)
|
2015-01-22 12:37:12 -05:00
|
|
|
except InconsistentRuleException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
2015-01-23 05:28:25 -05:00
|
|
|
except RuleNotFoundException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
2015-01-22 12:37:12 -05:00
|
|
|
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2015-01-22 14:32:17 -05:00
|
|
|
@defer.inlineCallbacks
|
2015-01-23 12:49:37 -05:00
|
|
|
def on_DELETE(self, request):
|
2015-02-05 04:35:21 -05:00
|
|
|
spec = _rule_spec_from_path(request.postpath)
|
2015-01-23 12:49:37 -05:00
|
|
|
|
2016-01-11 10:29:57 -05:00
|
|
|
requester = yield self.auth.get_user_by_req(request)
|
2016-03-03 09:57:45 -05:00
|
|
|
user_id = requester.user.to_string()
|
2015-01-23 12:49:37 -05:00
|
|
|
|
2015-02-05 04:35:21 -05:00
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
2015-01-23 12:49:37 -05:00
|
|
|
|
2015-02-05 04:35:21 -05:00
|
|
|
try:
|
2016-03-03 09:57:45 -05:00
|
|
|
yield self.store.delete_push_rule(
|
|
|
|
user_id, namespaced_rule_id
|
2015-02-05 04:35:21 -05:00
|
|
|
)
|
2016-03-03 09:57:45 -05:00
|
|
|
self.notify_user(user_id)
|
2015-02-05 04:35:21 -05:00
|
|
|
defer.returnValue((200, {}))
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError()
|
|
|
|
else:
|
|
|
|
raise
|
2015-01-23 12:49:37 -05:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2015-01-22 14:32:17 -05:00
|
|
|
def on_GET(self, request):
|
2016-01-11 10:29:57 -05:00
|
|
|
requester = yield self.auth.get_user_by_req(request)
|
2016-03-03 09:57:45 -05:00
|
|
|
user_id = requester.user.to_string()
|
2015-01-22 14:32:17 -05:00
|
|
|
|
|
|
|
# we build up the full structure and then decide which bits of it
|
|
|
|
# to send which means doing unnecessary work sometimes but is
|
|
|
|
# is probably not going to make a whole lot of difference
|
2016-06-01 09:27:07 -04:00
|
|
|
rules = yield self.store.get_push_rules_for_user(user_id)
|
2015-02-05 09:46:06 -05:00
|
|
|
|
2016-06-01 09:27:07 -04:00
|
|
|
rules = format_push_rules_for_user(requester.user, rules)
|
2015-01-22 14:32:17 -05:00
|
|
|
|
|
|
|
path = request.postpath[1:]
|
2015-01-23 06:19:02 -05:00
|
|
|
|
2015-01-22 14:32:17 -05:00
|
|
|
if path == []:
|
2015-01-23 06:19:02 -05:00
|
|
|
# we're a reference impl: pedantry is our job.
|
2015-01-29 11:10:35 -05:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-22 14:32:17 -05:00
|
|
|
|
2015-01-23 06:19:02 -05:00
|
|
|
if path[0] == '':
|
|
|
|
defer.returnValue((200, rules))
|
|
|
|
elif path[0] == 'global':
|
2015-01-22 14:32:17 -05:00
|
|
|
path = path[1:]
|
|
|
|
result = _filter_ruleset_with_path(rules['global'], path)
|
|
|
|
defer.returnValue((200, result))
|
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
def on_OPTIONS(self, _):
|
|
|
|
return 200, {}
|
|
|
|
|
2016-03-03 09:57:45 -05:00
|
|
|
def notify_user(self, user_id):
|
2016-03-04 09:44:01 -05:00
|
|
|
stream_id, _ = self.store.get_push_rules_stream_token()
|
2016-03-03 09:57:45 -05:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
"push_rules_key", stream_id, users=[user_id]
|
|
|
|
)
|
|
|
|
|
2016-01-13 08:08:59 -05:00
|
|
|
def set_rule_attr(self, user_id, spec, val):
|
2015-02-26 13:07:44 -05:00
|
|
|
if spec['attr'] == 'enabled':
|
2015-12-07 05:44:33 -05:00
|
|
|
if isinstance(val, dict) and "enabled" in val:
|
|
|
|
val = val["enabled"]
|
2015-02-26 13:07:44 -05:00
|
|
|
if not isinstance(val, bool):
|
2015-12-07 05:44:33 -05:00
|
|
|
# Legacy fallback
|
|
|
|
# This should *actually* take a dict, but many clients pass
|
|
|
|
# bools directly, so let's not break them.
|
2015-02-26 13:07:44 -05:00
|
|
|
raise SynapseError(400, "Value for 'enabled' must be boolean")
|
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
2016-03-03 09:57:45 -05:00
|
|
|
return self.store.set_push_rule_enabled(
|
2016-01-13 08:08:59 -05:00
|
|
|
user_id, namespaced_rule_id, val
|
2015-02-26 13:07:44 -05:00
|
|
|
)
|
2016-02-26 09:28:19 -05:00
|
|
|
elif spec['attr'] == 'actions':
|
|
|
|
actions = val.get('actions')
|
|
|
|
_check_actions(actions)
|
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
|
|
|
rule_id = spec['rule_id']
|
|
|
|
is_default_rule = rule_id.startswith(".")
|
|
|
|
if is_default_rule:
|
|
|
|
if namespaced_rule_id not in BASE_RULE_IDS:
|
|
|
|
raise SynapseError(404, "Unknown rule %r" % (namespaced_rule_id,))
|
2016-03-03 09:57:45 -05:00
|
|
|
return self.store.set_push_rule_actions(
|
2016-02-26 09:28:19 -05:00
|
|
|
user_id, namespaced_rule_id, actions, is_default_rule
|
|
|
|
)
|
2015-02-26 13:07:44 -05:00
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2015-03-02 10:58:12 -05:00
|
|
|
|
2015-02-05 04:35:21 -05:00
|
|
|
def _rule_spec_from_path(path):
|
|
|
|
if len(path) < 2:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
if path[0] != 'pushrules':
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
scope = path[1]
|
|
|
|
path = path[2:]
|
2016-02-18 11:05:13 -05:00
|
|
|
if scope != 'global':
|
2015-02-05 04:35:21 -05:00
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
if len(path) == 0:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
template = path[0]
|
|
|
|
path = path[1:]
|
|
|
|
|
2015-02-25 09:02:38 -05:00
|
|
|
if len(path) == 0 or len(path[0]) == 0:
|
2015-02-05 04:35:21 -05:00
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
rule_id = path[0]
|
|
|
|
|
|
|
|
spec = {
|
|
|
|
'scope': scope,
|
|
|
|
'template': template,
|
|
|
|
'rule_id': rule_id
|
|
|
|
}
|
2015-02-26 13:07:44 -05:00
|
|
|
|
|
|
|
path = path[1:]
|
|
|
|
|
|
|
|
if len(path) > 0 and len(path[0]) > 0:
|
|
|
|
spec['attr'] = path[0]
|
|
|
|
|
2015-02-05 04:35:21 -05:00
|
|
|
return spec
|
|
|
|
|
|
|
|
|
2016-02-18 11:05:13 -05:00
|
|
|
def _rule_tuple_from_request_object(rule_template, rule_id, req_obj):
|
2015-02-05 04:35:21 -05:00
|
|
|
if rule_template in ['override', 'underride']:
|
|
|
|
if 'conditions' not in req_obj:
|
|
|
|
raise InvalidRuleException("Missing 'conditions'")
|
|
|
|
conditions = req_obj['conditions']
|
|
|
|
for c in conditions:
|
|
|
|
if 'kind' not in c:
|
|
|
|
raise InvalidRuleException("Condition without 'kind'")
|
|
|
|
elif rule_template == 'room':
|
|
|
|
conditions = [{
|
|
|
|
'kind': 'event_match',
|
|
|
|
'key': 'room_id',
|
|
|
|
'pattern': rule_id
|
|
|
|
}]
|
|
|
|
elif rule_template == 'sender':
|
|
|
|
conditions = [{
|
|
|
|
'kind': 'event_match',
|
|
|
|
'key': 'user_id',
|
|
|
|
'pattern': rule_id
|
|
|
|
}]
|
|
|
|
elif rule_template == 'content':
|
|
|
|
if 'pattern' not in req_obj:
|
|
|
|
raise InvalidRuleException("Content rule missing 'pattern'")
|
|
|
|
pat = req_obj['pattern']
|
|
|
|
|
|
|
|
conditions = [{
|
|
|
|
'kind': 'event_match',
|
|
|
|
'key': 'content.body',
|
|
|
|
'pattern': pat
|
|
|
|
}]
|
|
|
|
else:
|
|
|
|
raise InvalidRuleException("Unknown rule template: %s" % (rule_template,))
|
|
|
|
|
|
|
|
if 'actions' not in req_obj:
|
|
|
|
raise InvalidRuleException("No actions found")
|
|
|
|
actions = req_obj['actions']
|
|
|
|
|
2016-02-26 09:28:19 -05:00
|
|
|
_check_actions(actions)
|
|
|
|
|
|
|
|
return conditions, actions
|
|
|
|
|
|
|
|
|
|
|
|
def _check_actions(actions):
|
|
|
|
if not isinstance(actions, list):
|
|
|
|
raise InvalidRuleException("No actions found")
|
|
|
|
|
2015-02-05 04:35:21 -05:00
|
|
|
for a in actions:
|
|
|
|
if a in ['notify', 'dont_notify', 'coalesce']:
|
|
|
|
pass
|
2015-02-27 13:38:56 -05:00
|
|
|
elif isinstance(a, dict) and 'set_tweak' in a:
|
2015-02-05 04:35:21 -05:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise InvalidRuleException("Unrecognised action")
|
|
|
|
|
|
|
|
|
2015-01-22 14:32:17 -05:00
|
|
|
def _filter_ruleset_with_path(ruleset, path):
|
|
|
|
if path == []:
|
2015-01-29 11:10:35 -05:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-23 06:19:02 -05:00
|
|
|
|
|
|
|
if path[0] == '':
|
2015-01-22 14:32:17 -05:00
|
|
|
return ruleset
|
|
|
|
template_kind = path[0]
|
|
|
|
if template_kind not in ruleset:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
path = path[1:]
|
|
|
|
if path == []:
|
2015-01-29 11:10:35 -05:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-23 06:19:02 -05:00
|
|
|
if path[0] == '':
|
2015-01-22 14:32:17 -05:00
|
|
|
return ruleset[template_kind]
|
|
|
|
rule_id = path[0]
|
2015-02-26 13:07:44 -05:00
|
|
|
|
|
|
|
the_rule = None
|
2015-01-22 14:32:17 -05:00
|
|
|
for r in ruleset[template_kind]:
|
|
|
|
if r['rule_id'] == rule_id:
|
2015-02-26 13:07:44 -05:00
|
|
|
the_rule = r
|
|
|
|
if the_rule is None:
|
|
|
|
raise NotFoundError
|
|
|
|
|
|
|
|
path = path[1:]
|
|
|
|
if len(path) == 0:
|
|
|
|
return the_rule
|
|
|
|
|
|
|
|
attr = path[0]
|
|
|
|
if attr in the_rule:
|
2016-02-25 10:13:07 -05:00
|
|
|
# Make sure we return a JSON object as the attribute may be a
|
|
|
|
# JSON value.
|
|
|
|
return {attr: the_rule[attr]}
|
2015-02-26 13:07:44 -05:00
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
2015-01-22 14:32:17 -05:00
|
|
|
|
2015-01-29 11:10:35 -05:00
|
|
|
|
2015-01-23 12:07:06 -05:00
|
|
|
def _priority_class_from_spec(spec):
|
2015-02-05 09:46:06 -05:00
|
|
|
if spec['template'] not in PRIORITY_CLASS_MAP.keys():
|
2016-02-16 13:00:30 -05:00
|
|
|
raise InvalidRuleException("Unknown template: %s" % (spec['template']))
|
2015-02-05 09:46:06 -05:00
|
|
|
pc = PRIORITY_CLASS_MAP[spec['template']]
|
2015-01-23 12:07:06 -05:00
|
|
|
|
|
|
|
return pc
|
|
|
|
|
2015-01-29 11:10:35 -05:00
|
|
|
|
2015-02-05 04:35:21 -05:00
|
|
|
def _namespaced_rule_id_from_spec(spec):
|
2016-01-27 05:24:20 -05:00
|
|
|
return _namespaced_rule_id(spec, spec['rule_id'])
|
|
|
|
|
|
|
|
|
|
|
|
def _namespaced_rule_id(spec, rule_id):
|
2016-02-18 11:05:13 -05:00
|
|
|
return "global/%s/%s" % (spec['template'], rule_id)
|
2015-02-05 04:35:21 -05:00
|
|
|
|
|
|
|
|
2015-01-22 12:37:12 -05:00
|
|
|
class InvalidRuleException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def register_servlets(hs, http_server):
|
|
|
|
PushRuleRestServlet(hs).register(http_server)
|