element-web/src/RoomNotifs.ts

206 lines
7.4 KiB
TypeScript
Raw Normal View History

/*
Copyright 2016 OpenMarket Ltd
2019-06-19 10:48:47 +00:00
Copyright 2019 The Matrix.org Foundation C.I.C.
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';
import { NotificationCountType, Room } from "matrix-js-sdk/src/models/room";
2022-05-13 19:13:21 +00:00
import {
ConditionKind,
IPushRule,
PushRuleActionName,
PushRuleKind,
TweakName,
} from "matrix-js-sdk/src/@types/PushRules";
import { EventType } from 'matrix-js-sdk/src/@types/event';
import { MatrixClient } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from './MatrixClientPeg';
export enum RoomNotifState {
AllMessagesLoud = 'all_messages_loud',
AllMessages = 'all_messages',
MentionsOnly = 'mentions_only',
Mute = 'mute',
}
export function getRoomNotifsState(client: MatrixClient, roomId: string): RoomNotifState {
if (client.isGuest()) return RoomNotifState.AllMessages;
2016-08-18 13:00:14 +00:00
// look through the override rules for a rule affecting this room:
// if one exists, it will take precedence.
const muteRule = findOverrideMuteRule(roomId);
2016-08-18 13:00:14 +00:00
if (muteRule) {
return RoomNotifState.Mute;
}
// for everything else, look at the room rule.
let roomRule = null;
try {
roomRule = client.getRoomPushRule('global', roomId);
} catch (err) {
// Possible that the client doesn't have pushRules yet. If so, it
// hasn't started either, so indicate that this room is not notifying.
return null;
}
// 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)
if (!roomRule?.enabled) return RoomNotifState.AllMessages;
// a mute at the room level will still allow mentions
// to notify
if (isMuteRule(roomRule)) return RoomNotifState.MentionsOnly;
const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions);
if (actionsObject.tweaks.sound) return RoomNotifState.AllMessagesLoud;
return null;
}
export function setRoomNotifsState(roomId: string, newState: RoomNotifState): Promise<void> {
if (newState === RoomNotifState.Mute) {
2016-08-18 13:00:14 +00:00
return setRoomNotifsStateMuted(roomId);
} else {
return setRoomNotifsStateUnmuted(roomId, newState);
}
}
export function getUnreadNotificationCount(
room: Room,
type: NotificationCountType,
threadId?: string,
): number {
let notificationCount = (!!threadId
? room.getThreadUnreadNotificationCount(threadId, type)
: room.getUnreadNotificationCount(type));
// 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.
const createEvent = room.currentState.getStateEvents(EventType.RoomCreate, "");
const predecessor = createEvent?.getContent().predecessor;
// Exclude threadId, as the same thread can't continue over a room upgrade
if (!threadId && predecessor) {
const oldRoomId = predecessor.room_id;
const oldRoom = MatrixClientPeg.get().getRoom(oldRoomId);
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".
notificationCount += oldRoom.getUnreadNotificationCount(NotificationCountType.Highlight);
}
}
return notificationCount;
}
function setRoomNotifsStateMuted(roomId: string): Promise<any> {
const cli = MatrixClientPeg.get();
const promises = [];
2016-08-18 13:00:14 +00:00
// delete the room rule
const roomRule = cli.getRoomPushRule('global', roomId);
if (roomRule) {
promises.push(cli.deletePushRule('global', PushRuleKind.RoomSpecific, roomRule.rule_id));
2016-08-18 13:00:14 +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.
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,
},
2016-08-18 13:00:14 +00:00
],
actions: [
2022-05-13 19:13:21 +00:00
PushRuleActionName.DontNotify,
],
2016-08-18 13:00:14 +00:00
}));
return Promise.all(promises);
2016-08-18 13:00:14 +00:00
}
function setRoomNotifsStateUnmuted(roomId: string, newState: RoomNotifState): Promise<any> {
2016-08-18 13:00:14 +00:00
const cli = MatrixClientPeg.get();
const promises = [];
const overrideMuteRule = findOverrideMuteRule(roomId);
if (overrideMuteRule) {
promises.push(cli.deletePushRule('global', PushRuleKind.Override, overrideMuteRule.rule_id));
2016-08-18 13:00:14 +00:00
}
if (newState === RoomNotifState.AllMessages) {
const roomRule = cli.getRoomPushRule('global', roomId);
if (roomRule) {
promises.push(cli.deletePushRule('global', PushRuleKind.RoomSpecific, roomRule.rule_id));
}
} else if (newState === RoomNotifState.MentionsOnly) {
promises.push(cli.addPushRule('global', PushRuleKind.RoomSpecific, roomId, {
actions: [
2022-05-13 19:13:21 +00:00
PushRuleActionName.DontNotify,
],
}));
2016-08-18 13:00:14 +00:00
// https://matrix.org/jira/browse/SPEC-400
promises.push(cli.setPushRuleEnabled('global', PushRuleKind.RoomSpecific, roomId, true));
} 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',
},
],
2016-08-18 13:00:14 +00:00
}));
// https://matrix.org/jira/browse/SPEC-400
promises.push(cli.setPushRuleEnabled('global', PushRuleKind.RoomSpecific, roomId, true));
}
return Promise.all(promises);
}
2021-11-19 17:35:11 +00:00
function findOverrideMuteRule(roomId: string): IPushRule {
const cli = MatrixClientPeg.get();
2021-11-19 17:35:11 +00:00
if (!cli?.pushRules?.global?.override) {
return null;
}
2021-11-19 17:35:11 +00:00
for (const rule of cli.pushRules.global.override) {
if (rule.enabled && isRuleForRoom(roomId, rule) && isMuteRule(rule)) {
return rule;
}
}
return null;
}
2021-11-19 17:35:11 +00:00
function isRuleForRoom(roomId: string, rule: IPushRule): boolean {
if (rule.conditions?.length !== 1) {
return false;
}
const cond = rule.conditions[0];
2021-11-19 17:35:11 +00:00
return (cond.kind === ConditionKind.EventMatch && cond.key === 'room_id' && cond.pattern === roomId);
}
2021-11-19 17:35:11 +00:00
function isMuteRule(rule: IPushRule): boolean {
return (rule.actions.length === 1 && rule.actions[0] === PushRuleActionName.DontNotify);
}