[MM-57348] Support notification metrics from the Desktop App client (#2998)

* [MM-57348] Support notification metrics from the Desktop App client

* Add timeout in case promise never resolves
This commit is contained in:
Devin Binnie
2024-04-05 10:35:12 -04:00
committed by GitHub
parent a0ad135fd3
commit e7cf7a81e9
10 changed files with 59 additions and 47 deletions

View File

@@ -248,7 +248,7 @@ function initializeBeforeAppReady() {
}
function initializeInterCommunicationEventListeners() {
ipcMain.on(NOTIFY_MENTION, handleMentionNotification);
ipcMain.handle(NOTIFY_MENTION, handleMentionNotification);
ipcMain.handle(GET_APP_INFO, handleAppVersion);
ipcMain.on(UPDATE_SHORTCUT_MENU, handleUpdateMenuEvent);
ipcMain.on(FOCUS_BROWSERVIEW, ViewManager.focusCurrentView);

View File

@@ -112,9 +112,9 @@ export function handleWelcomeScreenModal() {
}
}
export function handleMentionNotification(event: IpcMainEvent, title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, soundName: string) {
export function handleMentionNotification(event: IpcMainInvokeEvent, title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, soundName: string) {
log.debug('handleMentionNotification', {title, body, channelId, teamId, url, silent, soundName});
NotificationManager.displayMention(title, body, channelId, teamId, url, silent, event.sender, soundName);
return NotificationManager.displayMention(title, body, channelId, teamId, url, silent, event.sender, soundName);
}
export function handleOpenAppMenu() {

View File

@@ -32,19 +32,19 @@ class NotificationManager {
if (!Notification.isSupported()) {
log.error('notification not supported');
return;
return {result: 'error', reason: 'notification_api', data: 'notification not supported'};
}
if (await getDoNotDisturb()) {
return;
return {result: 'not_sent', reason: 'os_dnd'};
}
const view = ViewManager.getViewByWebContentsId(webcontents.id);
if (!view) {
return;
return {result: 'error', reason: 'missing_view'};
}
if (!view.view.shouldNotify) {
return;
return {result: 'error', reason: 'view_should_not_notify'};
}
const serverName = view.view.server.name;
@@ -56,32 +56,13 @@ class NotificationManager {
};
if (!await PermissionsManager.doPermissionRequest(webcontents.id, 'notifications', view.view.server.url.toString())) {
return;
return {result: 'not_sent', reason: 'notifications_permission_disallowed'};
}
const mention = new Mention(options, channelId, teamId);
const mentionKey = `${mention.teamId}:${mention.channelId}`;
this.allActiveNotifications.set(mention.uId, mention);
mention.on('show', () => {
log.debug('displayMention.show');
// On Windows, manually dismiss notifications from the same channel and only show the latest one
if (process.platform === 'win32') {
if (this.mentionsPerChannel.has(mentionKey)) {
log.debug(`close ${mentionKey}`);
this.mentionsPerChannel.get(mentionKey)?.close();
this.mentionsPerChannel.delete(mentionKey);
}
this.mentionsPerChannel.set(mentionKey, mention);
}
const notificationSound = mention.getNotificationSound();
if (notificationSound) {
MainWindow.sendToRenderer(PLAY_SOUND, notificationSound);
}
flashFrame(true);
});
mention.on('click', () => {
log.debug('notification click', serverName, mention);
@@ -97,10 +78,40 @@ class NotificationManager {
this.allActiveNotifications.delete(mention.uId);
});
mention.on('failed', () => {
this.allActiveNotifications.delete(mention.uId);
return new Promise((resolve) => {
// If mention never shows somehow, resolve the promise after 10s
const timeout = setTimeout(() => {
resolve({result: 'error', reason: 'notification_timeout'});
}, 10000);
mention.on('show', () => {
log.debug('displayMention.show');
// On Windows, manually dismiss notifications from the same channel and only show the latest one
if (process.platform === 'win32') {
if (this.mentionsPerChannel.has(mentionKey)) {
log.debug(`close ${mentionKey}`);
this.mentionsPerChannel.get(mentionKey)?.close();
this.mentionsPerChannel.delete(mentionKey);
}
this.mentionsPerChannel.set(mentionKey, mention);
}
const notificationSound = mention.getNotificationSound();
if (notificationSound) {
MainWindow.sendToRenderer(PLAY_SOUND, notificationSound);
}
flashFrame(true);
clearTimeout(timeout);
resolve({result: 'success'});
});
mention.on('failed', (_, error) => {
this.allActiveNotifications.delete(mention.uId);
clearTimeout(timeout);
resolve({result: 'error', reason: 'electron_notification_failed', data: error});
});
mention.show();
});
mention.show();
}
public async displayDownloadCompleted(fileName: string, path: string, serverName: string) {

View File

@@ -72,7 +72,7 @@ const desktopAPI: DesktopAPI = {
// Unreads/mentions/notifications
sendNotification: (title, body, channelId, teamId, url, silent, soundName) =>
ipcRenderer.send(NOTIFY_MENTION, title, body, channelId, teamId, url, silent, soundName),
ipcRenderer.invoke(NOTIFY_MENTION, title, body, channelId, teamId, url, silent, soundName),
onNotificationClicked: (listener) => createListener(NOTIFICATION_CLICKED, listener),
setUnreadsAndMentions: (isUnread, mentionCount) => ipcRenderer.send(UNREADS_AND_MENTIONS, isUnread, mentionCount),
@@ -286,7 +286,7 @@ window.addEventListener('message', ({origin, data = {}}: {origin?: string; data?
case 'dispatch-notification': {
const {title, body, channel, teamId, url, silent, data: messageData} = message;
channels.set(channel.id, channel);
ipcRenderer.send(NOTIFY_MENTION, title, body, channel.id, teamId, url, silent, messageData.soundName);
ipcRenderer.invoke(NOTIFY_MENTION, title, body, channel.id, teamId, url, silent, messageData.soundName);
break;
}
case BROWSER_HISTORY_PUSH: {