From b42b154e3ff69e7e9303e1938e025685186b67bb Mon Sep 17 00:00:00 2001 From: Krille Date: Thu, 21 Dec 2023 13:50:18 +0100 Subject: [PATCH] feat: Make client background service ready --- lib/src/client.dart | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index dee3b12bc..de4f8790d 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1270,21 +1270,6 @@ class Client extends MatrixApi { Duration timeoutForServerRequests = const Duration(seconds: 8), bool returnNullIfSeen = true, }) async { - // Get access token if necessary: - final database = _database ??= await databaseBuilder?.call(this); - if (!isLogged()) { - if (database == null) { - throw Exception( - 'Can not execute getEventByPushNotification() without a database'); - } - final clientInfoMap = await database.getClient(clientName); - final token = clientInfoMap?.tryGet('token'); - if (token == null) { - throw Exception('Client is not logged in.'); - } - accessToken = token; - } - // Check if the notification contains an event at all: final eventId = notification.eventId; final roomId = notification.roomId; @@ -1309,6 +1294,8 @@ class Client extends MatrixApi { senderId: 'UNKNOWN', originServerTs: DateTime.now(), )); + } else { + await room.loadHeroUsers(); } if (roomAlias != null) { room.setState(Event( @@ -1413,7 +1400,7 @@ class Client extends MatrixApi { if (storeInDatabase) { await database?.transaction(() async { - await database.storeEventUpdate( + await database?.storeEventUpdate( EventUpdate( roomID: roomId, type: EventUpdateType.timeline, @@ -1452,6 +1439,7 @@ class Client extends MatrixApi { String? newOlmAccount, bool waitForFirstSync = true, bool waitUntilLoadCompletedLoaded = true, + bool isBackgroundClient = false, /// Will be called if the app performs a migration task from the [legacyDatabaseBuilder] void Function()? onMigration, @@ -1569,13 +1557,15 @@ class Client extends MatrixApi { encryption?.pickledOlmAccount, ); } - userDeviceKeysLoading = database - .getUserDeviceKeys(this) - .then((keys) => _userDeviceKeys = keys); - roomsLoading = database.getRoomList(this).then((rooms) { - _rooms = rooms; - _sortRooms(); - }); + if (!isBackgroundClient) { + userDeviceKeysLoading = database + .getUserDeviceKeys(this) + .then((keys) => _userDeviceKeys = keys); + roomsLoading = database.getRoomList(this).then((rooms) { + _rooms = rooms; + _sortRooms(); + }); + } _accountDataLoading = database.getAccountData().then((data) { _accountData = data; _updatePushrules(); @@ -1595,7 +1585,10 @@ class Client extends MatrixApi { ); /// Timeout of 0, so that we don't see a spinner for 30 seconds. - firstSyncReceived = _sync(timeout: Duration.zero); + if (!isBackgroundClient) { + firstSyncReceived = _sync(timeout: Duration.zero); + } + if (waitForFirstSync) { await firstSyncReceived; } @@ -1653,7 +1646,7 @@ class Client extends MatrixApi { /// Immediately start a sync and wait for completion. /// If there is an active sync already, wait for the active sync instead. Future oneShotSync() { - return _sync(); + return _currentSync ??= _innerSync(); } /// Pass a timeout to set how long the server waits before sending an empty response.