This commit is contained in:
Niels Andriesse 2021-05-21 15:36:16 +10:00
parent e0c1456af4
commit c0f894e1b2
8 changed files with 51 additions and 66 deletions

View File

@ -491,15 +491,6 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
return threadId
}
override fun getDisplayName(publicKey: String): String? {
val contact = DatabaseFactory.getSessionContactDatabase(context).getContactWithSessionID(publicKey)
contact?.let {
val contactContext = Contact.contextForRecipient(Recipient.from(context, fromSerialized(publicKey), false))
return it.displayName(contactContext)
}
return DatabaseFactory.getLokiUserDatabase(context).getDisplayName(publicKey)
}
override fun getProfilePictureURL(publicKey: String): String? {
return DatabaseFactory.getLokiUserDatabase(context).getProfilePictureURL(publicKey)
}

View File

@ -10,6 +10,7 @@ import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
import org.thoughtcrime.securesms.loki.utilities.*
class SessionContactDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
companion object {
private const val sessionContactTable = "session_contact_database"
const val sessionID = "session_id"
@ -22,19 +23,19 @@ class SessionContactDatabase(context: Context, helper: SQLCipherOpenHelper) : Da
const val isTrusted = "is_trusted"
@JvmStatic val createSessionContactTableCommand =
"CREATE TABLE $sessionContactTable " +
"($sessionID STRING PRIMARY KEY, " +
"$name TEXT DEFAULT NULL, " +
"$nickname TEXT DEFAULT NULL, " +
"$profilePictureURL TEXT DEFAULT NULL, " +
"$profilePictureFileName TEXT DEFAULT NULL, " +
"$profilePictureEncryptionKey BLOB DEFAULT NULL, " +
"$threadID INTEGER DEFAULT -1, " +
"$isTrusted INTEGER DEFAULT 0);"
"($sessionID STRING PRIMARY KEY, " +
"$name TEXT DEFAULT NULL, " +
"$nickname TEXT DEFAULT NULL, " +
"$profilePictureURL TEXT DEFAULT NULL, " +
"$profilePictureFileName TEXT DEFAULT NULL, " +
"$profilePictureEncryptionKey BLOB DEFAULT NULL, " +
"$threadID INTEGER DEFAULT -1, " +
"$isTrusted INTEGER DEFAULT 0);"
}
fun getContactWithSessionID(sessionID: String): Contact? {
val database = databaseHelper.readableDatabase
return database.get(sessionContactTable, "${SessionContactDatabase.sessionID} = ?", arrayOf(sessionID)) { cursor ->
return database.get(sessionContactTable, "${SessionContactDatabase.sessionID} = ?", arrayOf( sessionID )) { cursor ->
contactFromCursor(cursor)
}
}
@ -59,7 +60,7 @@ class SessionContactDatabase(context: Context, helper: SQLCipherOpenHelper) : Da
}
contentValues.put(threadID, threadID)
contentValues.put(isTrusted, if (contact.isTrusted) 1 else 0)
database.insertOrUpdate(sessionContactTable, contentValues, "$sessionID = ?", arrayOf(contact.sessionID))
database.insertOrUpdate(sessionContactTable, contentValues, "$sessionID = ?", arrayOf( contact.sessionID ))
notifyConversationListListeners()
}

View File

