[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:
parent
a0ad135fd3
commit
e7cf7a81e9
|
@ -1,2 +1,3 @@
|
||||||
node_modules
|
node_modules
|
||||||
dist
|
dist
|
||||||
|
api-types/lib
|
|
@ -28,7 +28,7 @@ export type DesktopAPI = {
|
||||||
) => void) => () => void;
|
) => void) => () => void;
|
||||||
|
|
||||||
// Unreads/mentions/notifications
|
// Unreads/mentions/notifications
|
||||||
sendNotification: (title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, soundName: string) => void;
|
sendNotification: (title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, soundName: string) => Promise<{result: string; reason?: string; data?: string}>;
|
||||||
onNotificationClicked: (listener: (channelId: string, teamId: string, url: string) => void) => () => void;
|
onNotificationClicked: (listener: (channelId: string, teamId: string, url: string) => void) => () => void;
|
||||||
setUnreadsAndMentions: (isUnread: boolean, mentionCount: number) => void;
|
setUnreadsAndMentions: (isUnread: boolean, mentionCount: number) => void;
|
||||||
|
|
||||||
|
|
14
api-types/lib/index.d.ts
vendored
14
api-types/lib/index.d.ts
vendored
|
@ -1,6 +1,4 @@
|
||||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
export type DesktopSourcesOptions = {
|
||||||
// See LICENSE.txt for license information.
|
|
||||||
export declare type DesktopSourcesOptions = {
|
|
||||||
types: Array<'screen' | 'window'>;
|
types: Array<'screen' | 'window'>;
|
||||||
thumbnailSize?: {
|
thumbnailSize?: {
|
||||||
height: number;
|
height: number;
|
||||||
|
@ -8,12 +6,12 @@ export declare type DesktopSourcesOptions = {
|
||||||
};
|
};
|
||||||
fetchWindowIcons?: boolean;
|
fetchWindowIcons?: boolean;
|
||||||
};
|
};
|
||||||
export declare type DesktopCaptureSource = {
|
export type DesktopCaptureSource = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
thumbnailURL: string;
|
thumbnailURL: string;
|
||||||
};
|
};
|
||||||
export declare type DesktopAPI = {
|
export type DesktopAPI = {
|
||||||
isDev: () => Promise<boolean>;
|
isDev: () => Promise<boolean>;
|
||||||
getAppInfo: () => Promise<{
|
getAppInfo: () => Promise<{
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -22,7 +20,11 @@ export declare type DesktopAPI = {
|
||||||
reactAppInitialized: () => void;
|
reactAppInitialized: () => void;
|
||||||
setSessionExpired: (isExpired: boolean) => void;
|
setSessionExpired: (isExpired: boolean) => void;
|
||||||
onUserActivityUpdate: (listener: (userIsActive: boolean, idleTime: number, isSystemEvent: boolean) => void) => () => void;
|
onUserActivityUpdate: (listener: (userIsActive: boolean, idleTime: number, isSystemEvent: boolean) => void) => () => void;
|
||||||
sendNotification: (title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, soundName: string) => void;
|
sendNotification: (title: string, body: string, channelId: string, teamId: string, url: string, silent: boolean, soundName: string) => Promise<{
|
||||||
|
result: string;
|
||||||
|
reason?: string;
|
||||||
|
data?: string;
|
||||||
|
}>;
|
||||||
onNotificationClicked: (listener: (channelId: string, teamId: string, url: string) => void) => () => void;
|
onNotificationClicked: (listener: (channelId: string, teamId: string, url: string) => void) => () => void;
|
||||||
setUnreadsAndMentions: (isUnread: boolean, mentionCount: number) => void;
|
setUnreadsAndMentions: (isUnread: boolean, mentionCount: number) => void;
|
||||||
requestBrowserHistoryStatus: () => Promise<{
|
requestBrowserHistoryStatus: () => Promise<{
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
|
"use strict";
|
||||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||||
// See LICENSE.txt for license information.
|
// See LICENSE.txt for license information.
|
||||||
'use strict';
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
|
||||||
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
|
||||||
// See LICENSE.txt for license information.
|
|
||||||
Object.defineProperty(exports, '__esModule', {value: true});
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@mattermost/desktop-api",
|
"name": "@mattermost/desktop-api",
|
||||||
"version": "5.8.0-1",
|
"version": "5.8.0-3",
|
||||||
"description": "Shared types for the Desktop App API provided to the Web App",
|
"description": "Shared types for the Desktop App API provided to the Web App",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"mattermost"
|
"mattermost"
|
||||||
|
|
|
@ -12,7 +12,8 @@
|
||||||
"jsx": "react",
|
"jsx": "react",
|
||||||
"outDir": "./lib",
|
"outDir": "./lib",
|
||||||
"rootDir": ".",
|
"rootDir": ".",
|
||||||
"composite": true
|
"composite": true,
|
||||||
|
"types": []
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"./index.ts"
|
"./index.ts"
|
||||||
|
|
|
@ -248,7 +248,7 @@ function initializeBeforeAppReady() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function initializeInterCommunicationEventListeners() {
|
function initializeInterCommunicationEventListeners() {
|
||||||
ipcMain.on(NOTIFY_MENTION, handleMentionNotification);
|
ipcMain.handle(NOTIFY_MENTION, handleMentionNotification);
|
||||||
ipcMain.handle(GET_APP_INFO, handleAppVersion);
|
ipcMain.handle(GET_APP_INFO, handleAppVersion);
|
||||||
ipcMain.on(UPDATE_SHORTCUT_MENU, handleUpdateMenuEvent);
|
ipcMain.on(UPDATE_SHORTCUT_MENU, handleUpdateMenuEvent);
|
||||||
ipcMain.on(FOCUS_BROWSERVIEW, ViewManager.focusCurrentView);
|
ipcMain.on(FOCUS_BROWSERVIEW, ViewManager.focusCurrentView);
|
||||||
|
|
|
@ -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});
|
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() {
|
export function handleOpenAppMenu() {
|
||||||
|
|
|
@ -32,19 +32,19 @@ class NotificationManager {
|
||||||
|
|
||||||
if (!Notification.isSupported()) {
|
if (!Notification.isSupported()) {
|
||||||
log.error('notification not supported');
|
log.error('notification not supported');
|
||||||
return;
|
return {result: 'error', reason: 'notification_api', data: 'notification not supported'};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await getDoNotDisturb()) {
|
if (await getDoNotDisturb()) {
|
||||||
return;
|
return {result: 'not_sent', reason: 'os_dnd'};
|
||||||
}
|
}
|
||||||
|
|
||||||
const view = ViewManager.getViewByWebContentsId(webcontents.id);
|
const view = ViewManager.getViewByWebContentsId(webcontents.id);
|
||||||
if (!view) {
|
if (!view) {
|
||||||
return;
|
return {result: 'error', reason: 'missing_view'};
|
||||||
}
|
}
|
||||||
if (!view.view.shouldNotify) {
|
if (!view.view.shouldNotify) {
|
||||||
return;
|
return {result: 'error', reason: 'view_should_not_notify'};
|
||||||
}
|
}
|
||||||
const serverName = view.view.server.name;
|
const serverName = view.view.server.name;
|
||||||
|
|
||||||
|
@ -56,32 +56,13 @@ class NotificationManager {
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!await PermissionsManager.doPermissionRequest(webcontents.id, 'notifications', view.view.server.url.toString())) {
|
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 mention = new Mention(options, channelId, teamId);
|
||||||
const mentionKey = `${mention.teamId}:${mention.channelId}`;
|
const mentionKey = `${mention.teamId}:${mention.channelId}`;
|
||||||
this.allActiveNotifications.set(mention.uId, mention);
|
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', () => {
|
mention.on('click', () => {
|
||||||
log.debug('notification click', serverName, mention);
|
log.debug('notification click', serverName, mention);
|
||||||
|
|
||||||
|
@ -97,10 +78,40 @@ class NotificationManager {
|
||||||
this.allActiveNotifications.delete(mention.uId);
|
this.allActiveNotifications.delete(mention.uId);
|
||||||
});
|
});
|
||||||
|
|
||||||
mention.on('failed', () => {
|
return new Promise((resolve) => {
|
||||||
this.allActiveNotifications.delete(mention.uId);
|
// 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) {
|
public async displayDownloadCompleted(fileName: string, path: string, serverName: string) {
|
||||||
|
|
|
@ -72,7 +72,7 @@ const desktopAPI: DesktopAPI = {
|
||||||
|
|
||||||
// Unreads/mentions/notifications
|
// Unreads/mentions/notifications
|
||||||
sendNotification: (title, body, channelId, teamId, url, silent, soundName) =>
|
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),
|
onNotificationClicked: (listener) => createListener(NOTIFICATION_CLICKED, listener),
|
||||||
setUnreadsAndMentions: (isUnread, mentionCount) => ipcRenderer.send(UNREADS_AND_MENTIONS, isUnread, mentionCount),
|
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': {
|
case 'dispatch-notification': {
|
||||||
const {title, body, channel, teamId, url, silent, data: messageData} = message;
|
const {title, body, channel, teamId, url, silent, data: messageData} = message;
|
||||||
channels.set(channel.id, channel);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case BROWSER_HISTORY_PUSH: {
|
case BROWSER_HISTORY_PUSH: {
|
||||||
|
|
Loading…
Reference in a new issue