This commit is contained in:
Niels Andriesse
2021-05-21 15:56:38 +10:00
parent c0f894e1b2
commit dfd3ccc5d2
11 changed files with 47 additions and 152 deletions

View File

@@ -130,7 +130,6 @@ interface StorageProtocol {
fun getLastUpdated(threadID: Long): Long
// Contacts
fun getDisplayName(publicKey: String): String?
fun getProfilePictureURL(publicKey: String): String?
fun getContactWithSessionID(sessionID: String): Contact?
fun getAllContacts(): Set<Contact>

View File

@@ -1,22 +1,11 @@
package org.session.libsession.messaging.mentions
import org.session.libsession.messaging.MessagingModuleConfiguration
import org.session.libsession.messaging.contacts.Contact
import org.session.libsignal.database.LokiUserDatabaseProtocol
class MentionsManager(private val userPublicKey: String, private val userDatabase: LokiUserDatabaseProtocol) {
object MentionsManager {
var userPublicKeyCache = mutableMapOf<Long, Set<String>>() // Thread ID to set of user hex encoded public keys
companion object {
public lateinit var shared: MentionsManager
public fun configureIfNeeded(userPublicKey: String, userDatabase: LokiUserDatabaseProtocol) {
if (::shared.isInitialized) { return; }
shared = MentionsManager(userPublicKey, userDatabase)
}
}
fun cache(publicKey: String, threadID: Long) {
val cache = userPublicKeyCache[threadID]
if (cache != null) {
@@ -26,14 +15,16 @@ class MentionsManager(private val userPublicKey: String, private val userDatabas
}
}
fun getMentionCandidates(query: String, threadID: Long): List<Mention> {
// Prepare
fun getMentionCandidates(query: String, threadID: Long, isOpenGroup: Boolean): List<Mention> {
val cache = userPublicKeyCache[threadID] ?: return listOf()
// Prepare
val context = if (isOpenGroup) Contact.ContactContext.OPEN_GROUP else Contact.ContactContext.REGULAR
val storage = MessagingModuleConfiguration.shared.storage
val userPublicKey = storage.getUserPublicKey()
// Gather candidates
var candidates: List<Mention> = cache.mapNotNull { publicKey ->
val displayName = userDatabase.getDisplayName(publicKey)
if (displayName == null) { return@mapNotNull null }
if (displayName.startsWith("Anonymous")) { return@mapNotNull null }
val contact = storage.getContactWithSessionID(publicKey)
val displayName = contact?.displayName(context) ?: return@mapNotNull null
Mention(publicKey, displayName)
}
candidates = candidates.filter { it.publicKey != userPublicKey }

View File

@@ -132,7 +132,7 @@ private fun handleConfigurationMessage(message: ConfigurationMessage) {
val recipient = Recipient.from(context, Address.fromSerialized(userPublicKey), false)
if (message.displayName.isNotEmpty()) {
TextSecurePreferences.setProfileName(context, message.displayName)
profileManager.setProfileName(context, recipient, message.displayName)
profileManager.setName(context, recipient, message.displayName)
}
if (message.profileKey.isNotEmpty() && !message.profilePicture.isNullOrEmpty()
&& TextSecurePreferences.getProfilePictureURL(context) != message.profilePicture) {
@@ -166,9 +166,9 @@ fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, proto: SignalS
if (profile != null && userPublicKey != message.sender) {
val profileManager = SSKEnvironment.shared.profileManager
val recipient = Recipient.from(context, Address.fromSerialized(message.sender!!), false)
val displayName = profile.displayName!!
if (displayName.isNotEmpty()) {
profileManager.setProfileName(context, recipient, displayName)
val name = profile.displayName!!
if (name.isNotEmpty()) {
profileManager.setName(context, recipient, name)
}
if (profile.profileKey?.isNotEmpty() == true && profile.profilePictureURL?.isNotEmpty() == true
&& (recipient.profileKey == null || !MessageDigest.isEqual(recipient.profileKey, profile.profileKey))) {

View File

@@ -3,6 +3,7 @@ package org.session.libsession.messaging.utilities
import android.content.Context
import org.session.libsession.R
import org.session.libsession.messaging.MessagingModuleConfiguration
import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.sending_receiving.data_extraction.DataExtractionNotificationInfoMessage
import org.session.libsession.utilities.ExpirationUtil
@@ -12,8 +13,9 @@ object UpdateMessageBuilder {
var message = ""
val updateData = updateMessageData.kind ?: return message
if (!isOutgoing && sender == null) return message
val storage = MessagingModuleConfiguration.shared.storage
val senderName: String = if (!isOutgoing) {
MessagingModuleConfiguration.shared.storage.getDisplayName(sender!!) ?: sender
storage.getContactWithSessionID(sender!!)?.displayName(Contact.ContactContext.REGULAR) ?: sender
} else { context.getString(R.string.MessageRecord_you) }
when (updateData) {
@@ -33,7 +35,7 @@ object UpdateMessageBuilder {
}
is UpdateMessageData.Kind.GroupMemberAdded -> {
val members = updateData.updatedMembers.joinToString(", ") {
MessagingModuleConfiguration.shared.storage.getDisplayName(it) ?: it
storage.getContactWithSessionID(it)?.displayName(Contact.ContactContext.REGULAR) ?: it
}
message = if (isOutgoing) {
context.getString(R.string.MessageRecord_you_added_s_to_the_group, members)
@@ -54,7 +56,7 @@ object UpdateMessageBuilder {
} else {
// 2nd case: you are not part of the removed members
val members = updateData.updatedMembers.joinToString(", ") {
storage.getDisplayName(it) ?: it
storage.getContactWithSessionID(it)?.displayName(Contact.ContactContext.REGULAR) ?: it
}
if (isOutgoing) {
context.getString(R.string.MessageRecord_you_removed_s_from_the_group, members)
@@ -76,8 +78,9 @@ object UpdateMessageBuilder {
fun buildExpirationTimerMessage(context: Context, duration: Long, sender: String? = null, isOutgoing: Boolean = false): String {
if (!isOutgoing && sender == null) return ""
val storage = MessagingModuleConfiguration.shared.storage
val senderName: String? = if (!isOutgoing) {
MessagingModuleConfiguration.shared.storage.getDisplayName(sender!!) ?: sender
storage.getContactWithSessionID(sender!!)?.displayName(Contact.ContactContext.REGULAR) ?: sender
} else { context.getString(R.string.MessageRecord_you) }
return if (duration <= 0) {
if (isOutgoing) context.getString(R.string.MessageRecord_you_disabled_disappearing_messages)
@@ -90,7 +93,8 @@ object UpdateMessageBuilder {
}
fun buildDataExtractionMessage(context: Context, kind: DataExtractionNotificationInfoMessage.Kind, sender: String? = null): String {
val senderName = MessagingModuleConfiguration.shared.storage.getDisplayName(sender!!) ?: sender
val storage = MessagingModuleConfiguration.shared.storage
val senderName = storage.getContactWithSessionID(sender!!)?.displayName(Contact.ContactContext.REGULAR) ?: sender!!
return when (kind) {
DataExtractionNotificationInfoMessage.Kind.SCREENSHOT ->
context.getString(R.string.MessageRecord_s_took_a_screenshot, senderName)

View File

@@ -29,11 +29,11 @@ class SSKEnvironment(
const val NAME_PADDED_LENGTH = 26
}
fun setDisplayName(context: Context, recipient: Recipient, displayName: String?) // Client-side Nickname
fun setNickname(context: Context, recipient: Recipient, nickname: String?)
fun setName(context: Context, recipient: Recipient, name: String)
fun setProfilePictureURL(context: Context, recipient: Recipient, profilePictureURL: String)
fun setProfileKey(context: Context, recipient: Recipient, profileKey: ByteArray)
fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode)
fun getDisplayName(context: Context, recipient: Recipient): String?
}
interface MessageExpirationManagerProtocol {

View File

@@ -286,7 +286,6 @@ public class Recipient implements RecipientModifiedListener {
}
public synchronized @Nullable String getName() {
String displayName = MessagingModuleConfiguration.shared.getStorage().getDisplayName(this.address.toString());
if (displayName != null && !displayName.isEmpty()) { return displayName; }