@ -19,7 +19,7 @@ import org.session.libsession.utilities.recipients.Recipient
import org.session.libsession.utilities.SSKEnvironment
import org.thoughtcrime.securesms.mms.GlideApp
public class UserDetailsBottomSheet : BottomSheetDialogFragment() {
class UserDetailsBottomSheet : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_user_details_bottom_sheet, container, false)
@ -75,7 +75,7 @@ public class UserDetailsBottomSheet : BottomSheetDialogFragment() {
nameTextViewContainer.visibility = View.VISIBLE
nameEditContainer.visibility = View.INVISIBLE
var newNickName: String? = null
if (!nameEditText.text.isEmpty()) {
if (nameEditText.text.isNotEmpty()) {
newNickName = nameEditText.text.toString()
}
SSKEnvironment.shared.profileManager.setDisplayName(requireContext(), recipient, newNickName)

View File

@ -55,8 +55,7 @@ class UserView : LinearLayout {
} else {
val contact = DatabaseFactory.getSessionContactDatabase(context).getContactWithSessionID(publicKey)
contact?.let {
val contactContext = Contact.contextForRecipient(user)
return it.displayName(contactContext)
return it.displayName(Contact.ContactContext.REGULAR)
}
val result = DatabaseFactory.getLokiUserDatabase(context).getDisplayName(publicKey)
return result ?: publicKey

View File

@ -8,7 +8,7 @@ import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob
class ProfileManager: SSKEnvironment.ProfileManagerProtocol {
class ProfileManager : SSKEnvironment.ProfileManagerProtocol {
override fun setDisplayName(context: Context, recipient: Recipient, displayName: String?) {
val sessionID = recipient.address.serialize()

View File

@ -97,26 +97,6 @@
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/cancelButtonContainer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:clickable="true"
android:focusable="true"
android:visibility="gone">
<ImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@drawable/ic_baseline_clear_24"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp" />
</LinearLayout>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>

View File

@ -3,28 +3,44 @@ package org.session.libsession.messaging.contacts
import org.session.libsession.utilities.recipients.Recipient
class Contact(val sessionID: String) {
// The URL from which to fetch the contact's profile picture.
/**
* The URL from which to fetch the contact's profile picture.
*/
var profilePictureURL: String? = null
// The file name of the contact's profile picture on local storage.
/**
* The file name of the contact's profile picture on local storage.
*/
var profilePictureFileName: String? = null
// The key with which the profile picture is encrypted.
/**
* The key with which the profile picture is encrypted.
*/
var profilePictureEncryptionKey: ByteArray? = null
// The ID of the thread associated with this contact.
/**
* The ID of the thread associated with this contact.
*/
var threadID: Long? = null
// This flag is used to determine whether we should auto-download files sent by this contact.
/**
* This flag is used to determine whether we should auto-download files sent by this contact.
*/
var isTrusted = false
// region: Name
// The name of the contact. Use this whenever you need the "real", underlying name of a user (e.g. when sending a message).
// region Name
/**
* The name of the contact. Use this whenever you need the "real", underlying name of a user (e.g. when sending a message).
*/
var name: String? = null
// The contact's nickname, if the user set one.
/**
* The contact's nickname, if the user set one.
*/
var nickname: String? = null
// The name to display in the UI. For local use only.
/**
* The name to display in the UI. For local use only.
*/
fun displayName(context: ContactContext): String? {
this.nickname?.let { return it }
return when {
context == ContactContext.REGULAR -> this.name
context == ContactContext.OPEN_GROUP -> {
return when (context) {
ContactContext.REGULAR -> this.name
ContactContext.OPEN_GROUP -> {
// In open groups, where it's more likely that multiple users have the same name,
// we display a bit of the Session ID after a user's display name for added context.
this.name?.let {
@ -32,10 +48,9 @@ class Contact(val sessionID: String) {
}
return null
}
else -> throw Exception("Unknown contact context!")
}
}
//end region
// endregion
enum class ContactContext {
REGULAR, OPEN_GROUP
@ -48,11 +63,7 @@ class Contact(val sessionID: String) {
}
override fun equals(other: Any?): Boolean {
return if (other is Contact) {
other.sessionID == this.sessionID
} else {
false
}
return this.sessionID == (other as? Contact)?.sessionID
}
override fun hashCode(): Int {
@ -64,9 +75,13 @@ class Contact(val sessionID: String) {
}
companion object {
fun contextForRecipient(recipient: Recipient): ContactContext {
return if (recipient.isOpenGroupRecipient) { ContactContext.OPEN_GROUP }
else { ContactContext.REGULAR }
return if (recipient.isOpenGroupRecipient) {
ContactContext.OPEN_GROUP
} else {
ContactContext.REGULAR
}
}
}
}

View File

@ -30,7 +30,6 @@ class SSKEnvironment(
}
fun setDisplayName(context: Context, recipient: Recipient, displayName: String?) // Client-side Nickname
fun setProfileName(context: Context, recipient: Recipient, profileName: 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)