2016-08-17 17:26:37 +00:00
|
|
|
/*
|
2023-01-31 09:58:17 +00:00
|
|
|
Copyright 2016, 2019, 2023 The Matrix.org Foundation C.I.C.
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { PushProcessor } from "matrix-js-sdk/src/pushprocessor";
|
2023-08-04 07:36:16 +00:00
|
|
|
import {
|
|
|
|
NotificationCountType,
|
|
|
|
ConditionKind,
|
|
|
|
PushRuleActionName,
|
|
|
|
PushRuleKind,
|
|
|
|
TweakName,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2021-09-27 12:32:04 +00:00
|
|
|
|
2023-08-04 07:36:16 +00:00
|
|
|
import type { IPushRule, Room, MatrixClient } from "matrix-js-sdk/src/matrix";
|
2024-01-15 15:25:48 +00:00
|
|
|
import { NotificationLevel } from "./stores/notifications/NotificationLevel";
|
2023-01-31 09:58:17 +00:00
|
|
|
import { getUnsentMessages } from "./components/structures/RoomStatusBar";
|
|
|
|
import { doesRoomHaveUnreadMessages, doesRoomOrThreadHaveUnreadMessages } from "./Unread";
|
2023-09-19 11:24:35 +00:00
|
|
|
import { EffectiveMembership, getEffectiveMembership, isKnockDenied } from "./utils/membership";
|
2023-02-03 12:00:33 +00:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2021-09-27 12:32:04 +00:00
|
|
|
export enum RoomNotifState {
|
|
|
|
AllMessagesLoud = "all_messages_loud",
|
|
|
|
AllMessages = "all_messages",
|
|
|
|
MentionsOnly = "mentions_only",
|
|
|
|
Mute = "mute",
|
|
|
|
}
|
2016-08-17 17:26:37 +00:00
|
|
|
|
2023-01-31 09:58:17 +00:00
|
|
|
export function getRoomNotifsState(client: MatrixClient, roomId: string): RoomNotifState | null {
|
2022-11-29 10:55:15 +00:00
|
|
|
if (client.isGuest()) return RoomNotifState.AllMessages;
|
2016-08-18 13:00:14 +00:00
|
|
|
|
2016-08-17 17:26:37 +00:00
|
|
|
// look through the override rules for a rule affecting this room:
|
|
|
|
// if one exists, it will take precedence.
|
2023-06-01 13:43:24 +00:00
|
|
|
const muteRule = findOverrideMuteRule(client, roomId);
|
2016-08-18 13:00:14 +00:00
|
|
|
if (muteRule) {
|
2021-09-27 12:32:04 +00:00
|
|
|
return RoomNotifState.Mute;
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// for everything else, look at the room rule.
|
2023-02-13 17:01:43 +00:00
|
|
|
let roomRule: IPushRule | undefined;
|
2018-02-06 17:50:53 +00:00
|
|
|
try {
|
2022-11-29 10:55:15 +00:00
|
|
|
roomRule = client.getRoomPushRule("global", roomId);
|
2018-02-06 17:50:53 +00:00
|
|
|
} catch (err) {
|
|
|
|
// Possible that the client doesn't have pushRules yet. If so, it
|
2022-03-04 09:39:16 +00:00
|
|
|
// hasn't started either, so indicate that this room is not notifying.
|
2018-02-06 17:50:53 +00:00
|
|
|
return null;
|
|
|
|
}
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
// XXX: We have to assume the default is to notify for all messages
|
|
|
|
// (in particular this will be 'wrong' for one to one rooms because
|
|
|
|
// they will notify loudly for all messages)
|
2022-03-04 09:39:16 +00:00
|
|
|
if (!roomRule?.enabled) return RoomNotifState.AllMessages;
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
// a mute at the room level will still allow mentions
|
|
|
|
// to notify
|
2021-09-27 12:32:04 +00:00
|
|
|
if (isMuteRule(roomRule)) return RoomNotifState.MentionsOnly;
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions);
|
2021-09-27 12:32:04 +00:00
|
|
|
if (actionsObject.tweaks.sound) return RoomNotifState.AllMessagesLoud;
|
2016-08-17 17:26:37 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:43:24 +00:00
|
|
|
export function setRoomNotifsState(client: MatrixClient, roomId: string, newState: RoomNotifState): Promise<void> {
|
2021-09-27 12:32:04 +00:00
|
|
|
if (newState === RoomNotifState.Mute) {
|
2023-06-01 13:43:24 +00:00
|
|
|
return setRoomNotifsStateMuted(client, roomId);
|
2016-08-18 13:00:14 +00:00
|
|
|
} else {
|
2023-06-01 13:43:24 +00:00
|
|
|
return setRoomNotifsStateUnmuted(client, roomId, newState);
|
2016-08-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 17:52:48 +00:00
|
|
|
export function getUnreadNotificationCount(
|
|
|
|
room: Room,
|
|
|
|
type: NotificationCountType,
|
|
|
|
includeThreads: boolean,
|
|
|
|
threadId?: string,
|
|
|
|
): number {
|
|
|
|
const getCountShownForRoom = (r: Room, type: NotificationCountType): number => {
|
|
|
|
return includeThreads ? r.getUnreadNotificationCount(type) : r.getRoomUnreadNotificationCount(type);
|
|
|
|
};
|
|
|
|
|
2022-10-24 06:50:21 +00:00
|
|
|
let notificationCount = !!threadId
|
|
|
|
? room.getThreadUnreadNotificationCount(threadId, type)
|
2024-01-29 17:52:48 +00:00
|
|
|
: getCountShownForRoom(room, type);
|
2019-04-01 22:06:33 +00:00
|
|
|
|
|
|
|
// Check notification counts in the old room just in case there's some lost
|
|
|
|
// there. We only go one level down to avoid performance issues, and theory
|
|
|
|
// is that 1st generation rooms will have already been read by the 3rd generation.
|
2023-02-03 12:00:33 +00:00
|
|
|
const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors");
|
|
|
|
const predecessor = room.findPredecessor(msc3946ProcessDynamicPredecessor);
|
2022-10-24 06:50:21 +00:00
|
|
|
// Exclude threadId, as the same thread can't continue over a room upgrade
|
2023-02-03 12:00:33 +00:00
|
|
|
if (!threadId && predecessor?.roomId) {
|
|
|
|
const oldRoomId = predecessor.roomId;
|
2023-06-01 13:43:24 +00:00
|
|
|
const oldRoom = room.client.getRoom(oldRoomId);
|
2019-04-01 22:06:33 +00:00
|
|
|
if (oldRoom) {
|
|
|
|
// We only ever care if there's highlights in the old room. No point in
|
|
|
|
// notifying the user for unread messages because they would have extreme
|
|
|
|
// difficulty changing their notification preferences away from "All Messages"
|
|
|
|
// and "Noisy".
|
2024-01-29 17:52:48 +00:00
|
|
|
notificationCount += getCountShownForRoom(oldRoom, NotificationCountType.Highlight);
|
2019-04-01 22:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return notificationCount;
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:43:24 +00:00
|
|
|
function setRoomNotifsStateMuted(cli: MatrixClient, roomId: string): Promise<any> {
|
2023-02-13 17:01:43 +00:00
|
|
|
const promises: Promise<unknown>[] = [];
|
2016-08-17 17:26:37 +00:00
|
|
|
|
2016-08-18 13:00:14 +00:00
|
|
|
// delete the room rule
|
|
|
|
const roomRule = cli.getRoomPushRule("global", roomId);
|
|
|
|
if (roomRule) {
|
2021-09-27 12:32:04 +00:00
|
|
|
promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id));
|
2016-08-18 13:00:14 +00:00
|
|
|
}
|
2016-08-17 17:26:37 +00:00
|
|
|
|
2016-08-18 14:21:46 +00:00
|
|
|
// add/replace an override rule to squelch everything in this room
|
|
|
|
// NB. We use the room ID as the name of this rule too, although this
|
|
|
|
// is an override rule, not a room rule: it still pertains to this room
|
|
|
|
// though, so using the room ID as the rule ID is logical and prevents
|
|
|
|
// duplicate copies of the rule.
|
2021-09-27 12:32:04 +00:00
|
|
|
promises.push(
|
|
|
|
cli.addPushRule("global", PushRuleKind.Override, roomId, {
|
2016-08-18 13:00:14 +00:00
|
|
|
conditions: [
|
|
|
|
{
|
2022-05-13 19:13:21 +00:00
|
|
|
kind: ConditionKind.EventMatch,
|
2016-08-18 13:00:14 +00:00
|
|
|
key: "room_id",
|
|
|
|
pattern: roomId,
|
2017-07-01 13:28:12 +00:00
|
|
|
},
|
2016-08-18 13:00:14 +00:00
|
|
|
],
|
2022-05-13 19:13:21 +00:00
|
|
|
actions: [PushRuleActionName.DontNotify],
|
2016-08-18 13:00:14 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2017-07-12 13:04:20 +00:00
|
|
|
return Promise.all(promises);
|
2016-08-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:43:24 +00:00
|
|
|
function setRoomNotifsStateUnmuted(cli: MatrixClient, roomId: string, newState: RoomNotifState): Promise<any> {
|
2023-02-13 17:01:43 +00:00
|
|
|
const promises: Promise<unknown>[] = [];
|
2016-08-18 13:00:14 +00:00
|
|
|
|
2023-06-01 13:43:24 +00:00
|
|
|
const overrideMuteRule = findOverrideMuteRule(cli, roomId);
|
2016-08-18 13:00:14 +00:00
|
|
|
if (overrideMuteRule) {
|
2021-09-27 12:32:04 +00:00
|
|
|
promises.push(cli.deletePushRule("global", PushRuleKind.Override, overrideMuteRule.rule_id));
|
2016-08-18 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 12:32:04 +00:00
|
|
|
if (newState === RoomNotifState.AllMessages) {
|
2016-08-18 15:59:25 +00:00
|
|
|
const roomRule = cli.getRoomPushRule("global", roomId);
|
|
|
|
if (roomRule) {
|
2021-09-27 12:32:04 +00:00
|
|
|
promises.push(cli.deletePushRule("global", PushRuleKind.RoomSpecific, roomRule.rule_id));
|
2016-08-18 15:59:25 +00:00
|
|
|
}
|
2021-09-27 12:32:04 +00:00
|
|
|
} else if (newState === RoomNotifState.MentionsOnly) {
|
|
|
|
promises.push(
|
|
|
|
cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, {
|
2022-05-13 19:13:21 +00:00
|
|
|
actions: [PushRuleActionName.DontNotify],
|
2016-08-17 17:26:37 +00:00
|
|
|
}),
|
|
|
|
);
|
2021-09-27 12:32:04 +00:00
|
|
|
} else if (newState === RoomNotifState.AllMessagesLoud) {
|
|
|
|
promises.push(
|
|
|
|
cli.addPushRule("global", PushRuleKind.RoomSpecific, roomId, {
|
2016-08-18 13:00:14 +00:00
|
|
|
actions: [
|
2022-05-13 19:13:21 +00:00
|
|
|
PushRuleActionName.Notify,
|
2016-08-18 13:00:14 +00:00
|
|
|
{
|
2022-05-13 19:13:21 +00:00
|
|
|
set_tweak: TweakName.Sound,
|
2016-08-18 13:00:14 +00:00
|
|
|
value: "default",
|
2017-07-01 13:28:12 +00:00
|
|
|
},
|
|
|
|
],
|
2016-08-18 13:00:14 +00:00
|
|
|
}),
|
|
|
|
);
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-12 13:04:20 +00:00
|
|
|
return Promise.all(promises);
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 13:43:24 +00:00
|
|
|
function findOverrideMuteRule(cli: MatrixClient | undefined, roomId: string): IPushRule | null {
|
2021-11-19 17:35:11 +00:00
|
|
|
if (!cli?.pushRules?.global?.override) {
|
2018-02-06 17:50:53 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-11-19 17:35:11 +00:00
|
|
|
for (const rule of cli.pushRules.global.override) {
|
2023-05-05 01:53:26 +00:00
|
|
|
if (rule.enabled && isRuleRoomMuteRuleForRoomId(roomId, rule)) {
|
2022-03-04 09:39:16 +00:00
|
|
|
return rule;
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-05 01:53:26 +00:00
|
|
|
/**
|
|
|
|
* Checks if a given rule is a room mute rule as implemented by EW
|
|
|
|
* - matches every event in one room (one condition that is an event match on roomId)
|
|
|
|
* - silences notifications (one action that is `DontNotify`)
|
|
|
|
* @param rule - push rule
|
|
|
|
* @returns {boolean} - true when rule mutes a room
|
|
|
|
*/
|
|
|
|
export function isRuleMaybeRoomMuteRule(rule: IPushRule): boolean {
|
|
|
|
return (
|
|
|
|
// matches every event in one room
|
|
|
|
rule.conditions?.length === 1 &&
|
|
|
|
rule.conditions[0].kind === ConditionKind.EventMatch &&
|
|
|
|
rule.conditions[0].key === "room_id" &&
|
|
|
|
// silences notifications
|
|
|
|
isMuteRule(rule)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a given rule is a room mute rule as implemented by EW
|
|
|
|
* @param roomId - id of room to match
|
|
|
|
* @param rule - push rule
|
|
|
|
* @returns {boolean} true when rule mutes the given room
|
|
|
|
*/
|
|
|
|
function isRuleRoomMuteRuleForRoomId(roomId: string, rule: IPushRule): boolean {
|
|
|
|
if (!isRuleMaybeRoomMuteRule(rule)) {
|
2016-08-17 17:26:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-05 01:53:26 +00:00
|
|
|
// isRuleMaybeRoomMuteRule checks this condition exists
|
|
|
|
const cond = rule.conditions![0]!;
|
|
|
|
return cond.pattern === roomId;
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 17:35:11 +00:00
|
|
|
function isMuteRule(rule: IPushRule): boolean {
|
2023-06-28 14:07:02 +00:00
|
|
|
// DontNotify is equivalent to the empty actions array
|
|
|
|
return (
|
|
|
|
rule.actions.length === 0 || (rule.actions.length === 1 && rule.actions[0] === PushRuleActionName.DontNotify)
|
|
|
|
);
|
2016-08-17 17:26:37 +00:00
|
|
|
}
|
2023-01-31 09:58:17 +00:00
|
|
|
|
2024-01-29 17:52:48 +00:00
|
|
|
/**
|
|
|
|
* Returns an object giving information about the unread state of a room or thread
|
|
|
|
* @param room The room to query, or the room the thread is in
|
|
|
|
* @param threadId The thread to check the unread state of, or undefined to query the main thread
|
|
|
|
* @param includeThreads If threadId is undefined, true to include threads other than the main thread, or
|
|
|
|
* false to exclude them. Ignored if threadId is specified.
|
|
|
|
* @returns
|
|
|
|
*/
|
2023-01-31 09:58:17 +00:00
|
|
|
export function determineUnreadState(
|
2023-01-31 12:38:25 +00:00
|
|
|
room?: Room,
|
2023-01-31 09:58:17 +00:00
|
|
|
threadId?: string,
|
2024-01-29 17:52:48 +00:00
|
|
|
includeThreads?: boolean,
|
2024-01-15 15:25:48 +00:00
|
|
|
): { level: NotificationLevel; symbol: string | null; count: number } {
|
2023-01-31 12:38:25 +00:00
|
|
|
if (!room) {
|
2024-01-15 15:25:48 +00:00
|
|
|
return { symbol: null, count: 0, level: NotificationLevel.None };
|
2023-01-31 12:38:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 09:58:17 +00:00
|
|
|
if (getUnsentMessages(room, threadId).length > 0) {
|
2024-01-15 15:25:48 +00:00
|
|
|
return { symbol: "!", count: 1, level: NotificationLevel.Unsent };
|
2023-01-31 09:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getEffectiveMembership(room.getMyMembership()) === EffectiveMembership.Invite) {
|
2024-01-15 15:25:48 +00:00
|
|
|
return { symbol: "!", count: 1, level: NotificationLevel.Highlight };
|
2023-01-31 09:58:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 11:24:35 +00:00
|
|
|
if (SettingsStore.getValue("feature_ask_to_join") && isKnockDenied(room)) {
|
2024-01-15 15:25:48 +00:00
|
|
|
return { symbol: "!", count: 1, level: NotificationLevel.Highlight };
|
2023-09-19 11:24:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 09:58:17 +00:00
|
|
|
if (getRoomNotifsState(room.client, room.roomId) === RoomNotifState.Mute) {
|
2024-01-15 15:25:48 +00:00
|
|
|
return { symbol: null, count: 0, level: NotificationLevel.None };
|
2023-01-31 09:58:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 17:52:48 +00:00
|
|
|
const redNotifs = getUnreadNotificationCount(
|
|
|
|
room,
|
|
|
|
NotificationCountType.Highlight,
|
|
|
|
includeThreads ?? false,
|
|
|
|
threadId,
|
|
|
|
);
|
|
|
|
const greyNotifs = getUnreadNotificationCount(room, NotificationCountType.Total, includeThreads ?? false, threadId);
|
2023-01-31 09:58:17 +00:00
|
|
|
|
|
|
|
const trueCount = greyNotifs || redNotifs;
|
|
|
|
if (redNotifs > 0) {
|
2024-01-15 15:25:48 +00:00
|
|
|
return { symbol: null, count: trueCount, level: NotificationLevel.Highlight };
|
2023-01-31 09:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (greyNotifs > 0) {
|
2024-01-15 15:25:48 +00:00
|
|
|
return { symbol: null, count: trueCount, level: NotificationLevel.Notification };
|
2023-01-31 09:58:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 08:43:15 +00:00
|
|
|
// We don't have any notified messages, but we might have unread messages. Let's find out.
|
2023-11-29 13:36:52 +00:00
|
|
|
let hasUnread = false;
|
|
|
|
if (threadId) {
|
|
|
|
const thread = room.getThread(threadId);
|
|
|
|
if (thread) {
|
|
|
|
hasUnread = doesRoomOrThreadHaveUnreadMessages(thread);
|
|
|
|
}
|
|
|
|
// If the thread does not exist, assume it contains no unreads
|
|
|
|
} else {
|
2024-01-29 17:52:48 +00:00
|
|
|
hasUnread = doesRoomHaveUnreadMessages(room, includeThreads ?? false);
|
2023-11-29 13:36:52 +00:00
|
|
|
}
|
2023-01-31 09:58:17 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
symbol: null,
|
|
|
|
count: trueCount,
|
2024-01-15 15:25:48 +00:00
|
|
|
level: hasUnread ? NotificationLevel.Activity : NotificationLevel.None,
|
2023-01-31 09:58:17 +00:00
|
|
|
};
|
|
|
|
}
|