Skip to content

Commit

Permalink
Improve notification logging (#3040) (#3058)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9c6d809)

Co-authored-by: Devin Binnie <[email protected]>
  • Loading branch information
mattermost-build and devinbinnie authored Jun 10, 2024
1 parent af3b1d2 commit 84a3a15
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/main/notifications/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,28 @@ class NotificationManager {
private restartToUpgradeNotification?: UpgradeNotification;

public async displayMention(title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, webcontents: Electron.WebContents, soundName: string) {
log.debug('displayMention', {title, body, channelId, teamId, url, silent, soundName});
log.debug('displayMention', {title, channelId, teamId, url, silent, soundName});

if (!Notification.isSupported()) {
log.error('notification not supported');
return {status: 'error', reason: 'notification_api', data: 'notification not supported'};
}

if (await getDoNotDisturb()) {
log.debug('do not disturb is on, will not send');
return {status: 'not_sent', reason: 'os_dnd'};
}

const view = ViewManager.getViewByWebContentsId(webcontents.id);
if (!view) {
log.error('missing view', webcontents.id);
return {status: 'error', reason: 'missing_view'};
}
const serverName = view.view.server.name;
if (!view.view.shouldNotify) {
log.debug('should not notify for this view', webcontents.id);
return {status: 'not_sent', reason: 'view_should_not_notify'};
}
const serverName = view.view.server.name;

const options = {
title: `${serverName}: ${title}`,
Expand All @@ -56,15 +59,15 @@ class NotificationManager {
};

if (!await PermissionsManager.doPermissionRequest(webcontents.id, 'notifications', {requestingUrl: view.view.server.url.toString(), isMainFrame: false})) {
log.verbose('permissions disallowed', webcontents.id, serverName, view.view.server.url.toString());
return {status: '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('click', () => {
log.debug('notification click', serverName, mention);
log.debug('notification click', serverName, mention.uId);

this.allActiveNotifications.delete(mention.uId);

Expand All @@ -86,14 +89,16 @@ class NotificationManager {
return new Promise((resolve) => {
// If mention never shows somehow, resolve the promise after 10s
const timeout = setTimeout(() => {
log.debug('notification timeout', serverName, mention.uId);
resolve({status: 'error', reason: 'notification_timeout'});
}, 10000);

mention.on('show', () => {
log.debug('displayMention.show');
log.debug('displayMention.show', serverName, mention.uId);

// On Windows, manually dismiss notifications from the same channel and only show the latest one
if (process.platform === 'win32') {
const mentionKey = `${mention.teamId}:${mention.channelId}`;
if (this.mentionsPerChannel.has(mentionKey)) {
log.debug(`close ${mentionKey}`);
this.mentionsPerChannel.get(mentionKey)?.close();
Expand All @@ -113,6 +118,7 @@ class NotificationManager {
mention.on('failed', (_, error) => {
this.allActiveNotifications.delete(mention.uId);
clearTimeout(timeout);
log.error('notification failed to show', serverName, mention.uId, error);
resolve({status: 'error', reason: 'electron_notification_failed', data: error});
});
mention.show();
Expand Down

0 comments on commit 84a3a15

Please sign in to comment.