Support for channel notifications

Add condition type to check the sender's power level and add a base
rule using it for @channel notifications.
This commit is contained in:
David Baker 2017-10-05 12:39:18 +01:00
parent 44f8e383f3
commit fa969cfdde
3 changed files with 59 additions and 8 deletions

View File

@ -1,4 +1,5 @@
# Copyright 2015, 2016 OpenMarket Ltd # Copyright 2015, 2016 OpenMarket Ltd
# Copyright 2017 New Vector Ltd
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -238,6 +239,28 @@ BASE_APPEND_OVERRIDE_RULES = [
} }
] ]
}, },
{
'rule_id': 'global/underride/.m.rule.channelnotif',
'conditions': [
{
'kind': 'event_match',
'key': 'content.body',
'pattern': '*@channel*',
'_id': '_channelnotif_content',
},
{
'kind': 'sender_power_level',
'is': '>=50',
'_id': '_channelnotif_pl',
},
],
'actions': [
'notify', {
'set_tweak': 'highlight',
'value': True,
}
]
}
] ]

View File

@ -19,6 +19,7 @@ from twisted.internet import defer
from .push_rule_evaluator import PushRuleEvaluatorForEvent from .push_rule_evaluator import PushRuleEvaluatorForEvent
from synapse.event_auth import get_user_power_level
from synapse.api.constants import EventTypes, Membership from synapse.api.constants import EventTypes, Membership
from synapse.metrics import get_metrics_for from synapse.metrics import get_metrics_for
from synapse.util.caches import metrics as cache_metrics from synapse.util.caches import metrics as cache_metrics
@ -59,6 +60,7 @@ class BulkPushRuleEvaluator(object):
def __init__(self, hs): def __init__(self, hs):
self.hs = hs self.hs = hs
self.store = hs.get_datastore() self.store = hs.get_datastore()
self.auth = hs.get_auth()
self.room_push_rule_cache_metrics = cache_metrics.register_cache( self.room_push_rule_cache_metrics = cache_metrics.register_cache(
"cache", "cache",
@ -108,6 +110,17 @@ class BulkPushRuleEvaluator(object):
self.room_push_rule_cache_metrics, self.room_push_rule_cache_metrics,
) )
@defer.inlineCallbacks
def _get_sender_power_level(self, event, context):
auth_events_ids = yield self.auth.compute_auth_events(
event, context.prev_state_ids, for_verification=False,
)
auth_events = yield self.store.get_events(auth_events_ids)
auth_events = {
(e.type, e.state_key): e for e in auth_events.values()
}
defer.returnValue(get_user_power_level(event.sender, auth_events))
@defer.inlineCallbacks @defer.inlineCallbacks
def action_for_event_by_user(self, event, context): def action_for_event_by_user(self, event, context):
"""Given an event and context, evaluate the push rules and return """Given an event and context, evaluate the push rules and return
@ -123,7 +136,11 @@ class BulkPushRuleEvaluator(object):
event, context event, context
) )
evaluator = PushRuleEvaluatorForEvent(event, len(room_members)) sender_power_level = yield self._get_sender_power_level(event, context)
evaluator = PushRuleEvaluatorForEvent(
event, len(room_members), sender_power_level
)
condition_cache = {} condition_cache = {}

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2015, 2016 OpenMarket Ltd # Copyright 2015, 2016 OpenMarket Ltd
# Copyright 2015 New Vector Ltd
# #
# Licensed under the Apache License, Version 2.0 (the "License"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -29,6 +30,12 @@ INEQUALITY_EXPR = re.compile("^([=<>]*)([0-9]*)$")
def _room_member_count(ev, condition, room_member_count): def _room_member_count(ev, condition, room_member_count):
return _test_ineq_condition(condition, room_member_count)
def _sender_power_level(ev, condition, power_level):
return _test_ineq_condition(condition, power_level)
def _test_ineq_condition(condition, number):
if 'is' not in condition: if 'is' not in condition:
return False return False
m = INEQUALITY_EXPR.match(condition['is']) m = INEQUALITY_EXPR.match(condition['is'])
@ -41,19 +48,18 @@ def _room_member_count(ev, condition, room_member_count):
rhs = int(rhs) rhs = int(rhs)
if ineq == '' or ineq == '==': if ineq == '' or ineq == '==':
return room_member_count == rhs return number == rhs
elif ineq == '<': elif ineq == '<':
return room_member_count < rhs return number < rhs
elif ineq == '>': elif ineq == '>':
return room_member_count > rhs return number > rhs
elif ineq == '>=': elif ineq == '>=':
return room_member_count >= rhs return number >= rhs
elif ineq == '<=': elif ineq == '<=':
return room_member_count <= rhs return number <= rhs
else: else:
return False return False
def tweaks_for_actions(actions): def tweaks_for_actions(actions):
tweaks = {} tweaks = {}
for a in actions: for a in actions:
@ -65,9 +71,10 @@ def tweaks_for_actions(actions):
class PushRuleEvaluatorForEvent(object): class PushRuleEvaluatorForEvent(object):
def __init__(self, event, room_member_count): def __init__(self, event, room_member_count, sender_power_level):
self._event = event self._event = event
self._room_member_count = room_member_count self._room_member_count = room_member_count
self._sender_power_level = sender_power_level
# Maps strings of e.g. 'content.body' -> event["content"]["body"] # Maps strings of e.g. 'content.body' -> event["content"]["body"]
self._value_cache = _flatten_dict(event) self._value_cache = _flatten_dict(event)
@ -81,6 +88,10 @@ class PushRuleEvaluatorForEvent(object):
return _room_member_count( return _room_member_count(
self._event, condition, self._room_member_count self._event, condition, self._room_member_count
) )
elif condition['kind'] == 'sender_power_level':
return _sender_power_level(
self._event, condition, self._sender_power_level
)
else: else:
return True return True