| | const { promisify } = require( 'util' ); |
| | const { ipcMain: ipc, Notification } = require( 'electron' ); |
| | const { debounce } = require( 'lodash' ); |
| | const Config = require( '../../lib/config' ); |
| | const isCalypso = require( '../../lib/is-calypso' ); |
| | const log = require( '../../lib/logger' )( 'desktop:notifications' ); |
| | const ViewModel = require( '../../lib/notifications/viewmodel' ); |
| | const Platform = require( '../../lib/platform' ); |
| | const Settings = require( '../../lib/settings' ); |
| |
|
| | const webBase = Config.baseURL(); |
| |
|
| | const delay = promisify( setTimeout ); |
| |
|
| | let notificationBadgeCount = 0; |
| |
|
| | function updateNotificationBadge() { |
| | const badgeEnabled = Settings.getSetting( 'notification-badge' ); |
| | if ( ! badgeEnabled ) { |
| | return; |
| | } |
| |
|
| | const bounceEnabled = Settings.getSetting( 'notification-bounce' ); |
| |
|
| | if ( notificationBadgeCount > 0 ) { |
| | Platform.showNotificationsBadge( notificationBadgeCount, bounceEnabled ); |
| | } else { |
| | Platform.clearNotificationsBadge(); |
| | } |
| | } |
| |
|
| | module.exports = function ( { window, view } ) { |
| | ipc.on( 'clear-notices-count', function () { |
| | if ( notificationBadgeCount > 0 ) { |
| | log.info( 'Notification badge count reset' ); |
| | notificationBadgeCount = 0; |
| | updateNotificationBadge(); |
| | } |
| | } ); |
| |
|
| | ipc.on( 'preferences-changed-notification-badge', function ( _, enabled ) { |
| | if ( ! enabled ) { |
| | notificationBadgeCount = 0; |
| | updateNotificationBadge(); |
| | } |
| | } ); |
| |
|
| | |
| | |
| | |
| | ViewModel.on( |
| | 'notification', |
| | debounce( |
| | () => { |
| | view.webContents.send( 'notifications-panel-refresh' ); |
| | }, |
| | 100, |
| | { leading: true, trailing: false } |
| | ) |
| | ); |
| |
|
| | ViewModel.on( 'notification', function ( notification ) { |
| | log.info( 'Received notification: ', notification ); |
| |
|
| | if ( ! Settings.getSetting( 'notifications' ) ) { |
| | log.info( 'Notifications disabled!' ); |
| |
|
| | return; |
| | } |
| |
|
| | notificationBadgeCount++; |
| | updateNotificationBadge(); |
| |
|
| | const { id, type, title, subtitle, body, navigate } = notification; |
| |
|
| | log.info( `Received ${ type } notification for site ${ title }` ); |
| |
|
| | if ( Notification.isSupported() ) { |
| | const desktopNotification = new Notification( { |
| | title: title, |
| | subtitle: subtitle, |
| | body: body, |
| | silent: true, |
| | hasReply: false, |
| | } ); |
| |
|
| | desktopNotification.on( 'click', async function () { |
| | ViewModel.didClickNotification( id, () => |
| | |
| | |
| | window.webContents.send( 'notifications-panel-refresh' ) |
| | ); |
| |
|
| | if ( ! window.isVisible() ) { |
| | window.show(); |
| | await delay( 300 ); |
| | } |
| |
|
| | window.focus(); |
| |
|
| | if ( navigate ) { |
| | |
| | log.info( `Navigating user to URL: ${ navigate }` ); |
| |
|
| | if ( navigate.startsWith( 'http' ) ) { |
| | view.webContents.loadURL( navigate ); |
| | } else if ( isCalypso( view ) ) { |
| | log.info( `Navigating to '${ navigate }'` ); |
| | view.webContents.send( 'navigate', navigate ); |
| | } else { |
| | const path = stripLeadingSlashFromPath( navigate ); |
| | log.info( `Navigating to '${ webBase + path }'` ); |
| | view.webContents.loadURL( webBase + path ); |
| | } |
| | } else { |
| | |
| | if ( isCalypso( view ) ) { |
| | view.webContents.send( 'navigate', '/' ); |
| | } else { |
| | view.webContents.loadURL( webBase ); |
| | } |
| |
|
| | await delay( 300 ); |
| |
|
| | view.webContents.send( 'notifications-panel-show', true ); |
| | } |
| |
|
| | |
| | view.webContents.send( 'notification-clicked', notification ); |
| |
|
| | notificationBadgeCount = Math.max( notificationBadgeCount - 1, 0 ); |
| | updateNotificationBadge(); |
| | } ); |
| |
|
| | desktopNotification.show(); |
| | } |
| | } ); |
| | }; |
| |
|
| | function stripLeadingSlashFromPath( path ) { |
| | return path.replace( /^\/+/g, '' ); |
| | } |
| |
|