From 64e1ca539830a2140d6bac9d57c9c7e7b30e3687 Mon Sep 17 00:00:00 2001 From: hayyaksi <193020925+hayaksi1@users.noreply.github.com> Date: Thu, 13 Aug 2026 09:39:06 +0000 Subject: [PATCH] Stop showing an unread count on a room you have left (#34595) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- apps/web/src/RoomNotifs.test.ts | 14 ++++++++++++++ apps/web/src/RoomNotifs.ts | 11 +++++++++++ 2 files changed, 25 insertions(+) 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,