Honor “do not disturb” on Windows and macOS #966

This commit is contained in:
Ben Gotow 2018-11-22 14:44:37 -08:00
parent 6a24e46d9b
commit 3f183b7f82
6 changed files with 74 additions and 27 deletions

View file

@ -155,6 +155,7 @@ class ActivityEventStore extends MailspringStore {
const recipientName = action.recipient
? action.recipient.displayName()
: localized('Someone');
NativeNotifications.displayNotification({
title: localized(`New %@`, config.verb),
subtitle: localized(`%@ recently %@ %@`, recipientName, config.predicate, action.title),

View file

@ -149,10 +149,12 @@ export class Notifier {
},
});
if (!this.activeNotifications[thread.id]) {
this.activeNotifications[thread.id] = [notification];
} else {
this.activeNotifications[thread.id].push(notification);
if (notification) {
if (!this.activeNotifications[thread.id]) {
this.activeNotifications[thread.id] = [notification];
} else {
this.activeNotifications[thread.id].push(notification);
}
}
}
@ -170,6 +172,16 @@ export class Notifier {
}
}
_playNewMailSound = _.debounce(
() => {
if (!AppEnv.config.get('core.notifications.sounds')) return;
if (NativeNotifications.doNotDisturb()) return;
SoundRegistry.playSound('new-mail');
},
5000,
true
);
_onNewMessagesReceived(newMessages) {
if (newMessages.length === 0) {
return Promise.resolve();
@ -207,12 +219,7 @@ export class Notifier {
this.unnotifiedQueue.push({ message: msg, thread: threads[msg.threadId] });
}
if (!this.hasScheduledNotify) {
if (AppEnv.config.get('core.notifications.sounds')) {
this._playNewMailSound =
this._playNewMailSound ||
_.debounce(() => SoundRegistry.playSound('new-mail'), 5000, true);
this._playNewMailSound();
}
this._playNewMailSound();
this._notifyMessages();
}
}

36
app/package-lock.json generated
View file

@ -2786,6 +2786,16 @@
"resolved": "https://registry.npmjs.org/lsmod/-/lsmod-1.0.0.tgz",
"integrity": "sha1-mgD3bco26yP6BTUK/htYXUKZ5ks="
},
"macos-notification-state": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/macos-notification-state/-/macos-notification-state-1.1.0.tgz",
"integrity": "sha1-7llnHgXB7DiMCwkQHvYRyFtLTg4=",
"optional": true,
"requires": {
"bindings": "1.3.0",
"nan": "2.8.0"
}
},
"material-colors": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/material-colors/-/material-colors-1.2.5.tgz",
@ -2941,20 +2951,20 @@
}
},
"node-mac-notifier": {
"version": "0.0.13",
"resolved": "https://registry.npmjs.org/node-mac-notifier/-/node-mac-notifier-0.0.13.tgz",
"integrity": "sha1-1kt27RgfR5XURFui060Nb3KY9+I=",
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/node-mac-notifier/-/node-mac-notifier-1.1.0.tgz",
"integrity": "sha512-Fwv09eKMGkM4xn+Eby5g7lvmYF+1KYWX4V5QNn27l4cVFVS7MNYOxhbvGGHT5VuTWbWDQIi/Lr7l6so5vvmqVw==",
"optional": true,
"requires": {
"bindings": "1.3.0",
"event-target-shim": "1.1.1",
"node-uuid": "1.4.8"
"uuid": "3.3.2"
},
"dependencies": {
"node-uuid": {
"version": "1.4.8",
"resolved": "https://registry.npmjs.org/node-uuid/-/node-uuid-1.4.8.tgz",
"integrity": "sha1-sEDrCSOWivq/jTL7HxfxFn/auQc=",
"uuid": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz",
"integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==",
"optional": true
}
}
@ -4370,6 +4380,16 @@
"resolved": "https://registry.npmjs.org/window-size/-/window-size-0.1.0.tgz",
"integrity": "sha1-VDjNLqk7IC76Ohn+iIeu58lPnJ0="
},
"windows-quiet-hours": {
"version": "1.2.5",
"resolved": "https://registry.npmjs.org/windows-quiet-hours/-/windows-quiet-hours-1.2.5.tgz",
"integrity": "sha512-0JbfFw91Ys+A0vSR0pratoJgfTwWNFLHniu59dpOg1FC5IvpPSYQIATcQqhdy41HYSCijmT7Pyv9w0I2zC0+5w==",
"optional": true,
"requires": {
"bindings": "1.3.0",
"nan": "2.8.0"
}
},
"windows-shortcuts": {
"version": "github:emorikawa/windows-shortcuts#b0a0fc7fb86fb03e06ddceb9cbd6c9c5c29e571e"
},

