diff --git a/preload.js b/preload.js index c8cb16df72..30fad8331b 100644 --- a/preload.js +++ b/preload.js @@ -200,6 +200,7 @@ window.Signal.Migrations.Migrations0DatabaseWithAttachmentData = require('./js/m window.Signal.Migrations.Migrations1DatabaseWithoutAttachmentData = require('./js/modules/migrations/migrations_1_database_without_attachment_data'); window.Signal.Migrations.upgradeMessageSchema = upgradeMessageSchema; +window.Signal.Notifications = require('./ts/notifications'); window.Signal.OS = require('./js/modules/os'); window.Signal.Settings = require('./js/modules/settings'); window.Signal.Startup = require('./js/modules/startup'); diff --git a/ts/notifications/getStatus.ts b/ts/notifications/getStatus.ts new file mode 100644 index 0000000000..f23df14852 --- /dev/null +++ b/ts/notifications/getStatus.ts @@ -0,0 +1,60 @@ +interface Environment { + isAppFocused: boolean; + isAudioNotificationEnabled: boolean; + isAudioNotificationSupported: boolean; + isEnabled: boolean; + numNotifications: number; + userSetting: UserSetting; +} + +interface Status { + shouldClearNotifications: boolean; + shouldPlayNotificationSound: boolean; + shouldShowNotifications: boolean; + type: Type; +} + +type UserSetting = 'off' | 'count' | 'name' | 'message'; + +type Type = + | 'ok' + | 'disabled' + | 'appIsFocused' + | 'noNotifications' + | 'userSetting'; + +export const getStatus = (environment: Environment): Status => { + const type = ((): Type => { + if (!environment.isEnabled) { + return 'disabled'; + } + + const hasNotifications = environment.numNotifications > 0; + if (!hasNotifications) { + return 'noNotifications'; + } + + if (environment.isAppFocused) { + return 'appIsFocused'; + } + + if (environment.userSetting === 'off') { + return 'userSetting'; + } + + return 'ok'; + })(); + + const shouldPlayNotificationSound = + environment.isAudioNotificationSupported && + environment.isAudioNotificationEnabled; + const shouldShowNotifications = type === 'ok'; + const shouldClearNotifications = type === 'appIsFocused'; + + return { + shouldClearNotifications, + shouldPlayNotificationSound, + shouldShowNotifications, + type, + }; +}; diff --git a/ts/notifications/index.ts b/ts/notifications/index.ts new file mode 100644 index 0000000000..30cb2f1e9b --- /dev/null +++ b/ts/notifications/index.ts @@ -0,0 +1,3 @@ +import { getStatus } from './getStatus'; + +export { getStatus };