From afb7cc1485a859a297ab896940a92fb1fc9fce1a Mon Sep 17 00:00:00 2001 From: SessionHero01 <180888785+SessionHero01@users.noreply.github.com> Date: Mon, 21 Oct 2024 12:01:19 +1100 Subject: [PATCH] Leaving message sending optimisation --- .../securesms/groups/GroupManagerV2Impl.kt | 39 ++++++++++++------- .../messaging/groups/GroupManagerV2.kt | 3 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/groups/GroupManagerV2Impl.kt b/app/src/main/java/org/thoughtcrime/securesms/groups/GroupManagerV2Impl.kt index 641ca9626c..ae5d94f059 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/groups/GroupManagerV2Impl.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/groups/GroupManagerV2Impl.kt @@ -3,12 +3,15 @@ package org.thoughtcrime.securesms.groups import android.content.Context import com.google.protobuf.ByteString import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filterIsInstance import kotlinx.coroutines.flow.first +import kotlinx.coroutines.supervisorScope import kotlinx.coroutines.withContext import network.loki.messenger.libsession_util.ConfigBase.Companion.PRIORITY_VISIBLE import network.loki.messenger.libsession_util.util.Conversation @@ -388,7 +391,7 @@ class GroupManagerV2Impl @Inject constructor( } } - override suspend fun leaveGroup(groupId: AccountId, deleteOnLeave: Boolean) { + override suspend fun leaveGroup(groupId: AccountId, deleteOnLeave: Boolean) = withContext(dispatcher + SupervisorJob()) { val group = configFactory.getClosedGroup(groupId) // Only send the left/left notification group message when we are not kicked and we are not the only admin (only admin has a special treatment) @@ -400,30 +403,38 @@ class GroupManagerV2Impl @Inject constructor( if (group?.kicked == false) { val destination = Destination.ClosedGroup(groupId.hexString) + val sendMessageTasks = mutableListOf>() // Always send a "XXX left" message to the group if we can - MessageSender.send( - GroupUpdated( - GroupUpdateMessage.newBuilder() - .setMemberLeftNotificationMessage(DataMessage.GroupUpdateMemberLeftNotificationMessage.getDefaultInstance()) - .build() - ), - destination, - isSyncMessage = false - ) - - // If we are not the only admin, send a left message for other admin to handle the member removal - if (!weAreTheOnlyAdmin) { + sendMessageTasks += async { MessageSender.send( GroupUpdated( GroupUpdateMessage.newBuilder() - .setMemberLeftMessage(DataMessage.GroupUpdateMemberLeftMessage.getDefaultInstance()) + .setMemberLeftNotificationMessage(DataMessage.GroupUpdateMemberLeftNotificationMessage.getDefaultInstance()) .build() ), destination, isSyncMessage = false ).await() } + + + // If we are not the only admin, send a left message for other admin to handle the member removal + if (!weAreTheOnlyAdmin) { + sendMessageTasks += async { + MessageSender.send( + GroupUpdated( + GroupUpdateMessage.newBuilder() + .setMemberLeftMessage(DataMessage.GroupUpdateMemberLeftMessage.getDefaultInstance()) + .build() + ), + destination, + isSyncMessage = false + ).await() + } + } + + sendMessageTasks.awaitAll() } // If we are the only admin, leaving this group will destroy the group diff --git a/libsession/src/main/java/org/session/libsession/messaging/groups/GroupManagerV2.kt b/libsession/src/main/java/org/session/libsession/messaging/groups/GroupManagerV2.kt index 5ee249daa9..72b28f14f6 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/groups/GroupManagerV2.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/groups/GroupManagerV2.kt @@ -44,7 +44,8 @@ interface GroupManagerV2 { suspend fun handleMemberLeft(message: GroupUpdated, group: AccountId) - suspend fun leaveGroup(groupId: AccountId, deleteOnLeave: Boolean) + suspend fun leaveGroup(groupId: + AccountId, deleteOnLeave: Boolean) suspend fun promoteMember(group: AccountId, members: List)