Stop showing an unread count on a room you have left (#34595)

Leaving a room does not clear the notification counts the server last sent
for it, and nothing on our side clears them either, so a room sitting in
the historical list kept a badge that could never be got rid of: there is
no way to read a room you are not in.

The unread state of such a room is now nothing at all. Our own member
event is what decides it rather than getMyMembership(), which answers
"leave" both for a room we have left and for one we have not heard about
yet — blanking a badge that is merely late would be a worse bug than the
one being fixed here.

It goes below the invite and knock checks, which are leaves of a sort and
have their own answers, so a pending invite and a refused knock still show
what they showed before.

Tests: a left room with a count of 42 now reports nothing.

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
hayyaksi
2026-08-13 09:39:06 +00:00
committed by GitHub
parent e4da468d07
commit 64e1ca5398
2 changed files with 25 additions and 0 deletions
+14
View File
@@ -365,6 +365,20 @@ describe("RoomNotifs test", () => {
expect(count).toBeGreaterThan(0);
});
it("shows nothing for a room the user has left", async () => {
// Whatever the server last told us about is stuck there: leaving does not clear it, and
// there is no way to read a room you are not in.
room.setUnreadNotificationCount(NotificationCountType.Total, 42);
const roomMember = mkRoomMember(room.roomId, room.myUserId, KnownMembership.Leave);
vi.spyOn(room, "getMember").mockReturnValue(roomMember);
const { level, symbol, count } = determineUnreadState(room);
expect(symbol).toBe(null);
expect(level).toBe(NotificationLevel.None);
expect(count).toBe(0);
});
it("shows nothing for muted channels", async () => {
room.setUnreadNotificationCount(NotificationCountType.Highlight, 99);
room.setUnreadNotificationCount(NotificationCountType.Total, 99);
+11
View File
@@ -15,6 +15,7 @@ import {
TweakName,
EventStatus,
} from "matrix-js-sdk/src/matrix";
import { KnownMembership } from "matrix-js-sdk/src/types";
import type { IPushRule, Room, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { NotificationLevel } from "./stores/notifications/NotificationLevel";
@@ -281,6 +282,16 @@ export function determineUnreadState(
return { symbol: null, count: 0, level: NotificationLevel.None, invited: false };
}
// A room we are no longer in has nothing left to read, and whatever count it was holding when we
// left can never be cleared, so it would sit there for good. Our own member event is what says so:
// getMyMembership() cannot tell a room we have left apart from one we have yet to hear about, and
// treating the second as the first would blank a badge that is only late. The knock case above is
// also a leave, which is why this comes after it.
const myMembership = room.getMember(room.myUserId)?.membership;
if (myMembership === KnownMembership.Leave || myMembership === KnownMembership.Ban) {
return { symbol: null, count: 0, level: NotificationLevel.None, invited: false };
}
const redNotifs = getUnreadNotificationCount(
room,
NotificationCountType.Highlight,