mirror of
https://github.com/oxen-io/session-android.git
synced 2025-04-03 03:35:39 +00:00
store server hash value for incoming messages
This commit is contained in:
parent
c4a3463416
commit
ecc881bc7c
@ -1138,7 +1138,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
|
|||||||
|
|
||||||
private fun deleteLocally(message: MessageRecord) {
|
private fun deleteLocally(message: MessageRecord) {
|
||||||
buildUsendRequest(message)?.let { unsendRequest ->
|
buildUsendRequest(message)?.let { unsendRequest ->
|
||||||
TextSecurePreferences.getLocalNumber(this)?.let {
|
TextSecurePreferences.getLocalNumber(this@ConversationActivityV2)?.let {
|
||||||
MessageSender.send(unsendRequest, Address.fromSerialized(it))
|
MessageSender.send(unsendRequest, Address.fromSerialized(it))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,6 +148,11 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
|
|||||||
if (openGroupID.isNullOrEmpty() && threadID != null && threadID >= 0) {
|
if (openGroupID.isNullOrEmpty() && threadID != null && threadID >= 0) {
|
||||||
JobQueue.shared.add(TrimThreadJob(threadID))
|
JobQueue.shared.add(TrimThreadJob(threadID))
|
||||||
}
|
}
|
||||||
|
message.serverHash?.let { serverHash ->
|
||||||
|
messageID?.let { id ->
|
||||||
|
DatabaseFactory.getLokiMessageDatabase(context).setMessageServerHash(id, serverHash)
|
||||||
|
}
|
||||||
|
}
|
||||||
return messageID
|
return messageID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,9 +49,9 @@ class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Wor
|
|||||||
// DMs
|
// DMs
|
||||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
||||||
val dmsPromise = SnodeAPI.getMessages(userPublicKey).map { envelopes ->
|
val dmsPromise = SnodeAPI.getMessages(userPublicKey).map { envelopes ->
|
||||||
envelopes.map { envelope ->
|
envelopes.map { (envelope, serverHash) ->
|
||||||
// FIXME: Using a job here seems like a bad idea...
|
// FIXME: Using a job here seems like a bad idea...
|
||||||
MessageReceiveJob(envelope.toByteArray()).executeAsync()
|
MessageReceiveJob(envelope.toByteArray(), serverHash).executeAsync()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
promises.addAll(dmsPromise.get())
|
promises.addAll(dmsPromise.get())
|
||||||
|
@ -7,7 +7,7 @@ import org.session.libsession.messaging.sending_receiving.handle
|
|||||||
import org.session.libsession.messaging.utilities.Data
|
import org.session.libsession.messaging.utilities.Data
|
||||||
import org.session.libsignal.utilities.Log
|
import org.session.libsignal.utilities.Log
|
||||||
|
|
||||||
class MessageReceiveJob(val data: ByteArray, val openGroupMessageServerID: Long? = null, val openGroupID: String? = null) : Job {
|
class MessageReceiveJob(val data: ByteArray, val serverHash: String? = null, val openGroupMessageServerID: Long? = null, val openGroupID: String? = null) : Job {
|
||||||
override var delegate: JobDelegate? = null
|
override var delegate: JobDelegate? = null
|
||||||
override var id: String? = null
|
override var id: String? = null
|
||||||
override var failureCount: Int = 0
|
override var failureCount: Int = 0
|
||||||
@ -21,6 +21,7 @@ class MessageReceiveJob(val data: ByteArray, val openGroupMessageServerID: Long?
|
|||||||
|
|
||||||
// Keys used for database storage
|
// Keys used for database storage
|
||||||
private val DATA_KEY = "data"
|
private val DATA_KEY = "data"
|
||||||
|
private val SERVER_HASH_KEY = "serverHash"
|
||||||
private val OPEN_GROUP_MESSAGE_SERVER_ID_KEY = "openGroupMessageServerID"
|
private val OPEN_GROUP_MESSAGE_SERVER_ID_KEY = "openGroupMessageServerID"
|
||||||
private val OPEN_GROUP_ID_KEY = "open_group_id"
|
private val OPEN_GROUP_ID_KEY = "open_group_id"
|
||||||
}
|
}
|
||||||
@ -34,6 +35,7 @@ class MessageReceiveJob(val data: ByteArray, val openGroupMessageServerID: Long?
|
|||||||
try {
|
try {
|
||||||
val isRetry: Boolean = failureCount != 0
|
val isRetry: Boolean = failureCount != 0
|
||||||
val (message, proto) = MessageReceiver.parse(this.data, this.openGroupMessageServerID)
|
val (message, proto) = MessageReceiver.parse(this.data, this.openGroupMessageServerID)
|
||||||
|
message.serverHash = serverHash
|
||||||
synchronized(RECEIVE_LOCK) { // FIXME: Do we need this?
|
synchronized(RECEIVE_LOCK) { // FIXME: Do we need this?
|
||||||
MessageReceiver.handle(message, proto, this.openGroupID)
|
MessageReceiver.handle(message, proto, this.openGroupID)
|
||||||
}
|
}
|
||||||
@ -67,6 +69,7 @@ class MessageReceiveJob(val data: ByteArray, val openGroupMessageServerID: Long?
|
|||||||
|
|
||||||
override fun serialize(): Data {
|
override fun serialize(): Data {
|
||||||
val builder = Data.Builder().putByteArray(DATA_KEY, data)
|
val builder = Data.Builder().putByteArray(DATA_KEY, data)
|
||||||
|
serverHash?.let { builder.putString(SERVER_HASH_KEY, it) }
|
||||||
openGroupMessageServerID?.let { builder.putLong(OPEN_GROUP_MESSAGE_SERVER_ID_KEY, it) }
|
openGroupMessageServerID?.let { builder.putLong(OPEN_GROUP_MESSAGE_SERVER_ID_KEY, it) }
|
||||||
openGroupID?.let { builder.putString(OPEN_GROUP_ID_KEY, it) }
|
openGroupID?.let { builder.putString(OPEN_GROUP_ID_KEY, it) }
|
||||||
return builder.build();
|
return builder.build();
|
||||||
@ -81,6 +84,7 @@ class MessageReceiveJob(val data: ByteArray, val openGroupMessageServerID: Long?
|
|||||||
override fun create(data: Data): MessageReceiveJob {
|
override fun create(data: Data): MessageReceiveJob {
|
||||||
return MessageReceiveJob(
|
return MessageReceiveJob(
|
||||||
data.getByteArray(DATA_KEY),
|
data.getByteArray(DATA_KEY),
|
||||||
|
data.getString(SERVER_HASH_KEY),
|
||||||
data.getLong(OPEN_GROUP_MESSAGE_SERVER_ID_KEY),
|
data.getLong(OPEN_GROUP_MESSAGE_SERVER_ID_KEY),
|
||||||
data.getString(OPEN_GROUP_ID_KEY)
|
data.getString(OPEN_GROUP_ID_KEY)
|
||||||
)
|
)
|
||||||
|
@ -102,8 +102,8 @@ class ClosedGroupPollerV2 {
|
|||||||
}
|
}
|
||||||
promise.success { envelopes ->
|
promise.success { envelopes ->
|
||||||
if (!isPolling(groupPublicKey)) { return@success }
|
if (!isPolling(groupPublicKey)) { return@success }
|
||||||
envelopes.forEach { envelope ->
|
envelopes.forEach { (envelope, serverHash) ->
|
||||||
val job = MessageReceiveJob(envelope.toByteArray())
|
val job = MessageReceiveJob(envelope.toByteArray(), serverHash)
|
||||||
JobQueue.shared.add(job)
|
JobQueue.shared.add(job)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,8 +91,8 @@ class Poller {
|
|||||||
task { Unit } // The long polling connection has been canceled; don't recurse
|
task { Unit } // The long polling connection has been canceled; don't recurse
|
||||||
} else {
|
} else {
|
||||||
val messages = SnodeAPI.parseRawMessagesResponse(rawResponse, snode, userPublicKey)
|
val messages = SnodeAPI.parseRawMessagesResponse(rawResponse, snode, userPublicKey)
|
||||||
messages.forEach { envelope ->
|
messages.forEach { (envelope, serverHash) ->
|
||||||
val job = MessageReceiveJob(envelope.toByteArray())
|
val job = MessageReceiveJob(envelope.toByteArray(), serverHash)
|
||||||
JobQueue.shared.add(job)
|
JobQueue.shared.add(job)
|
||||||
}
|
}
|
||||||
poll(snode, deferred)
|
poll(snode, deferred)
|
||||||
|
@ -426,7 +426,7 @@ object SnodeAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseRawMessagesResponse(rawResponse: RawResponse, snode: Snode, publicKey: String): List<SignalServiceProtos.Envelope> {
|
fun parseRawMessagesResponse(rawResponse: RawResponse, snode: Snode, publicKey: String): List<Pair<SignalServiceProtos.Envelope, String?>> {
|
||||||
val messages = rawResponse["messages"] as? List<*>
|
val messages = rawResponse["messages"] as? List<*>
|
||||||
return if (messages != null) {
|
return if (messages != null) {
|
||||||
updateLastMessageHashValueIfPossible(snode, publicKey, messages)
|
updateLastMessageHashValueIfPossible(snode, publicKey, messages)
|
||||||
@ -465,14 +465,14 @@ object SnodeAPI {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun parseEnvelopes(rawMessages: List<*>): List<SignalServiceProtos.Envelope> {
|
private fun parseEnvelopes(rawMessages: List<*>): List<Pair<SignalServiceProtos.Envelope, String?>> {
|
||||||
return rawMessages.mapNotNull { rawMessage ->
|
return rawMessages.mapNotNull { rawMessage ->
|
||||||
val rawMessageAsJSON = rawMessage as? Map<*, *>
|
val rawMessageAsJSON = rawMessage as? Map<*, *>
|
||||||
val base64EncodedData = rawMessageAsJSON?.get("data") as? String
|
val base64EncodedData = rawMessageAsJSON?.get("data") as? String
|
||||||
val data = base64EncodedData?.let { Base64.decode(it) }
|
val data = base64EncodedData?.let { Base64.decode(it) }
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
try {
|
try {
|
||||||
MessageWrapper.unwrap(data)
|
Pair(MessageWrapper.unwrap(data), rawMessageAsJSON.get("hash") as? String)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.d("Loki", "Failed to unwrap data for message: ${rawMessage.prettifiedDescription()}.")
|
Log.d("Loki", "Failed to unwrap data for message: ${rawMessage.prettifiedDescription()}.")
|
||||||
null
|
null
|
||||||
@ -568,5 +568,5 @@ object SnodeAPI {
|
|||||||
|
|
||||||
// Type Aliases
|
// Type Aliases
|
||||||
typealias RawResponse = Map<*, *>
|
typealias RawResponse = Map<*, *>
|
||||||
typealias MessageListPromise = Promise<List<SignalServiceProtos.Envelope>, Exception>
|
typealias MessageListPromise = Promise<List<Pair<SignalServiceProtos.Envelope, String?>>, Exception>
|
||||||
typealias RawResponsePromise = Promise<RawResponse, Exception>
|
typealias RawResponsePromise = Promise<RawResponse, Exception>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user