| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  | // Copyright 2022 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import type { CallLogEventSyncEvent } from '../textsecure/messageReceiverEvents'; | 
					
						
							|  |  |  | import * as log from '../logging/log'; | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  | import { DataWriter } from '../sql/Client'; | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  | import type { CallLogEventTarget } from '../types/CallDisposition'; | 
					
						
							| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  | import { CallLogEvent } from '../types/CallDisposition'; | 
					
						
							|  |  |  | import { missingCaseError } from './missingCaseError'; | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  | import { strictAssert } from './assert'; | 
					
						
							|  |  |  | import { updateDeletedMessages } from './callDisposition'; | 
					
						
							| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export async function onCallLogEventSync( | 
					
						
							|  |  |  |   syncEvent: CallLogEventSyncEvent | 
					
						
							|  |  |  | ): Promise<void> { | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |   const { data, confirm } = syncEvent; | 
					
						
							| 
									
										
										
										
											2024-10-17 13:34:06 -07:00
										 |  |  |   const { type, peerIdAsConversationId, peerIdAsRoomId, callId, timestamp } = | 
					
						
							|  |  |  |     data.callLogEventDetails; | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const target: CallLogEventTarget = { | 
					
						
							| 
									
										
										
										
											2024-10-17 13:34:06 -07:00
										 |  |  |     peerIdAsConversationId, | 
					
						
							|  |  |  |     peerIdAsRoomId, | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |     callId, | 
					
						
							|  |  |  |     timestamp, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   log.info( | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |     `onCallLogEventSync: Processing event (Event: ${type}, CallId: ${callId}, Timestamp: ${timestamp})` | 
					
						
							| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  |   ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |   if (type === CallLogEvent.Clear) { | 
					
						
							|  |  |  |     log.info('onCallLogEventSync: Clearing call history'); | 
					
						
							| 
									
										
										
										
											2023-09-27 12:42:30 -07:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  |       const messageIds = await DataWriter.clearCallHistory(target); | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |       updateDeletedMessages(messageIds); | 
					
						
							| 
									
										
										
										
											2023-09-27 12:42:30 -07:00
										 |  |  |     } finally { | 
					
						
							|  |  |  |       // We want to reset the call history even if the clear fails.
 | 
					
						
							|  |  |  |       window.reduxActions.callHistory.resetCallHistory(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  |     confirm(); | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |   } else if (type === CallLogEvent.MarkedAsRead) { | 
					
						
							|  |  |  |     log.info('onCallLogEventSync: Marking call history read'); | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2024-08-27 06:20:23 -07:00
										 |  |  |       const count = await DataWriter.markAllCallHistoryRead(target); | 
					
						
							|  |  |  |       log.info( | 
					
						
							|  |  |  |         `onCallLogEventSync: Marked ${count} call history messages read` | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |     } finally { | 
					
						
							|  |  |  |       window.reduxActions.callHistory.updateCallHistoryUnreadCount(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     confirm(); | 
					
						
							|  |  |  |   } else if (type === CallLogEvent.MarkedAsReadInConversation) { | 
					
						
							|  |  |  |     log.info('onCallLogEventSync: Marking call history read in conversation'); | 
					
						
							| 
									
										
										
										
											2024-03-11 11:18:55 -07:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2024-10-17 13:34:06 -07:00
										 |  |  |       strictAssert(peerIdAsConversationId, 'Missing peerIdAsConversationId'); | 
					
						
							|  |  |  |       strictAssert(peerIdAsRoomId, 'Missing peerIdAsRoomId'); | 
					
						
							| 
									
										
										
										
											2024-08-27 06:20:23 -07:00
										 |  |  |       const count = | 
					
						
							|  |  |  |         await DataWriter.markAllCallHistoryReadInConversation(target); | 
					
						
							|  |  |  |       log.info( | 
					
						
							|  |  |  |         `onCallLogEventSync: Marked ${count} call history messages read` | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2024-03-11 11:18:55 -07:00
										 |  |  |     } finally { | 
					
						
							|  |  |  |       window.reduxActions.callHistory.updateCallHistoryUnreadCount(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     confirm(); | 
					
						
							| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2024-06-25 17:58:38 -07:00
										 |  |  |     throw missingCaseError(type); | 
					
						
							| 
									
										
										
										
											2023-08-08 17:53:06 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } |