[SES-3222] - Add "you" in the list of mention (#896)

* Add "you" as a candidate of mention

* Removed logs
pull/1709/head
SessionHero01 2 months ago committed by GitHub
parent 139dd2b307
commit d1ab9ccc66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,5 +1,6 @@
package org.thoughtcrime.securesms.conversation.v2.mention
import android.app.Application
import android.content.ContentResolver
import android.graphics.Typeface
import android.text.Editable
@ -26,10 +27,13 @@ import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import network.loki.messenger.R
import network.loki.messenger.libsession_util.allWithStatus
import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.utilities.SodiumUtilities
import org.session.libsession.utilities.ConfigFactoryProtocol
import org.session.libsignal.utilities.AccountId
import org.session.libsignal.utilities.IdPrefix
import org.thoughtcrime.securesms.database.DatabaseContentProviders.Conversation
import org.thoughtcrime.securesms.database.GroupDatabase
import org.thoughtcrime.securesms.database.GroupMemberDatabase
@ -48,6 +52,7 @@ import org.thoughtcrime.securesms.util.observeChanges
* 2. Set the EditText's editable factory to [editableFactory], via [android.widget.EditText.setEditableFactory]
*/
class MentionViewModel(
application: Application,
threadID: Long,
contentResolver: ContentResolver,
threadDatabase: ThreadDatabase,
@ -103,8 +108,14 @@ class MentionViewModel(
else -> listOf()
}
val openGroup = if (recipient.isCommunityRecipient) {
storage.getOpenGroup(threadID)
} else {
null
}
val moderatorIDs = if (recipient.isCommunityRecipient) {
val groupId = storage.getOpenGroup(threadID)?.id
val groupId = openGroup?.id
if (groupId.isNullOrBlank()) {
emptySet()
} else {
@ -129,13 +140,34 @@ class MentionViewModel(
Contact.ContactContext.REGULAR
}
contactDatabase.getContacts(memberIDs).map { contact ->
val myId = if (openGroup != null) {
AccountId(IdPrefix.BLINDED,
SodiumUtilities.blindedKeyPair(openGroup.publicKey,
requireNotNull(storage.getUserED25519KeyPair()))!!.publicKey.asBytes)
.hexString
} else {
requireNotNull(storage.getUserPublicKey())
}
(sequenceOf(
Member(
publicKey = contact.accountID,
name = contact.displayName(contactContext).orEmpty(),
isModerator = contact.accountID in moderatorIDs,
publicKey = myId,
name = application.getString(R.string.you),
isModerator = myId in moderatorIDs,
isMe = true
)
}
) + contactDatabase.getContacts(memberIDs)
.asSequence()
.filter { it.accountID != myId }
.map { contact ->
Member(
publicKey = contact.accountID,
name = contact.displayName(contactContext).orEmpty(),
isModerator = contact.accountID in moderatorIDs,
isMe = false
)
})
.toList()
}
.flowOn(dispatcher)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(10_000L), null)
@ -238,6 +270,7 @@ class MentionViewModel(
val publicKey: String,
val name: String,
val isModerator: Boolean,
val isMe: Boolean,
)
data class Candidate(
@ -248,7 +281,8 @@ class MentionViewModel(
val matchScore: Int,
) {
companion object {
val MENTION_LIST_COMPARATOR = compareBy<Candidate> { it.matchScore }
val MENTION_LIST_COMPARATOR = compareBy<Candidate> { !it.member.isMe }
.thenBy { it.matchScore }
.then(compareBy { it.member.name })
}
}
@ -275,6 +309,7 @@ class MentionViewModel(
private val storage: Storage,
private val memberDatabase: GroupMemberDatabase,
private val configFactory: ConfigFactoryProtocol,
private val application: Application,
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@ -288,6 +323,7 @@ class MentionViewModel(
memberDatabase = memberDatabase,
storage = storage,
configFactory = configFactory,
application = application,
) as T
}
}

Loading…
Cancel
Save