From 521b2445a8fe4c197a0655ec50bf08b0dd0e86dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 2 Jun 2021 10:18:32 +0200 Subject: [PATCH] Refactoring and fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/components/structures/CallEventGrouper.ts | 72 +++++++++---------- src/components/views/messages/CallEvent.tsx | 6 +- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/components/structures/CallEventGrouper.ts b/src/components/structures/CallEventGrouper.ts index 4d32d48fb3..8455eae0cd 100644 --- a/src/components/structures/CallEventGrouper.ts +++ b/src/components/structures/CallEventGrouper.ts @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ - import { EventType } from "matrix-js-sdk/src/@types/event"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { CallEvent, CallState, CallType, MatrixCall } from "matrix-js-sdk/src/webrtc/call"; @@ -38,9 +37,9 @@ export enum CustomCallState { } export default class CallEventGrouper extends EventEmitter { - events: Set = new Set(); - call: MatrixCall; - state: CallState | CustomCallState; + private events: Set = new Set(); + private call: MatrixCall; + public state: CallState | CustomCallState; constructor() { super(); @@ -52,6 +51,30 @@ export default class CallEventGrouper extends EventEmitter { return [...this.events].find((event) => event.getType() === EventType.CallInvite); } + private get hangup(): MatrixEvent { + return [...this.events].find((event) => event.getType() === EventType.CallHangup); + } + + public get isVoice(): boolean { + const invite = this.invite; + if (!invite) return; + + // 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.hangup?.getContent()?.reason; + } + + /** + * Returns true if there are only events from the other side - we missed the call + */ + private get callWasMissed(): boolean { + return ![...this.events].some((event) => event.sender?.userId === MatrixClientPeg.get().getUserId()); + } + public answerCall = () => { this.call?.answer(); } @@ -68,35 +91,6 @@ export default class CallEventGrouper extends EventEmitter { }); } - public isVoice(): boolean { - const invite = this.invite; - - // FIXME: Find a better way to determine this from the event? - let isVoice = true; - if ( - invite.getContent().offer && invite.getContent().offer.sdp && - invite.getContent().offer.sdp.indexOf('m=video') !== -1 - ) { - isVoice = false; - } - - return isVoice; - } - - public getState(): CallState | CustomCallState { - return this.state; - } - - public getHangupReason(): string | null { - return [...this.events].find((event) => event.getType() === EventType.CallHangup)?.getContent()?.reason; - } - - /** - * Returns true if there are only events from the other side - we missed the call - */ - private wasThisCallMissed(): boolean { - return ![...this.events].some((event) => event.sender?.userId === MatrixClientPeg.get().getUserId()); - } private setCallListeners() { if (!this.call) return; @@ -110,7 +104,7 @@ export default class CallEventGrouper extends EventEmitter { const lastEvent = [...this.events][this.events.size - 1]; const lastEventType = lastEvent.getType(); - if (this.wasThisCallMissed()) this.state = CustomCallState.Missed; + if (this.callWasMissed) this.state = CustomCallState.Missed; else if (lastEventType === EventType.CallHangup) this.state = CallState.Ended; else if (lastEventType === EventType.CallReject) this.state = CallState.Ended; else if (lastEventType === EventType.CallInvite && this.call) this.state = CallState.Connecting; @@ -119,16 +113,16 @@ export default class CallEventGrouper extends EventEmitter { } private setCall = () => { + if (this.call) return; + const callId = [...this.events][0].getContent().call_id; - if (!this.call) { - this.call = CallHandler.sharedInstance().getCallById(callId); - this.setCallListeners(); - } + this.call = CallHandler.sharedInstance().getCallById(callId); + this.setCallListeners(); this.setState(); } public add(event: MatrixEvent) { this.events.add(event); - this.setState(); + this.setCall(); } } diff --git a/src/components/views/messages/CallEvent.tsx b/src/components/views/messages/CallEvent.tsx index a4c0d02797..597c2feba8 100644 --- a/src/components/views/messages/CallEvent.tsx +++ b/src/components/views/messages/CallEvent.tsx @@ -43,7 +43,7 @@ export default class CallEvent extends React.Component { super(props); this.state = { - callState: this.props.callEventGrouper.getState(), + callState: this.props.callEventGrouper.state, } } @@ -77,7 +77,7 @@ export default class CallEvent extends React.Component { ); } if (state === CallState.Ended) { - const hangupReason = this.props.callEventGrouper.getHangupReason(); + const hangupReason = this.props.callEventGrouper.hangupReason; if (["user_hangup", "user hangup"].includes(hangupReason) || !hangupReason) { // workaround for https://github.com/vector-im/element-web/issues/5178 @@ -157,7 +157,7 @@ export default class CallEvent extends React.Component { render() { const event = this.props.mxEvent; const sender = event.sender ? event.sender.name : event.getSender(); - const callType = this.props.callEventGrouper.isVoice() ? _t("Voice call") : _t("Video call"); + const callType = this.props.callEventGrouper.isVoice ? _t("Voice call") : _t("Video call"); const content = this.renderContent(this.state.callState); return (