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-3302] - Add the ability to re-create legacy group for unknown members #944

Merged
merged 1 commit into from
Feb 13, 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 @@ -22,7 +22,6 @@ import org.session.libsignal.utilities.AccountId
import org.thoughtcrime.securesms.conversation.v2.utilities.TextUtilities.textSizeInBytes
import org.thoughtcrime.securesms.database.GroupDatabase
import org.thoughtcrime.securesms.dependencies.ConfigFactory
import javax.inject.Inject


@HiltViewModel(assistedFactory = CreateGroupViewModel.Factory::class)
Expand Down Expand Up @@ -68,12 +67,13 @@ class CreateGroupViewModel @AssistedInject constructor(
mutableGroupName.value = group.title
val myPublicKey = storage.getUserPublicKey()

selectContactsViewModel.selectAccountIDs(
group.members
.asSequence()
.filter { it.serialize() != myPublicKey }
.mapTo(mutableSetOf()) { AccountId(it.serialize()) }
)
val accountIDs = group.members
.asSequence()
.filter { it.serialize() != myPublicKey }
.mapTo(mutableSetOf()) { AccountId(it.serialize()) }

selectContactsViewModel.selectAccountIDs(accountIDs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should setManuallyAddedContacts internally call selectAccountIDs(accountIDs) as part of its own logic?
It feels weird to have to do two actions from here with the same data.

selectContactsViewModel.setManuallyAddedContacts(accountIDs)
}
} finally {
mutableIsLoading.value = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
Expand All @@ -19,6 +20,7 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
Expand All @@ -43,6 +45,10 @@ class SelectContactsViewModel @AssistedInject constructor(
// Input: The selected contact account IDs
private val mutableSelectedContactAccountIDs = MutableStateFlow(emptySet<AccountId>())

// Input: The manually added items to select from. This will be combined (and deduped) with the contacts
// the user has. This is useful for selecting contacts that are not in the user's contacts list.
private val mutableManuallyAddedContacts = MutableStateFlow(emptySet<AccountId>())

// Output: The search query
val searchQuery: StateFlow<String> get() = mutableSearchQuery

Expand All @@ -64,20 +70,30 @@ class SelectContactsViewModel @AssistedInject constructor(
scope.cancel()
}

@OptIn(ExperimentalCoroutinesApi::class)
private fun observeContacts() = (configFactory.configUpdateNotifications as Flow<Any>)
.debounce(100L)
.onStart { emit(Unit) }
.map {
withContext(Dispatchers.Default) {
val allContacts = configFactory.withUserConfigs { configs ->
configs.contacts.all().filter { it.approvedMe }
.flatMapLatest {
mutableManuallyAddedContacts.map { manuallyAdded ->
withContext(Dispatchers.Default) {
val allContacts =
(configFactory.withUserConfigs { configs -> configs.contacts.all() }
.asSequence()
.map { AccountId(it.id) } + manuallyAdded)

if (excludingAccountIDs.isEmpty()) {
allContacts.toSet()
} else {
allContacts.filterNotTo(mutableSetOf()) { it in excludingAccountIDs }
}.map {
Recipient.from(
appContext,
Address.fromSerialized(it.hexString),
false
)
}
}

if (excludingAccountIDs.isEmpty()) {
allContacts
} else {
allContacts.filterNot { AccountId(it.id) in excludingAccountIDs }
}.map { Recipient.from(appContext, Address.fromSerialized(it.id), false) }
}
}

Expand All @@ -102,6 +118,10 @@ class SelectContactsViewModel @AssistedInject constructor(
.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name })
}

fun setManuallyAddedContacts(accountIDs: Set<AccountId>) {
mutableManuallyAddedContacts.value = accountIDs
}

fun onSearchQueryChanged(query: String) {
mutableSearchQuery.value = query
}
Expand Down