diff --git a/desktop/flow-typed/npm/electron_v22.0.0.js b/desktop/flow-typed/npm/electron_v22.0.0.js index 0f4ce07a0..155bcce96 100644 --- a/desktop/flow-typed/npm/electron_v22.0.0.js +++ b/desktop/flow-typed/npm/electron_v22.0.0.js @@ -1,513 +1,513 @@ // @flow // flow-typed signature: f8bfa3876f1890f644b65b1ebd801ed8 // flow-typed version: <>/electron_v22.0.0/flow_v0.182.0 declare module 'electron' { declare export var app: App; declare type App = { quit(): void, whenReady(): Promise, hide(): void, show(): void, setName: (name: string) => void, setAppUserModelId: (id: string) => void, getVersion: () => string, dock: Dock, isPackaged: boolean, name: string, on: >( event: T, listener: $ElementType, ) => void, removeListener: >( event: T, listener: $ElementType, ) => void, }; declare type AppEvents = { 'window-all-closed': () => void, 'activate': (event: Event, hasVisibleWindows: boolean) => void, 'quit': (event: Event, exitCode: number) => void, 'render-process-gone': ( event: Event, webContents: WebContents, details: { reason: string, exitCode: number, }, ) => void, 'child-process-gone': ( event: Event, details: { type: string, reason: string, exitCode: number, serviceName?: string, name?: string, }, ) => void, }; declare export class BrowserWindow { constructor(options?: { +width?: number, +height?: number, +x?: number, +y?: number, +useContentSize?: boolean, +center?: boolean, +minWidth?: number, +minHeight?: number, +maxWidth?: number, +maxHeight?: number, +resizable?: boolean, +movable?: boolean, +minimizable?: boolean, +maximizable?: boolean, +closable?: boolean, +focusable?: boolean, +alwaysOnTop?: boolean, +fullscreen?: boolean, +fullscreenable?: boolean, +simpleFullscreen?: boolean, +skipTaskbar?: boolean, +kiosk?: boolean, +title?: string, +icon?: string, +show?: boolean, +paintWhenInitiallyHidden?: boolean, +frame?: boolean, +parent?: BrowserWindow, +modal?: boolean, +acceptFirstMouse?: boolean, +disableAutoHideCursor?: boolean, +autoHideMenuBar?: boolean, +enableLargerThanScreen?: boolean, +backgroundColor?: string, +hasShadow?: boolean, +opacity?: number, +darkTheme?: boolean, +transparent?: boolean, +type?: string, +visualEffectState?: 'followWindow' | 'active' | 'inactive', +titleBarStyle?: | 'default' | 'hidden' | 'hiddenInset' | 'customButtonsOnHover', +trafficLightPosition?: { +x: number, +y: number }, +roundedCorners?: boolean, +fullscreenWindowTitle?: boolean, +thickFrame?: boolean, +vibrancy?: | 'appearance-based' | 'light' | 'dark' | 'titlebar' | 'selection' | 'menu' | 'popover' | 'sidebar' | 'medium-light' | 'ultra-dark' | 'header' | 'sheet' | 'window' | 'hud' | 'fullscreen-ui' | 'tooltip' | 'content' | 'under-window' | 'under-page', +zoomToPageWidth?: boolean, +tabbingIdentifier?: string, +webPreferences?: { +preload?: string, }, +titleBarOverlay?: | { +color?: string, +symbolColor?: string, +height?: number } | boolean, }): void; destroy(): void; close(): void; show(): void; hide(): void; maximize(): void; unmaximize(): void; isMaximized(): boolean; minimize(): void; loadURL( url: string, options?: { +userAgent?: string, +extraHeaders?: string, +baseURLForDataURL?: string, }, ): Promise; loadFile( filePath: string, options?: { +query?: { [string]: string }, +search?: string, +hash?: string, }, ): Promise; reload(): void; setMenu(menu: Menu | null): void; isDestroyed(): boolean; static getAllWindows(): $ReadOnlyArray; webContents: WebContents; on>( event: T, listener: $ElementType, ): void; removeListener>( event: T, listener: $ElementType, ): void; } declare type BrowserWindowEvents = { close: (event: Event) => void, closed: () => void, }; declare export type Event = { preventDefault: () => void, }; declare export var contextBridge: ContextBridge; declare type ContextBridge = { exposeInMainWorld(apiKey: string, api: mixed): void, }; declare export var autoUpdater: AutoUpdater; declare class AutoUpdater { setFeedURL(options: { +url: string, +headers?: { +[string]: string }, +serverType?: 'json' | 'default', }): void; getFeedURL(): string; checkForUpdates(): void; quitAndInstall(): void; on>( event: T, listener: $ElementType, ): void; removeListener>( event: T, listener: $ElementType, ): void; } declare type AutoUpdaterEvents = { 'error': Error => void, 'checking-for-update': () => void, 'update-available': () => void, 'update-not-available': () => void, 'update-downloaded': ( event: Event, releaseNotes?: string, releaseName: string, releaseDate?: Date, updateURL?: string, ) => void, }; declare class Dock { setBadge(text: string): void; getBadge(): string; hide(): void; show(): Promise; } declare export var ipcMain: IpcMain; declare type IpcMain = { on( channel: string, listener: (event: IpcMainEvent, ...args: $ReadOnlyArray) => mixed, ): void, removeListener( channel: string, listener: (...args: $ReadOnlyArray) => void, ): void, }; declare export type IpcMainEvent = { +processId: number, +frameId: number, returnValue: mixed, +sender: WebContents, +reply: (channel: string, ...args: $ReadOnlyArray) => void, }; declare export var ipcRenderer: IpcRenderer; declare type IpcRenderer = { on( channel: string, listener: (event: IpcRendererEvent, ...args: $ReadOnlyArray) => void, ): void, removeListener( channel: string, listener: (...args: $ReadOnlyArray) => void, ): void, send(channel: string, ...args: $ReadOnlyArray): void, sendSync(channel: string, ...args: $ReadOnlyArray): any, }; declare export type MenuItemConstructorOptions = { click?: () => void, label?: string, submenu?: $ReadOnlyArray, type?: 'normal' | 'separator' | 'submenu' | 'checkbox' | 'radio', role?: | 'undo' | 'redo' | 'cut' | 'copy' | 'paste' | 'pasteAndMatchStyle' | 'delete' | 'selectAll' | 'reload' | 'forceReload' | 'toggleDevTools' | 'resetZoom' | 'zoomIn' | 'zoomOut' | 'toggleSpellChecker' | 'togglefullscreen' | 'window' | 'minimize' | 'close' | 'help' | 'about' | 'services' | 'hide' | 'hideOthers' | 'unhide' | 'quit' | 'showSubstitutions' | 'toggleSmartQuotes' | 'toggleSmartDashes' | 'toggleTextReplacement' | 'startSpeaking' | 'stopSpeaking' | 'zoom' | 'front' | 'appMenu' | 'fileMenu' | 'editMenu' | 'viewMenu' | 'shareMenu' | 'recentDocuments' | 'toggleTabBar' | 'selectNextTab' | 'selectPreviousTab' | 'mergeAllWindows' | 'clearRecentDocuments' | 'moveTabToNewWindow' | 'windowMenu', }; declare export class Menu { constructor(): void; static setApplicationMenu(menu: Menu | null): void; static buildFromTemplate( template: $ReadOnlyArray, ): Menu; } declare export var pushNotifications: PushNotifications; declare class PushNotifications { registerForAPNSNotifications(): Promise; unregisterForAPNSNotifications(): void; on>( event: T, listener: $ElementType, ): void; removeListener>( event: T, listener: $ElementType, ): void; } declare type PushNotificationsEvents = { 'received-apns-notification': ( event: Event, - userInfo: { +[string]: mixed, +encryptedPayload?: string }, + userInfo: {+[string]: mixed}, ) => void, }; declare export class Notification { constructor(options?: { +title?: string, +subtitle?: string, +body?: string, +silent?: boolean, +hasReply?: boolean, +timeoutType?: 'default' | 'never', +replyPlaceholder?: string, +sound?: string, +urgency?: 'normal' | 'critical' | 'low', +actions?: NotificationAction[], +closeButtonText?: string, +toastXml?: string, +icon?: string, }): void; static isSupported(): boolean; show(): void; close(): void; +title: string; +subtitle: string; +body: string; +replyPlaceholder: string; +sound: string; +closeButtonText: string; +silent: boolean; +hasReply: boolean; +urgency: 'normal' | 'critical' | 'low'; +timeoutType: 'default' | 'never'; +toastXml: string; on>( event: T, listener: $ElementType, ): void; removeListener>( event: T, listener: $ElementType, ): void; } declare type NotificationEvents = { show: (event: Event) => void, click: (event: Event) => void, close: (event: Event) => void, reply: (event: Event, reply: string) => void, action: (event: Event, index: number) => void, failed: (event: Event, error: string) => void, }; declare export var shell: Shell; declare type Shell = { openExternal( url: string, options?: { +activate?: boolean, +workingDirectory?: string }, ): Promise, }; declare export var systemPreferences: SystemPreferences; declare type SystemPreferences = { getUserDefault>( key: string, type: Type, ): $ElementType, }; declare export type UserDefaultTypes = { string: string, boolean: boolean, integer: number, float: number, double: number, url: string, array: Array, dictionary: { [string]: mixed }, }; declare class WebContents { loadURL( url: string, options?: { +userAgent?: string, +extraHeaders?: string, +baseURLForDataURL?: string, }, ): Promise; loadFile( filePath: string, options?: { +query?: { [string]: string }, +search?: string, +hash?: string, }, ): Promise; canGoBack(): boolean; canGoForward(): boolean; clearHistory(): void; insertCSS(css: string, options?: { +cssOrigin?: string }): Promise; setWindowOpenHandler( handler: (details: { +url: string, +frameName: string, +features: string, +disposition: | 'default' | 'foreground-tab' | 'background-tab' | 'new-window' | 'save-to-disk' | 'other', }) => | { +action: 'deny' } | { +action: 'allow', +outlivesOpener?: boolean }, ): void; send(channel: string, ...args: $ReadOnlyArray): void; on>( event: T, listener: $ElementType, ): void; removeListener: >( event: T, listener: $ElementType, ) => void; inspectSharedWorker(): void; } declare type WebContentsEvents = { 'did-finish-load': () => void, 'did-fail-load': ( event: Event, errorCode: number, errorDescription: string, validatedURL: string, isMainFrame: boolean, frameProcessId: number, frameRoutingId: number, ) => void, 'did-navigate-in-page': ( event: Event, url: string, isMainFrame: boolean, frameProcessId: number, frameRoutingId: number, ) => void, }; declare export type IpcRendererEvent = { sender: IpcRenderer, senderId: number, ports: $ReadOnlyArray, }; } declare module 'electron/main' { declare export { app, BrowserWindow, shell, Menu, ipcMain, systemPreferences, autoUpdater, pushNotifications, Notification, } from 'electron'; } declare module 'electron/renderer' { declare export { IpcRendererEvent, contextBridge, ipcRenderer, } from 'electron'; } diff --git a/desktop/src/main.js b/desktop/src/main.js index 0ae41ccf4..26cd70ae6 100644 --- a/desktop/src/main.js +++ b/desktop/src/main.js @@ -1,379 +1,383 @@ // @flow import type { Event, MenuItemConstructorOptions } from 'electron'; import { app, BrowserWindow, shell, Menu, ipcMain, systemPreferences, autoUpdater, // eslint-disable-next-line import/extensions } from 'electron/main'; import contextMenu from 'electron-context-menu'; import fs from 'fs'; import path from 'path'; import { initAutoUpdate } from './auto-update.js'; import { handleSquirrelEvent } from './handle-squirrel-event.js'; import { listenForNotifications, registerForNotifications, showNewNotification, } from './push-notifications.js'; const isDev = process.env.ENV === 'dev'; const url = isDev ? 'http://localhost:3000/webapp/' : 'https://web.comm.app'; const isMac = process.platform === 'darwin'; const scrollbarCSS = fs.promises.readFile( path.resolve(__dirname, '../scrollbar.css'), 'utf8', ); let mainWindow = null; const setApplicationMenu = () => { let mainMenu: MenuItemConstructorOptions[] = []; if (isMac) { mainMenu = [ { label: app.name, submenu: [ { role: 'about' }, { type: 'separator' }, { role: 'services' }, { type: 'separator' }, { role: 'hide' }, { role: 'hideOthers' }, { role: 'unhide' }, { type: 'separator' }, { role: 'quit' }, ], }, ]; } const viewMenu = { label: 'View', submenu: [ { role: 'reload' }, { type: 'separator' }, { role: 'resetZoom' }, { role: 'zoomIn' }, { role: 'zoomOut' }, { type: 'separator' }, { role: 'togglefullscreen' }, { role: 'toggleDevTools' }, { label: 'Toggle Shared Worker Developer Tools', click: () => { if (mainWindow) { mainWindow.webContents.inspectSharedWorker(); } }, }, ], }; const windowMenu = { label: 'Window', submenu: [ { role: 'minimize' }, ...(isMac ? [ { type: 'separator' }, { role: 'front' }, { type: 'separator' }, { role: 'window' }, ] : [{ role: 'close' }]), ], }; const menu = Menu.buildFromTemplate([ ...mainMenu, { role: 'fileMenu' }, { role: 'editMenu' }, viewMenu, windowMenu, ]); Menu.setApplicationMenu(menu); }; const createMainWindow = (urlPath?: string) => { const win = new BrowserWindow({ show: false, width: 1300, height: 800, minWidth: 1100, minHeight: 600, titleBarStyle: 'hidden', trafficLightPosition: { x: 20, y: 24 }, titleBarOverlay: { color: '#0A0A0A', symbolColor: '#FFFFFF', height: 64, }, backgroundColor: '#0A0A0A', webPreferences: { preload: path.resolve(__dirname, 'preload.js'), }, }); const updateNavigationState = () => { win.webContents.send('on-navigate', { canGoBack: win.webContents.canGoBack(), canGoForward: win.webContents.canGoForward(), }); }; win.webContents.on('did-navigate-in-page', updateNavigationState); const clearHistory = () => { win.webContents.clearHistory(); updateNavigationState(); }; ipcMain.on('clear-history', clearHistory); const doubleClickTopBar = () => { if (isMac) { // Possible values for AppleActionOnDoubleClick are Maximize, // Minimize or None. We handle the last two inside this if. // Maximize (which is the only behaviour for other platforms) // is handled in the later block. const action = systemPreferences.getUserDefault( 'AppleActionOnDoubleClick', 'string', ); if (action === 'None') { return; } else if (action === 'Minimize') { win.minimize(); return; } } if (win.isMaximized()) { win.unmaximize(); } else { win.maximize(); } }; ipcMain.on('double-click-top-bar', doubleClickTopBar); const updateDownloaded = ( event: Event, releaseNotes?: string, releaseName: string, ) => { win.webContents.send('on-new-version-available', releaseName); }; autoUpdater.on('update-downloaded', updateDownloaded); win.on('closed', () => { mainWindow = null; ipcMain.removeListener('clear-history', clearHistory); ipcMain.removeListener('double-click-top-bar', doubleClickTopBar); autoUpdater.removeListener('update-downloaded', updateDownloaded); }); win.webContents.setWindowOpenHandler(({ url: openURL }) => { void shell.openExternal(openURL); // Returning 'deny' prevents a new electron window from being created return { action: 'deny' }; }); void (async () => { const css = await scrollbarCSS; await win.webContents.insertCSS(css); })(); void win.loadURL(url + (urlPath ?? '')); mainWindow = win; return win; }; const createSplashWindow = () => { const win = new BrowserWindow({ width: 300, height: 300, resizable: false, frame: false, alwaysOnTop: true, center: true, backgroundColor: '#111827', }); void win.loadFile(path.resolve(__dirname, '../pages/splash.html')); return win; }; const createErrorWindow = () => { const win = new BrowserWindow({ show: false, width: 400, height: 300, resizable: false, center: true, titleBarStyle: 'hidden', trafficLightPosition: { x: 20, y: 24 }, backgroundColor: '#111827', }); win.on('close', () => { app.quit(); }); void win.loadFile(path.resolve(__dirname, '../pages/error.html')); return win; }; const sendDeviceTokenToWebApp = async () => { if (!mainWindow) { return; } const token = await registerForNotifications(); mainWindow?.webContents.send('on-device-token-registered', token); }; const show = (urlPath?: string) => { const splash = createSplashWindow(); const error = createErrorWindow(); const main = createMainWindow(urlPath); let loadedSuccessfully = true; const failedLoadHandler = () => { loadedSuccessfully = false; if (!splash.isDestroyed()) { splash.destroy(); } if (!error.isDestroyed()) { error.show(); } setTimeout(() => { loadedSuccessfully = true; void main.loadURL(url); }, 1000); }; main.webContents.on('did-fail-load', failedLoadHandler); main.webContents.on('did-finish-load', () => { if (loadedSuccessfully) { if (!splash.isDestroyed()) { splash.destroy(); } if (!error.isDestroyed()) { error.destroy(); } main.webContents.removeListener('did-fail-load', failedLoadHandler); main.show(); if (app.isPackaged) { void sendDeviceTokenToWebApp(); } } }); }; const run = () => { app.setName('Comm'); contextMenu({ showSaveImageAs: true, showSaveVideoAs: true, }); if (process.platform === 'win32') { app.setAppUserModelId('Comm'); } setApplicationMenu(); void (async () => { await app.whenReady(); const handleNotificationClick = (threadID?: string) => { if (mainWindow && threadID) { mainWindow.webContents.send('on-notification-clicked', { threadID, }); } else if (threadID) { show(`chat/thread/${threadID}/`); } else { show(); } }; - const handleEncryptedNotification = (encryptedPayload: string) => { + const handleEncryptedNotification = ( + encryptedPayload: string, + keyserverID: string, + ) => { if (mainWindow) { mainWindow.webContents.send('on-encrypted-notification', { encryptedPayload, + keyserverID, }); } }; if (app.isPackaged) { try { initAutoUpdate(); } catch (error) { console.error(error); } listenForNotifications( handleNotificationClick, handleEncryptedNotification, ); ipcMain.on('fetch-device-token', sendDeviceTokenToWebApp); } ipcMain.on('set-badge', (event, value) => { if (isMac) { app.dock.setBadge(value?.toString() ?? ''); } }); ipcMain.on('get-version', event => { event.returnValue = app.getVersion().toString(); }); ipcMain.on( 'show-decrypted-notification', (event, decryptedNotification) => { showNewNotification(decryptedNotification, handleNotificationClick); }, ); show(); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { show(); } }); })(); app.on('window-all-closed', () => { if (!isMac) { app.quit(); } }); app.on('render-process-gone', (event, webContents, details) => { console.error( `EVENT: render-process-gone. Reason: '${details.reason}'. ` + `ExitCode: '${details.exitCode}'.`, ); }); app.on('child-process-gone', (event, details) => { console.error( `EVENT: child-process-gone. Process type: '${details.type}'. ` + `Reason: '${details.reason}'. ExitCode: '${details.exitCode}'.`, ); }); }; if (app.isPackaged && process.platform === 'win32') { if (!handleSquirrelEvent()) { run(); } } else { run(); } diff --git a/desktop/src/push-notifications.js b/desktop/src/push-notifications.js index 77314bf86..2afa98b19 100644 --- a/desktop/src/push-notifications.js +++ b/desktop/src/push-notifications.js @@ -1,156 +1,167 @@ // @flow import type { PushNotificationManager as PushNotificationManagerType, PushNotificationReceivedEventArgs, } from '@commapp/windowspush'; // eslint-disable-next-line import/extensions import { app, pushNotifications, Notification } from 'electron/main'; import EventEmitter from 'events'; import { resolve } from 'path'; import { isNormalStartup } from './handle-squirrel-event.js'; let windowsPushNotificationManager; const windowsPushNotifEventEmitter = new EventEmitter(); if (process.platform === 'win32' && app.isPackaged && isNormalStartup()) { void (async () => { try { const { PushNotificationManager } = await import('@commapp/windowspush'); if (!PushNotificationManager.isSupported()) { return; } windowsPushNotificationManager = PushNotificationManager.default; const handleEvent = ( manager: PushNotificationManagerType, event: PushNotificationReceivedEventArgs, ) => { const byteArray = []; for (let i = 0; i < event.payload.length; i++) { byteArray.push(event.payload[i]); } const payload = Buffer.from(byteArray).toString('utf-8'); windowsPushNotifEventEmitter.emit( 'received-wns-notification', JSON.parse(payload), ); }; // Windows requires that this must be called before the call to `register` windowsPushNotificationManager.addListener('PushReceived', handleEvent); windowsPushNotificationManager.register(); app.on('quit', () => { windowsPushNotificationManager.removeListener( 'PushReceived', handleEvent, ); windowsPushNotificationManager.unregisterAll(); }); } catch (err) { console.error( `Error while loading windows push notifications ${err.message}`, ); } })(); } async function registerForNotifications(): Promise { if (process.platform === 'darwin') { try { const token = await pushNotifications.registerForAPNSNotifications(); return token; } catch (err) { console.error(err); } } else if (process.platform === 'win32' && windowsPushNotificationManager) { try { const token = await new Promise((resolvePromise, reject) => { windowsPushNotificationManager.createChannelAsync( 'f09f4211-a998-40c1-a515-689e3faecb62', (error, result) => { if (error) { reject(error); } resolvePromise(result.channel.uri); }, ); }); return token; } catch (err) { console.error(err); } } return null; } function showNewNotification( payload: { +[string]: mixed }, handleClick: (threadID?: string) => void, ) { const windowsIconPath = resolve(__dirname, '../icons/icon.ico'); if ( typeof payload.error === 'string' && typeof payload.displayErrorMessage === 'boolean' ) { const notif = new Notification({ title: 'Comm notification', body: payload.displayErrorMessage ? payload.error : undefined, icon: process.platform === 'win32' ? windowsIconPath : undefined, }); notif.on('click', () => handleClick()); notif.show(); return; } if ( typeof payload.title !== 'string' || typeof payload.body !== 'string' || typeof payload.threadID !== 'string' ) { return; } const { title, body, threadID } = payload; const notif = new Notification({ title, body, icon: process.platform === 'win32' ? windowsIconPath : undefined, }); notif.on('click', () => handleClick(threadID)); notif.show(); } function listenForNotifications( handleClick: (threadID?: string) => void, - handleEncryptedNotification: (encryptedPayload: string) => void, + handleEncryptedNotification: ( + encryptedPayload: string, + keyserverID: string, + ) => void, ) { if (process.platform === 'darwin') { pushNotifications.on('received-apns-notification', (event, userInfo) => { - if (userInfo.encryptedPayload) { - handleEncryptedNotification(userInfo.encryptedPayload); - } else { - showNewNotification(userInfo, handleClick); + const { keyserverID, encryptedPayload } = userInfo; + if ( + typeof keyserverID === 'string' && + typeof encryptedPayload === 'string' + ) { + handleEncryptedNotification(encryptedPayload, keyserverID); + return; } + showNewNotification(userInfo, handleClick); }); } else if (process.platform === 'win32') { windowsPushNotifEventEmitter.on('received-wns-notification', payload => { - if (payload.encryptedPayload) { - handleEncryptedNotification(payload.encryptedPayload); - } else { - showNewNotification(payload, handleClick); + const { keyserverID, encryptedPayload } = payload; + if ( + typeof keyserverID === 'string' && + typeof encryptedPayload === 'string' + ) { + handleEncryptedNotification(encryptedPayload, keyserverID); + return; } + showNewNotification(payload, handleClick); }); } } export { listenForNotifications, registerForNotifications, showNewNotification, }; diff --git a/lib/types/electron-types.js b/lib/types/electron-types.js index d0ea5ed5f..2d380fade 100644 --- a/lib/types/electron-types.js +++ b/lib/types/electron-types.js @@ -1,34 +1,35 @@ // @flow type OnNavigateListener = ({ +canGoBack: boolean, +canGoForward: boolean, }) => void; type OnNewVersionAvailableListener = (version: string) => void; type OnDeviceTokenRegisteredListener = (token: ?string) => void; type OnNotificationClickedListener = (data: { threadID: string }) => void; type OnEncryptedNotificationListener = (data: { encryptedPayload: string, + keyserverID?: string, }) => mixed; export type ElectronBridge = { // Returns a callback that you can call to remove the listener +onNavigate: OnNavigateListener => () => void, +clearHistory: () => void, +doubleClickTopBar: () => void, +setBadge: (number | string | null) => void, +version?: string, // Returns a callback that you can call to remove the listener +onNewVersionAvailable?: OnNewVersionAvailableListener => () => void, +updateToNewVersion?: () => void, +platform?: 'windows' | 'macos', +onDeviceTokenRegistered?: OnDeviceTokenRegisteredListener => () => void, +onNotificationClicked?: OnNotificationClickedListener => () => void, +fetchDeviceToken: () => void, +onEncryptedNotification?: OnEncryptedNotificationListener => () => void, +showDecryptedNotification: (decryptedPayload: { +[string]: mixed }) => void, }; diff --git a/web/account/account-hooks.js b/web/account/account-hooks.js index 011129e14..c1063ba5c 100644 --- a/web/account/account-hooks.js +++ b/web/account/account-hooks.js @@ -1,411 +1,411 @@ // @flow import olm from '@commapp/olm'; import invariant from 'invariant'; import localforage from 'localforage'; import * as React from 'react'; import uuid from 'uuid'; import { initialEncryptedMessageContent } from 'lib/shared/crypto-utils.js'; import { OlmSessionCreatorContext } from 'lib/shared/olm-session-creator-context.js'; import type { SignedIdentityKeysBlob, CryptoStore, IdentityKeysBlob, CryptoStoreContextType, OLMIdentityKeys, NotificationsOlmDataType, } from 'lib/types/crypto-types.js'; import { type IdentityDeviceKeyUpload } from 'lib/types/identity-service-types.js'; import type { OlmSessionInitializationInfo } from 'lib/types/request-types.js'; import { retrieveAccountKeysSet } from 'lib/utils/olm-utils.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; import { generateCryptoKey, encryptData, exportKeyToJWK, } from '../crypto/aes-gcm-crypto-utils.js'; import { initOlm } from '../olm/olm-utils.js'; import { getOlmDataContentKeyForCookie, getOlmEncryptionKeyDBLabelForCookie, } from '../push-notif/notif-crypto-utils.js'; import { setCryptoStore } from '../redux/crypto-store-reducer.js'; import { useSelector } from '../redux/redux-utils.js'; import { isDesktopSafari } from '../shared-worker/utils/db-utils.js'; const CryptoStoreContext: React.Context = React.createContext(null); type Props = { +children: React.Node, }; function GetOrCreateCryptoStoreProvider(props: Props): React.Node { const dispatch = useDispatch(); const createCryptoStore = React.useCallback(async () => { await initOlm(); const identityAccount = new olm.Account(); identityAccount.create(); const { ed25519: identityED25519, curve25519: identityCurve25519 } = JSON.parse(identityAccount.identity_keys()); const identityAccountPicklingKey = uuid.v4(); const pickledIdentityAccount = identityAccount.pickle( identityAccountPicklingKey, ); const notificationAccount = new olm.Account(); notificationAccount.create(); const { ed25519: notificationED25519, curve25519: notificationCurve25519 } = JSON.parse(notificationAccount.identity_keys()); const notificationAccountPicklingKey = uuid.v4(); const pickledNotificationAccount = notificationAccount.pickle( notificationAccountPicklingKey, ); const newCryptoStore = { primaryAccount: { picklingKey: identityAccountPicklingKey, pickledAccount: pickledIdentityAccount, }, primaryIdentityKeys: { ed25519: identityED25519, curve25519: identityCurve25519, }, notificationAccount: { picklingKey: notificationAccountPicklingKey, pickledAccount: pickledNotificationAccount, }, notificationIdentityKeys: { ed25519: notificationED25519, curve25519: notificationCurve25519, }, }; dispatch({ type: setCryptoStore, payload: newCryptoStore }); return newCryptoStore; }, [dispatch]); const currentCryptoStore = useSelector(state => state.cryptoStore); const createCryptoStorePromiseRef = React.useRef>(null); const getCryptoStorePromise = React.useCallback(() => { if (currentCryptoStore) { return Promise.resolve(currentCryptoStore); } const currentCreateCryptoStorePromiseRef = createCryptoStorePromiseRef.current; if (currentCreateCryptoStorePromiseRef) { return currentCreateCryptoStorePromiseRef; } const newCreateCryptoStorePromise = (async () => { try { return await createCryptoStore(); } catch (e) { createCryptoStorePromiseRef.current = undefined; throw e; } })(); createCryptoStorePromiseRef.current = newCreateCryptoStorePromise; return newCreateCryptoStorePromise; }, [createCryptoStore, currentCryptoStore]); const isCryptoStoreSet = !!currentCryptoStore; React.useEffect(() => { if (!isCryptoStoreSet) { createCryptoStorePromiseRef.current = undefined; } }, [isCryptoStoreSet]); const contextValue = React.useMemo( () => ({ getInitializedCryptoStore: getCryptoStorePromise, }), [getCryptoStorePromise], ); return ( {props.children} ); } function useGetOrCreateCryptoStore(): () => Promise { const context = React.useContext(CryptoStoreContext); invariant(context, 'CryptoStoreContext not found'); return context.getInitializedCryptoStore; } function useGetSignedIdentityKeysBlob(): () => Promise { const getOrCreateCryptoStore = useGetOrCreateCryptoStore(); return React.useCallback(async () => { const [{ primaryAccount, primaryIdentityKeys, notificationIdentityKeys }] = await Promise.all([getOrCreateCryptoStore(), initOlm()]); const primaryOLMAccount = new olm.Account(); primaryOLMAccount.unpickle( primaryAccount.picklingKey, primaryAccount.pickledAccount, ); const identityKeysBlob: IdentityKeysBlob = { primaryIdentityPublicKeys: primaryIdentityKeys, notificationIdentityPublicKeys: notificationIdentityKeys, }; const payloadToBeSigned: string = JSON.stringify(identityKeysBlob); const signedIdentityKeysBlob: SignedIdentityKeysBlob = { payload: payloadToBeSigned, signature: primaryOLMAccount.sign(payloadToBeSigned), }; return signedIdentityKeysBlob; }, [getOrCreateCryptoStore]); } function useGetDeviceKeyUpload(): () => Promise { const getOrCreateCryptoStore = useGetOrCreateCryptoStore(); // `getSignedIdentityKeysBlob()` will initialize OLM, so no need to do it // again const getSignedIdentityKeysBlob = useGetSignedIdentityKeysBlob(); const dispatch = useDispatch(); return React.useCallback(async () => { const [signedIdentityKeysBlob, cryptoStore] = await Promise.all([ getSignedIdentityKeysBlob(), getOrCreateCryptoStore(), ]); const primaryOLMAccount = new olm.Account(); const notificationOLMAccount = new olm.Account(); primaryOLMAccount.unpickle( cryptoStore.primaryAccount.picklingKey, cryptoStore.primaryAccount.pickledAccount, ); notificationOLMAccount.unpickle( cryptoStore.notificationAccount.picklingKey, cryptoStore.notificationAccount.pickledAccount, ); const primaryAccountKeysSet = retrieveAccountKeysSet(primaryOLMAccount); const notificationAccountKeysSet = retrieveAccountKeysSet( notificationOLMAccount, ); const pickledPrimaryAccount = primaryOLMAccount.pickle( cryptoStore.primaryAccount.picklingKey, ); const pickledNotificationAccount = notificationOLMAccount.pickle( cryptoStore.notificationAccount.picklingKey, ); const updatedCryptoStore = { primaryAccount: { picklingKey: cryptoStore.primaryAccount.picklingKey, pickledAccount: pickledPrimaryAccount, }, primaryIdentityKeys: cryptoStore.primaryIdentityKeys, notificationAccount: { picklingKey: cryptoStore.notificationAccount.picklingKey, pickledAccount: pickledNotificationAccount, }, notificationIdentityKeys: cryptoStore.notificationIdentityKeys, }; dispatch({ type: setCryptoStore, payload: updatedCryptoStore }); return { keyPayload: signedIdentityKeysBlob.payload, keyPayloadSignature: signedIdentityKeysBlob.signature, contentPrekey: primaryAccountKeysSet.prekey, contentPrekeySignature: primaryAccountKeysSet.prekeySignature, notifPrekey: notificationAccountKeysSet.prekey, notifPrekeySignature: notificationAccountKeysSet.prekeySignature, contentOneTimeKeys: primaryAccountKeysSet.oneTimeKeys, notifOneTimeKeys: notificationAccountKeysSet.oneTimeKeys, }; }, [dispatch, getOrCreateCryptoStore, getSignedIdentityKeysBlob]); } function OlmSessionCreatorProvider(props: Props): React.Node { const getOrCreateCryptoStore = useGetOrCreateCryptoStore(); const currentCryptoStore = useSelector(state => state.cryptoStore); const createNewNotificationsSession = React.useCallback( async ( cookie: ?string, notificationsIdentityKeys: OLMIdentityKeys, notificationsInitializationInfo: OlmSessionInitializationInfo, keyserverID: string, ) => { const [{ notificationAccount }, encryptionKey] = await Promise.all([ getOrCreateCryptoStore(), generateCryptoKey({ extractable: isDesktopSafari }), initOlm(), ]); const account = new olm.Account(); const { picklingKey, pickledAccount } = notificationAccount; account.unpickle(picklingKey, pickledAccount); const notificationsPrekey = notificationsInitializationInfo.prekey; const session = new olm.Session(); session.create_outbound( account, notificationsIdentityKeys.curve25519, notificationsIdentityKeys.ed25519, notificationsPrekey, notificationsInitializationInfo.prekeySignature, notificationsInitializationInfo.oneTimeKey, ); const { body: initialNotificationsEncryptedMessage } = session.encrypt( JSON.stringify(initialEncryptedMessageContent), ); const mainSession = session.pickle(picklingKey); const notificationsOlmData: NotificationsOlmDataType = { mainSession, pendingSessionUpdate: mainSession, updateCreationTimestamp: Date.now(), picklingKey, }; const encryptedOlmData = await encryptData( new TextEncoder().encode(JSON.stringify(notificationsOlmData)), encryptionKey, ); const notifsOlmDataEncryptionKeyDBLabel = - getOlmEncryptionKeyDBLabelForCookie(cookie); + getOlmEncryptionKeyDBLabelForCookie(cookie, keyserverID); const notifsOlmDataContentKey = getOlmDataContentKeyForCookie( cookie, keyserverID, ); const persistEncryptionKeyPromise = (async () => { let cryptoKeyPersistentForm; if (isDesktopSafari) { // Safari doesn't support structured clone algorithm in service // worker context so we have to store CryptoKey as JSON cryptoKeyPersistentForm = await exportKeyToJWK(encryptionKey); } else { cryptoKeyPersistentForm = encryptionKey; } await localforage.setItem( notifsOlmDataEncryptionKeyDBLabel, cryptoKeyPersistentForm, ); })(); await Promise.all([ localforage.setItem(notifsOlmDataContentKey, encryptedOlmData), persistEncryptionKeyPromise, ]); return initialNotificationsEncryptedMessage; }, [getOrCreateCryptoStore], ); const createNewContentSession = React.useCallback( async ( contentIdentityKeys: OLMIdentityKeys, contentInitializationInfo: OlmSessionInitializationInfo, ) => { const [{ primaryAccount }] = await Promise.all([ getOrCreateCryptoStore(), initOlm(), ]); const account = new olm.Account(); const { picklingKey, pickledAccount } = primaryAccount; account.unpickle(picklingKey, pickledAccount); const contentPrekey = contentInitializationInfo.prekey; const session = new olm.Session(); session.create_outbound( account, contentIdentityKeys.curve25519, contentIdentityKeys.ed25519, contentPrekey, contentInitializationInfo.prekeySignature, contentInitializationInfo.oneTimeKey, ); const { body: initialContentEncryptedMessage } = session.encrypt( JSON.stringify(initialEncryptedMessageContent), ); return initialContentEncryptedMessage; }, [getOrCreateCryptoStore], ); const notificationsSessionPromise = React.useRef>(null); const createNotificationsSession = React.useCallback( async ( cookie: ?string, notificationsIdentityKeys: OLMIdentityKeys, notificationsInitializationInfo: OlmSessionInitializationInfo, keyserverID: string, ) => { if (notificationsSessionPromise.current) { return notificationsSessionPromise.current; } const newNotificationsSessionPromise = (async () => { try { return await createNewNotificationsSession( cookie, notificationsIdentityKeys, notificationsInitializationInfo, keyserverID, ); } catch (e) { notificationsSessionPromise.current = undefined; throw e; } })(); notificationsSessionPromise.current = newNotificationsSessionPromise; return newNotificationsSessionPromise; }, [createNewNotificationsSession], ); const isCryptoStoreSet = !!currentCryptoStore; React.useEffect(() => { if (!isCryptoStoreSet) { notificationsSessionPromise.current = undefined; } }, [isCryptoStoreSet]); const contextValue = React.useMemo( () => ({ notificationsSessionCreator: createNotificationsSession, contentSessionCreator: createNewContentSession, }), [createNewContentSession, createNotificationsSession], ); return ( {props.children} ); } export { useGetSignedIdentityKeysBlob, useGetOrCreateCryptoStore, OlmSessionCreatorProvider, GetOrCreateCryptoStoreProvider, useGetDeviceKeyUpload, }; diff --git a/web/push-notif/notif-crypto-utils.js b/web/push-notif/notif-crypto-utils.js index f20cf26fd..376f9998a 100644 --- a/web/push-notif/notif-crypto-utils.js +++ b/web/push-notif/notif-crypto-utils.js @@ -1,378 +1,384 @@ // @flow import olm from '@commapp/olm'; import localforage from 'localforage'; import { olmEncryptedMessageTypes, type NotificationsOlmDataType, } from 'lib/types/crypto-types.js'; import type { PlainTextWebNotification, EncryptedWebNotification, } from 'lib/types/notif-types.js'; import { getCookieIDFromCookie } from 'lib/utils/cookie-utils.js'; import { type EncryptedData, decryptData, encryptData, importJWKKey, } from '../crypto/aes-gcm-crypto-utils.js'; import { initOlm } from '../olm/olm-utils.js'; import { NOTIFICATIONS_OLM_DATA_CONTENT, NOTIFICATIONS_OLM_DATA_ENCRYPTION_KEY, } from '../shared-worker/utils/constants.js'; import { isDesktopSafari } from '../shared-worker/utils/db-utils.js'; export type WebNotifDecryptionError = { +id: string, +error: string, +displayErrorMessage?: boolean, }; export type WebNotifsServiceUtilsData = { +olmWasmPath: string, +staffCanSee: boolean, }; type DecryptionResult = { +newPendingSessionUpdate: string, +newUpdateCreationTimestamp: number, +decryptedNotification: T, }; export const WEB_NOTIFS_SERVICE_UTILS_KEY = 'webNotifsServiceUtils'; const SESSION_UPDATE_MAX_PENDING_TIME = 10 * 1000; async function decryptWebNotification( encryptedNotification: EncryptedWebNotification, ): Promise { const { id, encryptedPayload } = encryptedNotification; const utilsData = await localforage.getItem( WEB_NOTIFS_SERVICE_UTILS_KEY, ); if (!utilsData) { return { id, error: 'Necessary data not found in IndexedDB' }; } const { olmWasmPath, staffCanSee } = (utilsData: WebNotifsServiceUtilsData); let olmDBKeys; try { olmDBKeys = await getNotifsOlmSessionDBKeys(); } catch (e) { return { id, error: e.message, displayErrorMessage: staffCanSee, }; } const { olmDataContentKey, encryptionKeyDBKey } = olmDBKeys; const [encryptedOlmData, encryptionKey] = await Promise.all([ localforage.getItem(olmDataContentKey), retrieveEncryptionKey(encryptionKeyDBKey), ]); if (!encryptionKey || !encryptedOlmData) { return { id, error: 'Received encrypted notification but olm session was not created', displayErrorMessage: staffCanSee, }; } try { await olm.init({ locateFile: () => olmWasmPath }); const decryptedNotification = await commonDecrypt( encryptedOlmData, olmDataContentKey, encryptionKey, encryptedPayload, ); return { id, ...decryptedNotification }; } catch (e) { return { id, error: e.message, displayErrorMessage: staffCanSee, }; } } async function decryptDesktopNotification( encryptedPayload: string, staffCanSee: boolean, + // eslint-disable-next-line no-unused-vars + keyserverID?: string, ): Promise<{ +[string]: mixed }> { let encryptedOlmData, encryptionKey, olmDataContentKey; try { const { olmDataContentKey: olmDataContentKeyValue, encryptionKeyDBKey } = await getNotifsOlmSessionDBKeys(); olmDataContentKey = olmDataContentKeyValue; [encryptedOlmData, encryptionKey] = await Promise.all([ localforage.getItem(olmDataContentKey), retrieveEncryptionKey(encryptionKeyDBKey), initOlm(), ]); } catch (e) { return { error: e.message, displayErrorMessage: staffCanSee, }; } if (!encryptionKey || !encryptedOlmData) { return { error: 'Received encrypted notification but olm session was not created', displayErrorMessage: staffCanSee, }; } try { return await commonDecrypt( encryptedOlmData, olmDataContentKey, encryptionKey, encryptedPayload, ); } catch (e) { return { error: e.message, staffCanSee, }; } } async function commonDecrypt( encryptedOlmData: EncryptedData, olmDataContentKey: string, encryptionKey: CryptoKey, encryptedPayload: string, ): Promise { const serializedOlmData = await decryptData(encryptedOlmData, encryptionKey); const { mainSession, picklingKey, pendingSessionUpdate, updateCreationTimestamp, }: NotificationsOlmDataType = JSON.parse( new TextDecoder().decode(serializedOlmData), ); let updatedOlmData: NotificationsOlmDataType; let decryptedNotification: T; const shouldUpdateMainSession = Date.now() - updateCreationTimestamp > SESSION_UPDATE_MAX_PENDING_TIME; const decryptionWithPendingSessionResult = decryptWithPendingSession( pendingSessionUpdate, picklingKey, encryptedPayload, ); if (decryptionWithPendingSessionResult.decryptedNotification) { const { decryptedNotification: notifDecryptedWithPendingSession, newPendingSessionUpdate, newUpdateCreationTimestamp, } = decryptionWithPendingSessionResult; decryptedNotification = notifDecryptedWithPendingSession; updatedOlmData = { mainSession: shouldUpdateMainSession ? pendingSessionUpdate : mainSession, pendingSessionUpdate: newPendingSessionUpdate, updateCreationTimestamp: newUpdateCreationTimestamp, picklingKey, }; } else { const { newUpdateCreationTimestamp, decryptedNotification: notifDecryptedWithMainSession, } = decryptWithSession(mainSession, picklingKey, encryptedPayload); decryptedNotification = notifDecryptedWithMainSession; updatedOlmData = { mainSession: mainSession, pendingSessionUpdate, updateCreationTimestamp: newUpdateCreationTimestamp, picklingKey, }; } const updatedEncryptedSession = await encryptData( new TextEncoder().encode(JSON.stringify(updatedOlmData)), encryptionKey, ); await localforage.setItem(olmDataContentKey, updatedEncryptedSession); return decryptedNotification; } function decryptWithSession( pickledSession: string, picklingKey: string, encryptedPayload: string, ): DecryptionResult { const session = new olm.Session(); session.unpickle(picklingKey, pickledSession); const decryptedNotification: T = JSON.parse( session.decrypt(olmEncryptedMessageTypes.TEXT, encryptedPayload), ); const newPendingSessionUpdate = session.pickle(picklingKey); const newUpdateCreationTimestamp = Date.now(); return { decryptedNotification, newUpdateCreationTimestamp, newPendingSessionUpdate, }; } function decryptWithPendingSession( pendingSessionUpdate: string, picklingKey: string, encryptedPayload: string, ): DecryptionResult | { +error: string } { try { const { decryptedNotification, newPendingSessionUpdate, newUpdateCreationTimestamp, } = decryptWithSession( pendingSessionUpdate, picklingKey, encryptedPayload, ); return { newPendingSessionUpdate, newUpdateCreationTimestamp, decryptedNotification, }; } catch (e) { return { error: e.message }; } } async function retrieveEncryptionKey( encryptionKeyDBLabel: string, ): Promise { if (!isDesktopSafari) { return await localforage.getItem(encryptionKeyDBLabel); } // Safari doesn't support structured clone algorithm in service // worker context so we have to store CryptoKey as JSON const persistedCryptoKey = await localforage.getItem(encryptionKeyDBLabel); if (!persistedCryptoKey) { return null; } return await importJWKKey(persistedCryptoKey); } async function getNotifsOlmSessionDBKeys(): Promise<{ +olmDataContentKey: string, +encryptionKeyDBKey: string, }> { const dbKeys = await localforage.keys(); const olmDataContentKeys = sortOlmDBKeysArray( dbKeys.filter(key => key.startsWith(NOTIFICATIONS_OLM_DATA_CONTENT)), ); const encryptionKeyDBLabels = sortOlmDBKeysArray( dbKeys.filter(key => key.startsWith(NOTIFICATIONS_OLM_DATA_ENCRYPTION_KEY)), ); if (olmDataContentKeys.length === 0 || encryptionKeyDBLabels.length === 0) { throw new Error( 'Received encrypted notification but olm session was not created', ); } const latestDataContentKey = olmDataContentKeys[olmDataContentKeys.length - 1]; const latestEncryptionKeyDBKey = encryptionKeyDBLabels[encryptionKeyDBLabels.length - 1]; const latestDataContentCookieID = getCookieIDFromOlmDBKey(latestDataContentKey); const latestEncryptionKeyCookieID = getCookieIDFromOlmDBKey( latestEncryptionKeyDBKey, ); if (latestDataContentCookieID !== latestEncryptionKeyCookieID) { throw new Error( 'Olm sessions and their encryption keys out of sync. Latest cookie ' + `id for olm sessions ${latestDataContentCookieID}. Latest cookie ` + `id for olm session encryption keys ${latestEncryptionKeyCookieID}`, ); } const olmDBKeys = { olmDataContentKey: latestDataContentKey, encryptionKeyDBKey: latestEncryptionKeyDBKey, }; const keysToDelete: $ReadOnlyArray = [ ...olmDataContentKeys.slice(0, olmDataContentKeys.length - 1), ...encryptionKeyDBLabels.slice(0, encryptionKeyDBLabels.length - 1), ]; await Promise.all(keysToDelete.map(key => localforage.removeItem(key))); return olmDBKeys; } function getOlmDataContentKeyForCookie( cookie: ?string, // eslint-disable-next-line no-unused-vars keyserverID: string, ): string { if (!cookie) { return NOTIFICATIONS_OLM_DATA_CONTENT; } const cookieID = getCookieIDFromCookie(cookie); return `${NOTIFICATIONS_OLM_DATA_CONTENT}:${cookieID}`; } -function getOlmEncryptionKeyDBLabelForCookie(cookie: ?string): string { +function getOlmEncryptionKeyDBLabelForCookie( + cookie: ?string, + // eslint-disable-next-line no-unused-vars + keyserverID: string, +): string { if (!cookie) { return NOTIFICATIONS_OLM_DATA_ENCRYPTION_KEY; } const cookieID = getCookieIDFromCookie(cookie); return `${NOTIFICATIONS_OLM_DATA_ENCRYPTION_KEY}:${cookieID}`; } function getCookieIDFromOlmDBKey(olmDBKey: string): string | '0' { const cookieID = olmDBKey.split(':')[1]; return cookieID ?? '0'; } function sortOlmDBKeysArray( olmDBKeysArray: $ReadOnlyArray, ): $ReadOnlyArray { return olmDBKeysArray .map(key => ({ cookieID: Number(getCookieIDFromOlmDBKey(key)), key, })) .sort( ({ cookieID: cookieID1 }, { cookieID: cookieID2 }) => cookieID1 - cookieID2, ) .map(({ key }) => key); } export { decryptWebNotification, decryptDesktopNotification, getOlmDataContentKeyForCookie, getOlmEncryptionKeyDBLabelForCookie, }; diff --git a/web/push-notif/push-notifs-handler.js b/web/push-notif/push-notifs-handler.js index f9ec81169..fad1305c9 100644 --- a/web/push-notif/push-notifs-handler.js +++ b/web/push-notif/push-notifs-handler.js @@ -1,224 +1,231 @@ // @flow import * as React from 'react'; import { useSetDeviceTokenFanout, setDeviceTokenActionTypes, } from 'lib/actions/device-actions.js'; import { useModalContext } from 'lib/components/modal-provider.react.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { convertNonPendingIDToNewSchema } from 'lib/utils/migration-utils.js'; import { shouldSkipPushPermissionAlert, recordNotifPermissionAlertActionType, } from 'lib/utils/push-alerts.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; import { decryptDesktopNotification } from './notif-crypto-utils.js'; import { authoritativeKeyserverID } from '../authoritative-keyserver.js'; import electron from '../electron.js'; import PushNotifModal from '../modals/push-notif-modal.react.js'; import { updateNavInfoActionType } from '../redux/action-types.js'; import { useSelector } from '../redux/redux-utils.js'; import { getOlmWasmPath } from '../shared-worker/utils/constants.js'; import { useStaffCanSee } from '../utils/staff-utils.js'; function useCreateDesktopPushSubscription() { const dispatchActionPromise = useDispatchActionPromise(); const callSetDeviceToken = useSetDeviceTokenFanout(); const staffCanSee = useStaffCanSee(); React.useEffect( () => electron?.onDeviceTokenRegistered?.((token: ?string) => { void dispatchActionPromise( setDeviceTokenActionTypes, callSetDeviceToken(token), ); }), [callSetDeviceToken, dispatchActionPromise], ); React.useEffect(() => { electron?.fetchDeviceToken?.(); }, []); React.useEffect( () => electron?.onEncryptedNotification?.( - async ({ encryptedPayload }: { encryptedPayload: string }) => { + async ({ + encryptedPayload, + keyserverID, + }: { + encryptedPayload: string, + keyserverID?: string, + }) => { const decryptedPayload = await decryptDesktopNotification( encryptedPayload, staffCanSee, + keyserverID, ); electron?.showDecryptedNotification(decryptedPayload); }, ), [staffCanSee], ); const dispatch = useDispatch(); React.useEffect( () => electron?.onNotificationClicked?.( ({ threadID }: { +threadID: string }) => { const convertedThreadID = convertNonPendingIDToNewSchema( threadID, authoritativeKeyserverID, ); const payload = { chatMode: 'view', activeChatThreadID: convertedThreadID, tab: 'chat', }; dispatch({ type: updateNavInfoActionType, payload }); }, ), [dispatch], ); } function useCreatePushSubscription(): () => Promise { const publicKey = useSelector(state => state.pushApiPublicKey); const dispatchActionPromise = useDispatchActionPromise(); const callSetDeviceToken = useSetDeviceTokenFanout(); const staffCanSee = useStaffCanSee(); return React.useCallback(async () => { if (!publicKey) { return; } const workerRegistration = await navigator.serviceWorker?.ready; if (!workerRegistration || !workerRegistration.pushManager) { return; } workerRegistration.active?.postMessage({ olmWasmPath: getOlmWasmPath(), staffCanSee, }); const subscription = await workerRegistration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: publicKey, }); void dispatchActionPromise( setDeviceTokenActionTypes, callSetDeviceToken(JSON.stringify(subscription)), ); }, [callSetDeviceToken, dispatchActionPromise, publicKey, staffCanSee]); } function PushNotificationsHandler(): React.Node { useCreateDesktopPushSubscription(); const createPushSubscription = useCreatePushSubscription(); const notifPermissionAlertInfo = useSelector( state => state.notifPermissionAlertInfo, ); const modalContext = useModalContext(); const loggedIn = useSelector(isLoggedIn); const dispatch = useDispatch(); const supported = 'Notification' in window && !electron; React.useEffect(() => { void (async () => { if (!navigator.serviceWorker || !supported) { return; } await navigator.serviceWorker.register('worker/notif', { scope: '/' }); if (Notification.permission === 'granted') { // Make sure the subscription is current if we have the permissions await createPushSubscription(); } else if ( Notification.permission === 'default' && loggedIn && !shouldSkipPushPermissionAlert(notifPermissionAlertInfo) ) { // Ask existing users that are already logged in for permission modalContext.pushModal(); dispatch({ type: recordNotifPermissionAlertActionType, payload: { time: Date.now() }, }); } })(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // Ask for permission on login const prevLoggedIn = React.useRef(loggedIn); React.useEffect(() => { if (!navigator.serviceWorker || !supported) { return; } if (!prevLoggedIn.current && loggedIn) { if (Notification.permission === 'granted') { void createPushSubscription(); } else if ( Notification.permission === 'default' && !shouldSkipPushPermissionAlert(notifPermissionAlertInfo) ) { modalContext.pushModal(); dispatch({ type: recordNotifPermissionAlertActionType, payload: { time: Date.now() }, }); } } prevLoggedIn.current = loggedIn; }, [ createPushSubscription, dispatch, loggedIn, modalContext, notifPermissionAlertInfo, prevLoggedIn, supported, ]); // Redirect to thread on notification click React.useEffect(() => { if (!navigator.serviceWorker || !supported) { return undefined; } const callback = (event: MessageEvent) => { if (typeof event.data !== 'object' || !event.data) { return; } if (event.data.targetThreadID) { const payload = { chatMode: 'view', activeChatThreadID: event.data.targetThreadID, tab: 'chat', }; dispatch({ type: updateNavInfoActionType, payload }); } }; navigator.serviceWorker.addEventListener('message', callback); return () => navigator.serviceWorker?.removeEventListener('message', callback); }, [dispatch, supported]); return null; } export { PushNotificationsHandler, useCreatePushSubscription };