snappymail/dev/Stores/User/Notification.js

174 lines
4.7 KiB
JavaScript
Raw Normal View History

import ko from 'ko';
2020-08-14 04:58:41 +08:00
import { DesktopNotification } from 'Common/Enums';
import Audio from 'Common/Audio';
2019-07-05 03:19:24 +08:00
class NotificationUserStore {
constructor() {
this.enableSoundNotification = ko.observable(false);
this.soundNotificationIsSupported = ko.observable(false);
this.allowDesktopNotification = ko.observable(false);
2019-07-05 03:19:24 +08:00
this.desktopNotificationPermissions = ko
.computed(() => {
this.allowDesktopNotification();
let result = DesktopNotification.NotSupported;
const NotificationClass = this.notificationClass();
if (NotificationClass && NotificationClass.permission) {
switch (NotificationClass.permission.toLowerCase()) {
case 'granted':
result = DesktopNotification.Allowed;
break;
case 'denied':
result = DesktopNotification.Denied;
break;
case 'default':
result = DesktopNotification.NotAllowed;
break;
// no default
}
2019-07-05 03:19:24 +08:00
} else if (window.webkitNotifications && window.webkitNotifications.checkPermission) {
result = window.webkitNotifications.checkPermission();
}
2019-07-05 03:19:24 +08:00
return result;
})
.extend({ notify: 'always' });
this.enableDesktopNotification = ko
.computed({
read: () =>
this.allowDesktopNotification() && DesktopNotification.Allowed === this.desktopNotificationPermissions(),
write: (value) => {
if (value) {
const NotificationClass = this.notificationClass(),
permission = this.desktopNotificationPermissions();
if (NotificationClass && DesktopNotification.Allowed === permission) {
this.allowDesktopNotification(true);
} else if (NotificationClass && DesktopNotification.NotAllowed === permission) {
NotificationClass.requestPermission(() => {
this.allowDesktopNotification.valueHasMutated();
if (DesktopNotification.Allowed === this.desktopNotificationPermissions()) {
if (this.allowDesktopNotification()) {
this.allowDesktopNotification.valueHasMutated();
} else {
this.allowDesktopNotification(true);
}
} else {
if (this.allowDesktopNotification()) {
this.allowDesktopNotification(false);
} else {
this.allowDesktopNotification.valueHasMutated();
}
}
2019-07-05 03:19:24 +08:00
});
} else {
this.allowDesktopNotification(false);
}
} else {
this.allowDesktopNotification(false);
}
}
2019-07-05 03:19:24 +08:00
})
.extend({ notify: 'always' });
2019-07-05 03:19:24 +08:00
if (!this.enableDesktopNotification.valueHasMutated) {
this.enableDesktopNotification.valueHasMutated = () => {
this.allowDesktopNotification.valueHasMutated();
};
}
this.computers();
this.initNotificationPlayer();
}
2016-06-30 08:02:45 +08:00
computers() {
this.isDesktopNotificationSupported = ko.computed(
() => DesktopNotification.NotSupported !== this.desktopNotificationPermissions()
);
2016-06-30 08:02:45 +08:00
this.isDesktopNotificationDenied = ko.computed(
2019-07-05 03:19:24 +08:00
() =>
DesktopNotification.NotSupported === this.desktopNotificationPermissions() ||
DesktopNotification.Denied === this.desktopNotificationPermissions()
);
2016-06-30 08:02:45 +08:00
}
initNotificationPlayer() {
2019-07-05 03:19:24 +08:00
if (Audio && Audio.supportedNotification) {
this.soundNotificationIsSupported(true);
2019-07-05 03:19:24 +08:00
} else {
this.enableSoundNotification(false);
this.soundNotificationIsSupported(false);
}
2016-06-30 08:02:45 +08:00
}
playSoundNotification(skipSetting) {
2019-07-05 03:19:24 +08:00
if (Audio && Audio.supportedNotification && (skipSetting ? true : this.enableSoundNotification())) {
Audio.playNotification();
}
2016-06-30 08:02:45 +08:00
}
displayDesktopNotification(imageSrc, title, text, messageData) {
2019-07-05 03:19:24 +08:00
if (this.enableDesktopNotification()) {
const NotificationClass = this.notificationClass(),
notification = NotificationClass
? new NotificationClass(title, {
body: text,
icon: imageSrc
})
: null;
if (notification) {
if (notification.show) {
notification.show();
}
2015-07-07 02:46:44 +08:00
if (messageData) {
notification.onclick = () => {
focus();
2015-07-07 02:46:44 +08:00
if (messageData.Folder && messageData.Uid) {
dispatchEvent(new CustomEvent('mailbox.message.show', {detail:messageData}));
}
};
}
setTimeout(
2019-07-05 03:19:24 +08:00
(function(localNotifications) {
return () => {
if (localNotifications.cancel) {
localNotifications.cancel();
} else if (localNotifications.close) {
localNotifications.close();
}
};
})(notification),
2020-08-14 04:58:41 +08:00
7000
2019-07-05 03:19:24 +08:00
);
}
2016-06-30 08:02:45 +08:00
}
}
populate() {
this.enableSoundNotification(!!rl.settings.get('SoundNotification'));
this.enableDesktopNotification(!!rl.settings.get('DesktopNotifications'));
}
/**
* @returns {*|null}
*/
notificationClass() {
return window.Notification && Notification.requestPermission ? Notification : null;
}
}
2016-06-30 08:02:45 +08:00
export default new NotificationUserStore();