Inhibit local notifications when local notifications are silenced (#9328)

This commit is contained in:
Germain 2022-09-29 15:23:02 +01:00 committed by GitHub
parent 951cad98d3
commit a49603b9b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 5 deletions

View file

@ -46,6 +46,7 @@ import { mediaFromMxc } from "./customisations/Media";
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
import LegacyCallHandler from "./LegacyCallHandler";
import VoipUserMapper from "./VoipUserMapper";
import { localNotificationsAreSilenced } from "./utils/notifications";
/*
* Dispatches:
@ -90,8 +91,9 @@ export const Notifier = {
return TextForEvent.textForEvent(ev);
},
_displayPopupNotification: function(ev: MatrixEvent, room: Room) {
_displayPopupNotification: function(ev: MatrixEvent, room: Room): void {
const plaf = PlatformPeg.get();
const cli = MatrixClientPeg.get();
if (!plaf) {
return;
}
@ -99,6 +101,10 @@ export const Notifier = {
return;
}
if (localNotificationsAreSilenced(cli)) {
return;
}
let msg = this.notificationMessageForEvent(ev);
if (!msg) return;
@ -170,7 +176,12 @@ export const Notifier = {
};
},
_playAudioNotification: async function(ev: MatrixEvent, room: Room) {
_playAudioNotification: async function(ev: MatrixEvent, room: Room): Promise<void> {
const cli = MatrixClientPeg.get();
if (localNotificationsAreSilenced(cli)) {
return;
}
const sound = this.getSoundForRoom(room.roomId);
logger.log(`Got sound ${sound && sound.name || "default"} for ${room.roomId}`);
@ -325,7 +336,7 @@ export const Notifier = {
}
const isGuest = client.isGuest();
return !isGuest && this.supportsDesktopNotifications() && !isPushNotifyDisabled() &&
!this.isEnabled() && !this._isPromptHidden();
!localNotificationsAreSilenced(client) && !this.isEnabled() && !this._isPromptHidden();
},
_isPromptHidden: function() {

View file

@ -15,6 +15,7 @@ limitations under the License.
*/
import { LOCAL_NOTIFICATION_SETTINGS_PREFIX } from "matrix-js-sdk/src/@types/event";
import { LocalNotificationSettings } from "matrix-js-sdk/src/@types/local_notifications";
import { MatrixClient } from "matrix-js-sdk/src/client";
import SettingsStore from "../settings/SettingsStore";
@ -32,7 +33,6 @@ export function getLocalNotificationAccountDataEventType(deviceId: string): stri
export async function createLocalNotificationSettingsIfNeeded(cli: MatrixClient): Promise<void> {
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
const event = cli.getAccountData(eventType);
// New sessions will create an account data event to signify they support
// remote toggling of push notifications on this device. Default `is_silenced=true`
// For backwards compat purposes, older sessions will need to check settings value
@ -47,3 +47,9 @@ export async function createLocalNotificationSettingsIfNeeded(cli: MatrixClient)
});
}
}
export function localNotificationsAreSilenced(cli: MatrixClient): boolean {
const eventType = getLocalNotificationAccountDataEventType(cli.deviceId);
const event = cli.getAccountData(eventType);
return event?.getContent<LocalNotificationSettings>()?.is_silenced ?? true;
}