Skip to content

Commit 950bb22

Browse files
authored
Fix muted rooms notifs (#750)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes muted rooms showing normal notification bubbles. #### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings ### AI disclosure: - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You must prove you understand what the code in this PR does. -->
2 parents bdf1e9d + 4d25ede commit 950bb22

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

.changeset/fix-notifs-muted.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Fix muted rooms appearing as standard unread rooms.

src/app/utils/room.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import type {
1515
StateEvents,
1616
} from '$types/matrix-sdk';
1717
import {
18-
ConditionKind,
1918
EventTimeline,
2019
EventType,
2120
JoinRule,
2221
NotificationCountType,
2322
PushProcessor,
23+
PushRuleActionName,
2424
RelationType,
2525
MsgType,
2626
KnownMembership,
@@ -178,15 +178,25 @@ export const getOrphanParents = (roomToParents: RoomToParents, roomId: string):
178178
return Array.from(parents).filter((parentRoomId) => !roomToParents.has(parentRoomId));
179179
};
180180

181-
export const isMutedRule = (rule: IPushRule) =>
182-
// Check for empty actions (new spec) or dont_notify (deprecated)
183-
(rule.actions.length === 0 || (rule.actions[0] as unknown as string) === 'dont_notify') &&
184-
rule.conditions?.[0]?.kind === ConditionKind.EventMatch;
181+
const hasNotifyPushAction = (actions: IPushRule['actions']): boolean =>
182+
actions.some((a) => typeof a === 'string' && a === PushRuleActionName.Notify);
185183

186-
export const findMutedRule = (overrideRules: IPushRule[], roomId: string) =>
187-
overrideRules.find((rule) => rule.rule_id === roomId && isMutedRule(rule));
184+
const findRoomMuteOverrideRule = (
185+
overrideRules: IPushRule[] | undefined,
186+
roomId: string
187+
): IPushRule | undefined =>
188+
overrideRules?.find(
189+
(rule) =>
190+
rule.rule_id === roomId && rule.rule_id.startsWith('!') && !hasNotifyPushAction(rule.actions)
191+
);
188192

189193
export const getNotificationType = (mx: MatrixClient, roomId: string): NotificationType => {
194+
const overrideRules = mx.getAccountData(EventType.PushRules)?.getContent<IPushRules>()
195+
?.global?.override;
196+
if (findRoomMuteOverrideRule(overrideRules, roomId)) {
197+
return NotificationType.Mute;
198+
}
199+
190200
let roomPushRule: IPushRule | undefined;
191201
try {
192202
roomPushRule = mx.getRoomPushRule('global', roomId);
@@ -195,11 +205,7 @@ export const getNotificationType = (mx: MatrixClient, roomId: string): Notificat
195205
}
196206

197207
if (!roomPushRule) {
198-
const overrideRules = mx.getAccountData(EventType.PushRules)?.getContent<IPushRules>()
199-
?.global?.override;
200-
if (!overrideRules) return NotificationType.Default;
201-
202-
return findMutedRule(overrideRules, roomId) ? NotificationType.Mute : NotificationType.Default;
208+
return NotificationType.Default;
203209
}
204210

205211
if ((roomPushRule.actions[0] as string) === 'notify') return NotificationType.AllMessages;
@@ -254,6 +260,7 @@ export const roomHaveNotification = (room: Room): boolean => {
254260
};
255261

256262
export const roomHaveUnread = (mx: MatrixClient, room: Room) => {
263+
if (getNotificationType(mx, room.roomId) === NotificationType.Mute) return false;
257264
const userId = mx.getUserId();
258265
if (!userId) return false;
259266
const readUpToId = room.getEventReadUpTo(userId);
@@ -290,6 +297,10 @@ type UnreadInfoOptions = {
290297
const unreadInfoFixupInProgress = new WeakSet<Room>();
291298

292299
export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadInfo => {
300+
if (getNotificationType(room.client, room.roomId) === NotificationType.Mute) {
301+
return { roomId: room.roomId, highlight: 0, total: 0 };
302+
}
303+
293304
const userId = room.client.getUserId();
294305
if (userId && options?.applyFixup && !unreadInfoFixupInProgress.has(room)) {
295306
unreadInfoFixupInProgress.add(room);

0 commit comments

Comments
 (0)