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

89 lines
3.1 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 nl.komponents.kovenant.functional.map
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.RequestBody
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
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
2021-02-03 02:22:40 +01:00
import org.session.libsignal.utilities.logging.Log
2021-02-01 01:35:53 +01:00
import org.session.libsignal.utilities.JsonUtil
2020-12-02 06:39:02 +01:00
import org.session.libsignal.service.loki.utilities.retryIfNeeded
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() {
val server = PushNotificationAPI.server
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))
val request = Request.Builder().url(url).post(body)
retryIfNeeded(4) {
OnionRequestAPI.sendOnionRequest(request.build(), server, PushNotificationAPI.serverPublicKey, "/loki/v2/lsrpc").map { json ->
val code = json["code"] as? Int
if (code == null || code == 0) {
2021-05-12 08:17:25 +02:00
Log.d("Loki", "Couldn't notify PN server due to error: ${json["message"] as? String ?: "null"}.")
2020-12-02 06:39:02 +01: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()
}. fail {
handleFailure(it)
}
}
private fun handleSuccess() {
delegate?.handleJobSucceeded(this)
}
private fun handleFailure(error: Exception) {
delegate?.handleJobFailed(this, error)
}
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)
kryo.writeObject(output, message)
output.close()
2021-05-12 08:17:25 +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)
val message: SnodeMessage = 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
}