session-android/libsession/src/main/java/org/session/libsession/messaging/jobs/NotifyPNServerJob.kt

96 lines
3.4 KiB
Kotlin
Raw Normal View History

2020-12-02 06:39:02 +01:00
package org.session.libsession.messaging.jobs
2020-11-25 02:06:41 +01:00
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
2020-12-02 06:39:02 +01:00
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.RequestBody
import org.session.libsession.messaging.jobs.Job.Companion.MAX_BUFFER_SIZE
2020-12-02 06:39:02 +01:00
2023-06-20 15:04:10 +02:00
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager.server
2021-05-13 02:31:06 +02:00
import org.session.libsession.messaging.utilities.Data
2020-12-02 06:39:02 +01:00
import org.session.libsession.snode.SnodeMessage
import org.session.libsession.snode.OnionRequestAPI
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
import org.session.libsession.snode.Version
2020-12-02 06:39:02 +01:00
2021-05-18 01:12:33 +02:00
import org.session.libsignal.utilities.Log
2021-02-01 01:35:53 +01:00
import org.session.libsignal.utilities.JsonUtil
2021-05-18 01:34:45 +02:00
import org.session.libsignal.utilities.retryIfNeeded
2020-12-02 06:39:02 +01:00
class NotifyPNServerJob(val message: SnodeMessage) : Job {
override var delegate: JobDelegate? = null
override var id: String? = null
override var failureCount: Int = 0
override val maxFailureCount: Int = 20
companion object {
2021-01-28 05:24:27 +01:00
val KEY: String = "NotifyPNServerJob"
2021-04-26 02:58:48 +02:00
// Keys used for database storage
2021-05-12 08:17:25 +02:00
private val MESSAGE_KEY = "message"
2020-12-02 06:39:02 +01:00
}
override fun execute(dispatcherName: String) {
2020-12-02 06:39:02 +01:00
val parameters = mapOf( "data" to message.data, "send_to" to message.recipient )
val url = "${server}/notify"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
2023-06-16 03:08:33 +02:00
val request = Request.Builder().url(url).post(body).build()
2020-12-02 06:39:02 +01:00
retryIfNeeded(4) {
2023-06-16 03:08:33 +02:00
OnionRequestAPI.sendOnionRequest(
request,
server,
2023-06-20 15:04:10 +02:00
LegacyGroupsPushManager.serverPublicKey,
2023-06-16 03:08:33 +02:00
Version.V2
) success { response ->
when (response.info["code"]) {
null, 0 -> Log.d("Loki", "Couldn't notify PN server due to error: ${response.info["message"]}.")
2020-12-02 06:39:02 +01:00
}
2023-06-16 03:08:33 +02:00
} fail { exception ->
2021-05-12 08:17:25 +02:00
Log.d("Loki", "Couldn't notify PN server due to error: $exception.")
2020-12-02 06:39:02 +01:00
}
}.success {
handleSuccess(dispatcherName)
2020-12-02 06:39:02 +01:00
}. fail {
handleFailure(dispatcherName, it)
2020-12-02 06:39:02 +01:00
}
}
private fun handleSuccess(dispatcherName: String) {
delegate?.handleJobSucceeded(this, dispatcherName)
2020-12-02 06:39:02 +01:00
}
private fun handleFailure(dispatcherName: String, error: Exception) {
delegate?.handleJobFailed(this, dispatcherName, error)
2020-12-02 06:39:02 +01:00
}
2021-01-22 05:19:41 +01:00
override fun serialize(): Data {
val kryo = Kryo()
kryo.isRegistrationRequired = false
val serializedMessage = ByteArray(4096)
val output = Output(serializedMessage, MAX_BUFFER_SIZE)
kryo.writeObject(output, message)
output.close()
2021-05-21 01:04:32 +02:00
return Data.Builder()
.putByteArray(MESSAGE_KEY, serializedMessage)
.build();
}
2021-01-28 05:24:27 +01:00
override fun getFactoryKey(): String {
2021-03-03 05:14:45 +01:00
return KEY
2021-01-28 05:24:27 +01:00
}
2021-05-12 08:17:25 +02:00
class Factory : Job.Factory<NotifyPNServerJob> {
2021-04-26 02:58:48 +02:00
2021-01-22 05:19:41 +01:00
override fun create(data: Data): NotifyPNServerJob {
2021-05-12 08:17:25 +02:00
val serializedMessage = data.getByteArray(MESSAGE_KEY)
val kryo = Kryo()
kryo.isRegistrationRequired = false
val input = Input(serializedMessage)
2021-05-21 01:04:32 +02:00
val message = kryo.readObject(input, SnodeMessage::class.java)
input.close()
2021-01-28 05:24:27 +01:00
return NotifyPNServerJob(message)
}
}
2020-11-25 02:06:41 +01:00
}