From 2738451ff62f00f5a88cb52276d644732dca0a37 Mon Sep 17 00:00:00 2001 From: Yash-Garg Date: Tue, 22 Apr 2025 14:49:34 +0530 Subject: [PATCH] fix: handling of existing calls in `onCallInvite` --- lib/src/voip/voip.dart | 2 +- test/calls_test.dart | 52 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/src/voip/voip.dart b/lib/src/voip/voip.dart index ab073804..d4d1a747 100644 --- a/lib/src/voip/voip.dart +++ b/lib/src/voip/voip.dart @@ -384,7 +384,7 @@ class VoIP { '[glare] got new call ${content.tryGet('call_id')} and currently room id is mapped to ${incomingCallRoomId.tryGet(room.id)}', ); - if (call != null && call.state == CallState.kEnded) { + if (call != null && call.state != CallState.kEnded) { // Session already exist. Logs().v('[VOIP] onCallInvite: Session [$callId] already exist.'); return; diff --git a/test/calls_test.dart b/test/calls_test.dart index 14f76dc0..b19688a9 100644 --- a/test/calls_test.dart +++ b/test/calls_test.dart @@ -932,5 +932,57 @@ void main() { expect(room.groupCallParticipantCount('participants_count'), 2); expect(room.hasActiveGroupCall, true); }); + + test('call persists after sending invite', () async { + CallSession? incomingCall; + + // incoming call should not be created yet + incomingCall = voip.calls[voip.currentCID]; + expect(incomingCall, isNull); + expect(incomingCall?.pc, isNull); + + // send invite for the call + final outgoingCall = await voip.inviteToCall( + room, + CallType.kVoice, + userId: '@alice:testing.com', + ); + + // acknowledge the invite + await matrix.handleSync( + SyncUpdate( + nextBatch: 'something', + rooms: RoomsUpdate( + join: { + room.id: JoinedRoomUpdate( + timeline: TimelineUpdate( + events: [ + MatrixEvent( + type: 'm.call.invite', + content: { + 'lifetime': 60000, + 'call_id': outgoingCall.callId, + 'party_id': outgoingCall.localPartyId, + 'version': '1', + 'offer': {'type': 'offer', 'sdp': 'sdp'}, + }, + senderId: '@alice:testing.com', + eventId: 'outgoingCallInviteEvent', + originServerTs: DateTime.now(), + ), + ], + ), + ), + }, + ), + ), + ); + + // incoming call pc should be created + // if this fails, the call was not properly created + incomingCall = voip.calls[voip.currentCID]; + expect(incomingCall, isNotNull); + expect(incomingCall?.pc, isNotNull); + }); }); }