snappymail/dev/Stores/User/Notification.js

177 lines
4.8 KiB
JavaScript
Raw Normal View History

import window from 'window';
import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { DesktopNotification, Magics } from 'Common/Enums';
import * as Events from 'Common/Events';
import Audio from 'Common/Audio';
import * as Settings from 'Storage/Settings';
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, nessageData) {
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
2019-07-05 03:19:24 +08:00
if (nessageData) {
notification.onclick = () => {
window.focus();
2015-07-07 02:46:44 +08:00
2019-07-05 03:19:24 +08:00
if (nessageData.Folder && nessageData.Uid) {
Events.pub('mailbox.message.show', [nessageData.Folder, nessageData.Uid]);
}
};
}
2019-07-05 03:19:24 +08:00
window.setTimeout(
(function(localNotifications) {
return () => {
if (localNotifications.cancel) {
localNotifications.cancel();
} else if (localNotifications.close) {
localNotifications.close();
}
};
})(notification),
Magics.Time7s
);
}
2016-06-30 08:02:45 +08:00
}
}
populate() {
this.enableSoundNotification(!!Settings.settingsGet('SoundNotification'));
this.enableDesktopNotification(!!Settings.settingsGet('DesktopNotifications'));
}
/**
* @returns {*|null}
*/
notificationClass() {
return window.Notification && window.Notification.requestPermission ? window.Notification : null;
}
}
2016-06-30 08:02:45 +08:00
export default new NotificationUserStore();