session-android/libsession/src/main/java/org/session/libsession/messaging/contacts/Contact.kt

85 lines
2.5 KiB
Kotlin

package org.session.libsession.messaging.contacts
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import org.session.libsession.utilities.recipients.Recipient
@Parcelize
class Contact(
val sessionID: String,
/**
* 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.
*/
var profilePictureFileName: String? = null,
/**
* The key with which the profile picture is encrypted.
*/
var profilePictureEncryptionKey: ByteArray? = null,
/**
* The ID of the thread associated with this contact.
*/
var threadID: Long? = null,
/**
* 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.
*/
var nickname: String? = null,
): Parcelable {
constructor(id: String): this(sessionID = id)
/**
* The name to display in the UI. For local use only.
*/
fun displayName(context: ContactContext): String? {
nickname?.let { return it }
return when (context) {
ContactContext.REGULAR -> 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.
name?.let {
return "$name (${sessionID.take(4)}...${sessionID.takeLast(4)})"
}
return null
}
}
}
// endregion
enum class ContactContext {
REGULAR, OPEN_GROUP
}
fun isValid(): Boolean {
if (profilePictureURL != null) { return profilePictureEncryptionKey != null }
if (profilePictureEncryptionKey != null) { return profilePictureURL != null}
return true
}
override fun equals(other: Any?): Boolean {
return this.sessionID == (other as? Contact)?.sessionID
}
override fun hashCode(): Int {
return sessionID.hashCode()
}
override fun toString(): String {
return nickname ?: name ?: sessionID
}
companion object {
fun contextForRecipient(recipient: Recipient): ContactContext {
return if (recipient.isOpenGroupRecipient) ContactContext.OPEN_GROUP else ContactContext.REGULAR
}
}
}