signal-desktop/ts/services/notify.ts
Sidney Keese dabab60f56 Remove TSLint
Co-authored-by: Chris Svenningsen <chris@carbonfive.com>
2020-10-12 18:10:07 -04:00

48 lines
1.2 KiB
TypeScript

import { Sound } from '../util/Sound';
import {
AudioNotificationSupport,
getAudioNotificationSupport,
} from '../types/Settings';
import * as OS from '../OS';
function filter(text: string) {
return (text || '')
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}
type NotificationType = {
icon: string;
message: string;
onNotificationClick: () => void;
silent: boolean;
title: string;
};
export function notify({
icon,
message,
onNotificationClick,
silent,
title,
}: NotificationType): Notification {
const audioNotificationSupport = getAudioNotificationSupport();
const notification = new window.Notification(title, {
body: OS.isLinux() ? filter(message) : message,
icon,
silent:
silent || audioNotificationSupport !== AudioNotificationSupport.Native,
});
notification.onclick = onNotificationClick;
if (!silent && audioNotificationSupport === AudioNotificationSupport.Custom) {
// We kick off the sound to be played. No neet to await it.
new Sound({ src: 'sounds/notification.ogg' }).play();
}
return notification;
}