Skip to content

Commit

Permalink
Honor “do not disturb” on Windows and macOS #966
Browse files Browse the repository at this point in the history
  • Loading branch information
bengotow committed Nov 22, 2018
1 parent 6a24e46 commit 3f183b7
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 27 deletions.
Expand Up @@ -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),
Expand Down
27 changes: 17 additions & 10 deletions app/internal_packages/unread-notifications/lib/main.es6
Expand Up @@ -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);
}
}
}

Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
}
Expand Down
36 changes: 28 additions & 8 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion app/package.json
Expand Up @@ -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"
}
}
19 changes: 18 additions & 1 deletion app/src/native-notifications.es6
@@ -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) {
Expand All @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 3f183b7

@foundry376-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Mailspring Community. There might be relevant details there:

https://community.getmailspring.com/t/mute-notifications/777/6

Please sign in to comment.