2015-01-22 17:37:12 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2014 OpenMarket Ltd
|
|
|
|
#
|
|
|
|
# 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 16:30:48 +00:00
|
|
|
from synapse.api.errors import (
|
|
|
|
SynapseError, Codes, UnrecognizedRequestError, NotFoundError, StoreError
|
|
|
|
)
|
2015-12-01 17:34:32 +00:00
|
|
|
from .base import ClientV1RestServlet, client_path_patterns
|
2015-02-10 16:30:48 +00:00
|
|
|
from synapse.storage.push_rule import (
|
|
|
|
InconsistentRuleException, RuleNotFoundException
|
|
|
|
)
|
2015-01-30 15:54:29 +00:00
|
|
|
import synapse.push.baserules as baserules
|
2015-02-10 16:30:48 +00:00
|
|
|
from synapse.push.rulekinds import (
|
|
|
|
PRIORITY_CLASS_MAP, PRIORITY_CLASS_INVERSE_MAP
|
|
|
|
)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
2015-02-11 14:23:10 +00:00
|
|
|
import simplejson as json
|
2015-01-22 17:37:12 +00:00
|
|
|
|
|
|
|
|
2015-01-28 14:10:46 +00:00
|
|
|
class PushRuleRestServlet(ClientV1RestServlet):
|
2015-12-01 17:34:32 +00:00
|
|
|
PATTERNS = client_path_patterns("/pushrules/.*$")
|
2015-01-28 14:26:03 +00:00
|
|
|
SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR = (
|
|
|
|
"Unrecognised request: You probably wanted a trailing slash")
|
2015-01-22 17:37:12 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_PUT(self, request):
|
2015-02-05 09:35:21 +00:00
|
|
|
spec = _rule_spec_from_path(request.postpath)
|
2015-01-22 17:37:12 +00:00
|
|
|
try:
|
2015-01-23 17:07:06 +00:00
|
|
|
priority_class = _priority_class_from_spec(spec)
|
2015-01-22 17:37:12 +00:00
|
|
|
except InvalidRuleException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
|
|
|
|
2015-11-04 17:29:07 +00:00
|
|
|
user, _, _ = yield self.auth.get_user_by_req(request)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
2015-02-05 14:46:06 +00:00
|
|
|
if '/' in spec['rule_id'] or '\\' in spec['rule_id']:
|
|
|
|
raise SynapseError(400, "rule_id may not contain slashes")
|
2015-02-05 09:35:21 +00:00
|
|
|
|
2015-01-22 17:37:12 +00:00
|
|
|
content = _parse_json(request)
|
|
|
|
|
2015-02-26 18:07:44 +00:00
|
|
|
if 'attr' in spec:
|
|
|
|
self.set_rule_attr(user.to_string(), spec, content)
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2015-01-22 17:37:12 +00:00
|
|
|
try:
|
2015-02-05 09:35:21 +00:00
|
|
|
(conditions, actions) = _rule_tuple_from_request_object(
|
2015-01-22 17:37:12 +00:00
|
|
|
spec['template'],
|
|
|
|
spec['rule_id'],
|
2015-01-23 13:23:10 +00:00
|
|
|
content,
|
|
|
|
device=spec['device'] if 'device' in spec else None
|
2015-01-22 17:37:12 +00:00
|
|
|
)
|
|
|
|
except InvalidRuleException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
|
|
|
|
|
|
|
before = request.args.get("before", None)
|
|
|
|
if before and len(before):
|
|
|
|
before = before[0]
|
|
|
|
after = request.args.get("after", None)
|
|
|
|
if after and len(after):
|
|
|
|
after = after[0]
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield self.hs.get_datastore().add_push_rule(
|
|
|
|
user_name=user.to_string(),
|
2015-02-05 09:35:21 +00:00
|
|
|
rule_id=_namespaced_rule_id_from_spec(spec),
|
2015-01-22 17:37:12 +00:00
|
|
|
priority_class=priority_class,
|
|
|
|
conditions=conditions,
|
|
|
|
actions=actions,
|
|
|
|
before=before,
|
|
|
|
after=after
|
|
|
|
)
|
|
|
|
except InconsistentRuleException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
2015-01-23 10:28:25 +00:00
|
|
|
except RuleNotFoundException as e:
|
|
|
|
raise SynapseError(400, e.message)
|
2015-01-22 17:37:12 +00:00
|
|
|
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
|
2015-01-22 19:32:17 +00:00
|
|
|
@defer.inlineCallbacks
|
2015-01-23 17:49:37 +00:00
|
|
|
def on_DELETE(self, request):
|
2015-02-05 09:35:21 +00:00
|
|
|
spec = _rule_spec_from_path(request.postpath)
|
2015-01-23 17:49:37 +00:00
|
|
|
|
2015-11-04 17:29:07 +00:00
|
|
|
user, _, _ = yield self.auth.get_user_by_req(request)
|
2015-01-23 17:49:37 +00:00
|
|
|
|
2015-02-05 09:35:21 +00:00
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
2015-01-23 17:49:37 +00:00
|
|
|
|
2015-02-05 09:35:21 +00:00
|
|
|
try:
|
|
|
|
yield self.hs.get_datastore().delete_push_rule(
|
|
|
|
user.to_string(), namespaced_rule_id
|
|
|
|
)
|
|
|
|
defer.returnValue((200, {}))
|
|
|
|
except StoreError as e:
|
|
|
|
if e.code == 404:
|
|
|
|
raise NotFoundError()
|
|
|
|
else:
|
|
|
|
raise
|
2015-01-23 17:49:37 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2015-01-22 19:32:17 +00:00
|
|
|
def on_GET(self, request):
|
2015-11-04 17:29:07 +00:00
|
|
|
user, _, _ = yield self.auth.get_user_by_req(request)
|
2015-01-22 19:32:17 +00: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
|
2015-03-02 18:17:19 +00:00
|
|
|
rawrules = yield self.hs.get_datastore().get_push_rules_for_user(
|
2015-02-10 16:30:48 +00:00
|
|
|
user.to_string()
|
|
|
|
)
|
2015-02-05 14:46:06 +00:00
|
|
|
|
2015-05-26 09:14:15 +00:00
|
|
|
ruleslist = []
|
|
|
|
for rawrule in rawrules:
|
|
|
|
rule = dict(rawrule)
|
|
|
|
rule["conditions"] = json.loads(rawrule["conditions"])
|
|
|
|
rule["actions"] = json.loads(rawrule["actions"])
|
|
|
|
ruleslist.append(rule)
|
|
|
|
|
|
|
|
ruleslist = baserules.list_with_base_rules(ruleslist, user)
|
2015-01-22 19:32:17 +00:00
|
|
|
|
|
|
|
rules = {'global': {}, 'device': {}}
|
|
|
|
|
|
|
|
rules['global'] = _add_empty_priority_class_arrays(rules['global'])
|
|
|
|
|
2015-02-26 18:07:44 +00:00
|
|
|
enabled_map = yield self.hs.get_datastore().\
|
2015-03-02 18:17:19 +00:00
|
|
|
get_push_rules_enabled_for_user(user.to_string())
|
2015-02-26 18:07:44 +00:00
|
|
|
|
2015-02-05 14:46:06 +00:00
|
|
|
for r in ruleslist:
|
2015-01-22 19:32:17 +00:00
|
|
|
rulearray = None
|
|
|
|
|
|
|
|
template_name = _priority_class_to_template_name(r['priority_class'])
|
|
|
|
|
2015-02-05 14:46:06 +00:00
|
|
|
if r['priority_class'] > PRIORITY_CLASS_MAP['override']:
|
2015-01-22 19:32:17 +00:00
|
|
|
# per-device rule
|
2015-02-03 16:51:07 +00:00
|
|
|
profile_tag = _profile_tag_from_conditions(r["conditions"])
|
2015-01-23 17:07:06 +00:00
|
|
|
r = _strip_device_condition(r)
|
2015-02-03 16:51:07 +00:00
|
|
|
if not profile_tag:
|
2015-01-22 19:32:17 +00:00
|
|
|
continue
|
2015-02-03 16:51:07 +00:00
|
|
|
if profile_tag not in rules['device']:
|
|
|
|
rules['device'][profile_tag] = {}
|
|
|
|
rules['device'][profile_tag] = (
|
2015-01-28 14:35:00 +00:00
|
|
|
_add_empty_priority_class_arrays(
|
2015-02-03 16:51:07 +00:00
|
|
|
rules['device'][profile_tag]
|
2015-01-28 14:35:00 +00:00
|
|
|
)
|
|
|
|
)
|
2015-01-22 19:32:17 +00:00
|
|
|
|
2015-02-03 16:51:07 +00:00
|
|
|
rulearray = rules['device'][profile_tag][template_name]
|
2015-01-22 19:32:17 +00:00
|
|
|
else:
|
|
|
|
rulearray = rules['global'][template_name]
|
|
|
|
|
|
|
|
template_rule = _rule_to_template(r)
|
|
|
|
if template_rule:
|
2015-02-26 18:07:44 +00:00
|
|
|
if r['rule_id'] in enabled_map:
|
|
|
|
template_rule['enabled'] = enabled_map[r['rule_id']]
|
2015-03-10 17:26:25 +00:00
|
|
|
elif 'enabled' in r:
|
|
|
|
template_rule['enabled'] = r['enabled']
|
|
|
|
else:
|
|
|
|
template_rule['enabled'] = True
|
2015-01-22 19:32:17 +00:00
|
|
|
rulearray.append(template_rule)
|
|
|
|
|
|
|
|
path = request.postpath[1:]
|
2015-01-23 11:19:02 +00:00
|
|
|
|
2015-01-22 19:32:17 +00:00
|
|
|
if path == []:
|
2015-01-23 11:19:02 +00:00
|
|
|
# we're a reference impl: pedantry is our job.
|
2015-01-29 16:10:35 +00:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-22 19:32:17 +00:00
|
|
|
|
2015-01-23 11:19:02 +00:00
|
|
|
if path[0] == '':
|
|
|
|
defer.returnValue((200, rules))
|
|
|
|
elif path[0] == 'global':
|
2015-01-22 19:32:17 +00:00
|
|
|
path = path[1:]
|
|
|
|
result = _filter_ruleset_with_path(rules['global'], path)
|
|
|
|
defer.returnValue((200, result))
|
|
|
|
elif path[0] == 'device':
|
|
|
|
path = path[1:]
|
|
|
|
if path == []:
|
2015-01-29 16:10:35 +00:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-23 13:23:10 +00:00
|
|
|
if path[0] == '':
|
|
|
|
defer.returnValue((200, rules['device']))
|
|
|
|
|
2015-02-03 16:51:07 +00:00
|
|
|
profile_tag = path[0]
|
2015-01-23 17:07:06 +00:00
|
|
|
path = path[1:]
|
2015-02-03 16:51:07 +00:00
|
|
|
if profile_tag not in rules['device']:
|
2015-01-22 19:32:17 +00:00
|
|
|
ret = {}
|
|
|
|
ret = _add_empty_priority_class_arrays(ret)
|
|
|
|
defer.returnValue((200, ret))
|
2015-02-03 16:51:07 +00:00
|
|
|
ruleset = rules['device'][profile_tag]
|
2015-01-22 19:32:17 +00:00
|
|
|
result = _filter_ruleset_with_path(ruleset, path)
|
|
|
|
defer.returnValue((200, result))
|
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
2015-01-22 17:37:12 +00:00
|
|
|
def on_OPTIONS(self, _):
|
|
|
|
return 200, {}
|
|
|
|
|
2015-02-26 18:07:44 +00:00
|
|
|
def set_rule_attr(self, user_name, spec, val):
|
|
|
|
if spec['attr'] == 'enabled':
|
|
|
|
if not isinstance(val, bool):
|
|
|
|
raise SynapseError(400, "Value for 'enabled' must be boolean")
|
|
|
|
namespaced_rule_id = _namespaced_rule_id_from_spec(spec)
|
|
|
|
self.hs.get_datastore().set_push_rule_enabled(
|
|
|
|
user_name, namespaced_rule_id, val
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
def get_rule_attr(self, user_name, namespaced_rule_id, attr):
|
|
|
|
if attr == 'enabled':
|
2015-03-02 18:17:19 +00:00
|
|
|
return self.hs.get_datastore().get_push_rule_enabled_by_user_rule_id(
|
2015-02-26 18:07:44 +00:00
|
|
|
user_name, namespaced_rule_id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
2015-01-29 16:10:35 +00:00
|
|
|
|
2015-03-02 15:58:12 +00:00
|
|
|
|
2015-02-05 09:35:21 +00: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:]
|
|
|
|
if scope not in ['global', 'device']:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
device = None
|
|
|
|
if scope == 'device':
|
|
|
|
if len(path) == 0:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
device = path[0]
|
|
|
|
path = path[1:]
|
|
|
|
|
|
|
|
if len(path) == 0:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
template = path[0]
|
|
|
|
path = path[1:]
|
|
|
|
|
2015-02-25 14:02:38 +00:00
|
|
|
if len(path) == 0 or len(path[0]) == 0:
|
2015-02-05 09:35:21 +00:00
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
|
|
|
|
rule_id = path[0]
|
|
|
|
|
|
|
|
spec = {
|
|
|
|
'scope': scope,
|
|
|
|
'template': template,
|
|
|
|
'rule_id': rule_id
|
|
|
|
}
|
|
|
|
if device:
|
|
|
|
spec['profile_tag'] = device
|
2015-02-26 18:07:44 +00:00
|
|
|
|
|
|
|
path = path[1:]
|
|
|
|
|
|
|
|
if len(path) > 0 and len(path[0]) > 0:
|
|
|
|
spec['attr'] = path[0]
|
|
|
|
|
2015-02-05 09:35:21 +00:00
|
|
|
return spec
|
|
|
|
|
|
|
|
|
|
|
|
def _rule_tuple_from_request_object(rule_template, rule_id, req_obj, device=None):
|
|
|
|
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 device:
|
|
|
|
conditions.append({
|
|
|
|
'kind': 'device',
|
|
|
|
'profile_tag': device
|
|
|
|
})
|
|
|
|
|
|
|
|
if 'actions' not in req_obj:
|
|
|
|
raise InvalidRuleException("No actions found")
|
|
|
|
actions = req_obj['actions']
|
|
|
|
|
|
|
|
for a in actions:
|
|
|
|
if a in ['notify', 'dont_notify', 'coalesce']:
|
|
|
|
pass
|
2015-02-27 18:38:56 +00:00
|
|
|
elif isinstance(a, dict) and 'set_tweak' in a:
|
2015-02-05 09:35:21 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise InvalidRuleException("Unrecognised action")
|
|
|
|
|
|
|
|
return conditions, actions
|
|
|
|
|
|
|
|
|
2015-01-22 19:32:17 +00:00
|
|
|
def _add_empty_priority_class_arrays(d):
|
2015-02-05 14:46:06 +00:00
|
|
|
for pc in PRIORITY_CLASS_MAP.keys():
|
2015-01-22 19:32:17 +00:00
|
|
|
d[pc] = []
|
|
|
|
return d
|
|
|
|
|
2015-01-29 16:10:35 +00:00
|
|
|
|
2015-02-03 16:51:07 +00:00
|
|
|
def _profile_tag_from_conditions(conditions):
|
2015-01-22 19:32:17 +00:00
|
|
|
"""
|
2015-02-04 16:24:15 +00:00
|
|
|
Given a list of conditions, return the profile tag of the
|
2015-01-22 19:32:17 +00:00
|
|
|
device rule if there is one
|
|
|
|
"""
|
|
|
|
for c in conditions:
|
|
|
|
if c['kind'] == 'device':
|
2015-02-03 16:51:07 +00:00
|
|
|
return c['profile_tag']
|
2015-01-22 19:32:17 +00:00
|
|
|
return None
|
|
|
|
|
2015-01-29 16:10:35 +00:00
|
|
|
|
2015-01-22 19:32:17 +00:00
|
|
|
def _filter_ruleset_with_path(ruleset, path):
|
|
|
|
if path == []:
|
2015-01-29 16:10:35 +00:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-23 11:19:02 +00:00
|
|
|
|
|
|
|
if path[0] == '':
|
2015-01-22 19:32:17 +00:00
|
|
|
return ruleset
|
|
|
|
template_kind = path[0]
|
|
|
|
if template_kind not in ruleset:
|
|
|
|
raise UnrecognizedRequestError()
|
|
|
|
path = path[1:]
|
|
|
|
if path == []:
|
2015-01-29 16:10:35 +00:00
|
|
|
raise UnrecognizedRequestError(
|
|
|
|
PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR
|
|
|
|
)
|
2015-01-23 11:19:02 +00:00
|
|
|
if path[0] == '':
|
2015-01-22 19:32:17 +00:00
|
|
|
return ruleset[template_kind]
|
|
|
|
rule_id = path[0]
|
2015-02-26 18:07:44 +00:00
|
|
|
|
|
|
|
the_rule = None
|
2015-01-22 19:32:17 +00:00
|
|
|
for r in ruleset[template_kind]:
|
|
|
|
if r['rule_id'] == rule_id:
|
2015-02-26 18:07:44 +00: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:
|
|
|
|
return the_rule[attr]
|
|
|
|
else:
|
|
|
|
raise UnrecognizedRequestError()
|
2015-01-22 19:32:17 +00:00
|
|
|
|
2015-01-29 16:10:35 +00:00
|
|
|
|
2015-01-23 17:07:06 +00:00
|
|
|
def _priority_class_from_spec(spec):
|
2015-02-05 14:46:06 +00:00
|
|
|
if spec['template'] not in PRIORITY_CLASS_MAP.keys():
|
2015-01-23 17:07:06 +00:00
|
|
|
raise InvalidRuleException("Unknown template: %s" % (spec['kind']))
|
2015-02-05 14:46:06 +00:00
|
|
|
pc = PRIORITY_CLASS_MAP[spec['template']]
|
2015-01-23 17:07:06 +00:00
|
|
|
|
|
|
|
if spec['scope'] == 'device':
|
2015-02-05 14:46:06 +00:00
|
|
|
pc += len(PRIORITY_CLASS_MAP)
|
2015-01-23 17:07:06 +00:00
|
|
|
|
|
|
|
return pc
|
|
|
|
|
2015-01-29 16:10:35 +00:00
|
|
|
|
2015-01-22 19:32:17 +00:00
|
|
|
def _priority_class_to_template_name(pc):
|
2015-02-05 14:46:06 +00:00
|
|
|
if pc > PRIORITY_CLASS_MAP['override']:
|
2015-01-22 19:32:17 +00:00
|
|
|
# per-device
|
2015-03-02 17:37:22 +00:00
|
|
|
prio_class_index = pc - len(PRIORITY_CLASS_MAP)
|
2015-02-05 14:46:06 +00:00
|
|
|
return PRIORITY_CLASS_INVERSE_MAP[prio_class_index]
|
2015-01-22 19:32:17 +00:00
|
|
|
else:
|
2015-02-05 14:46:06 +00:00
|
|
|
return PRIORITY_CLASS_INVERSE_MAP[pc]
|
2015-01-22 19:32:17 +00:00
|
|
|
|
2015-01-29 16:10:35 +00:00
|
|
|
|
2015-01-22 19:32:17 +00:00
|
|
|
def _rule_to_template(rule):
|
2015-02-05 14:46:06 +00:00
|
|
|
unscoped_rule_id = None
|
|
|
|
if 'rule_id' in rule:
|
2015-02-05 15:11:38 +00:00
|
|
|
unscoped_rule_id = _rule_id_from_namespaced(rule['rule_id'])
|
2015-02-05 09:35:21 +00:00
|
|
|
|
2015-01-22 19:32:17 +00:00
|
|
|
template_name = _priority_class_to_template_name(rule['priority_class'])
|
2015-02-05 14:46:06 +00:00
|
|
|
if template_name in ['override', 'underride']:
|
|
|
|
templaterule = {k: rule[k] for k in ["conditions", "actions"]}
|
2015-01-22 19:32:17 +00:00
|
|
|
elif template_name in ["sender", "room"]:
|
2015-02-05 14:46:06 +00:00
|
|
|
templaterule = {'actions': rule['actions']}
|
|
|
|
unscoped_rule_id = rule['conditions'][0]['pattern']
|
2015-01-22 19:32:17 +00:00
|
|
|
elif template_name == 'content':
|
|
|
|
if len(rule["conditions"]) != 1:
|
|
|
|
return None
|
|
|
|
thecond = rule["conditions"][0]
|
|
|
|
if "pattern" not in thecond:
|
|
|
|
return None
|
2015-02-05 14:46:06 +00:00
|
|
|
templaterule = {'actions': rule['actions']}
|
|
|
|
templaterule["pattern"] = thecond["pattern"]
|
|
|
|
|
|
|
|
if unscoped_rule_id:
|
|
|
|
templaterule['rule_id'] = unscoped_rule_id
|
2015-02-05 15:11:38 +00:00
|
|
|
if 'default' in rule:
|
|
|
|
templaterule['default'] = rule['default']
|
2015-02-05 14:46:06 +00:00
|
|
|
return templaterule
|
2015-01-22 19:32:17 +00:00
|
|
|
|
2015-01-29 16:10:35 +00:00
|
|
|
|
2015-01-23 17:07:06 +00:00
|
|
|
def _strip_device_condition(rule):
|
2015-01-29 16:10:35 +00:00
|
|
|
for i, c in enumerate(rule['conditions']):
|
2015-01-23 17:07:06 +00:00
|
|
|
if c['kind'] == 'device':
|
|
|
|
del rule['conditions'][i]
|
|
|
|
return rule
|
|
|
|
|
|
|
|
|
2015-02-05 09:35:21 +00:00
|
|
|
def _namespaced_rule_id_from_spec(spec):
|
|
|
|
if spec['scope'] == 'global':
|
|
|
|
scope = 'global'
|
|
|
|
else:
|
2015-02-05 14:46:06 +00:00
|
|
|
scope = 'device/%s' % (spec['profile_tag'])
|
|
|
|
return "%s/%s/%s" % (scope, spec['template'], spec['rule_id'])
|
2015-02-05 09:35:21 +00:00
|
|
|
|
|
|
|
|
2015-02-05 14:46:06 +00:00
|
|
|
def _rule_id_from_namespaced(in_rule_id):
|
|
|
|
return in_rule_id.split('/')[-1]
|
2015-02-05 09:35:21 +00:00
|
|
|
|
2015-02-10 16:30:48 +00:00
|
|
|
|
2015-01-22 17:37:12 +00:00
|
|
|
class InvalidRuleException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# XXX: C+ped from rest/room.py - surely this should be common?
|
|
|
|
def _parse_json(request):
|
|
|
|
try:
|
|
|
|
content = json.loads(request.content.read())
|
|
|
|
return content
|
|
|
|
except ValueError:
|
|
|
|
raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON)
|
|
|
|
|
|
|
|
|
|
|
|
def register_servlets(hs, http_server):
|
|
|
|
PushRuleRestServlet(hs).register(http_server)
|