Skip to content

Commit

Permalink
fix: prevent auto-dropping already accepted or rejected calls
Browse files Browse the repository at this point in the history
  • Loading branch information
myandrienko committed Dec 11, 2024
1 parent 064cdc8 commit c5fb1b9
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions packages/client/src/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,22 @@ export class Call {
}),
);

this.leaveCallHooks.add(
createSubscription(this.state.session$, (session) => {
if (!this.ringing) return;

const receiverId = this.clientStore.connectedUser?.id;
if (!receiverId) return;

const isAcceptedByMe = Boolean(session?.accepted_by[receiverId]);
const isRejectedByMe = Boolean(session?.rejected_by[receiverId]);

if (isAcceptedByMe || isRejectedByMe) {
this.cancelAutoDrop();
}
}),
);

this.leaveCallHooks.add(
// watch for auto drop cancellation
createSubscription(this.state.callingState$, (callingState) => {
Expand Down Expand Up @@ -2000,28 +2016,33 @@ export class Call {
* Applicable only for ringing calls.
*/
private scheduleAutoDrop = () => {
clearTimeout(this.dropTimeout);
this.leaveCallHooks.add(
createSubscription(this.state.settings$, (settings) => {
if (!settings) return;
// ignore if the call is not ringing
if (this.state.callingState !== CallingState.RINGING) return;
this.cancelAutoDrop();

const timeoutInMs = this.isCreatedByMe
? settings.ring.auto_cancel_timeout_ms
: settings.ring.incoming_call_timeout_ms;
const settings = this.state.settings;
if (!settings) return;
// ignore if the call is not ringing
if (this.state.callingState !== CallingState.RINGING) return;

// 0 means no auto-drop
if (timeoutInMs <= 0) return;
const timeoutInMs = this.isCreatedByMe
? settings.ring.auto_cancel_timeout_ms
: settings.ring.incoming_call_timeout_ms;

clearTimeout(this.dropTimeout);
this.dropTimeout = setTimeout(() => {
this.leave({ reject: true, reason: 'timeout' }).catch((err) => {
this.logger('error', 'Failed to drop call', err);
});
}, timeoutInMs);
}),
);
// 0 means no auto-drop
if (timeoutInMs <= 0) return;

this.dropTimeout = setTimeout(() => {
this.leave({ reject: true, reason: 'timeout' }).catch((err) => {
this.logger('error', 'Failed to drop call', err);
});
}, timeoutInMs);
};

/**
* Cancels a scheduled auto-drop timeout.
*/
private cancelAutoDrop = () => {
clearTimeout(this.dropTimeout);
this.dropTimeout = undefined;
};

/**
Expand Down

0 comments on commit c5fb1b9

Please sign in to comment.