diff --git a/package-lock.json b/package-lock.json index f662106f6..c1a9f7fa4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -223,6 +223,44 @@ "any-observable": "^0.3.0" } }, + "@sindresorhus/do-not-disturb": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/do-not-disturb/-/do-not-disturb-0.2.0.tgz", + "integrity": "sha512-zfHPXIW6CYhQ0ta7gZJDCNyQU7ydl1W2lCDKo5wTOC5yJ20Ew4XBer42vVC47kaFo3NZajrGLyt/7ZrRz9I52w==", + "requires": { + "electron-util": "^0.7.0", + "execa": "^0.9.0" + }, + "dependencies": { + "electron-is-dev": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/electron-is-dev/-/electron-is-dev-0.3.0.tgz", + "integrity": "sha1-FOb9pcaOnk7L7/nM8DfL18BcWv4=" + }, + "electron-util": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/electron-util/-/electron-util-0.7.0.tgz", + "integrity": "sha512-oo+XiQi/cCFaVMNQsGh2hd4Zg2O/RaUTqrKo4/4y4fVzq+womk5Csdum3ucdbcIt+RRX0U9hgCdwg53JAr99+g==", + "requires": { + "electron-is-dev": "^0.3.0" + } + }, + "execa": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/execa/-/execa-0.9.0.tgz", + "integrity": "sha512-BbUMBiX4hqiHZUA5+JujIjNb6TyAlp2D5KLheMjMluwOuzcnylDL4AxZYLLn1n2AGB49eSWwyKvvEQoRpnAtmA==", + "requires": { + "cross-spawn": "^5.0.1", + "get-stream": "^3.0.0", + "is-stream": "^1.1.0", + "npm-run-path": "^2.0.0", + "p-finally": "^1.0.0", + "signal-exit": "^3.0.0", + "strip-eof": "^1.0.0" + } + } + } + }, "@sindresorhus/is": { "version": "0.14.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-0.14.0.tgz", @@ -266,6 +304,11 @@ "@types/p-cancelable": "*" } }, + "@types/event-emitter": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/@types/event-emitter/-/event-emitter-0.3.3.tgz", + "integrity": "sha512-UfnOK1pIxO7P+EgPRZXD9jMpimd8QEFcEZ5R67R1UhGbv4zghU5+NE7U8M8G9H5Jc8FI51rqDWQs6FtUfq2e/Q==" + }, "@types/is-online": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/@types/is-online/-/is-online-8.0.0.tgz", @@ -1579,7 +1622,6 @@ "version": "5.1.0", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz", "integrity": "sha1-6L0O/uWPz/b4+UUQoKVUu/ojVEk=", - "dev": true, "requires": { "lru-cache": "^4.0.1", "shebang-command": "^1.2.0", @@ -1601,6 +1643,14 @@ "array-find-index": "^1.0.1" } }, + "d": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/d/-/d-1.0.0.tgz", + "integrity": "sha1-dUu1v+VUUdpppYuU1F9MWwRi1Y8=", + "requires": { + "es5-ext": "^0.10.9" + } + }, "dashdash": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/dashdash/-/dashdash-1.14.1.tgz", @@ -2289,6 +2339,35 @@ "is-arrayish": "^0.2.1" } }, + "es5-ext": { + "version": "0.10.48", + "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.48.tgz", + "integrity": "sha512-CdRvPlX/24Mj5L4NVxTs4804sxiS2CjVprgCmrgoDkdmjdY4D+ySHa7K3jJf8R40dFg0tIm3z/dk326LrnuSGw==", + "requires": { + "es6-iterator": "~2.0.3", + "es6-symbol": "~3.1.1", + "next-tick": "1" + } + }, + "es6-iterator": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", + "integrity": "sha1-p96IkUGgWpSwhUQDstCg+/qY87c=", + "requires": { + "d": "1", + "es5-ext": "^0.10.35", + "es6-symbol": "^3.1.1" + } + }, + "es6-symbol": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.1.tgz", + "integrity": "sha1-vwDvT9q2uhtG7Le2KbTH7VcVzHc=", + "requires": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -2863,6 +2942,15 @@ "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", "dev": true }, + "event-emitter": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", + "integrity": "sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=", + "requires": { + "d": "1", + "es5-ext": "~0.10.14" + } + }, "execa": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/execa/-/execa-0.7.0.tgz", @@ -4450,8 +4538,7 @@ "is-stream": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz", - "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=", - "dev": true + "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ=" }, "is-supported-regexp-flag": { "version": "1.0.1", @@ -4510,8 +4597,7 @@ "isexe": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=", - "dev": true + "integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=" }, "isobject": { "version": "3.0.1", @@ -5058,7 +5144,6 @@ "version": "4.1.5", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", - "dev": true, "requires": { "pseudomap": "^1.0.2", "yallist": "^2.1.2" @@ -5400,6 +5485,11 @@ "resolved": "https://registry.npmjs.org/new-github-issue-url/-/new-github-issue-url-0.2.0.tgz", "integrity": "sha512-LtOjIk0xghnMVjndjfF9JLc1WIDNmgCAfDtvPMsMJcecBMDziaYOyXkwR8Tgli2ZA2mAOP/6C8DpBq+uCmU5aQ==" }, + "next-tick": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.0.0.tgz", + "integrity": "sha1-yobR/ogoFpsBICCOPchCS524NCw=" + }, "nice-try": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", @@ -5808,7 +5898,6 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-2.0.2.tgz", "integrity": "sha1-NakjLfo11wZ7TLLd8jV7GHFTbF8=", - "dev": true, "requires": { "path-key": "^2.0.0" } @@ -6227,8 +6316,7 @@ "path-key": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-2.0.1.tgz", - "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=", - "dev": true + "integrity": "sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=" }, "path-parse": { "version": "1.0.6", @@ -6663,8 +6751,7 @@ "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", - "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", - "dev": true + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" }, "psl": { "version": "1.1.31", @@ -7418,7 +7505,6 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", "integrity": "sha1-RKrGW2lbAzmJaMOfNj/uXer98eo=", - "dev": true, "requires": { "shebang-regex": "^1.0.0" } @@ -7426,8 +7512,7 @@ "shebang-regex": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-1.0.0.tgz", - "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=", - "dev": true + "integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=" }, "signal-exit": { "version": "3.0.2", @@ -7825,8 +7910,7 @@ "strip-eof": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", - "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", - "dev": true + "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=" }, "strip-indent": { "version": "1.0.1", @@ -8896,7 +8980,6 @@ "version": "1.3.1", "resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz", "integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==", - "dev": true, "requires": { "isexe": "^2.0.0" } @@ -9461,8 +9544,7 @@ "yallist": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", - "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", - "dev": true + "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=" }, "yargs": { "version": "12.0.5", diff --git a/package.json b/package.json index f0f33dc89..60c272563 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,8 @@ "publish-snap": "del dist && tsc && electron-builder --linux && snapcraft push --release=stable dist/*.snap" }, "dependencies": { + "@sindresorhus/do-not-disturb": "^0.2.0", + "@types/event-emitter": "^0.3.3", "@types/is-online": "^8.0.0", "electron-context-menu": "^0.11.0", "electron-debug": "^2.1.0", @@ -36,9 +38,10 @@ "electron-updater": "^4.0.6", "electron-util": "^0.11.0", "element-ready": "^3.0.0", + "event-emitter": "^0.3.5", "facebook-locales": "^1.0.591", - "lodash": "^4.17.11", "is-online": "^8.0.0", + "lodash": "^4.17.11", "p-wait-for": "^2.0.1" }, "devDependencies": { diff --git a/source/do-not-disturb.ts b/source/do-not-disturb.ts new file mode 100644 index 000000000..8e584b34a --- /dev/null +++ b/source/do-not-disturb.ts @@ -0,0 +1,25 @@ +import {is} from 'electron-util'; +import macosDND from '@sindresorhus/do-not-disturb'; +import emitter from './event-emitter'; + +const pollingInterval = 3000; +const getDoNotDisturb = async (): Promise => (is.macos ? macosDND.isEnabled() : false); + +const startPolling = async (): Promise => { + let currentValue = await getDoNotDisturb(); + + setInterval(async () => { + const newValue = await getDoNotDisturb(); + + if (newValue !== currentValue) { + currentValue = newValue; + emitter.emit('dndprefs_changed', newValue); + } + }, pollingInterval); +}; + +if (is.macos) { + startPolling(); +} + +export {getDoNotDisturb}; diff --git a/source/event-emitter.ts b/source/event-emitter.ts new file mode 100644 index 000000000..f564a9c8d --- /dev/null +++ b/source/event-emitter.ts @@ -0,0 +1,3 @@ +import ee from 'event-emitter'; + +export default ee(); diff --git a/source/index.ts b/source/index.ts index cbdfb5e73..0eb59452b 100644 --- a/source/index.ts +++ b/source/index.ts @@ -23,7 +23,6 @@ import electronContextMenu from 'electron-context-menu'; import isDev from 'electron-is-dev'; import electronDebug from 'electron-debug'; import {darkMode, is} from 'electron-util'; -import {getDoNotDisturb} from 'electron-notification-state'; import {bestFacebookLocaleFor} from 'facebook-locales'; import updateAppMenu from './menu'; import config from './config'; @@ -31,6 +30,8 @@ import tray from './tray'; import {sendAction, sendBackgroundAction} from './util'; import {process as processEmojiUrl} from './emoji'; import ensureOnline from './ensure-online'; +import {getDoNotDisturb} from './do-not-disturb'; +import emitter from './event-emitter'; import './touch-bar'; // eslint-disable-line import/no-unassigned-import ipcMain.setMaxListeners(100); @@ -101,9 +102,7 @@ async function updateBadge(conversations: Conversation[]): Promise { } if (is.macos) { - const isDNDEnabled = await getDoNotDisturb(); - - if (!isDNDEnabled && config.get('bounceDockOnMessage') && prevMessageCount !== messageCount) { + if (config.get('bounceDockOnMessage') && prevMessageCount !== messageCount) { app.dock.bounce('informational'); prevMessageCount = messageCount; } @@ -367,6 +366,16 @@ function createMainWindow(): BrowserWindow { mainWindow.show(); } + if (is.macos) { + emitter.on('dndprefs_changed', (doNotDisturb: boolean) => { + webContents.send('toggle-sounds', !doNotDisturb); + webContents.send( + 'toggle-mute-notifications', + config.get('notificationsMuted') || doNotDisturb + ); + }); + } + const isDNDEnabled = await getDoNotDisturb(); webContents.send('toggle-sounds', !isDNDEnabled); diff --git a/source/sindresorhus-do-not-disturb.d.ts b/source/sindresorhus-do-not-disturb.d.ts new file mode 100644 index 000000000..f331df2c1 --- /dev/null +++ b/source/sindresorhus-do-not-disturb.d.ts @@ -0,0 +1 @@ +declare module '@sindresorhus/do-not-disturb';