2016-08-18 01:26:37 +08:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2019-06-19 18:48:47 +08:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2016-08-18 01:26:37 +08: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 20:11:58 +08:00
|
|
|
import { PushProcessor } from 'matrix-js-sdk/src/pushprocessor';
|
2021-09-27 20:32:04 +08:00
|
|
|
import { NotificationCountType, Room } from "matrix-js-sdk/src/models/room";
|
2021-11-20 01:35:11 +08:00
|
|
|
import { ConditionKind, IPushRule, PushRuleActionName, PushRuleKind } from "matrix-js-sdk/src/@types/PushRules";
|
2021-09-27 20:32:04 +08:00
|
|
|
|
2021-10-23 06:23:32 +08:00
|
|
|
import { MatrixClientPeg } from './MatrixClientPeg';
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export enum RoomNotifState {
|
|
|
|
AllMessagesLoud = 'all_messages_loud',
|
|
|
|
AllMessages = 'all_messages',
|
|
|
|
MentionsOnly = 'mentions_only',
|
|
|
|
Mute = 'mute',
|
|
|
|
}
|
2016-08-18 01:26:37 +08:00
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export const BADGE_STATES = [RoomNotifState.AllMessages, RoomNotifState.AllMessagesLoud];
|
|
|
|
export const MENTION_BADGE_STATES = [...BADGE_STATES, RoomNotifState.MentionsOnly];
|
2019-02-06 18:51:29 +08:00
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export function shouldShowNotifBadge(roomNotifState: RoomNotifState): boolean {
|
2019-06-19 19:12:19 +08:00
|
|
|
return BADGE_STATES.includes(roomNotifState);
|
2019-02-06 18:51:29 +08:00
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export function shouldShowMentionBadge(roomNotifState: RoomNotifState): boolean {
|
2019-06-19 19:12:19 +08:00
|
|
|
return MENTION_BADGE_STATES.includes(roomNotifState);
|
2019-02-06 18:51:29 +08:00
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export function aggregateNotificationCount(rooms: Room[]): {count: number, highlight: boolean} {
|
|
|
|
return rooms.reduce<{count: number, highlight: boolean}>((result, room) => {
|
2019-02-06 18:51:29 +08:00
|
|
|
const roomNotifState = getRoomNotifsState(room.roomId);
|
2021-09-27 20:32:04 +08:00
|
|
|
const highlight = room.getUnreadNotificationCount(NotificationCountType.Highlight) > 0;
|
2020-06-25 22:02:52 +08:00
|
|
|
// use helper method to include highlights in the previous version of the room
|
|
|
|
const notificationCount = getUnreadNotificationCount(room);
|
2019-02-06 18:51:29 +08:00
|
|
|
|
2019-06-19 18:46:24 +08:00
|
|
|
const notifBadges = notificationCount > 0 && shouldShowNotifBadge(roomNotifState);
|
|
|
|
const mentionBadges = highlight && shouldShowMentionBadge(roomNotifState);
|
2019-02-06 18:51:29 +08:00
|
|
|
const badges = notifBadges || mentionBadges;
|
|
|
|
|
|
|
|
if (badges) {
|
|
|
|
result.count += notificationCount;
|
|
|
|
if (highlight) {
|
|
|
|
result.highlight = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2021-06-29 20:11:58 +08:00
|
|
|
}, { count: 0, highlight: false });
|
2019-02-06 18:51:29 +08:00
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export function getRoomHasBadge(room: Room): boolean {
|
2019-02-06 18:51:29 +08:00
|
|
|
const roomNotifState = getRoomNotifsState(room.roomId);
|
2021-09-27 20:32:04 +08:00
|
|
|
const highlight = room.getUnreadNotificationCount(NotificationCountType.Highlight) > 0;
|
2019-02-06 18:51:29 +08:00
|
|
|
const notificationCount = room.getUnreadNotificationCount();
|
|
|
|
|
2019-06-19 18:46:24 +08:00
|
|
|
const notifBadges = notificationCount > 0 && shouldShowNotifBadge(roomNotifState);
|
|
|
|
const mentionBadges = highlight && shouldShowMentionBadge(roomNotifState);
|
2019-02-06 18:51:29 +08:00
|
|
|
|
|
|
|
return notifBadges || mentionBadges;
|
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export function getRoomNotifsState(roomId: string): RoomNotifState {
|
|
|
|
if (MatrixClientPeg.get().isGuest()) return RoomNotifState.AllMessages;
|
2016-08-18 21:00:14 +08:00
|
|
|
|
2016-08-18 01:26:37 +08: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 21:00:14 +08:00
|
|
|
if (muteRule) {
|
2021-09-27 20:32:04 +08:00
|
|
|
return RoomNotifState.Mute;
|
2016-08-18 01:26:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// for everything else, look at the room rule.
|
2018-02-07 01:50:53 +08:00
|
|
|
let roomRule = null;
|
|
|
|
try {
|
|
|
|
roomRule = MatrixClientPeg.get().getRoomPushRule('global', roomId);
|
|
|
|
} catch (err) {
|
|
|
|
// Possible that the client doesn't have pushRules yet. If so, it
|
|
|
|
// hasn't started eiher, so indicate that this room is not notifying.
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-18 01:26:37 +08: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)
|
2021-09-27 20:32:04 +08:00
|
|
|
if (!roomRule || !roomRule.enabled) return RoomNotifState.AllMessages;
|
2016-08-18 01:26:37 +08:00
|
|
|
|
|
|
|
// a mute at the room level will still allow mentions
|
|
|
|
// to notify
|
2021-09-27 20:32:04 +08:00
|
|
|
if (isMuteRule(roomRule)) return RoomNotifState.MentionsOnly;
|
2016-08-18 01:26:37 +08:00
|
|
|
|
|
|
|
const actionsObject = PushProcessor.actionListToActionsObject(roomRule.actions);
|
2021-09-27 20:32:04 +08:00
|
|
|
if (actionsObject.tweaks.sound) return RoomNotifState.AllMessagesLoud;
|
2016-08-18 01:26:37 +08:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export function setRoomNotifsState(roomId: string, newState: RoomNotifState): Promise<void> {
|
|
|
|
if (newState === RoomNotifState.Mute) {
|
2016-08-18 21:00:14 +08:00
|
|
|
return setRoomNotifsStateMuted(roomId);
|
|
|
|
} else {
|
|
|
|
return setRoomNotifsStateUnmuted(roomId, newState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
export function getUnreadNotificationCount(room: Room, type: NotificationCountType = null): number {
|
2019-04-02 06:06:33 +08:00
|
|
|
let notificationCount = 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("m.room.create", "");
|
|
|
|
if (createEvent && createEvent.getContent()['predecessor']) {
|
|
|
|
const oldRoomId = createEvent.getContent()['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".
|
2021-09-27 20:32:04 +08:00
|
|
|
notificationCount += oldRoom.getUnreadNotificationCount(NotificationCountType.Highlight);
|
2019-04-02 06:06:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return notificationCount;
|
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
function setRoomNotifsStateMuted(roomId: string): Promise<any> {
|
2016-08-18 01:26:37 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const promises = [];
|
|
|
|
|
2016-08-18 21:00:14 +08:00
|
|
|
// delete the room rule
|
|
|
|
const roomRule = cli.getRoomPushRule('global', roomId);
|
|
|
|
if (roomRule) {
|
2021-09-27 20:32:04 +08:00
|
|
|
promises.push(cli.deletePushRule('global', PushRuleKind.RoomSpecific, roomRule.rule_id));
|
2016-08-18 21:00:14 +08:00
|
|
|
}
|
2016-08-18 01:26:37 +08:00
|
|
|
|
2016-08-18 22:21:46 +08: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 20:32:04 +08:00
|
|
|
promises.push(cli.addPushRule('global', PushRuleKind.Override, roomId, {
|
2016-08-18 21:00:14 +08:00
|
|
|
conditions: [
|
|
|
|
{
|
|
|
|
kind: 'event_match',
|
|
|
|
key: 'room_id',
|
|
|
|
pattern: roomId,
|
2017-07-01 21:28:12 +08:00
|
|
|
},
|
2016-08-18 21:00:14 +08:00
|
|
|
],
|
|
|
|
actions: [
|
|
|
|
'dont_notify',
|
2017-07-01 21:28:12 +08:00
|
|
|
],
|
2016-08-18 21:00:14 +08:00
|
|
|
}));
|
|
|
|
|
2017-07-12 21:04:20 +08:00
|
|
|
return Promise.all(promises);
|
2016-08-18 21:00:14 +08:00
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
function setRoomNotifsStateUnmuted(roomId: string, newState: RoomNotifState): Promise<any> {
|
2016-08-18 21:00:14 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
const overrideMuteRule = findOverrideMuteRule(roomId);
|
|
|
|
if (overrideMuteRule) {
|
2021-09-27 20:32:04 +08:00
|
|
|
promises.push(cli.deletePushRule('global', PushRuleKind.Override, overrideMuteRule.rule_id));
|
2016-08-18 21:00:14 +08:00
|
|
|
}
|
|
|
|
|
2021-09-27 20:32:04 +08:00
|
|
|
if (newState === RoomNotifState.AllMessages) {
|
2016-08-18 23:59:25 +08:00
|
|
|
const roomRule = cli.getRoomPushRule('global', roomId);
|
|
|
|
if (roomRule) {
|
2021-09-27 20:32:04 +08:00
|
|
|
promises.push(cli.deletePushRule('global', PushRuleKind.RoomSpecific, roomRule.rule_id));
|
2016-08-18 23:59:25 +08:00
|
|
|
}
|
2021-09-27 20:32:04 +08:00
|
|
|
} else if (newState === RoomNotifState.MentionsOnly) {
|
|
|
|
promises.push(cli.addPushRule('global', PushRuleKind.RoomSpecific, roomId, {
|
2016-08-18 01:26:37 +08:00
|
|
|
actions: [
|
|
|
|
'dont_notify',
|
2017-07-01 21:28:12 +08:00
|
|
|
],
|
2016-08-18 01:26:37 +08:00
|
|
|
}));
|
2016-08-18 21:00:14 +08:00
|
|
|
// https://matrix.org/jira/browse/SPEC-400
|
2021-09-27 20:32:04 +08:00
|
|
|
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 21:00:14 +08:00
|
|
|
actions: [
|
|
|
|
'notify',
|
|
|
|
{
|
|
|
|
set_tweak: 'sound',
|
|
|
|
value: 'default',
|
2017-07-01 21:28:12 +08:00
|
|
|
},
|
|
|
|
],
|
2016-08-18 21:00:14 +08:00
|
|
|
}));
|
|
|
|
// https://matrix.org/jira/browse/SPEC-400
|
2021-09-27 20:32:04 +08:00
|
|
|
promises.push(cli.setPushRuleEnabled('global', PushRuleKind.RoomSpecific, roomId, true));
|
2016-08-18 01:26:37 +08:00
|
|
|
}
|
|
|
|
|
2017-07-12 21:04:20 +08:00
|
|
|
return Promise.all(promises);
|
2016-08-18 01:26:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-20 01:35:11 +08:00
|
|
|
function findOverrideMuteRule(roomId: string): IPushRule {
|
2020-12-17 01:16:15 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
2021-11-20 01:35:11 +08:00
|
|
|
if (!cli?.pushRules?.global?.override) {
|
2018-02-07 01:50:53 +08:00
|
|
|
return null;
|
|
|
|
}
|
2021-11-20 01:35:11 +08:00
|
|
|
for (const rule of cli.pushRules.global.override) {
|
2016-08-18 01:26:37 +08:00
|
|
|
if (isRuleForRoom(roomId, rule)) {
|
2016-08-18 21:00:14 +08:00
|
|
|
if (isMuteRule(rule) && rule.enabled) {
|
2016-08-18 01:26:37 +08:00
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-11-20 01:35:11 +08:00
|
|
|
function isRuleForRoom(roomId: string, rule: IPushRule): boolean {
|
2016-08-18 01:26:37 +08:00
|
|
|
if (rule.conditions.length !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const cond = rule.conditions[0];
|
2021-11-20 01:35:11 +08:00
|
|
|
return (cond.kind === ConditionKind.EventMatch && cond.key === 'room_id' && cond.pattern === roomId);
|
2016-08-18 01:26:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-20 01:35:11 +08:00
|
|
|
function isMuteRule(rule: IPushRule): boolean {
|
|
|
|
return (rule.actions.length === 1 && rule.actions[0] === PushRuleActionName.DontNotify);
|
2016-08-18 01:26:37 +08:00
|
|
|
}
|