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

85 lines
2.5 KiB
Kotlin
Raw Normal View History

2021-04-29 06:59:04 +02:00
package org.session.libsession.messaging.contacts
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import org.session.libsession.utilities.recipients.Recipient
2021-04-29 06:59:04 +02:00
@Parcelize
class Contact(
val sessionID: String,
2021-05-21 07:36:16 +02:00
/**
* The URL from which to fetch the contact's profile picture.
*/
var profilePictureURL: String? = null,
2021-05-21 07:36:16 +02:00
/**
* The file name of the contact's profile picture on local storage.
*/
var profilePictureFileName: String? = null,
2021-05-21 07:36:16 +02:00
/**
* The key with which the profile picture is encrypted.
*/
var profilePictureEncryptionKey: ByteArray? = null,
2021-05-21 07:36:16 +02:00
/**
* The ID of the thread associated with this contact.
*/
var threadID: Long? = null,
2021-05-21 07:36:16 +02:00
/**
* 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,
2021-05-21 07:36:16 +02:00
/**
* The contact's nickname, if the user set one.
*/
var nickname: String? = null,
): Parcelable {
constructor(id: String): this(sessionID = id)
2021-05-21 07:36:16 +02:00
/**
* The name to display in the UI. For local use only.
*/
2021-04-29 06:59:04 +02:00
fun displayName(context: ContactContext): String? {
2021-05-24 05:32:16 +02:00
nickname?.let { return it }
2021-05-21 07:36:16 +02:00
return when (context) {
2021-05-24 05:32:16 +02:00
ContactContext.REGULAR -> name
2021-05-21 07:36:16 +02:00
ContactContext.OPEN_GROUP -> {
2021-04-29 06:59:04 +02:00
// 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.
2021-05-24 05:32:16 +02:00
name?.let {
Add Session Id blinding (#862) * feat: Add Session Id blinding Including modified version of lazysodium-android to expose missing libsodium functions, we could build from a fork which we still need to setup. * Add v4 onion request handling * Update SOGS signature construction * Fix SOGS signature construction * Update onion request * Update signature data * Keep path prefixes for v4 endpoints * Update SOGS signature message * Rename to remove api version suffix * Update onion response parsing * Refactor file download paths * Implement request batching * Refactor batch response handling * Handle batch endpoint responses * Update batch endpoint responses * Update attachment download handling * Handle file downloads * Handle inbox messages * Fix issue with file downloads * Preserve image bytearray encoding * Refactor * Open group message requests * Check id blinding in user detail bottom sheet rather * Message validation refactor * Cache last inbox/outbox server ids * Update message encryption/decryption * Refactor * Refactor * Bypass user details bottom sheet in open groups for blinded session ids * Fix capabilities call auth * Refactor * Revert default server details * Update sodium dependency to forked repo * Fix attachment upload * Revert "Update sodium dependency to forked repo" This reverts commit c7db9529f900d09585ab94e440f6645faa88544e. * Add signed sodium lib * Update contact id truncation and mention logic * Open group inbox messaging fix * Refactor * Update blinded id check * Fix open group message sends * Fix crash on open group direct message send * Direct message refactor * Direct message encrypt/decrypt fixes * Use updated curve25519 version * Updated lazysodium dependency * Update encryption/decryption calls * Handle direct message parse errors * Minor refactor * Existing chat refactor * Update encryption & decryption parameters * Fix authenticated ciphertext size * Set direct message sync target * Update direct message thread lookup * Add blinded id mapping table * Add blinded id mapping table * Update threads after sends * Update open group message timestamp handling * Filter unblinded contacts * Format blinded id mentions * Add message deleted field * Hide open group inbox id * Update message request response handling * Update message request response sender handling * Fix mentions of blinded ids * Handle open group poll failure * fix: add log for failed open group onion request, add decoding body for blinding required error at destination * fix: change the error check * Persist group members * Reschedule polling after capabilities update * Retry on other exceptions * Minor refactor * Open group profile fix * Group member db schema update * Fix ban request key * Update ban response type * Ban endpoint updates * Ban endpoint updates * Delete messages Co-authored-by: charles <charles@oxen.io> Co-authored-by: jubb <hjubb@users.noreply.github.com>
2022-08-10 10:17:48 +02:00
return "$name (${sessionID.take(4)}...${sessionID.takeLast(4)})"
2021-04-29 06:59:04 +02:00
}
return null
}
}
}
2021-05-21 07:36:16 +02:00
// endregion
2021-04-29 06:59:04 +02:00
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 {
2021-05-21 07:36:16 +02:00
return this.sessionID == (other as? Contact)?.sessionID
2021-04-29 06:59:04 +02:00
}
override fun hashCode(): Int {
return sessionID.hashCode()
}
override fun toString(): String {
return nickname ?: name ?: sessionID
}
companion object {
2021-05-21 07:36:16 +02:00
2021-04-29 06:59:04 +02:00
fun contextForRecipient(recipient: Recipient): ContactContext {
2021-06-18 07:54:24 +02:00
return if (recipient.isOpenGroupRecipient) ContactContext.OPEN_GROUP else ContactContext.REGULAR
2021-04-29 06:59:04 +02:00
}
}
}