import * as mainWindow from './windows/main' ; import { app , Notification , NativeImage , nativeImage } from 'electron' ; import { appName } from '../shared/constants' ; import { appIcon } from './application-constants' ; import DeltaChatController from './deltachat/controller' ; import { ExtendedAppMainProcess } from './types' ; import { FullChat , MessageType } from '../shared/shared-types' ; import { C } from 'deltachat-node/dist/constants' ; import { getLogger } from '../shared/logger' ; import { platform } from 'os' ; const log = getLogger ( 'main/notifications' ) ; const isMac = platform ( ) === 'darwin' ; export default function ( dc , settings ) { if ( ! Notification . isSupported ( ) ) return ; async function isMuted ( chatId ) { return await dc . callMethod ( null , 'chatList.isChatMuted' , [ chatId ] ) ; } async function createNotification ( chatId , msgId ) { const tx = ( app as ExtendedAppMainProcess ) . translate ; if ( ! settings . showNotificationContent ) { return new Notification ( { title : appName , body : tx ( 'notify_new_message' ) , icon : appIcon ( ) , } ) ; } else { const [ chatInfo , message_json ] = await Promise . all ( [ dc . callMethod ( null , 'chatList.getFullChatById' , [ chatId ] ) , dc . callMethod ( null , 'messageList.messageIdToJson' , [ msgId ] ) , ] ) ; const summary = message_json . summary ; let icon ; if ( message_json ?. viewType === C . DC_MSG_IMAGE ) { icon = nativeImage . createFromPath ( message_json ?. file ) ; } else if ( chatInfo . profileImage ) { icon = nativeImage . createFromPath ( chatInfo . profileImage ) ; } if ( ! icon || icon . isEmpty ( ) ) { if ( ! isMac ) { icon = nativeImage . createFromPath ( appIcon ( ) ) ; } } const notificationOptions = { title : `${ chatInfo . name }` , body : summary . text1 ? `${ summary . text1 }: ${ summary . text2 }` : summary . text2 , icon , timeoutType : 'default' , } ; if ( process . platform === 'win32' ) { notificationOptions . closeButtonText = null ; } return new Notification ( notificationOptions ) ; } } let notifications = { } ; function clearNotificationsForChat ( chatId ) { if ( notifications [ chatId ] ) { for ( const notify of notifications [ chatId ] ) { notify . close ( ) ; } notifications [ chatId ] = null ; } } function clearAllNotifications ( ) { for ( const chatId of Object . keys ( notifications ) ) { if ( isNaN ( Number ( chatId ) ) ) { clearNotificationsForChat ( Number ( chatId ) ) ; } } notifications = { } ; } function addNotificationForChat ( chatId , notify ) { if ( notifications [ chatId ] ) { notifications [ chatId ] . push ( notify ) ; } else { notifications [ chatId ] = [ notify ] ; } } function onClickNotification ( chatId , msgId , _ev ) { dc . sendToRenderer ( 'ClickOnNotification' , { chatId , msgId } ) ; clearNotificationsForChat ( chatId ) ; mainWindow . show ( ) ; app . focus ( ) ; } dc . on ( 'DC_EVENT_INCOMING_MSG' , async ( accountId , chatId , msgId ) => { if ( settings . notifications && ( mainWindow . window . hidden || ! mainWindow . window . isVisible ( ) ) ) { if ( accountId !== dc . selectedAccountId ) { return ; } if ( await isMuted ( chatId ) ) { return ; } log . debug ( 'Creating notification for chat:' , chatId , 'with msgId:' , msgId ) ; const notify = await createNotification ( chatId , msgId ) ; notify . on ( 'click' , Event => { onClickNotification ( chatId , msgId , Event ) ; } ) ; addNotificationForChat ( chatId , notify ) ; notify . show ( ) ; } } ) ; dc . on ( 'DESKTOP_CLEAR_NOTIFICATIONS_FOR_CHAT' , chatId => { clearNotificationsForChat ( chatId ) ; } ) ; dc . on ( 'DESKTOP_CLEAR_ALL_NOTIFICATIONS' , _ => { clearAllNotifications ( ) ; } ) ; }