2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2015-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-05-09 22:12:31 +00:00
|
|
|
/* global Signal:false */
|
2018-05-02 20:43:32 +00:00
|
|
|
/* global Backbone: false */
|
|
|
|
|
2019-09-06 20:04:31 +00:00
|
|
|
/* global drawAttention: false */
|
2018-05-02 20:43:32 +00:00
|
|
|
/* global i18n: false */
|
|
|
|
/* global storage: false */
|
|
|
|
/* global Whisper: false */
|
2018-05-11 00:07:42 +00:00
|
|
|
/* global _: false */
|
2018-05-02 20:43:32 +00:00
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2020-11-18 15:15:42 +00:00
|
|
|
(function () {
|
2018-04-27 21:25:04 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2015-03-17 22:06:21 +00:00
|
|
|
|
2020-09-04 23:07:24 +00:00
|
|
|
// The keys and values don't match here. This is because the values correspond to old
|
|
|
|
// setting names. In the future, we may wish to migrate these to match.
|
2018-05-02 22:02:49 +00:00
|
|
|
const SettingNames = {
|
2020-09-04 23:07:24 +00:00
|
|
|
NO_NAME_OR_MESSAGE: 'count',
|
|
|
|
NAME_ONLY: 'name',
|
|
|
|
NAME_AND_MESSAGE: 'message',
|
2018-04-27 21:25:04 +00:00
|
|
|
};
|
2016-02-18 00:08:17 +00:00
|
|
|
|
2020-09-04 23:07:24 +00:00
|
|
|
// Electron, at least on Windows and macOS, only shows one notification at a time (see
|
|
|
|
// issues [#15364][0] and [#21646][1], among others). Because of that, we have a
|
|
|
|
// single slot for notifications, and once a notification is dismissed, all of
|
|
|
|
// Signal's notifications are dismissed.
|
|
|
|
// [0]: https://github.com/electron/electron/issues/15364
|
|
|
|
// [1]: https://github.com/electron/electron/issues/21646
|
|
|
|
Whisper.Notifications = {
|
|
|
|
...Backbone.Events,
|
|
|
|
|
|
|
|
isEnabled: false,
|
|
|
|
|
|
|
|
// This is either a standard `Notification` or null.
|
|
|
|
lastNotification: null,
|
|
|
|
|
|
|
|
// This is either null or an object of this shape:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// conversationId: string;
|
|
|
|
// messageId: string;
|
|
|
|
// senderTitle: string;
|
|
|
|
// message: string;
|
|
|
|
// notificationIconUrl: string | void;
|
|
|
|
// isExpiringMessage: boolean;
|
|
|
|
// reaction: {
|
|
|
|
// emoji: string;
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
notificationData: null,
|
|
|
|
|
|
|
|
add(notificationData) {
|
|
|
|
this.notificationData = notificationData;
|
|
|
|
this.update();
|
|
|
|
},
|
|
|
|
|
|
|
|
removeBy({ conversationId, messageId }) {
|
|
|
|
const shouldClear =
|
|
|
|
Boolean(this.notificationData) &&
|
|
|
|
((conversationId &&
|
|
|
|
this.notificationData.conversationId === conversationId) ||
|
|
|
|
(messageId && this.notificationData.messageId === messageId));
|
|
|
|
if (shouldClear) {
|
|
|
|
this.clear();
|
|
|
|
this.update();
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
2020-09-04 23:07:24 +00:00
|
|
|
|
|
|
|
fastUpdate() {
|
2018-05-11 00:27:22 +00:00
|
|
|
if (this.lastNotification) {
|
|
|
|
this.lastNotification.close();
|
|
|
|
this.lastNotification = null;
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const { isEnabled } = this;
|
2019-09-19 22:16:46 +00:00
|
|
|
const isAppFocused = window.isActive();
|
2018-04-27 21:25:04 +00:00
|
|
|
const isAudioNotificationEnabled =
|
|
|
|
storage.get('audio-notification') || false;
|
2018-05-02 22:04:00 +00:00
|
|
|
const userSetting = this.getUserSetting();
|
|
|
|
|
|
|
|
const status = Signal.Notifications.getStatus({
|
|
|
|
isAppFocused,
|
|
|
|
isAudioNotificationEnabled,
|
2018-05-02 22:33:58 +00:00
|
|
|
isEnabled,
|
2020-09-04 23:07:24 +00:00
|
|
|
hasNotifications: Boolean(this.notificationData),
|
2018-05-02 22:04:00 +00:00
|
|
|
userSetting,
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2018-02-23 21:25:11 +00:00
|
|
|
|
2018-05-02 22:04:00 +00:00
|
|
|
if (status.type !== 'ok') {
|
2020-12-17 23:12:15 +00:00
|
|
|
window.log.info(
|
|
|
|
`Not updating notifications; notification status is ${status.type}. ${
|
|
|
|
status.shouldClearNotifications ? 'Also clearing notifications' : ''
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
|
2018-05-02 22:04:00 +00:00
|
|
|
if (status.shouldClearNotifications) {
|
2020-09-04 23:07:24 +00:00
|
|
|
this.notificationData = null;
|
2018-05-02 22:04:00 +00:00
|
|
|
}
|
2017-09-29 16:15:28 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-12-17 23:12:15 +00:00
|
|
|
window.log.info('Showing a notification');
|
2016-02-18 00:08:17 +00:00
|
|
|
|
2020-09-04 23:07:24 +00:00
|
|
|
let notificationTitle;
|
|
|
|
let notificationMessage;
|
|
|
|
let notificationIconUrl;
|
|
|
|
|
|
|
|
const {
|
|
|
|
conversationId,
|
|
|
|
messageId,
|
|
|
|
senderTitle,
|
|
|
|
message,
|
|
|
|
isExpiringMessage,
|
|
|
|
reaction,
|
|
|
|
} = this.notificationData;
|
|
|
|
|
|
|
|
if (
|
|
|
|
userSetting === SettingNames.NAME_ONLY ||
|
|
|
|
userSetting === SettingNames.NAME_AND_MESSAGE
|
|
|
|
) {
|
|
|
|
notificationTitle = senderTitle;
|
|
|
|
({ notificationIconUrl } = this.notificationData);
|
|
|
|
|
|
|
|
const shouldHideExpiringMessageBody =
|
|
|
|
isExpiringMessage && Signal.OS.isMacOS();
|
|
|
|
if (shouldHideExpiringMessageBody) {
|
|
|
|
notificationMessage = i18n('newMessage');
|
|
|
|
} else if (userSetting === SettingNames.NAME_ONLY) {
|
|
|
|
if (reaction) {
|
|
|
|
notificationMessage = i18n('notificationReaction', {
|
|
|
|
sender: senderTitle,
|
|
|
|
emoji: reaction.emoji,
|
2020-07-29 23:20:05 +00:00
|
|
|
});
|
2018-05-09 22:12:31 +00:00
|
|
|
} else {
|
2020-09-04 23:07:24 +00:00
|
|
|
notificationMessage = i18n('newMessage');
|
2018-05-09 22:12:31 +00:00
|
|
|
}
|
2020-09-04 23:07:24 +00:00
|
|
|
} else if (reaction) {
|
|
|
|
notificationMessage = i18n('notificationReactionMessage', {
|
|
|
|
sender: senderTitle,
|
|
|
|
emoji: reaction.emoji,
|
|
|
|
message,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
notificationMessage = message;
|
2018-05-09 22:12:31 +00:00
|
|
|
}
|
2020-09-04 23:07:24 +00:00
|
|
|
} else {
|
|
|
|
if (userSetting !== SettingNames.NO_NAME_OR_MESSAGE) {
|
2018-07-21 19:00:08 +00:00
|
|
|
window.log.error(
|
2018-05-09 22:12:31 +00:00
|
|
|
`Error: Unknown user notification setting: '${userSetting}'`
|
|
|
|
);
|
2020-09-04 23:07:24 +00:00
|
|
|
}
|
|
|
|
notificationTitle = 'Signal';
|
|
|
|
notificationMessage = i18n('newMessage');
|
2018-05-09 22:12:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 21:45:31 +00:00
|
|
|
const shouldDrawAttention = storage.get(
|
|
|
|
'notification-draw-attention',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
if (shouldDrawAttention) {
|
|
|
|
drawAttention();
|
|
|
|
}
|
2019-09-06 20:04:31 +00:00
|
|
|
|
2020-06-04 18:16:19 +00:00
|
|
|
this.lastNotification = window.Signal.Services.notify({
|
2020-09-04 23:07:24 +00:00
|
|
|
title: notificationTitle,
|
|
|
|
icon: notificationIconUrl,
|
|
|
|
message: notificationMessage,
|
2018-05-02 23:06:03 +00:00
|
|
|
silent: !status.shouldPlayNotificationSound,
|
2020-06-04 18:16:19 +00:00
|
|
|
onNotificationClick: () => {
|
2020-09-04 23:07:24 +00:00
|
|
|
this.trigger('click', conversationId, messageId);
|
2020-06-04 18:16:19 +00:00
|
|
|
},
|
2018-05-02 23:06:03 +00:00
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
2020-09-04 23:07:24 +00:00
|
|
|
|
2018-05-02 22:04:00 +00:00
|
|
|
getUserSetting() {
|
2020-09-04 23:07:24 +00:00
|
|
|
return (
|
|
|
|
storage.get('notification-setting') || SettingNames.NAME_AND_MESSAGE
|
|
|
|
);
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
clear() {
|
2020-09-04 23:07:24 +00:00
|
|
|
window.log.info('Removing notification');
|
|
|
|
this.notificationData = null;
|
2018-05-11 00:27:22 +00:00
|
|
|
this.update();
|
|
|
|
},
|
|
|
|
// We don't usually call this, but when the process is shutting down, we should at
|
|
|
|
// least try to remove the notification immediately instead of waiting for the
|
|
|
|
// normal debounce.
|
|
|
|
fastClear() {
|
2020-09-04 23:07:24 +00:00
|
|
|
this.notificationData = null;
|
2018-05-11 00:27:22 +00:00
|
|
|
this.fastUpdate();
|
2018-04-27 21:25:04 +00:00
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
enable() {
|
2018-04-27 21:25:04 +00:00
|
|
|
const needUpdate = !this.isEnabled;
|
|
|
|
this.isEnabled = true;
|
|
|
|
if (needUpdate) {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
},
|
2018-05-02 20:43:32 +00:00
|
|
|
disable() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.isEnabled = false;
|
|
|
|
},
|
2020-09-04 23:07:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Testing indicated that trying to create/destroy notifications too quickly
|
|
|
|
// resulted in notifications that stuck around forever, requiring the user
|
|
|
|
// to manually close them. This introduces a minimum amount of time between calls,
|
|
|
|
// and batches up the quick successive update() calls we get from an incoming
|
|
|
|
// read sync, which might have a number of messages referenced inside of it.
|
|
|
|
Whisper.Notifications.update = _.debounce(
|
|
|
|
Whisper.Notifications.fastUpdate.bind(Whisper.Notifications),
|
|
|
|
1000
|
|
|
|
);
|
2015-03-17 22:06:21 +00:00
|
|
|
})();
|