refactor model

This commit is contained in:
callebtc
2025-07-22 18:03:56 +02:00
parent 1e60c5607c
commit d13a9620b1
5 changed files with 125 additions and 108 deletions
@@ -3,6 +3,7 @@ package com.bitchat.android.mesh
import android.util.Log import android.util.Log
import com.bitchat.android.model.BitchatMessage import com.bitchat.android.model.BitchatMessage
import com.bitchat.android.model.DeliveryAck import com.bitchat.android.model.DeliveryAck
import com.bitchat.android.model.NoiseIdentityAnnouncement
import com.bitchat.android.model.ReadReceipt import com.bitchat.android.model.ReadReceipt
import com.bitchat.android.model.RoutedPacket import com.bitchat.android.model.RoutedPacket
import com.bitchat.android.protocol.BitchatPacket import com.bitchat.android.protocol.BitchatPacket
@@ -473,47 +474,6 @@ class MessageHandler(private val myPeerID: String) {
} }
} }
/**
* Noise Identity Announcement data class (compatible with iOS version)
*/
data class NoiseIdentityAnnouncement(
val peerID: String,
val nickname: String,
val publicKey: ByteArray,
val timestamp: Date,
val signature: ByteArray,
val fingerprint: String?,
val previousPeerID: String? = null
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as NoiseIdentityAnnouncement
if (peerID != other.peerID) return false
if (nickname != other.nickname) return false
if (!publicKey.contentEquals(other.publicKey)) return false
if (timestamp != other.timestamp) return false
if (!signature.contentEquals(other.signature)) return false
if (fingerprint != other.fingerprint) return false
if (previousPeerID != other.previousPeerID) return false
return true
}
override fun hashCode(): Int {
var result = peerID.hashCode()
result = 31 * result + nickname.hashCode()
result = 31 * result + publicKey.contentHashCode()
result = 31 * result + timestamp.hashCode()
result = 31 * result + signature.contentHashCode()
result = 31 * result + (fingerprint?.hashCode() ?: 0)
result = 31 * result + (previousPeerID?.hashCode() ?: 0)
return result
}
}
/** /**
* Delegate interface for message handler callbacks * Delegate interface for message handler callbacks
*/ */
@@ -342,71 +342,4 @@ data class BitchatMessage(
} }
} }
/**
* Delivery acknowledgment structure - exact same as iOS version
*/
@Parcelize
data class DeliveryAck(
val originalMessageID: String,
val ackID: String = UUID.randomUUID().toString(),
val recipientID: String,
val recipientNickname: String,
val timestamp: Date = Date(),
val hopCount: UInt
) : Parcelable {
private val gson = GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
.create()
fun encode(): ByteArray? {
return try {
gson.toJson(this).toByteArray(Charsets.UTF_8)
} catch (e: Exception) {
null
}
}
companion object {
fun decode(data: ByteArray): DeliveryAck? {
return try {
val json = String(data, Charsets.UTF_8)
com.google.gson.Gson().fromJson(json, DeliveryAck::class.java)
} catch (e: Exception) {
null
}
}
}
}
/**
* Read receipt structure - exact same as iOS version
*/
@Parcelize
data class ReadReceipt(
val originalMessageID: String,
val receiptID: String = UUID.randomUUID().toString(),
val readerID: String,
val readerNickname: String,
val timestamp: Date = Date()
) : Parcelable {
fun encode(): ByteArray? {
return try {
com.google.gson.Gson().toJson(this).toByteArray(Charsets.UTF_8)
} catch (e: Exception) {
null
}
}
companion object {
fun decode(data: ByteArray): ReadReceipt? {
return try {
val json = String(data, Charsets.UTF_8)
com.google.gson.Gson().fromJson(json, ReadReceipt::class.java)
} catch (e: Exception) {
null
}
}
}
}
@@ -0,0 +1,43 @@
package com.bitchat.android.model
import android.os.Parcelable
import com.google.gson.GsonBuilder
import kotlinx.parcelize.Parcelize
import java.util.*
/**
* Delivery acknowledgment structure - exact same as iOS version
*/
@Parcelize
data class DeliveryAck(
val originalMessageID: String,
val ackID: String = UUID.randomUUID().toString(),
val recipientID: String,
val recipientNickname: String,
val timestamp: Date = Date(),
val hopCount: UInt
) : Parcelable {
private val gson = GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
.create()
fun encode(): ByteArray? {
return try {
gson.toJson(this).toByteArray(Charsets.UTF_8)
} catch (e: Exception) {
null
}
}
companion object {
fun decode(data: ByteArray): DeliveryAck? {
return try {
val json = String(data, Charsets.UTF_8)
com.google.gson.Gson().fromJson(json, DeliveryAck::class.java)
} catch (e: Exception) {
null
}
}
}
}
@@ -0,0 +1,44 @@
package com.bitchat.android.model
import java.util.*
/**
* Noise Identity Announcement data class (compatible with iOS version)
*/
data class NoiseIdentityAnnouncement(
val peerID: String,
val nickname: String,
val publicKey: ByteArray,
val timestamp: Date,
val signature: ByteArray,
val fingerprint: String?,
val previousPeerID: String? = null
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as NoiseIdentityAnnouncement
if (peerID != other.peerID) return false
if (nickname != other.nickname) return false
if (!publicKey.contentEquals(other.publicKey)) return false
if (timestamp != other.timestamp) return false
if (!signature.contentEquals(other.signature)) return false
if (fingerprint != other.fingerprint) return false
if (previousPeerID != other.previousPeerID) return false
return true
}
override fun hashCode(): Int {
var result = peerID.hashCode()
result = 31 * result + nickname.hashCode()
result = 31 * result + publicKey.contentHashCode()
result = 31 * result + timestamp.hashCode()
result = 31 * result + signature.contentHashCode()
result = 31 * result + (fingerprint?.hashCode() ?: 0)
result = 31 * result + (previousPeerID?.hashCode() ?: 0)
return result
}
}
@@ -0,0 +1,37 @@
package com.bitchat.android.model
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import java.util.*
/**
* Read receipt structure - exact same as iOS version
*/
@Parcelize
data class ReadReceipt(
val originalMessageID: String,
val receiptID: String = UUID.randomUUID().toString(),
val readerID: String,
val readerNickname: String,
val timestamp: Date = Date()
) : Parcelable {
fun encode(): ByteArray? {
return try {
com.google.gson.Gson().toJson(this).toByteArray(Charsets.UTF_8)
} catch (e: Exception) {
null
}
}
companion object {
fun decode(data: ByteArray): ReadReceipt? {
return try {
val json = String(data, Charsets.UTF_8)
com.google.gson.Gson().fromJson(json, ReadReceipt::class.java)
} catch (e: Exception) {
null
}
}
}
}