From 76a4ce7f6776319f815359fff609ae558e04b236 Mon Sep 17 00:00:00 2001 From: krille-chan Date: Sun, 24 Mar 2024 18:02:20 +0100 Subject: [PATCH 1/2] fix: Make database deleteable without the need to init the boxcollection --- lib/src/database/indexeddb_box.dart | 10 +++++----- lib/src/database/matrix_sdk_database.dart | 5 ++++- lib/src/database/sqflite_box.dart | 10 +++++----- test/box_test.dart | 5 +++-- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/src/database/indexeddb_box.dart b/lib/src/database/indexeddb_box.dart index fe869f15..a5f56571 100644 --- a/lib/src/database/indexeddb_box.dart +++ b/lib/src/database/indexeddb_box.dart @@ -9,10 +9,9 @@ import 'package:matrix/src/database/zone_transaction_mixin.dart'; class BoxCollection with ZoneTransactionMixin { final Database _db; final Set boxNames; - final String _name; - final IdbFactory _idbFactory; + final String name; - BoxCollection(this._db, this.boxNames, this._name, this._idbFactory); + BoxCollection(this._db, this.boxNames, this.name); static Future open( String name, @@ -29,7 +28,7 @@ class BoxCollection with ZoneTransactionMixin { db.createObjectStore(name, autoIncrement: true); } }); - return BoxCollection(db, boxNames, name, idbFactory); + return BoxCollection(db, boxNames, name); } Box openBox(String name) { @@ -81,7 +80,8 @@ class BoxCollection with ZoneTransactionMixin { return _db.close(); } - Future delete() => _idbFactory.deleteDatabase(_name); + static Future delete(String path, [IdbFactory? factory]) => + (factory ?? window.indexedDB!).deleteDatabase(path); } class Box { diff --git a/lib/src/database/matrix_sdk_database.dart b/lib/src/database/matrix_sdk_database.dart index 8e39bdca..89a89575 100644 --- a/lib/src/database/matrix_sdk_database.dart +++ b/lib/src/database/matrix_sdk_database.dart @@ -1594,5 +1594,8 @@ class MatrixSdkDatabase extends DatabaseApi { } @override - Future delete() => _collection.delete(); + Future delete() => BoxCollection.delete( + name, + sqfliteFactory ?? idbFactory, + ); } diff --git a/lib/src/database/sqflite_box.dart b/lib/src/database/sqflite_box.dart index 459be4a7..61c10ad8 100644 --- a/lib/src/database/sqflite_box.dart +++ b/lib/src/database/sqflite_box.dart @@ -10,9 +10,9 @@ import 'package:matrix/src/database/zone_transaction_mixin.dart'; class BoxCollection with ZoneTransactionMixin { final Database _db; final Set boxNames; - final DatabaseFactory? _factory; + final String name; - BoxCollection(this._db, this.boxNames, this._factory); + BoxCollection(this._db, this.boxNames, this.name); static Future open( String name, @@ -32,7 +32,7 @@ class BoxCollection with ZoneTransactionMixin { batch.execute('CREATE INDEX IF NOT EXISTS k_index ON $name (k)'); } await batch.commit(noResult: true); - return BoxCollection(sqfliteDatabase, boxNames, sqfliteFactory); + return BoxCollection(sqfliteDatabase, boxNames, name); } Box openBox(String name) { @@ -67,8 +67,8 @@ class BoxCollection with ZoneTransactionMixin { Future close() => _db.close(); - Future delete() => - (_factory ?? databaseFactory).deleteDatabase(_db.path); + static Future delete(String path, [DatabaseFactory? factory]) => + (factory ?? databaseFactory).deleteDatabase(path); } class Box { diff --git a/test/box_test.dart b/test/box_test.dart index 5b66514d..e5d352ef 100644 --- a/test/box_test.dart +++ b/test/box_test.dart @@ -9,8 +9,9 @@ void main() { const Set boxNames = {'cats', 'dogs'}; const data = {'name': 'Fluffy', 'age': 2}; const data2 = {'name': 'Loki', 'age': 4}; + late Database db; setUp(() async { - final db = await databaseFactoryFfi.openDatabase(':memory:'); + db = await databaseFactoryFfi.openDatabase(':memory:'); collection = await BoxCollection.open( 'testbox', boxNames, @@ -91,7 +92,7 @@ void main() { }); test('Box.delete', () async { - await collection.delete(); + await BoxCollection.delete(db.path, databaseFactoryFfi); }); }); } From 76ca4243bc68329f5316b95e5c4b4b946f183729 Mon Sep 17 00:00:00 2001 From: td Date: Mon, 25 Mar 2024 17:09:07 +0530 Subject: [PATCH 2/2] chore: emit handleCallEnded on ice fail --- lib/src/voip/call.dart | 21 +++++++++++---------- lib/src/voip/group_call.dart | 4 +++- lib/src/voip/voip.dart | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/src/voip/call.dart b/lib/src/voip/call.dart index fd10290d..12815de3 100644 --- a/lib/src/voip/call.dart +++ b/lib/src/voip/call.dart @@ -525,7 +525,7 @@ class CallSession { Logs().v('[VOIP] Call invite has expired. Hanging up.'); hangupParty = CallParty.kRemote; // effectively fireCallEvent(CallEvent.kHangup); - hangup(CallErrorCode.InviteTimeout); + hangup(reason: CallErrorCode.InviteTimeout); } ringingTimer?.cancel(); ringingTimer = null; @@ -550,7 +550,7 @@ class CallSession { successor = newCall; onCallReplaced.add(newCall); // ignore: unawaited_futures - hangup(CallErrorCode.Replaced, true); + hangup(reason: CallErrorCode.Replaced); } Future sendAnswer(RTCSessionDescription answer) async { @@ -1164,7 +1164,7 @@ class CallSession { if (state != CallState.kRinging && state != CallState.kFledgling) { Logs().e( '[VOIP] Call must be in \'ringing|fledgling\' state to reject! (current state was: ${state.toString()}) Calling hangup instead'); - await hangup(reason, shouldEmit); + await hangup(reason: reason, shouldEmit: shouldEmit); return; } Logs().d('[VOIP] Rejecting call: $callId'); @@ -1174,7 +1174,7 @@ class CallSession { } } - Future hangup([String? reason, bool shouldEmit = true]) async { + Future hangup({String? reason, bool shouldEmit = true}) async { await terminate( CallParty.kLocal, reason ?? CallErrorCode.UserHangup, shouldEmit); @@ -1303,8 +1303,9 @@ class CallSession { capabilities: callCapabilities, metadata: metadata); // just incase we ended the call but already sent the invite + // raraley happens during glares if (state == CallState.kEnded) { - await hangup(CallErrorCode.Replaced, false); + await hangup(reason: CallErrorCode.Replaced); return; } inviteOrAnswerSent = true; @@ -1318,7 +1319,7 @@ class CallSession { inviteTimer = Timer(CallTimeouts.callInviteLifetime, () { if (state == CallState.kInviteSent) { - hangup(CallErrorCode.InviteTimeout); + hangup(reason: CallErrorCode.InviteTimeout); } inviteTimer?.cancel(); inviteTimer = null; @@ -1404,7 +1405,7 @@ class CallSession { await updateMuteStatus(); missedCall = false; } else if (state == RTCIceConnectionState.RTCIceConnectionStateFailed) { - await hangup(CallErrorCode.IceFailed, false); + await hangup(reason: CallErrorCode.IceFailed); } }; } catch (e) { @@ -1614,7 +1615,7 @@ class CallSession { 'Failed to send candidates on attempt $candidateSendTries Giving up on this call.'); lastError = CallError(CallErrorCode.SignallingFailed, 'Signalling failed', e); - await hangup(CallErrorCode.SignallingFailed, true); + await hangup(reason: CallErrorCode.SignallingFailed); return; } @@ -1655,7 +1656,7 @@ class CallSession { fireCallEvent(CallEvent.kError); lastError = CallError( CallErrorCode.LocalOfferFailed, 'Failed to get local offer!', err); - await terminate(CallParty.kLocal, CallErrorCode.LocalOfferFailed, false); + await terminate(CallParty.kLocal, CallErrorCode.LocalOfferFailed, true); } Future _getUserMediaFailed(dynamic err) async { @@ -1666,7 +1667,7 @@ class CallSession { CallErrorCode.NoUserMedia, 'Couldn\'t start capturing media! Is your microphone set up and does this app have permission?', err); - await terminate(CallParty.kLocal, CallErrorCode.NoUserMedia, false); + await terminate(CallParty.kLocal, CallErrorCode.NoUserMedia, true); } } diff --git a/lib/src/voip/group_call.dart b/lib/src/voip/group_call.dart index 44f364bb..b0c42275 100644 --- a/lib/src/voip/group_call.dart +++ b/lib/src/voip/group_call.dart @@ -1029,7 +1029,9 @@ class GroupCall { } if (call.state != CallState.kEnded) { - await call.hangup(hangupReason, false); + // no need to emit individual handleCallEnded on group calls + // also prevents a loop of hangup and onCallHangupNotifierForGroupCalls + await call.hangup(reason: hangupReason, shouldEmit: false); } final usermediaStream = getUserMediaStreamByUserId(opponentMemberId); diff --git a/lib/src/voip/voip.dart b/lib/src/voip/voip.dart index c099815b..931449a9 100644 --- a/lib/src/voip/voip.dart +++ b/lib/src/voip/voip.dart @@ -244,6 +244,7 @@ class VoIP { (confId == null || confId != currentGroupCID)) { Logs().v( '[VOIP] onCallInvite: Unable to handle new calls, maybe user is busy.'); + // no need to emit here because handleNewCall was never triggered yet await newCall.reject(reason: CallErrorCode.UserBusy, shouldEmit: false); await delegate.handleMissedCall(newCall); return;