@@ -227,7 +227,7 @@ async def get_unread_event_push_actions_by_room_for_user(
227227 user_id : str ,
228228 ) -> NotifCounts :
229229 """Get the notification count, the highlight count and the unread message count
230- for a given user in a given room after the given read receipt.
230+ for a given user in a given room after their latest read receipt.
231231
232232 Note that this function assumes the user to be a current member of the room,
233233 since it's either called by the sync handler to handle joined room entries, or by
@@ -238,9 +238,8 @@ async def get_unread_event_push_actions_by_room_for_user(
238238 user_id: The user to retrieve the counts for.
239239
240240 Returns
241- A dict containing the counts mentioned earlier in this docstring,
242- respectively under the keys "notify_count", "highlight_count" and
243- "unread_count".
241+ A NotifCounts object containing the notification count, the highlight count
242+ and the unread message count.
244243 """
245244 return await self .db_pool .runInteraction (
246245 "get_unread_event_push_actions_by_room" ,
@@ -255,6 +254,7 @@ def _get_unread_counts_by_receipt_txn(
255254 room_id : str ,
256255 user_id : str ,
257256 ) -> NotifCounts :
257+ # Get the stream ordering of the user's latest receipt in the room.
258258 result = self .get_last_receipt_for_user_txn (
259259 txn ,
260260 user_id ,
@@ -266,13 +266,11 @@ def _get_unread_counts_by_receipt_txn(
266266 ),
267267 )
268268
269- stream_ordering = None
270269 if result :
271270 _ , stream_ordering = result
272271
273- if stream_ordering is None :
274- # Either last_read_event_id is None, or it's an event we don't have (e.g.
275- # because it's been purged), in which case retrieve the stream ordering for
272+ else :
273+ # If the user has no receipts in the room, retrieve the stream ordering for
276274 # the latest membership event from this user in this room (which we assume is
277275 # a join).
278276 event_id = self .db_pool .simple_select_one_onecol_txn (
@@ -289,10 +287,26 @@ def _get_unread_counts_by_receipt_txn(
289287 )
290288
291289 def _get_unread_counts_by_pos_txn (
292- self , txn : LoggingTransaction , room_id : str , user_id : str , stream_ordering : int
290+ self ,
291+ txn : LoggingTransaction ,
292+ room_id : str ,
293+ user_id : str ,
294+ receipt_stream_ordering : int ,
293295 ) -> NotifCounts :
294296 """Get the number of unread messages for a user/room that have happened
295297 since the given stream ordering.
298+
299+ Args:
300+ txn: The database transaction.
301+ room_id: The room ID to get unread counts for.
302+ user_id: The user ID to get unread counts for.
303+ receipt_stream_ordering: The stream ordering of the user's latest
304+ receipt in the room. If there are no receipts, the stream ordering
305+ of the user's join event.
306+
307+ Returns
308+ A NotifCounts object containing the notification count, the highlight count
309+ and the unread message count.
296310 """
297311
298312 counts = NotifCounts ()
@@ -320,7 +334,7 @@ def _get_unread_counts_by_pos_txn(
320334 OR last_receipt_stream_ordering = ?
321335 )
322336 """ ,
323- (room_id , user_id , stream_ordering , stream_ordering ),
337+ (room_id , user_id , receipt_stream_ordering , receipt_stream_ordering ),
324338 )
325339 row = txn .fetchone ()
326340
@@ -338,17 +352,20 @@ def _get_unread_counts_by_pos_txn(
338352 AND stream_ordering > ?
339353 AND highlight = 1
340354 """
341- txn .execute (sql , (user_id , room_id , stream_ordering ))
355+ txn .execute (sql , (user_id , room_id , receipt_stream_ordering ))
342356 row = txn .fetchone ()
343357 if row :
344358 counts .highlight_count += row [0 ]
345359
346360 # Finally we need to count push actions that aren't included in the
347- # summary returned above, e.g. recent events that haven't been
348- # summarised yet, or the summary is empty due to a recent read receipt.
349- stream_ordering = max (stream_ordering , summary_stream_ordering )
361+ # summary returned above. This might be due to recent events that haven't
362+ # been summarised yet or the summary is out of date due to a recent read
363+ # receipt.
364+ start_unread_stream_ordering = max (
365+ receipt_stream_ordering , summary_stream_ordering
366+ )
350367 notify_count , unread_count = self ._get_notif_unread_count_for_user_room (
351- txn , room_id , user_id , stream_ordering
368+ txn , room_id , user_id , start_unread_stream_ordering
352369 )
353370
354371 counts .notify_count += notify_count
@@ -1151,8 +1168,6 @@ def _rotate_notifs_before_txn(
11511168 txn: The database transaction.
11521169 old_rotate_stream_ordering: The previous maximum event stream ordering.
11531170 rotate_to_stream_ordering: The new maximum event stream ordering to summarise.
1154-
1155- Returns whether the archiving process has caught up or not.
11561171 """
11571172
11581173 # Calculate the new counts that should be upserted into event_push_summary
@@ -1238,9 +1253,7 @@ def _rotate_notifs_before_txn(
12381253 (rotate_to_stream_ordering ,),
12391254 )
12401255
1241- async def _remove_old_push_actions_that_have_rotated (
1242- self ,
1243- ) -> None :
1256+ async def _remove_old_push_actions_that_have_rotated (self ) -> None :
12441257 """Clear out old push actions that have been summarised."""
12451258
12461259 # We want to clear out anything that is older than a day that *has* already
0 commit comments