Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions apps/meteor/server/services/media-call/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { api, ServiceClassInternal, type IMediaCallService, Authorization } from '@rocket.chat/core-services';
import type { IMediaCall, IUser, IRoom, IInternalMediaCallHistoryItem, CallHistoryItemState } from '@rocket.chat/core-typings';
import type {
IMediaCall,
IUser,
IRoom,
IInternalMediaCallHistoryItem,
CallHistoryItemState,
IExternalMediaCallHistoryItem,
} from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
import { callServer, type IMediaCallServerSettings } from '@rocket.chat/media-calls';
import { isClientMediaSignal, type ClientMediaSignal, type ServerMediaSignal } from '@rocket.chat/media-signaling';
Expand Down Expand Up @@ -81,12 +88,52 @@ export class MediaCallService extends ServiceClassInternal implements IMediaCall
}

if (call.uids.length !== 2) {
return;
return this.saveExternalCallToHistory(call);
}

return this.saveInternalCallToHistory(call);
}

private async saveExternalCallToHistory(call: IMediaCall): Promise<void> {
const callerIsInternal = call.caller.type === 'user';
const calleeIsInternal = call.callee.type === 'user';

if (callerIsInternal && calleeIsInternal) {
logger.warn({ msg: 'Attempt to save an external call history with a call that is not external', callId: call._id });
return;
}

if (!callerIsInternal && !calleeIsInternal) {
logger.warn({ msg: 'Attempt to save an external call history with an invalid call', callId: call._id });
return;
}

const state = this.getCallHistoryItemState(call);
const duration = this.getCallDuration(call);
const direction = callerIsInternal ? 'outbound' : 'inbound';
const uid = callerIsInternal ? call.caller.id : call.callee.id;
const contact = callerIsInternal ? call.callee : call.caller;

const contactExtension = contact.sipExtension || contact.id;

const historyItem: InsertionModel<IExternalMediaCallHistoryItem> = {
uid,
ts: call.createdAt,
callId: call._id,
state,
type: 'media-call',
duration,
endedAt: call.endedAt || new Date(),
external: true,
direction,
contactExtension,
};

await CallHistory.insertOne(historyItem).catch((error: unknown) =>
logger.error({ msg: 'Failed to insert item into Call History', error }),
);
}

private async saveInternalCallToHistory(call: IMediaCall): Promise<void> {
if (call.caller.type !== 'user' || call.callee.type !== 'user') {
logger.warn({ msg: 'Attempt to save an internal call history with a call that is not internal', callId: call._id });
Expand Down
9 changes: 6 additions & 3 deletions packages/core-typings/src/ICallHistoryItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export interface IInternalMediaCallHistoryItem extends IMediaCallHistoryItem {
messageId?: IMessage['_id']; // Id of the message that was sent after the call ended
}

// TODO: IExternalMediaCallHistoryItem, planned for 8.0
// TODO: IVideoConfHistoryItem, expected in the future but not yet on the roadmap
export interface IExternalMediaCallHistoryItem extends IMediaCallHistoryItem {
external: true;

export type CallHistoryItem = IInternalMediaCallHistoryItem;
contactExtension: string;
}

export type CallHistoryItem = IInternalMediaCallHistoryItem | IExternalMediaCallHistoryItem;
Loading