make per-device rules work

This commit is contained in:
David Baker 2015-01-23 13:23:10 +00:00
parent 49fe31792b
commit 6188c4f69c

View File

@ -73,7 +73,7 @@ class PushRuleRestServlet(RestServlet):
spec['device'] = device spec['device'] = device
return spec return spec
def rule_tuple_from_request_object(self, rule_template, rule_id, req_obj): def rule_tuple_from_request_object(self, rule_template, rule_id, req_obj, device=None):
if rule_template in ['override', 'underride']: if rule_template in ['override', 'underride']:
if 'conditions' not in req_obj: if 'conditions' not in req_obj:
raise InvalidRuleException("Missing 'conditions'") raise InvalidRuleException("Missing 'conditions'")
@ -104,6 +104,12 @@ class PushRuleRestServlet(RestServlet):
else: else:
raise InvalidRuleException("Unknown rule template: %s" % (rule_template)) raise InvalidRuleException("Unknown rule template: %s" % (rule_template))
if device:
conditions.append({
'kind': 'device',
'instance_handle': device
})
if 'actions' not in req_obj: if 'actions' not in req_obj:
raise InvalidRuleException("No actions found") raise InvalidRuleException("No actions found")
actions = req_obj['actions'] actions = req_obj['actions']
@ -144,7 +150,8 @@ class PushRuleRestServlet(RestServlet):
(conditions, actions) = self.rule_tuple_from_request_object( (conditions, actions) = self.rule_tuple_from_request_object(
spec['template'], spec['template'],
spec['rule_id'], spec['rule_id'],
content content,
device=spec['device'] if 'device' in spec else None
) )
except InvalidRuleException as e: except InvalidRuleException as e:
raise SynapseError(400, e.message) raise SynapseError(400, e.message)
@ -200,11 +207,11 @@ class PushRuleRestServlet(RestServlet):
if not instance_handle: if not instance_handle:
continue continue
if instance_handle not in rules['device']: if instance_handle not in rules['device']:
rules['device'][instance_handle] = [] rules['device'][instance_handle] = {}
rules['device'][instance_handle] = \ rules['device'][instance_handle] = \
_add_empty_priority_class_arrays(rules['device'][instance_handle]) _add_empty_priority_class_arrays(rules['device'][instance_handle])
rulearray = rules['device'][instance_handle] rulearray = rules['device'][instance_handle][template_name]
else: else:
rulearray = rules['global'][template_name] rulearray = rules['global'][template_name]
@ -227,7 +234,10 @@ class PushRuleRestServlet(RestServlet):
elif path[0] == 'device': elif path[0] == 'device':
path = path[1:] path = path[1:]
if path == []: if path == []:
raise UnrecognizedRequestError raise UnrecognizedRequestError(PushRuleRestServlet.SLIGHTLY_PEDANTIC_TRAILING_SLASH_ERROR)
if path[0] == '':
defer.returnValue((200, rules['device']))
instance_handle = path[0] instance_handle = path[0]
if instance_handle not in rules['device']: if instance_handle not in rules['device']:
ret = {} ret = {}