commit 2fcc16b77a2239df927392bcc098b7c434fd6940
parent 688051fd63b3e1a13cef938dfcdb02f06b302635
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Sun, 22 Oct 2023 18:12:12 +0200

refactor(app/bridge): triggerScopeSync error handling

Diffstat:
Mapp/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeService.kt | 62+++++++++++++++++++++++++++++---------------------------------
1 file changed, 29 insertions(+), 33 deletions(-)

diff --git a/app/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeService.kt b/app/src/main/kotlin/me/rhunk/snapenhance/bridge/BridgeService.kt @@ -43,35 +43,39 @@ class BridgeService : Service() { } fun triggerScopeSync(scope: SocialScope, id: String, updateOnly: Boolean = false) { - val modDatabase = remoteSideContext.modDatabase - val syncedObject = when (scope) { - SocialScope.FRIEND -> { - if (updateOnly && modDatabase.getFriendInfo(id) == null) return - syncCallback.syncFriend(id) - } - SocialScope.GROUP -> { - if (updateOnly && modDatabase.getGroupInfo(id) == null) return - syncCallback.syncGroup(id) + runCatching { + val modDatabase = remoteSideContext.modDatabase + val syncedObject = when (scope) { + SocialScope.FRIEND -> { + if (updateOnly && modDatabase.getFriendInfo(id) == null) return + syncCallback.syncFriend(id) + } + SocialScope.GROUP -> { + if (updateOnly && modDatabase.getGroupInfo(id) == null) return + syncCallback.syncGroup(id) + } + else -> null } - else -> null - } - if (syncedObject == null) { - remoteSideContext.log.error("Failed to sync $scope $id") - return - } + if (syncedObject == null) { + remoteSideContext.log.error("Failed to sync $scope $id") + return + } - when (scope) { - SocialScope.FRIEND -> { - SerializableDataObject.fromJson<FriendInfo>(syncedObject).let { - modDatabase.syncFriend(it) + when (scope) { + SocialScope.FRIEND -> { + SerializableDataObject.fromJson<FriendInfo>(syncedObject).let { + modDatabase.syncFriend(it) + } } - } - SocialScope.GROUP -> { - SerializableDataObject.fromJson<MessagingGroupInfo>(syncedObject).let { - modDatabase.syncGroupInfo(it) + SocialScope.GROUP -> { + SerializableDataObject.fromJson<MessagingGroupInfo>(syncedObject).let { + modDatabase.syncGroupInfo(it) + } } } + }.onFailure { + remoteSideContext.log.error("Failed to sync $scope $id", it) } } @@ -147,18 +151,10 @@ class BridgeService : Service() { syncCallback = callback measureTimeMillis { remoteSideContext.modDatabase.getFriends().map { it.userId } .forEach { friendId -> - runCatching { - triggerScopeSync(SocialScope.FRIEND, friendId, true) - }.onFailure { - remoteSideContext.log.error("Failed to sync friend $friendId", it) - } + triggerScopeSync(SocialScope.FRIEND, friendId, true) } remoteSideContext.modDatabase.getGroups().map { it.conversationId }.forEach { groupId -> - runCatching { - triggerScopeSync(SocialScope.GROUP, groupId, true) - }.onFailure { - remoteSideContext.log.error("Failed to sync group $groupId", it) - } + triggerScopeSync(SocialScope.GROUP, groupId, true) } }.also { remoteSideContext.log.verbose("Syncing remote took $it ms")