Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 9de5654

Browse files
clokepgermain-ggjustjanne
authored
Check each thread for unread messages. (#9723)
Co-authored-by: Germain <[email protected]> Co-authored-by: Janne Mareike Koschinski <[email protected]>
1 parent dec72c7 commit 9de5654

File tree

3 files changed

+308
-96
lines changed

3 files changed

+308
-96
lines changed

src/Unread.ts

+23-22
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import { Room } from "matrix-js-sdk/src/models/room";
18+
import { Thread } from "matrix-js-sdk/src/models/thread";
1819
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
1920
import { EventType } from "matrix-js-sdk/src/@types/event";
2021
import { M_BEACON } from "matrix-js-sdk/src/@types/beacon";
@@ -59,34 +60,34 @@ export function doesRoomHaveUnreadMessages(room: Room): boolean {
5960
return false;
6061
}
6162

62-
const myUserId = MatrixClientPeg.get().getUserId();
63-
64-
// get the most recent read receipt sent by our account.
65-
// N.B. this is NOT a read marker (RM, aka "read up to marker"),
66-
// despite the name of the method :((
67-
const readUpToId = room.getEventReadUpTo(myUserId);
68-
69-
if (!SettingsStore.getValue("feature_thread")) {
70-
// as we don't send RRs for our own messages, make sure we special case that
71-
// if *we* sent the last message into the room, we consider it not unread!
72-
// Should fix: https://github.com/vector-im/element-web/issues/3263
73-
// https://github.com/vector-im/element-web/issues/2427
74-
// ...and possibly some of the others at
75-
// https://github.com/vector-im/element-web/issues/3363
76-
if (room.timeline.length && room.timeline[room.timeline.length - 1].getSender() === myUserId) {
77-
return false;
63+
for (const timeline of [room, ...room.getThreads()]) {
64+
// If the current timeline has unread messages, we're done.
65+
if (doesRoomOrThreadHaveUnreadMessages(timeline)) {
66+
return true;
7867
}
7968
}
69+
// If we got here then no timelines were found with unread messages.
70+
return false;
71+
}
72+
73+
function doesRoomOrThreadHaveUnreadMessages(room: Room | Thread): boolean {
74+
const myUserId = MatrixClientPeg.get().getUserId();
8075

81-
// if the read receipt relates to an event is that part of a thread
82-
// we consider that there are no unread messages
83-
// This might be a false negative, but probably the best we can do until
84-
// the read receipts have evolved to cater for threads
85-
const event = room.findEventById(readUpToId);
86-
if (event?.getThread()) {
76+
// as we don't send RRs for our own messages, make sure we special case that
77+
// if *we* sent the last message into the room, we consider it not unread!
78+
// Should fix: https://github.com/vector-im/element-web/issues/3263
79+
// https://github.com/vector-im/element-web/issues/2427
80+
// ...and possibly some of the others at
81+
// https://github.com/vector-im/element-web/issues/3363
82+
if (room.timeline.at(-1)?.getSender() === myUserId) {
8783
return false;
8884
}
8985

86+
// get the most recent read receipt sent by our account.
87+
// N.B. this is NOT a read marker (RM, aka "read up to marker"),
88+
// despite the name of the method :((
89+
const readUpToId = room.getEventReadUpTo(myUserId!);
90+
9091
// this just looks at whatever history we have, which if we've only just started
9192
// up probably won't be very much, so if the last couple of events are ones that
9293
// don't count, we don't know if there are any events that do count between where

0 commit comments

Comments
 (0)