Files
ThreadNet-Web/apps/web/src/components/structures/LegacyCallEventGrouper.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

209 lines
6.9 KiB
TypeScript
Raw Normal View History

2021-06-01 09:30:37 +02:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2021-06-01 09:30:37 +02:00
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2021-06-01 09:30:37 +02:00
*/
2025-02-05 13:25:06 +00:00
import { EventType, type MatrixEvent } from "matrix-js-sdk/src/matrix";
import { CallEvent, CallState, CallType, type MatrixCall } from "matrix-js-sdk/src/webrtc/call";
2021-06-01 09:30:37 +02:00
import { EventEmitter } from "events";
2021-10-22 17:23:32 -05:00
import LegacyCallHandler, { LegacyCallHandlerEvent } from "../../LegacyCallHandler";
2021-06-01 11:28:45 +02:00
import { MatrixClientPeg } from "../../MatrixClientPeg";
2021-06-01 09:30:37 +02:00
export enum LegacyCallEventGrouperEvent {
2021-06-01 09:30:37 +02:00
StateChanged = "state_changed",
2021-06-19 20:02:51 +02:00
SilencedChanged = "silenced_changed",
2021-08-27 16:23:06 +02:00
LengthChanged = "length_changed",
2021-06-01 09:30:37 +02:00
}
2021-08-06 12:42:38 +02:00
const CONNECTING_STATES = [
CallState.Connecting,
CallState.WaitLocalMedia,
CallState.CreateOffer,
CallState.CreateAnswer,
];
const SUPPORTED_STATES = [CallState.Connected, CallState.Ringing, CallState.Ended];
2021-06-01 10:33:44 +02:00
const isCallEventType = (eventType: string): boolean =>
eventType.startsWith("m.call.") || eventType.startsWith("org.matrix.call.");
export const isCallEvent = (event: MatrixEvent): boolean => isCallEventType(event.getType());
export function buildLegacyCallEventGroupers(
callEventGroupers: Map<string, LegacyCallEventGrouper>,
events?: MatrixEvent[],
): Map<string, LegacyCallEventGrouper> {
const newCallEventGroupers = new Map();
events?.forEach((ev) => {
if (!isCallEvent(ev)) {
return;
}
const callId = ev.getContent().call_id;
if (!newCallEventGroupers.has(callId)) {
if (callEventGroupers.has(callId)) {
// reuse the LegacyCallEventGrouper object where possible
newCallEventGroupers.set(callId, callEventGroupers.get(callId));
} else {
newCallEventGroupers.set(callId, new LegacyCallEventGrouper());
}
}
newCallEventGroupers.get(callId).add(ev);
});
return newCallEventGroupers;
}
export default class LegacyCallEventGrouper extends EventEmitter {
2021-06-02 10:18:32 +02:00
private events: Set<MatrixEvent> = new Set<MatrixEvent>();
private call: MatrixCall | null = null;
public state?: CallState;
2021-06-01 09:30:37 +02:00
public constructor() {
2021-06-01 15:31:46 +02:00
super();
LegacyCallHandler.instance.addListener(LegacyCallHandlerEvent.CallsChanged, this.setCall);
LegacyCallHandler.instance.addListener(
LegacyCallHandlerEvent.SilencedCallsChanged,
this.onSilencedCallsChanged,
);
2021-06-01 15:31:46 +02:00
}
private get invite(): MatrixEvent | undefined {
2021-06-01 16:28:57 +02:00
return [...this.events].find((event) => event.getType() === EventType.CallInvite);
2021-06-01 10:42:21 +02:00
}
private get hangup(): MatrixEvent | undefined {
2021-06-02 10:18:32 +02:00
return [...this.events].find((event) => event.getType() === EventType.CallHangup);
}
private get reject(): MatrixEvent | undefined {
2021-06-02 10:30:17 +02:00
return [...this.events].find((event) => event.getType() === EventType.CallReject);
}
private get selectAnswer(): MatrixEvent | undefined {
2021-08-06 13:59:26 +02:00
return [...this.events].find((event) => event.getType() === EventType.CallSelectAnswer);
}
public get isVoice(): boolean | undefined {
2021-06-02 10:18:32 +02:00
const invite = this.invite;
if (!invite) return undefined;
2021-06-02 10:18:32 +02:00
// FIXME: Find a better way to determine this from the event?
if (invite.getContent()?.offer?.sdp?.indexOf("m=video") !== -1) return false;
return true;
}
public get hangupReason(): string | null {
return this.call?.hangupReason ?? this.hangup?.getContent()?.reason ?? null;
2021-06-02 10:18:32 +02:00
}
public get rejectParty(): string | undefined {
2021-07-25 10:28:27 +02:00
return this.reject?.getSender();
}
public get gotRejected(): boolean {
return Boolean(this.reject);
}
2022-11-29 16:21:51 -05:00
public get duration(): number | null {
if (!this.hangup?.getDate() || !this.selectAnswer?.getDate()) return null;
return this.hangup.getDate()!.getTime() - this.selectAnswer.getDate()!.getTime();
2021-08-06 13:59:26 +02:00
}
2021-06-02 10:18:32 +02:00
/**
* Returns true if there are only events from the other side - we missed the call
*/
public get callWasMissed(): boolean {
return (
this.state === CallState.Ended &&
![...this.events].some((event) => event.sender?.userId === MatrixClientPeg.safeGet().getUserId())
);
2021-06-02 10:18:32 +02:00
}
2021-09-02 17:52:00 +02:00
private get callId(): string | undefined {
return [...this.events][0]?.getContent()?.call_id;
2021-06-19 20:02:51 +02:00
}
2021-09-02 17:52:00 +02:00
private get roomId(): string | undefined {
return [...this.events][0]?.getRoomId();
2021-06-19 20:02:51 +02:00
}
private onSilencedCallsChanged = (): void => {
const newState = LegacyCallHandler.instance.isCallSilenced(this.callId);
this.emit(LegacyCallEventGrouperEvent.SilencedChanged, newState);
2021-07-02 13:20:02 +02:00
};
2021-06-19 20:02:51 +02:00
2021-08-27 16:23:06 +02:00
private onLengthChanged = (length: number): void => {
this.emit(LegacyCallEventGrouperEvent.LengthChanged, length);
2021-08-27 16:23:06 +02:00
};
2021-11-30 19:09:13 +01:00
public answerCall = (): void => {
const roomId = this.roomId;
if (!roomId) return;
LegacyCallHandler.instance.answerCall(roomId);
2021-07-02 13:20:02 +02:00
};
2021-06-01 09:30:37 +02:00
2021-11-30 19:09:13 +01:00
public rejectCall = (): void => {
const roomId = this.roomId;
if (!roomId) return;
LegacyCallHandler.instance.hangupOrReject(roomId, true);
2021-07-02 13:20:02 +02:00
};
2021-06-01 09:30:37 +02:00
2021-11-30 19:09:13 +01:00
public callBack = (): void => {
const roomId = this.roomId;
if (!roomId) return;
LegacyCallHandler.instance.placeCall(roomId, this.isVoice ? CallType.Voice : CallType.Video);
2021-07-02 13:20:02 +02:00
};
2021-06-01 09:30:37 +02:00
public toggleSilenced = (): void => {
const silenced = LegacyCallHandler.instance.isCallSilenced(this.callId);
2024-10-16 16:43:07 +01:00
if (silenced) {
LegacyCallHandler.instance.unSilenceCall(this.callId);
} else {
LegacyCallHandler.instance.silenceCall(this.callId);
}
2021-07-02 13:20:02 +02:00
};
2021-06-01 13:40:25 +02:00
private setCallListeners(): void {
2021-06-01 10:55:03 +02:00
if (!this.call) return;
2021-06-01 15:31:46 +02:00
this.call.addListener(CallEvent.State, this.setState);
2021-08-27 16:23:06 +02:00
this.call.addListener(CallEvent.LengthChanged, this.onLengthChanged);
2021-06-01 09:30:37 +02:00
}
private setState = (): void => {
if (this.call && CONNECTING_STATES.includes(this.call.state)) {
2021-08-06 12:42:38 +02:00
this.state = CallState.Connecting;
} else if (this.call && SUPPORTED_STATES.includes(this.call.state)) {
2021-06-01 10:33:44 +02:00
this.state = this.call.state;
2021-06-01 10:55:03 +02:00
} else {
if (this.reject) {
this.state = CallState.Ended;
} else if (this.hangup) {
this.state = CallState.Ended;
} else if (this.invite && this.call) {
this.state = CallState.Connecting;
}
2021-06-01 10:33:44 +02:00
}
this.emit(LegacyCallEventGrouperEvent.StateChanged, this.state);
2021-07-02 13:20:02 +02:00
};
2021-06-01 09:30:37 +02:00
private setCall = (): void => {
const callId = this.callId;
if (!callId || this.call) return;
2021-06-02 10:18:32 +02:00
this.call = LegacyCallHandler.instance.getCallById(callId);
2021-06-02 10:18:32 +02:00
this.setCallListeners();
2021-06-01 15:31:46 +02:00
this.setState();
2021-07-02 13:20:02 +02:00
};
2021-06-01 15:31:46 +02:00
public add(event: MatrixEvent): void {
if (this.events.has(event)) return; // nothing to do
2021-06-01 16:28:57 +02:00
this.events.add(event);
2021-06-02 10:18:32 +02:00
this.setCall();
2021-06-01 09:30:37 +02:00
}
}