Merge pull request #520 from matrix-org/dbkr/bulk_push_overlay_enabled

Overlay the push_rules_enabled map for users
This commit is contained in:
David Baker 2016-01-22 15:25:52 +00:00
commit 74f49f99f9
2 changed files with 46 additions and 0 deletions

View File

@ -36,6 +36,7 @@ def decode_rule_json(rule):
@defer.inlineCallbacks @defer.inlineCallbacks
def _get_rules(room_id, user_ids, store): def _get_rules(room_id, user_ids, store):
rules_by_user = yield store.bulk_get_push_rules(user_ids) rules_by_user = yield store.bulk_get_push_rules(user_ids)
rules_enabled_by_user = yield store.bulk_get_push_rules_enabled(user_ids)
rules_by_user = { rules_by_user = {
uid: baserules.list_with_base_rules([ uid: baserules.list_with_base_rules([
@ -44,6 +45,22 @@ def _get_rules(room_id, user_ids, store):
]) ])
for uid in user_ids for uid in user_ids
} }
# We apply the rules-enabled map here: bulk_get_push_rules doesn't
# fetch disabled rules, but this won't account for any server default
# rules the user has disabled, so we need to do this too.
for uid in user_ids:
if uid not in rules_enabled_by_user:
continue
user_enabled_map = rules_enabled_by_user[uid]
for rule in rules_by_user[uid]:
rule_id = rule['rule_id']
if rule_id in user_enabled_map:
rule['enabled'] = user_enabled_map[rule_id]
defer.returnValue(rules_by_user) defer.returnValue(rules_by_user)

View File

@ -93,6 +93,35 @@ class PushRuleStore(SQLBaseStore):
results.setdefault(row['user_name'], []).append(row) results.setdefault(row['user_name'], []).append(row)
defer.returnValue(results) defer.returnValue(results)
@defer.inlineCallbacks
def bulk_get_push_rules_enabled(self, user_ids):
if not user_ids:
defer.returnValue({})
batch_size = 100
def f(txn, user_ids_to_fetch):
sql = (
"SELECT user_name, rule_id, enabled"
" FROM push_rules_enable"
" WHERE user_name"
" IN (" + ",".join("?" for _ in user_ids_to_fetch) + ")"
)
txn.execute(sql, user_ids_to_fetch)
return self.cursor_to_dict(txn)
results = {}
chunks = [user_ids[i:i+batch_size] for i in xrange(0, len(user_ids), batch_size)]
for batch_user_ids in chunks:
rows = yield self.runInteraction(
"bulk_get_push_rules_enabled", f, batch_user_ids
)
for row in rows:
results.setdefault(row['user_name'], {})[row['rule_id']] = row['enabled']
defer.returnValue(results)
@defer.inlineCallbacks @defer.inlineCallbacks
def add_push_rule(self, before, after, **kwargs): def add_push_rule(self, before, after, **kwargs):
vals = kwargs vals = kwargs