diff --git a/apps/web/src/RoomNotifs.test.ts b/apps/web/src/RoomNotifs.test.ts index a94f8435d4..9b27d81c28 100644 --- a/apps/web/src/RoomNotifs.test.ts +++ b/apps/web/src/RoomNotifs.test.ts @@ -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); diff --git a/apps/web/src/RoomNotifs.ts b/apps/web/src/RoomNotifs.ts index 4489dbf6c3..3dc07089ea 100644 --- a/apps/web/src/RoomNotifs.ts +++ b/apps/web/src/RoomNotifs.ts @@ -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,