Skip to content

Commit

Permalink
Add doc comments in notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodh committed Dec 11, 2024
1 parent 91ef68c commit 3edca1f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
34 changes: 22 additions & 12 deletions packages/frontend/src/system-integration/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import type { T } from '@deltachat/jsonrpc-client'

const log = getLogger('renderer/notifications')

/**
* Notification handling:
*
* - listens for incoming notifications
* - reflects notification settings
* - prepares notification data (DcNotification)
* - queues notifications if needed to avoid "mass" notifications
* - sends notifications to runtime (which invokes ipcBackend)
*/

export function initNotifications() {
BackendRemote.on('IncomingMsg', (accountId, { chatId, msgId }) => {
incomingMessageHandler(accountId, chatId, msgId)
})
BackendRemote.on('IncomingWebxdcNotify', (accountId, { msgId }) => {
incomingWebxdcEventHandler(accountId, msgId)
})
BackendRemote.on('IncomingMsgBunch', accountId => {
flushNotifications(accountId)
})
}

function isMuted(accountId: number, chatId: number) {
return BackendRemote.rpc.isChatMuted(accountId, chatId)
}
Expand Down Expand Up @@ -294,15 +316,3 @@ function getNotificationIcon(
return null
}
}

export function initNotifications() {
BackendRemote.on('IncomingMsg', (accountId, { chatId, msgId }) => {
incomingMessageHandler(accountId, chatId, msgId)
})
BackendRemote.on('IncomingWebxdcNotify', (accountId, { msgId }) => {
incomingWebxdcEventHandler(accountId, msgId)
})
BackendRemote.on('IncomingMsgBunch', accountId => {
flushNotifications(accountId)
})
}
43 changes: 30 additions & 13 deletions packages/target-electron/src/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,34 @@ import { getLogger } from '../../shared/logger.js'

import type { NativeImage, IpcMainInvokeEvent } from 'electron'

/**
* Notification related functions to:
* - show notifications in operating system
* - handle click on notification
*
* is triggered from renderer process (!)
* by handling events (ipcMain.handle)
*
* see: frontend/src/system-integration/notifications.ts
*/

const log = getLogger('main/notifications')

const isMac = platform() === 'darwin'

if (Notification.isSupported()) {
ipcMain.handle('notifications.show', showNotification)
ipcMain.handle('notifications.clear', clearNotificationsForChat)
ipcMain.handle('notifications.clearAll', clearAll)
process.on('beforeExit', clearAll)
} else {
// Register no-op handlers for notifications to silently fail when
// no notifications are supported
ipcMain.handle('notifications.show', () => {})
ipcMain.handle('notifications.clear', () => {})
ipcMain.handle('notifications.clearAll', () => {})
}

function createNotification(data: DcNotification): Notification {
let icon: NativeImage | undefined = data.icon
? data.icon.indexOf('base64') > -1
Expand Down Expand Up @@ -69,6 +93,12 @@ function onClickNotification(

const notifications: { [chatId: number]: Notification[] } = {}

/**
* triggers creation of a notification, adds appropriate
* callbacks and shows it via electron Notification API
*
* @param data is passed from renderer process
*/
function showNotification(_event: IpcMainInvokeEvent, data: DcNotification) {
const chatId = data.chatId

Expand Down Expand Up @@ -135,19 +165,6 @@ function clearAll() {
}
}

if (Notification.isSupported()) {
ipcMain.handle('notifications.show', showNotification)
ipcMain.handle('notifications.clear', clearNotificationsForChat)
ipcMain.handle('notifications.clearAll', clearAll)
process.on('beforeExit', clearAll)
} else {
// Register no-op handlers for notifications to silently fail when
// no notifications are supported
ipcMain.handle('notifications.show', () => {})
ipcMain.handle('notifications.clear', () => {})
ipcMain.handle('notifications.clearAll', () => {})
}

// Thanks to Signal for this function
// https://github.com/signalapp/Signal-Desktop/blob/ae9181a4b26264ce553c7d8379a3ee5a07de018b/ts/services/notifications.ts#L485
// it is licensed AGPL-3.0-only
Expand Down

0 comments on commit 3edca1f

Please sign in to comment.