Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SES-3270] - Group invitation tweaks #933

Merged
merged 5 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.thoughtcrime.securesms.emoji.EmojiSource;
import org.thoughtcrime.securesms.groups.OpenGroupManager;
import org.thoughtcrime.securesms.groups.handler.AdminStateSync;
import org.thoughtcrime.securesms.groups.handler.CleanupInvitationHandler;
import org.thoughtcrime.securesms.groups.handler.DestroyedGroupSync;
import org.thoughtcrime.securesms.groups.handler.RemoveGroupMemberHandler;
import org.thoughtcrime.securesms.home.HomeActivity;
Expand Down Expand Up @@ -173,6 +174,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
@Inject EmojiSearchDatabase emojiSearchDb;
@Inject LegacyClosedGroupPollerV2 legacyClosedGroupPollerV2;
@Inject LegacyGroupDeprecationManager legacyGroupDeprecationManager;
@Inject CleanupInvitationHandler cleanupInvitationHandler;

public volatile boolean isAppVisible;
public String KEYGUARD_LOCK_TAG = NonTranslatableStringConstants.APP_NAME + ":KeyguardLock";
Expand Down Expand Up @@ -293,6 +295,7 @@ public void onCreate() {
removeGroupMemberHandler.start();
destroyedGroupSync.start();
adminStateSync.start();
cleanupInvitationHandler.start();

// add our shortcut debug menu if we are not in a release build
if (BuildConfig.BUILD_TYPE != "release") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class ConfigFactory @Inject constructor(
if (recreateConfigInstances) {
synchronized(groupConfigs) {
groupConfigs.remove(groupId)
}
}?.second?.dumpIfNeeded(clock)
}

val (lock, configs) = ensureGroupConfigsInitialized(groupId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ class EditGroupViewModel @AssistedInject constructor(
get() = groupInfo.value?.second?.mapTo(hashSetOf()) { it.accountId }.orEmpty()

fun onContactSelected(contacts: Set<AccountId>) {
performGroupOperation(errorMessage = { err ->
if (err is GroupInviteException) {
err.format(context, storage).toString()
} else {
null
performGroupOperation(
showLoading = false,
errorMessage = { err ->
if (err is GroupInviteException) {
err.format(context, storage).toString()
} else {
null
}
}
}) {
) {
groupManager.inviteMembers(
groupId,
contacts.toList(),
Expand All @@ -99,13 +102,13 @@ class EditGroupViewModel @AssistedInject constructor(
}

fun onPromoteContact(memberSessionId: AccountId) {
performGroupOperation {
performGroupOperation(showLoading = false) {
groupManager.promoteMember(groupId, listOf(memberSessionId))
}
}

fun onRemoveContact(contactSessionId: AccountId, removeMessages: Boolean) {
performGroupOperation {
performGroupOperation(showLoading = false) {
groupManager.removeMembers(
groupAccountId = groupId,
removedMembers = listOf(contactSessionId),
Expand Down Expand Up @@ -170,10 +173,13 @@ class EditGroupViewModel @AssistedInject constructor(
* This is a helper function that encapsulates the common error handling and progress tracking.
*/
private fun performGroupOperation(
showLoading: Boolean = true,
errorMessage: ((Throwable) -> String?)? = null,
operation: suspend () -> Unit) {
viewModelScope.launch {
mutableInProgress.value = true
if (showLoading) {
mutableInProgress.value = true
}

// We need to use GlobalScope here because we don't want
// any group operation to be cancelled when the view model is cleared.
Expand All @@ -188,7 +194,9 @@ class EditGroupViewModel @AssistedInject constructor(
mutableError.value = errorMessage?.invoke(e)
?: context.getString(R.string.errorUnknown)
} finally {
mutableInProgress.value = false
if (showLoading) {
mutableInProgress.value = false
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ class GroupManagerV2Impl @Inject constructor(

// Make sure every request is successful
response.requireAllRequestsSuccessful("Failed to invite members")

// Wait for the group configs to be pushed
configFactory.waitUntilGroupConfigsPushed(group)
} catch (e: Exception) {
// Update every member's status to "invite failed" and return group name
val groupName = configFactory.withMutableGroupConfigs(group) { configs ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.thoughtcrime.securesms.groups.handler

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import network.loki.messenger.libsession_util.allWithStatus
import network.loki.messenger.libsession_util.util.GroupMember
import org.session.libsession.messaging.groups.GroupScope
import org.session.libsession.utilities.ConfigFactoryProtocol
import org.session.libsession.utilities.TextSecurePreferences
import javax.inject.Inject

/**
* This handler is responsible for cleaning up the intermediate states that are created when
* sending invitations to join a group. This clean-up is necessary because the app could crash
* or being killed before the invitation is sent, and we don't want to leave the group member in a
* "pending" state while it's not really pending.
*
* This is achieved by checking the group members and marking the "pending" state as "failed"
* after the app is started, and only done once on every app process.
*/
class CleanupInvitationHandler @Inject constructor(
private val prefs: TextSecurePreferences,
private val configFactory: ConfigFactoryProtocol,
private val groupScope: GroupScope
) {
fun start() {
GlobalScope.launch {
// Wait for the local number to be available
prefs.watchLocalNumber().first { it != null }

val allGroups = configFactory.withUserConfigs {
it.userGroups.allClosedGroupInfo()
}

allGroups
.asSequence()
.filter { !it.kicked && !it.destroyed && it.hasAdminKey() }
.forEach { group ->
groupScope.launch(group.groupAccountId, debugName = "CleanupInvitationHandler") {
configFactory.withMutableGroupConfigs(group.groupAccountId) { configs ->
configs.groupMembers
.allWithStatus()
.filter { it.second == GroupMember.Status.INVITE_SENDING }
.forEach { (member, _) ->
member.setInviteFailed()
configs.groupMembers.set(member)
}
}
}
}
}
}
}