2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-11 21:23:11 +00:00
|
|
|
import { Sound } from '../util/Sound';
|
|
|
|
import {
|
|
|
|
AudioNotificationSupport,
|
|
|
|
getAudioNotificationSupport,
|
|
|
|
} from '../types/Settings';
|
|
|
|
import * as OS from '../OS';
|
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
function filter(text: string) {
|
|
|
|
return (text || '')
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''')
|
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>');
|
|
|
|
}
|
|
|
|
|
|
|
|
type NotificationType = {
|
|
|
|
icon: string;
|
|
|
|
message: string;
|
|
|
|
onNotificationClick: () => void;
|
|
|
|
silent: boolean;
|
|
|
|
title: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function notify({
|
|
|
|
icon,
|
|
|
|
message,
|
|
|
|
onNotificationClick,
|
|
|
|
silent,
|
|
|
|
title,
|
|
|
|
}: NotificationType): Notification {
|
2020-09-11 21:23:11 +00:00
|
|
|
const audioNotificationSupport = getAudioNotificationSupport();
|
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
const notification = new window.Notification(title, {
|
2020-09-11 21:23:11 +00:00
|
|
|
body: OS.isLinux() ? filter(message) : message,
|
2020-06-04 18:16:19 +00:00
|
|
|
icon,
|
2020-09-11 21:23:11 +00:00
|
|
|
silent:
|
|
|
|
silent || audioNotificationSupport !== AudioNotificationSupport.Native,
|
2020-06-04 18:16:19 +00:00
|
|
|
});
|
|
|
|
notification.onclick = onNotificationClick;
|
2020-09-11 21:23:11 +00:00
|
|
|
|
|
|
|
if (!silent && audioNotificationSupport === AudioNotificationSupport.Custom) {
|
2021-02-03 16:45:35 +00:00
|
|
|
// We kick off the sound to be played. No need to await it.
|
2020-09-11 21:23:11 +00:00
|
|
|
new Sound({ src: 'sounds/notification.ogg' }).play();
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
return notification;
|
|
|
|
}
|