Consolidate duplicated logic to retrieve groups in common.

Merges all of these into GroupsInCommonRepository:
- ConversationSettingsRepository.getGroupsInCommon()
- CallLinkIncomingRequestRepository.getGroupsInCommon()
- ContactSearchPagedDataSourceRepository.getGroupsInCommon()
- ReviewUtil.getGroupsInCommonCount()
- AboutSheetRepository.getGroupsInCommonCount()
This commit is contained in:
Jeffrey Starke
2025-04-08 11:02:29 -04:00
committed by Michelle Tang
parent c9795141df
commit aa7b61ecb1
15 changed files with 112 additions and 144 deletions

View File

@@ -464,7 +464,7 @@ open class ContactSearchAdapter(
override fun bindNumberField(model: RecipientModel) {
val recipient = getRecipient(model)
if (model.knownRecipient.sectionKey == ContactSearchConfiguration.SectionKey.GROUP_MEMBERS) {
number.text = model.knownRecipient.groupsInCommon.toDisplayText(context)
number.text = model.knownRecipient.groupsInCommon.toDisplayText(context, displayGroupsLimit = 2)
number.visible = true
} else if (model.shortSummary && recipient.isGroup) {
val count = recipient.participantIds.size

View File

@@ -6,6 +6,7 @@ import org.thoughtcrime.securesms.contacts.HeaderAction
import org.thoughtcrime.securesms.database.model.DistributionListPrivacyMode
import org.thoughtcrime.securesms.database.model.GroupRecord
import org.thoughtcrime.securesms.database.model.ThreadRecord
import org.thoughtcrime.securesms.groups.GroupsInCommonSummary
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.search.MessageResult
@@ -32,7 +33,7 @@ sealed class ContactSearchData(val contactSearchKey: ContactSearchKey) {
val recipient: Recipient,
val shortSummary: Boolean = false,
val headerLetter: String? = null,
val groupsInCommon: GroupsInCommon = GroupsInCommon(0, listOf())
val groupsInCommon: GroupsInCommonSummary = GroupsInCommonSummary(listOf())
) : ContactSearchData(ContactSearchKey.RecipientSearchKey(recipient.id, false))
/**

View File

@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.contacts.paged
import android.database.Cursor
import androidx.annotation.WorkerThread
import org.signal.core.util.requireLong
import org.signal.paging.PagedDataSource
import org.thoughtcrime.securesms.R
@@ -168,6 +169,7 @@ class ContactSearchPagedDataSource(
.filter { contactSearchPagedDataSourceRepository.recipientNameContainsQuery(it.recipient, query) }
}
@WorkerThread
private fun getSectionData(section: ContactSearchConfiguration.Section, query: String?, startIndex: Int, endIndex: Int): List<ContactSearchData> {
return when (section) {
is ContactSearchConfiguration.Section.Groups -> getGroupContactsData(section, query, startIndex, endIndex)
@@ -432,6 +434,7 @@ class ContactSearchPagedDataSource(
}
}
@WorkerThread
private fun getGroupMembersContactData(section: ContactSearchConfiguration.Section.GroupMembers, query: String?, startIndex: Int, endIndex: Int): List<ContactSearchData> {
return getGroupMembersSearchIterator(query).use { records ->
readContactData(

View File

@@ -2,6 +2,9 @@ package org.thoughtcrime.securesms.contacts.paged
import android.content.Context
import android.database.Cursor
import androidx.annotation.WorkerThread
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.signal.core.util.CursorUtil
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.contacts.ContactRepository
@@ -13,6 +16,8 @@ import org.thoughtcrime.securesms.database.SignalDatabase
import org.thoughtcrime.securesms.database.ThreadTable
import org.thoughtcrime.securesms.database.model.DistributionListPrivacyMode
import org.thoughtcrime.securesms.database.model.GroupRecord
import org.thoughtcrime.securesms.groups.GroupsInCommonRepository
import org.thoughtcrime.securesms.groups.GroupsInCommonSummary
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.keyvalue.StorySend
import org.thoughtcrime.securesms.recipients.Recipient
@@ -105,14 +110,13 @@ open class ContactSearchPagedDataSourceRepository(
return Recipient.resolved(RecipientId.from(CursorUtil.requireLong(cursor, RecipientTable.ID)))
}
open fun getGroupsInCommon(recipient: Recipient): GroupsInCommon {
val groupsInCommon = SignalDatabase.groups.getPushGroupsContainingMember(recipient.id)
val groupRecipientIds = groupsInCommon.take(2).map { it.recipientId }
val names = Recipient.resolvedList(groupRecipientIds)
.map { it.getDisplayName(context) }
.sorted()
return GroupsInCommon(groupsInCommon.size, names)
@WorkerThread
open fun getGroupsInCommon(recipient: Recipient): GroupsInCommonSummary {
return runBlocking {
GroupsInCommonRepository
.getGroupsInCommonSummary(context, recipient.id)
.first()
}
}
open fun getRecipientFromGroupRecord(groupRecord: GroupRecord): Recipient {

View File

@@ -1,34 +0,0 @@
package org.thoughtcrime.securesms.contacts.paged
import android.content.Context
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.R
/**
* Groups in common helper class
*/
data class GroupsInCommon(
private val total: Int,
private val names: List<String>
) {
fun toDisplayText(context: Context): String {
return when (total) {
0 -> {
Log.w(TAG, "Member with no groups in common!")
return ""
}
1 -> context.getString(R.string.MessageRequestProfileView_member_of_one_group, names[0])
2 -> context.getString(R.string.MessageRequestProfileView_member_of_two_groups, names[0], names[1])
else -> context.getString(
R.string.MessageRequestProfileView_member_of_many_groups,
names[0],
names[1],
context.resources.getQuantityString(R.plurals.MessageRequestProfileView_member_of_d_additional_groups, total - 2, total - 2)
)
}
}
companion object {
private val TAG = Log.tag(GroupsInCommon::class.java)
}
}