Replace custom notification sound with system sound (#2108)

This commit is contained in:
Daniel Gasienica 2018-03-05 10:55:04 -05:00
commit 4562de8b24
12 changed files with 185 additions and 34 deletions

View file

@ -121,7 +121,6 @@ module.exports = function(grunt) {
'!js/register.js'
],
res: [
'audio/**',
'images/**/*',
'fonts/*',
]

Binary file not shown.

View file

@ -606,10 +606,12 @@
</div>
</div>
<br />
{{ #isAudioNotificationSupported }}
<div class='audio-notification-setting'>
<input type='checkbox' name='audio-notification' id='audio-notification'/>
<label for='audio-notification'>{{ audioNotificationDescription }}</label>
</div>
{{ /isAudioNotificationSupported }}
</div>
</script>
<script type='text/x-tmpl-mustache' id='syncSettings'>

10
js/modules/os.js Normal file
View file

@ -0,0 +1,10 @@
/* eslint-env node */
exports.isMacOS = () =>
process.platform === 'darwin';
exports.isLinux = () =>
process.platform === 'linux';
exports.isWindows = () =>
process.platform === 'win32';

View file

@ -0,0 +1,4 @@
const OS = require('../os');
exports.isAudioNotificationSupported = () =>
!OS.isLinux();

View file

@ -5,6 +5,7 @@
;(function() {
'use strict';
window.Whisper = window.Whisper || {};
const { Settings } = window.Signal.Types;
var SETTINGS = {
OFF : 'off',
@ -13,11 +14,9 @@
MESSAGE : 'message'
};
var enabled = false;
var sound = new Audio('audio/NewMessage.mp3');
Whisper.Notifications = new (Backbone.Collection.extend({
initialize: function() {
this.isEnabled = false;
this.on('add', this.update);
this.on('remove', this.onRemove);
},
@ -27,15 +26,25 @@
},
update: function() {
const isFocused = window.isFocused();
const isAudioNotificationEnabled = storage.get('audio-notification') || false;
const isAudioNotificationSupported = Settings.isAudioNotificationSupported();
const shouldPlayNotificationSound = isAudioNotificationSupported &&
isAudioNotificationEnabled;
const numNotifications = this.length;
console.log(
'updating notifications - count:', this.length,
'focused:', isFocused,
'enabled:', enabled
'Update notifications:',
'isFocused:', isFocused,
'isEnabled:', this.isEnabled,
'numNotifications:', numNotifications,
'shouldPlayNotificationSound:', shouldPlayNotificationSound
);
if (!enabled) {
return; // wait til we are re-enabled
if (!this.isEnabled) {
return;
}
if (this.length === 0) {
const hasNotifications = numNotifications > 0;
if (!hasNotifications) {
return;
}
@ -45,11 +54,6 @@
return;
}
var audioNotification = storage.get('audio-notification') || false;
if (audioNotification) {
sound.play();
}
var setting = storage.get('notification-setting') || 'message';
if (setting === SETTINGS.OFF) {
return;
@ -61,9 +65,13 @@
var message;
var iconUrl;
// NOTE: i18n has more complex rules for pluralization than just
// distinguishing between zero (0) and other (non-zero),
// e.g. Russian:
// http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html
var newMessageCount = [
this.length,
this.length === 1 ? i18n('newMessage') : i18n('newMessages')
numNotifications,
numNotifications === 1 ? i18n('newMessage') : i18n('newMessages')
].join(' ');
var last = this.last();
@ -78,7 +86,7 @@
iconUrl = last.get('iconUrl');
break;
case SETTINGS.MESSAGE:
if (this.length === 1) {
if (numNotifications === 1) {
title = last.get('title');
} else {
title = newMessageCount;
@ -92,7 +100,7 @@
window.nodeNotifier.notify({
title: title,
message: message,
sound: false
sound: false,
});
window.nodeNotifier.on('click', function(notifierObject, options) {
last.get('conversationId');
@ -102,7 +110,7 @@
body : message,
icon : iconUrl,
tag : 'signal',
silent : true
silent : !shouldPlayNotificationSound,
});
notification.onclick = this.onClick.bind(this, last.get('conversationId'));
@ -122,15 +130,14 @@
this.reset([]);
},
enable: function() {
var update = !enabled;
enabled = true;
if (update) {
const needUpdate = !this.isEnabled;
this.isEnabled = true;
if (needUpdate) {
this.update();
}
},
disable: function() {
enabled = false;
this.isEnabled = false;
},
}))();
})();

View file

@ -4,6 +4,8 @@
(function () {
'use strict';
window.Whisper = window.Whisper || {};
const { OS } = window.Signal;
const { Settings } = window.Signal.Types;
var CheckboxView = Whisper.View.extend({
initialize: function(options) {
@ -68,11 +70,13 @@
name: 'theme-setting',
event: 'change-theme'
});
new CheckboxView({
el: this.$('.audio-notification-setting'),
defaultValue: false,
name: 'audio-notification'
});
if (Settings.isAudioNotificationSupported()) {
new CheckboxView({
el: this.$('.audio-notification-setting'),
defaultValue: false,
name: 'audio-notification'
});
}
new CheckboxView({
el: this.$('.menu-bar-setting'),
defaultValue: false,
@ -100,6 +104,7 @@
noNameOrMessage: i18n('noNameOrMessage'),
nameOnly: i18n('nameOnly'),
audioNotificationDescription: i18n('audioNotificationDescription'),
isAudioNotificationSupported: Settings.isAudioNotificationSupported(),
themeAndroidDark: i18n('themeAndroidDark'),
hideMenuBar: i18n('hideMenuBar'),
};

View file

@ -514,9 +514,6 @@ ipc.on('draw-attention', () => {
app.dock.bounce();
} else if (process.platform === 'win32') {
mainWindow.flashFrame(true);
setTimeout(() => {
mainWindow.flashFrame(false);
}, 1000);
} else if (process.platform === 'linux') {
mainWindow.flashFrame(true);
}

View file

@ -99,6 +99,7 @@
"mocha-testcheck": "^1.0.0-rc.0",
"node-sass-import-once": "^1.2.0",
"nyc": "^11.4.1",
"sinon": "^4.4.2",
"spectron": "^3.8.0",
"tmp": "^0.0.33"
},
@ -194,7 +195,6 @@
"app/*",
"preload.js",
"main.js",
"audio/**",
"images/**",
"fonts/*",
"build/assets",

View file

@ -101,10 +101,12 @@
// ES2015+ modules
window.Signal = window.Signal || {};
window.Signal.OS = require('./js/modules/os');
window.Signal.Types = window.Signal.Types || {};
window.Signal.Types.Attachment = require('./js/modules/types/attachment');
window.Signal.Types.Message = require('./js/modules/types/message');
window.Signal.Types.MIME = require('./js/modules/types/mime');
window.Signal.Types.Settings = require('./js/modules/types/settings');
// We pull this in last, because the native module involved appears to be sensitive to
// /tmp mounted as noexec on Linux.

View file

@ -0,0 +1,53 @@
const sinon = require('sinon');
const { assert } = require('chai');
const Settings = require('../../../js/modules/types/settings');
describe('Settings', () => {
const sandbox = sinon.createSandbox();
describe('isAudioNotificationSupported', () => {
context('on macOS', () => {
beforeEach(() => {
sandbox.stub(process, 'platform').value('darwin');
});
afterEach(() => {
sandbox.restore();
});
it('should return true', () => {
assert.isTrue(Settings.isAudioNotificationSupported());
});
});
context('on Windows', () => {
beforeEach(() => {
sandbox.stub(process, 'platform').value('win32');
});
afterEach(() => {
sandbox.restore();
});
it('should return true', () => {
assert.isTrue(Settings.isAudioNotificationSupported());
});
});
context('on Linux', () => {
beforeEach(() => {
sandbox.stub(process, 'platform').value('linux');
});
afterEach(() => {
sandbox.restore();
});
it('should return false', () => {
assert.isFalse(Settings.isAudioNotificationSupported());
});
});
});
});

View file

@ -22,6 +22,12 @@
"7zip-bin-mac" "~1.0.1"
"7zip-bin-win" "~2.2.0"
"@sinonjs/formatio@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@sinonjs/formatio/-/formatio-2.0.0.tgz#84db7e9eb5531df18a8c5e0bfb6e449e55e654b2"
dependencies:
samsam "1.3.0"
"@types/node@^8.0.24":
version "8.9.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.9.4.tgz#dfd327582a06c114eb6e0441fa3d6fab35edad48"
@ -1228,6 +1234,10 @@ diff@3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.3.1.tgz#aa8567a6eed03c531fc89d3f711cd0e5259dec75"
diff@^3.1.0:
version "3.4.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.4.0.tgz#b1d85507daf3964828de54b37d0d73ba67dda56c"
dmg-builder@4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/dmg-builder/-/dmg-builder-4.1.1.tgz#a12214eb3eb3cba0addccfd129f1981c9805045c"
@ -2452,6 +2462,10 @@ has-flag@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-2.0.0.tgz#e8207af1cc7b30d446cc70b734b5e8be18f88d51"
has-flag@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd"
has-unicode@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/has-unicode/-/has-unicode-2.0.1.tgz#e0e6fe6a28cf51138855e086d1691e771de2a8b9"
@ -3088,6 +3102,10 @@ jsprim@^1.2.2:
json-schema "0.2.3"
verror "1.3.6"
just-extend@^1.1.27:
version "1.1.27"
resolved "https://registry.yarnpkg.com/just-extend/-/just-extend-1.1.27.tgz#ec6e79410ff914e472652abfa0e603c03d60e905"
kew@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/kew/-/kew-0.7.0.tgz#79d93d2d33363d6fdd2970b335d9141ad591d79b"
@ -3207,6 +3225,10 @@ lodash.defaults@^4.0.1:
version "4.2.0"
resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"
lodash.get@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99"
lodash.isarray@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-4.0.0.tgz#2aca496b28c4ca6d726715313590c02e6ea34403"
@ -3255,6 +3277,10 @@ lodash@~4.6.1:
version "4.6.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.6.1.tgz#df00c1164ad236b183cfc3887a5e8d38cc63cbbc"
lolex@^2.2.0, lolex@^2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/lolex/-/lolex-2.3.2.tgz#85f9450425103bf9e7a60668ea25dc43274ca807"
longest@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
@ -3521,6 +3547,16 @@ netmask@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/netmask/-/netmask-1.0.6.tgz#20297e89d86f6f6400f250d9f4f6b4c1945fcd35"
nise@^1.2.0:
version "1.2.6"
resolved "https://registry.yarnpkg.com/nise/-/nise-1.2.6.tgz#42b054981a5c869d6c447be5776cc6f137f00ac5"
dependencies:
"@sinonjs/formatio" "^2.0.0"
just-extend "^1.1.27"
lolex "^2.3.2"
path-to-regexp "^1.7.0"
text-encoding "^0.6.4"
"node-fetch@https://github.com/scottnonnenberg/node-fetch.git#3e5f51e08c647ee5f20c43b15cf2d352d61c36b4":
version "1.7.3"
resolved "https://github.com/scottnonnenberg/node-fetch.git#3e5f51e08c647ee5f20c43b15cf2d352d61c36b4"
@ -3896,6 +3932,12 @@ path-parse@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1"
path-to-regexp@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"
dependencies:
isarray "0.0.1"
path-type@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441"
@ -4464,6 +4506,10 @@ safe-json-stringify@~1:
version "1.0.4"
resolved "https://registry.yarnpkg.com/safe-json-stringify/-/safe-json-stringify-1.0.4.tgz#81a098f447e4bbc3ff3312a243521bc060ef5911"
samsam@1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.3.0.tgz#8d1d9350e25622da30de3e44ba692b5221ab7c50"
sanitize-filename@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/sanitize-filename/-/sanitize-filename-1.6.1.tgz#612da1c96473fa02dccda92dcd5b4ab164a6772a"
@ -4556,6 +4602,18 @@ single-line-log@^1.1.2:
dependencies:
string-width "^1.0.1"
sinon@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/sinon/-/sinon-4.4.2.tgz#c4c41d4bd346e1d33594daec2d5df0548334fc65"
dependencies:
"@sinonjs/formatio" "^2.0.0"
diff "^3.1.0"
lodash.get "^4.4.2"
lolex "^2.2.0"
nise "^1.2.0"
supports-color "^5.1.0"
type-detect "^4.0.5"
slice-ansi@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slice-ansi/-/slice-ansi-1.0.0.tgz#044f1a49d8842ff307aad6b505ed178bd950134d"
@ -4871,6 +4929,12 @@ supports-color@^3.1.2:
dependencies:
has-flag "^1.0.0"
supports-color@^5.1.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.2.0.tgz#b0d5333b1184dd3666cbe5aa0b45c5ac7ac17a4a"
dependencies:
has-flag "^3.0.0"
supports-color@~5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.0.1.tgz#1c5331f22250c84202805b2f17adf16699f3a39a"
@ -4943,6 +5007,10 @@ testcheck@^1.0.0-rc, testcheck@^1.0.0-rc.2:
version "1.0.0-rc.2"
resolved "https://registry.yarnpkg.com/testcheck/-/testcheck-1.0.0-rc.2.tgz#11356a25b84575efe0b0857451e85b5fa74ee4e4"
text-encoding@^0.6.4:
version "0.6.4"
resolved "https://registry.yarnpkg.com/text-encoding/-/text-encoding-0.6.4.tgz#e399a982257a276dae428bb92845cb71bdc26d19"
text-table@~0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
@ -5085,6 +5153,10 @@ type-detect@^4.0.0:
version "4.0.5"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.5.tgz#d70e5bc81db6de2a381bcaca0c6e0cbdc7635de2"
type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
type-is@~1.6.10:
version "1.6.15"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.15.tgz#cab10fb4909e441c82842eafe1ad646c81804410"