View file

@ -73,6 +73,8 @@
"windows-shortcuts": "emorikawa/windows-shortcuts#b0a0fc7"
},
"optionalDependencies": {
"node-mac-notifier": "0.0.13"
"macos-notification-state": "^1.1.0",
"node-mac-notifier": "1.1.0",
"windows-quiet-hours": "^1.2.5"
}
}

View file

@ -1,6 +1,8 @@
/* eslint global-require: 0 */
const platform = process.platform;
let MacNotifierNotification = null;
if (process.platform === 'darwin') {
if (platform === 'darwin') {
try {
MacNotifierNotification = require('node-mac-notifier');
} catch (err) {
@ -22,9 +24,24 @@ class NativeNotifications {
});
}
}
doNotDisturb() {
if (platform === 'win32' && require('windows-quiet-hours').getIsQuietHours()) {
return true;
}
if (platform === 'darwin' && require('macos-notification-state').getDoNotDisturb()) {
return true;
}
return false;
}
displayNotification({ title, subtitle, body, tag, canReply, onActivate = () => {} } = {}) {
let notif = null;
if (this.doNotDisturb()) {
return null;
}
if (MacNotifierNotification) {
if (tag && this._macNotificationsByTag[tag]) {
this._macNotificationsByTag[tag].close();

14
package-lock.json generated
View file

@ -5,9 +5,9 @@
"requires": true,
"dependencies": {
"@types/node": {
"version": "8.10.34",
"resolved": "https://registry.npmjs.org/@types/node/-/node-8.10.34.tgz",
"integrity": "sha512-alypNiaAEd0RBGXoWehJ2gchPYCITmw4CYBoB5nDlji8l8on7FsklfdfIs4DDmgpKLSX3OF3ha6SV+0W7cTzUA==",
"version": "8.10.38",
"resolved": "https://registry.npmjs.org/@types/node/-/node-8.10.38.tgz",
"integrity": "sha512-EibsnbJerd0hBFaDjJStFrVbVBAtOy4dgL8zZFw0uOvPqzBAX59Ci8cgjg3+RgJIWhsB5A4c+pi+D4P9tQQh/A==",
"dev": true
},
"abab": {
@ -1746,12 +1746,12 @@
}
},
"electron": {
"version": "2.0.11",
"resolved": "https://registry.npmjs.org/electron/-/electron-2.0.11.tgz",
"integrity": "sha512-bFTMDQN3epfiymqTPdgffyTxuy/7A52sIkW7Hos+hY5XLPArOXLXAKx1JtB3dM7CcPfZa+5qp/J3cPCidh5WXg==",
"version": "2.0.14",
"resolved": "https://registry.npmjs.org/electron/-/electron-2.0.14.tgz",
"integrity": "sha512-8HLVZuscZxVhoMUL6RlF5kMcwGUAMWw5HNwrEmRgzZyBIBbdCO4aMo9z0qknnPTUDROz8xXZFNhFvBXDu61g5Q==",
"dev": true,
"requires": {
"@types/node": "8.10.34",
"@types/node": "8.10.38",
"electron-download": "3.3.0",
"extract-zip": "1.6.6"
}