|
1 | 1 | import { api, ServiceClassInternal, type IMediaCallService, Authorization } from '@rocket.chat/core-services'; |
2 | | -import type { IMediaCall, IUser, IRoom, IInternalMediaCallHistoryItem, CallHistoryItemState } from '@rocket.chat/core-typings'; |
| 2 | +import type { |
| 3 | + IMediaCall, |
| 4 | + IUser, |
| 5 | + IRoom, |
| 6 | + IInternalMediaCallHistoryItem, |
| 7 | + CallHistoryItemState, |
| 8 | + IExternalMediaCallHistoryItem, |
| 9 | +} from '@rocket.chat/core-typings'; |
3 | 10 | import { Logger } from '@rocket.chat/logger'; |
4 | 11 | import { callServer, type IMediaCallServerSettings } from '@rocket.chat/media-calls'; |
5 | 12 | import { isClientMediaSignal, type ClientMediaSignal, type ServerMediaSignal } from '@rocket.chat/media-signaling'; |
@@ -81,12 +88,52 @@ export class MediaCallService extends ServiceClassInternal implements IMediaCall |
81 | 88 | } |
82 | 89 |
|
83 | 90 | if (call.uids.length !== 2) { |
84 | | - return; |
| 91 | + return this.saveExternalCallToHistory(call); |
85 | 92 | } |
86 | 93 |
|
87 | 94 | return this.saveInternalCallToHistory(call); |
88 | 95 | } |
89 | 96 |
|
| 97 | + private async saveExternalCallToHistory(call: IMediaCall): Promise<void> { |
| 98 | + const callerIsInternal = call.caller.type === 'user'; |
| 99 | + const calleeIsInternal = call.callee.type === 'user'; |
| 100 | + |
| 101 | + if (callerIsInternal && calleeIsInternal) { |
| 102 | + logger.warn({ msg: 'Attempt to save an external call history with a call that is not external', callId: call._id }); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if (!callerIsInternal && !calleeIsInternal) { |
| 107 | + logger.warn({ msg: 'Attempt to save an external call history with an invalid call', callId: call._id }); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const state = this.getCallHistoryItemState(call); |
| 112 | + const duration = this.getCallDuration(call); |
| 113 | + const direction = callerIsInternal ? 'outbound' : 'inbound'; |
| 114 | + const uid = callerIsInternal ? call.caller.id : call.callee.id; |
| 115 | + const contact = callerIsInternal ? call.callee : call.caller; |
| 116 | + |
| 117 | + const contactExtension = contact.sipExtension || contact.id; |
| 118 | + |
| 119 | + const historyItem: InsertionModel<IExternalMediaCallHistoryItem> = { |
| 120 | + uid, |
| 121 | + ts: call.createdAt, |
| 122 | + callId: call._id, |
| 123 | + state, |
| 124 | + type: 'media-call', |
| 125 | + duration, |
| 126 | + endedAt: call.endedAt || new Date(), |
| 127 | + external: true, |
| 128 | + direction, |
| 129 | + contactExtension, |
| 130 | + }; |
| 131 | + |
| 132 | + await CallHistory.insertOne(historyItem).catch((error: unknown) => |
| 133 | + logger.error({ msg: 'Failed to insert item into Call History', error }), |
| 134 | + ); |
| 135 | + } |
| 136 | + |
90 | 137 | private async saveInternalCallToHistory(call: IMediaCall): Promise<void> { |
91 | 138 | if (call.caller.type !== 'user' || call.callee.type !== 'user') { |
92 | 139 | logger.warn({ msg: 'Attempt to save an internal call history with a call that is not internal', callId: call._id }); |
|
0 commit comments