fchat-rising/chat/notifications.ts

46 lines
1.9 KiB
TypeScript

import core from './core';
import {Conversation, Notifications as Interface} from './interfaces';
const codecs: {[key: string]: string} = {mpeg: 'mp3', wav: 'wav', ogg: 'ogg'};
export default class Notifications implements Interface {
isInBackground = false;
notify(conversation: Conversation, title: string, body: string, icon: string, sound: string): void {
if(!this.isInBackground && conversation === core.conversations.selectedConversation && !core.state.settings.alwaysNotify) return;
if(core.characters.ownCharacter.status === 'dnd') return;
this.playSound(sound);
if(core.state.settings.notifications) {
/*tslint:disable-next-line:no-object-literal-type-assertion*///false positive
const notification = new Notification(title, <NotificationOptions & {silent: boolean}>{body, icon, silent: true});
notification.onclick = () => {
conversation.show();
window.focus();
notification.close();
};
}
}
playSound(sound: string): void {
if(!core.state.settings.playSound) return;
const id = `soundplayer-${sound}`;
let audio = <HTMLAudioElement | null>document.getElementById(id);
if(audio === null) {
audio = document.createElement('audio');
audio.id = id;
for(const name in codecs) {
const src = document.createElement('source');
src.type = `audio/${name}`;
//tslint:disable-next-line:no-require-imports
src.src = <string>require(`./assets/${sound}.${codecs[name]}`);
audio.appendChild(src);
}
}
//tslint:disable-next-line:no-floating-promises
audio.play();
}
async requestPermission(): Promise<void> {
await Notification.requestPermission();
}
}