From dfd3ccc5d274eb586972f9bfefc56669a45abdc5 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 21 May 2021 15:56:38 +1000 Subject: [PATCH] Clean --- .../loki/database/LokiUserDatabase.kt | 64 ------------------- .../sskenvironment/ProfileManager.kt | 60 +++++++---------- .../libsession/database/StorageProtocol.kt | 1 - .../messaging/mentions/MentionsManager.kt | 27 +++----- .../ReceivedMessageHandler.kt | 8 +-- .../utilities/UpdateMessageBuilder.kt | 14 ++-- .../libsession/utilities/SSKEnvironment.kt | 4 +- .../utilities/recipients/Recipient.java | 1 - .../database/LokiAPIDatabaseProtocol.kt | 12 +--- .../database/LokiMessageDatabaseProtocol.kt | 1 - .../database/LokiUserDatabaseProtocol.kt | 7 -- 11 files changed, 47 insertions(+), 152 deletions(-) delete mode 100644 libsignal/src/main/java/org/session/libsignal/database/LokiUserDatabaseProtocol.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiUserDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiUserDatabase.kt index 1a7efd48ff..e69de29bb2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiUserDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiUserDatabase.kt @@ -1,64 +0,0 @@ -package org.thoughtcrime.securesms.loki.database - -import android.content.ContentValues -import android.content.Context -import android.database.sqlite.SQLiteDatabase -import org.session.libsignal.utilities.Log -import org.session.libsession.utilities.Address -import org.thoughtcrime.securesms.database.Database -import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper -import org.thoughtcrime.securesms.loki.utilities.get -import org.thoughtcrime.securesms.loki.utilities.insertOrUpdate -import org.session.libsession.utilities.recipients.Recipient -import org.session.libsession.utilities.TextSecurePreferences -import org.session.libsignal.database.LokiUserDatabaseProtocol - -class LokiUserDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), LokiUserDatabaseProtocol { - - companion object { - // Shared - private val displayName = "display_name" - // Display name cache - private val displayNameTable = "loki_user_display_name_database" - private val publicKey = "hex_encoded_public_key" - @JvmStatic val createDisplayNameTableCommand = "CREATE TABLE $displayNameTable ($publicKey TEXT PRIMARY KEY, $displayName TEXT);" - // Server display name cache - private val serverDisplayNameTable = "loki_user_server_display_name_database" - private val serverID = "server_id" - @JvmStatic val createServerDisplayNameTableCommand = "CREATE TABLE $serverDisplayNameTable ($publicKey TEXT, $serverID TEXT, $displayName TEXT, PRIMARY KEY ($publicKey, $serverID));" - } - - override fun getDisplayName(publicKey: String): String? { - if (publicKey == TextSecurePreferences.getLocalNumber(context)) { - return TextSecurePreferences.getProfileName(context) - } else { - val database = databaseHelper.readableDatabase - val result = database.get(displayNameTable, "${Companion.publicKey} = ?", arrayOf( publicKey )) { cursor -> - cursor.getString(cursor.getColumnIndexOrThrow(displayName)) - } ?: return null - val suffix = " (...${publicKey.substring(publicKey.count() - 8)})" - if (result.endsWith(suffix)) { - return result.substring(0..(result.count() - suffix.count())) - } else { - return result - } - } - } - - fun setDisplayName(publicKey: String, displayName: String) { - val database = databaseHelper.writableDatabase - val row = ContentValues(2) - row.put(Companion.publicKey, publicKey) - row.put(Companion.displayName, displayName) - database.insertOrUpdate(displayNameTable, row, "${Companion.publicKey} = ?", arrayOf( publicKey )) - Recipient.from(context, Address.fromSerialized(publicKey), false).notifyListeners() - } - - override fun getProfilePictureURL(publicKey: String): String? { - return if (publicKey == TextSecurePreferences.getLocalNumber(context)) { - TextSecurePreferences.getProfilePictureURL(context) - } else { - Recipient.from(context, Address.fromSerialized(publicKey), false).resolve().profileAvatar - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt b/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt index af1f8e7cda..e7fdbc509f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/sskenvironment/ProfileManager.kt @@ -10,42 +10,38 @@ import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob class ProfileManager : SSKEnvironment.ProfileManagerProtocol { - override fun setDisplayName(context: Context, recipient: Recipient, displayName: String?) { + override fun setNickname(context: Context, recipient: Recipient, nickname: String?) { val sessionID = recipient.address.serialize() - // New API val contactDatabase = DatabaseFactory.getSessionContactDatabase(context) var contact = contactDatabase.getContactWithSessionID(sessionID) if (contact == null) contact = Contact(sessionID) contact.threadID = DatabaseFactory.getStorage(context).getThreadId(recipient.address) - if (contact.nickname != displayName) { - contact.nickname = displayName + if (contact.nickname != nickname) { + contact.nickname = nickname + contactDatabase.setContact(contact) + } + } + + override fun setName(context: Context, recipient: Recipient, name: String) { + // New API + val sessionID = recipient.address.serialize() + val contactDatabase = DatabaseFactory.getSessionContactDatabase(context) + var contact = contactDatabase.getContactWithSessionID(sessionID) + if (contact == null) contact = Contact(sessionID) + contact.threadID = DatabaseFactory.getStorage(context).getThreadId(recipient.address) + if (contact.name != name) { + contact.name = name contactDatabase.setContact(contact) } // Old API - if (displayName == null) return - val database = DatabaseFactory.getLokiUserDatabase(context) - database.setDisplayName(sessionID, displayName) - } - - override fun setProfileName(context: Context, recipient: Recipient, profileName: String) { val database = DatabaseFactory.getRecipientDatabase(context) - database.setProfileName(recipient, profileName) + database.setProfileName(recipient, name) recipient.notifyListeners() - // New API - val sessionID = recipient.address.serialize() - val contactDatabase = DatabaseFactory.getSessionContactDatabase(context) - var contact = contactDatabase.getContactWithSessionID(sessionID) - if (contact == null) contact = Contact(sessionID) - contact.threadID = DatabaseFactory.getStorage(context).getThreadId(recipient.address) - if (contact.name != profileName) { - contact.name = profileName - contactDatabase.setContact(contact) - } } override fun setProfilePictureURL(context: Context, recipient: Recipient, profilePictureURL: String) { - ApplicationContext.getInstance(context).jobManager.add(RetrieveProfileAvatarJob(recipient, profilePictureURL)) - // New API + val job = RetrieveProfileAvatarJob(recipient, profilePictureURL) + ApplicationContext.getInstance(context).jobManager.add(job) val sessionID = recipient.address.serialize() val contactDatabase = DatabaseFactory.getSessionContactDatabase(context) var contact = contactDatabase.getContactWithSessionID(sessionID) @@ -58,8 +54,6 @@ class ProfileManager : SSKEnvironment.ProfileManagerProtocol { } override fun setProfileKey(context: Context, recipient: Recipient, profileKey: ByteArray) { - val database = DatabaseFactory.getRecipientDatabase(context) - database.setProfileKey(recipient, profileKey) // New API val sessionID = recipient.address.serialize() val contactDatabase = DatabaseFactory.getSessionContactDatabase(context) @@ -70,23 +64,13 @@ class ProfileManager : SSKEnvironment.ProfileManagerProtocol { contact.profilePictureEncryptionKey = profileKey contactDatabase.setContact(contact) } + // Old API + val database = DatabaseFactory.getRecipientDatabase(context) + database.setProfileKey(recipient, profileKey) } override fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode) { val database = DatabaseFactory.getRecipientDatabase(context) database.setUnidentifiedAccessMode(recipient, unidentifiedAccessMode) } - - override fun getDisplayName(context: Context, recipient: Recipient): String? { - val sessionID = recipient.address.serialize() - val contactDatabase = DatabaseFactory.getSessionContactDatabase(context) - var contact = contactDatabase.getContactWithSessionID(sessionID) - if (contact == null) { - contact = Contact(sessionID) - contact.threadID = DatabaseFactory.getStorage(context).getThreadId(recipient.address) - contact.name = DatabaseFactory.getLokiUserDatabase(context).getDisplayName(sessionID) ?: recipient.profileName ?: recipient.name - contactDatabase.setContact(contact) - } - return contact.displayName(Contact.contextForRecipient(recipient)) - } } \ No newline at end of file diff --git a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt index 1d9211846e..f9516b9604 100644 --- a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt @@ -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 diff --git a/libsession/src/main/java/org/session/libsession/messaging/mentions/MentionsManager.kt b/libsession/src/main/java/org/session/libsession/messaging/mentions/MentionsManager.kt index d4b1b35c68..e9eae0ba5a 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/mentions/MentionsManager.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/mentions/MentionsManager.kt @@ -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>() // 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 { - // Prepare + fun getMentionCandidates(query: String, threadID: Long, isOpenGroup: Boolean): List { 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 = 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 } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt index b52e3b6466..aa347084e0 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt @@ -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))) { diff --git a/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt b/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt index dbf0865bc9..90b71f578c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/utilities/UpdateMessageBuilder.kt @@ -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) diff --git a/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt b/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt index f674df6f88..0374b9c001 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/SSKEnvironment.kt @@ -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 { diff --git a/libsession/src/main/java/org/session/libsession/utilities/recipients/Recipient.java b/libsession/src/main/java/org/session/libsession/utilities/recipients/Recipient.java index ae291b4653..5055237969 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/recipients/Recipient.java +++ b/libsession/src/main/java/org/session/libsession/utilities/recipients/Recipient.java @@ -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; } diff --git a/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt b/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt index 4202c34741..2390b29768 100644 --- a/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt +++ b/libsignal/src/main/java/org/session/libsignal/database/LokiAPIDatabaseProtocol.kt @@ -19,24 +19,14 @@ interface LokiAPIDatabaseProtocol { fun setReceivedMessageHashValues(publicKey: String, newValue: Set) fun getAuthToken(server: String): String? fun setAuthToken(server: String, newValue: String?) - fun getLastMessageServerID(group: Long, server: String): Long? - fun setLastMessageServerID(group: Long, server: String, newValue: Long) - fun getLastDeletionServerID(group: Long, server: String): Long? - fun setLastDeletionServerID(group: Long, server: String, newValue: Long) fun setUserCount(group: Long, server: String, newValue: Int) + fun setUserCount(room: String, server: String, newValue: Int) fun getLastMessageServerID(room: String, server: String): Long? fun setLastMessageServerID(room: String, server: String, newValue: Long) fun getLastDeletionServerID(room: String, server: String): Long? fun setLastDeletionServerID(room: String, server: String, newValue: Long) - fun setUserCount(room: String, server: String, newValue: Int) - fun getSessionRequestSentTimestamp(publicKey: String): Long? - fun setSessionRequestSentTimestamp(publicKey: String, newValue: Long) - fun getSessionRequestProcessedTimestamp(publicKey: String): Long? - fun setSessionRequestProcessedTimestamp(publicKey: String, newValue: Long) fun getOpenGroupPublicKey(server: String): String? fun setOpenGroupPublicKey(server: String, newValue: String) - fun setOpenGroupProfilePictureURL(group: Long, server: String, newValue: String) - fun getOpenGroupProfilePictureURL(group: Long, server: String): String? fun getLastSnodePoolRefreshDate(): Date? fun setLastSnodePoolRefreshDate(newValue: Date) fun getUserX25519KeyPair(): ECKeyPair diff --git a/libsignal/src/main/java/org/session/libsignal/database/LokiMessageDatabaseProtocol.kt b/libsignal/src/main/java/org/session/libsignal/database/LokiMessageDatabaseProtocol.kt index e7c740f6b4..633471bf5e 100644 --- a/libsignal/src/main/java/org/session/libsignal/database/LokiMessageDatabaseProtocol.kt +++ b/libsignal/src/main/java/org/session/libsignal/database/LokiMessageDatabaseProtocol.kt @@ -2,6 +2,5 @@ package org.session.libsignal.database interface LokiMessageDatabaseProtocol { - fun getQuoteServerID(quoteID: Long, quoteePublicKey: String): Long? fun setServerID(messageID: Long, serverID: Long, isSms: Boolean) } diff --git a/libsignal/src/main/java/org/session/libsignal/database/LokiUserDatabaseProtocol.kt b/libsignal/src/main/java/org/session/libsignal/database/LokiUserDatabaseProtocol.kt deleted file mode 100644 index e22ecb1059..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/database/LokiUserDatabaseProtocol.kt +++ /dev/null @@ -1,7 +0,0 @@ -package org.session.libsignal.database - -interface LokiUserDatabaseProtocol { - - fun getDisplayName(publicKey: String): String? - fun getProfilePictureURL(publicKey: String): String? -}