| 
									
										
										
										
											2023-01-03 11:55:46 -08:00
										 |  |  | // Copyright 2020 Signal Messenger, LLC
 | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-26 14:15:33 -05:00
										 |  |  | import type { LocalizerType } from '../types/Util'; | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  | import { CallMode } from '../types/Calling'; | 
					
						
							|  |  |  | import { missingCaseError } from './missingCaseError'; | 
					
						
							| 
									
										
										
										
											2021-09-17 14:27:53 -04:00
										 |  |  | import * as log from '../logging/log'; | 
					
						
							| 
									
										
										
										
											2023-01-12 15:29:07 -08:00
										 |  |  | import type { ConversationType } from '../state/ducks/conversations'; | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 12:07:05 -06:00
										 |  |  | type DirectCallNotificationType = { | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   callMode: CallMode.Direct; | 
					
						
							| 
									
										
										
										
											2022-02-11 12:21:45 -06:00
										 |  |  |   activeCallConversationId?: string; | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   wasIncoming: boolean; | 
					
						
							|  |  |  |   wasVideoCall: boolean; | 
					
						
							|  |  |  |   wasDeclined: boolean; | 
					
						
							|  |  |  |   acceptedTime?: number; | 
					
						
							| 
									
										
										
										
											2023-01-09 16:52:01 -08:00
										 |  |  |   endedTime?: number; | 
					
						
							| 
									
										
										
										
											2021-01-14 12:07:05 -06:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-14 12:07:05 -06:00
										 |  |  | type GroupCallNotificationType = { | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   activeCallConversationId?: string; | 
					
						
							|  |  |  |   callMode: CallMode.Group; | 
					
						
							|  |  |  |   conversationId: string; | 
					
						
							| 
									
										
										
										
											2023-01-12 15:29:07 -08:00
										 |  |  |   creator?: ConversationType; | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   ended: boolean; | 
					
						
							|  |  |  |   deviceCount: number; | 
					
						
							|  |  |  |   maxDevices: number; | 
					
						
							|  |  |  |   startedTime: number; | 
					
						
							| 
									
										
										
										
											2021-01-14 12:07:05 -06:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type CallingNotificationType = | 
					
						
							|  |  |  |   | DirectCallNotificationType | 
					
						
							|  |  |  |   | GroupCallNotificationType; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getDirectCallNotificationText( | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     wasIncoming, | 
					
						
							|  |  |  |     wasVideoCall, | 
					
						
							|  |  |  |     wasDeclined, | 
					
						
							|  |  |  |     acceptedTime, | 
					
						
							|  |  |  |   }: DirectCallNotificationType, | 
					
						
							|  |  |  |   i18n: LocalizerType | 
					
						
							|  |  |  | ): string { | 
					
						
							|  |  |  |   const wasAccepted = Boolean(acceptedTime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (wasIncoming) { | 
					
						
							|  |  |  |     if (wasDeclined) { | 
					
						
							|  |  |  |       if (wasVideoCall) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |         return i18n('icu:declinedIncomingVideoCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |       return i18n('icu:declinedIncomingAudioCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (wasAccepted) { | 
					
						
							|  |  |  |       if (wasVideoCall) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |         return i18n('icu:acceptedIncomingVideoCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |       return i18n('icu:acceptedIncomingAudioCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (wasVideoCall) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |       return i18n('icu:missedIncomingVideoCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |     return i18n('icu:missedIncomingAudioCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (wasAccepted) { | 
					
						
							|  |  |  |     if (wasVideoCall) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |       return i18n('icu:acceptedOutgoingVideoCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |     return i18n('icu:acceptedOutgoingAudioCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (wasVideoCall) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |     return i18n('icu:missedOrDeclinedOutgoingVideoCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |   return i18n('icu:missedOrDeclinedOutgoingAudioCall'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getGroupCallNotificationText( | 
					
						
							|  |  |  |   notification: GroupCallNotificationType, | 
					
						
							|  |  |  |   i18n: LocalizerType | 
					
						
							|  |  |  | ): string { | 
					
						
							|  |  |  |   if (notification.ended) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |     return i18n('icu:calling__call-notification__ended'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (!notification.creator) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |     return i18n('icu:calling__call-notification__started-by-someone'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   } | 
					
						
							|  |  |  |   if (notification.creator.isMe) { | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |     return i18n('icu:calling__call-notification__started-by-you'); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-03-29 17:03:25 -07:00
										 |  |  |   return i18n('icu:calling__call-notification__started', { | 
					
						
							| 
									
										
										
										
											2023-03-27 16:37:39 -07:00
										 |  |  |     name: notification.creator.systemGivenName ?? notification.creator.title, | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function getCallingNotificationText( | 
					
						
							|  |  |  |   notification: CallingNotificationType, | 
					
						
							|  |  |  |   i18n: LocalizerType | 
					
						
							|  |  |  | ): string { | 
					
						
							|  |  |  |   switch (notification.callMode) { | 
					
						
							|  |  |  |     case CallMode.Direct: | 
					
						
							|  |  |  |       return getDirectCallNotificationText(notification, i18n); | 
					
						
							|  |  |  |     case CallMode.Group: | 
					
						
							|  |  |  |       return getGroupCallNotificationText(notification, i18n); | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-09-17 14:27:53 -04:00
										 |  |  |       log.error( | 
					
						
							| 
									
										
										
										
											2021-05-19 15:00:57 -07:00
										 |  |  |         `getCallingNotificationText: missing case ${missingCaseError( | 
					
						
							|  |  |  |           notification | 
					
						
							|  |  |  |         )}`
 | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2020-12-07 14:43:19 -06:00
										 |  |  |       return ''; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-08-26 16:51:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | type CallingIconType = | 
					
						
							|  |  |  |   | 'audio-incoming' | 
					
						
							|  |  |  |   | 'audio-missed' | 
					
						
							|  |  |  |   | 'audio-outgoing' | 
					
						
							|  |  |  |   | 'phone' | 
					
						
							|  |  |  |   | 'video' | 
					
						
							|  |  |  |   | 'video-incoming' | 
					
						
							|  |  |  |   | 'video-missed' | 
					
						
							|  |  |  |   | 'video-outgoing'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getDirectCallingIcon({ | 
					
						
							|  |  |  |   wasIncoming, | 
					
						
							|  |  |  |   wasVideoCall, | 
					
						
							|  |  |  |   acceptedTime, | 
					
						
							|  |  |  | }: DirectCallNotificationType): CallingIconType { | 
					
						
							|  |  |  |   const wasAccepted = Boolean(acceptedTime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // video
 | 
					
						
							|  |  |  |   if (wasVideoCall) { | 
					
						
							|  |  |  |     if (wasAccepted) { | 
					
						
							|  |  |  |       return wasIncoming ? 'video-incoming' : 'video-outgoing'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 'video-missed'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (wasAccepted) { | 
					
						
							|  |  |  |     return wasIncoming ? 'audio-incoming' : 'audio-outgoing'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 'audio-missed'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function getCallingIcon( | 
					
						
							|  |  |  |   notification: CallingNotificationType | 
					
						
							|  |  |  | ): CallingIconType { | 
					
						
							|  |  |  |   switch (notification.callMode) { | 
					
						
							|  |  |  |     case CallMode.Direct: | 
					
						
							|  |  |  |       return getDirectCallingIcon(notification); | 
					
						
							|  |  |  |     case CallMode.Group: | 
					
						
							|  |  |  |       return 'video'; | 
					
						
							|  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-09-17 14:27:53 -04:00
										 |  |  |       log.error( | 
					
						
							| 
									
										
										
										
											2021-08-26 16:51:55 -04:00
										 |  |  |         `getCallingNotificationText: missing case ${missingCaseError( | 
					
						
							|  |  |  |           notification | 
					
						
							|  |  |  |         )}`
 | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       return 'phone'; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |