0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-11-16 15:01:23 +01:00

Use a more efficient way of calculating counters

This commit is contained in:
Brendan Abolivier 2020-06-10 17:54:33 +01:00
parent ef345c5a7b
commit c7b99a1180
No known key found for this signature in database
GPG key ID: 1E015C145F1916CD

View file

@ -123,22 +123,31 @@ class EventPushActionsWorkerStore(SQLBaseStore):
def _get_unread_counts_by_pos_txn(self, txn, room_id, user_id, stream_ordering): def _get_unread_counts_by_pos_txn(self, txn, room_id, user_id, stream_ordering):
# First get number of notifications. # First get number of actions, grouped on whether the action notifies.
# We ignore the notif column, given we want unread counts irrespective of
# whether the notification actually sent a push or not.
sql = ( sql = (
"SELECT count(*)" "SELECT count(*), notif"
" FROM event_push_actions ea" " FROM event_push_actions ea"
" WHERE" " WHERE"
" user_id = ?" " user_id = ?"
" AND room_id = ?" " AND room_id = ?"
" AND stream_ordering > ?" " AND stream_ordering > ?"
" AND notif = 1" " GROUP BY notif"
) )
txn.execute(sql, (user_id, room_id, stream_ordering)) txn.execute(sql, (user_id, room_id, stream_ordering))
row = txn.fetchone() rows = txn.fetchall()
notify_count = row[0] if row else 0
# We should get a maximum number of two rows: one for notif = 0, which is the
# number of actions that contribute to the unread_count but not to the
# notify_count, and one for notif = 1, which is the number of actions that
# contribute to both counters. If one or both rows don't appear, then the
# value for the matching counter should be 0.
unread_count = 0
notify_count = 0
for row in rows:
if row[1] == 0:
unread_count = row[0]
if row[1] == 1:
notify_count = row[0]
txn.execute( txn.execute(
""" """
@ -151,20 +160,8 @@ class EventPushActionsWorkerStore(SQLBaseStore):
if rows: if rows:
notify_count += rows[0][0] notify_count += rows[0][0]
# Now get the number of unread messages in the room, i.e. messages that matched # Now that we've got the final notify_count, add it to unread_count, as notify
# both a mark_unread rule and a notify one. # actions also contribute to the unread count.
sql = (
"SELECT count(*)"
" FROM event_push_actions ea"
" WHERE"
" user_id = ?"
" AND room_id = ?"
" AND stream_ordering > ?"
" AND notif = 0"
)
txn.execute(sql, (user_id, room_id, stream_ordering))
row = txn.fetchone()
unread_count = row[0] if row else 0
unread_count += notify_count unread_count += notify_count
# Now get the number of highlights # Now get the number of highlights
@ -183,9 +180,9 @@ class EventPushActionsWorkerStore(SQLBaseStore):
highlight_count = row[0] if row else 0 highlight_count = row[0] if row else 0
return { return {
"unread_count": unread_count,
"notify_count": notify_count, "notify_count": notify_count,
"highlight_count": highlight_count, "highlight_count": highlight_count,
"unread_count": unread_count,
} }
@defer.inlineCallbacks @defer.inlineCallbacks