Use notification levels in power_levels
Rather than making the condition directly require a specific power level. This way the level require to notify a room can be configured per room.
This commit is contained in:
parent
c9f034b4ac
commit
0f1eb3e914
|
@ -249,8 +249,8 @@ BASE_APPEND_OVERRIDE_RULES = [
|
||||||
'_id': '_roomnotif_content',
|
'_id': '_roomnotif_content',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'kind': 'sender_power_level',
|
'kind': 'sender_notification_permission',
|
||||||
'is': '>=50',
|
'key': 'room',
|
||||||
'_id': '_roomnotif_pl',
|
'_id': '_roomnotif_pl',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
|
@ -113,7 +113,7 @@ class BulkPushRuleEvaluator(object):
|
||||||
)
|
)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _get_sender_power_level(self, event, context):
|
def _get_power_levels_and_sender_level(self, event, context):
|
||||||
pl_event_id = context.prev_state_ids.get(POWER_KEY)
|
pl_event_id = context.prev_state_ids.get(POWER_KEY)
|
||||||
if pl_event_id:
|
if pl_event_id:
|
||||||
# fastpath: if there's a power level event, that's all we need, and
|
# fastpath: if there's a power level event, that's all we need, and
|
||||||
|
@ -129,7 +129,9 @@ class BulkPushRuleEvaluator(object):
|
||||||
(e.type, e.state_key): e for e in auth_events.itervalues()
|
(e.type, e.state_key): e for e in auth_events.itervalues()
|
||||||
}
|
}
|
||||||
|
|
||||||
defer.returnValue(get_user_power_level(event.sender, auth_events))
|
sender_level = get_user_power_level(event.sender, auth_events)
|
||||||
|
|
||||||
|
defer.returnValue((auth_events[POWER_KEY].content, sender_level))
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def action_for_event_by_user(self, event, context):
|
def action_for_event_by_user(self, event, context):
|
||||||
|
@ -146,10 +148,10 @@ class BulkPushRuleEvaluator(object):
|
||||||
event, context
|
event, context
|
||||||
)
|
)
|
||||||
|
|
||||||
sender_power_level = yield self._get_sender_power_level(event, context)
|
(power_levels, sender_power_level) = yield self._get_power_levels_and_sender_level(event, context)
|
||||||
|
|
||||||
evaluator = PushRuleEvaluatorForEvent(
|
evaluator = PushRuleEvaluatorForEvent(
|
||||||
event, len(room_members), sender_power_level
|
event, len(room_members), sender_power_level, power_levels,
|
||||||
)
|
)
|
||||||
|
|
||||||
condition_cache = {}
|
condition_cache = {}
|
||||||
|
|
|
@ -33,8 +33,15 @@ def _room_member_count(ev, condition, room_member_count):
|
||||||
return _test_ineq_condition(condition, room_member_count)
|
return _test_ineq_condition(condition, room_member_count)
|
||||||
|
|
||||||
|
|
||||||
def _sender_power_level(ev, condition, power_level):
|
def _sender_notification_permission(ev, condition, sender_power_level, power_levels):
|
||||||
return _test_ineq_condition(condition, power_level)
|
notif_level_key = condition.get('key')
|
||||||
|
if notif_level_key is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
notif_levels = power_levels.get('notifications', {})
|
||||||
|
room_notif_level = notif_levels.get(notif_level_key, 50)
|
||||||
|
|
||||||
|
return sender_power_level >= room_notif_level;
|
||||||
|
|
||||||
|
|
||||||
def _test_ineq_condition(condition, number):
|
def _test_ineq_condition(condition, number):
|
||||||
|
@ -74,10 +81,11 @@ def tweaks_for_actions(actions):
|
||||||
|
|
||||||
|
|
||||||
class PushRuleEvaluatorForEvent(object):
|
class PushRuleEvaluatorForEvent(object):
|
||||||
def __init__(self, event, room_member_count, sender_power_level):
|
def __init__(self, event, room_member_count, sender_power_level, power_levels):
|
||||||
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
|
self._sender_power_level = sender_power_level
|
||||||
|
self._power_levels = power_levels
|
||||||
|
|
||||||
# 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)
|
||||||
|
@ -91,9 +99,9 @@ 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':
|
elif condition['kind'] == 'sender_notification_permission':
|
||||||
return _sender_power_level(
|
return _sender_notification_permission(
|
||||||
self._event, condition, self._sender_power_level
|
self._event, condition, self._sender_power_level, self._power_levels,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in New Issue