mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 15:17:43 +00:00
Utilise TokenManager and ExpiryManager
This commit is contained in:
@@ -10,6 +10,7 @@ import okhttp3.RequestBody
|
||||
import org.session.libsession.messaging.jobs.Job.Companion.MAX_BUFFER_SIZE
|
||||
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI.server
|
||||
import org.session.libsession.messaging.utilities.Data
|
||||
import org.session.libsession.snode.SnodeMessage
|
||||
import org.session.libsession.snode.OnionRequestAPI
|
||||
@@ -33,18 +34,21 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job {
|
||||
}
|
||||
|
||||
override fun execute(dispatcherName: String) {
|
||||
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)
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
retryIfNeeded(4) {
|
||||
OnionRequestAPI.sendOnionRequest(request.build(), server, PushNotificationAPI.serverPublicKey, Version.V2).map { response ->
|
||||
val code = response.info["code"] as? Int
|
||||
if (code == null || code == 0) {
|
||||
Log.d("Loki", "Couldn't notify PN server due to error: ${response.info["message"] as? String ?: "null"}.")
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
server,
|
||||
PushNotificationAPI.serverPublicKey,
|
||||
Version.V2
|
||||
) success { response ->
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> Log.d("Loki", "Couldn't notify PN server due to error: ${response.info["message"]}.")
|
||||
}
|
||||
}.fail { exception ->
|
||||
} fail { exception ->
|
||||
Log.d("Loki", "Couldn't notify PN server due to error: $exception.")
|
||||
}
|
||||
}.success {
|
||||
|
@@ -54,37 +54,39 @@ data class UnsubscriptionRequest(
|
||||
val service_info: Map<String, String>,
|
||||
)
|
||||
|
||||
/** invalid values, missing reuqired arguments etc, details in message */
|
||||
private const val UNPARSEABLE_ERROR = 1
|
||||
/** the "service" value is not active / valid */
|
||||
private const val SERVICE_NOT_AVAILABLE = 2
|
||||
/** something getting wrong internally talking to the backend */
|
||||
private const val SERVICE_TIMEOUT = 3
|
||||
/** other error processing the subscription (details in the message) */
|
||||
private const val GENERIC_ERROR = 4
|
||||
|
||||
@Serializable
|
||||
data class SubscriptionResponse(
|
||||
val error: Int? = null,
|
||||
val message: String? = null,
|
||||
val success: Boolean? = null,
|
||||
override val error: Int? = null,
|
||||
override val message: String? = null,
|
||||
override val success: Boolean? = null,
|
||||
val added: Boolean? = null,
|
||||
val updated: Boolean? = null,
|
||||
) {
|
||||
companion object {
|
||||
/** invalid values, missing reuqired arguments etc, details in message */
|
||||
const val UNPARSEABLE_ERROR = 1
|
||||
/** the "service" value is not active / valid */
|
||||
const val SERVICE_NOT_AVAILABLE = 2
|
||||
/** something getting wrong internally talking to the backend */
|
||||
const val SERVICE_TIMEOUT = 3
|
||||
/** other error processing the subscription (details in the message) */
|
||||
const val GENERIC_ERROR = 4
|
||||
}
|
||||
fun isSuccess() = success == true && error == null
|
||||
fun errorInfo() = if (success != true && error != null) {
|
||||
error to message
|
||||
} else null to null
|
||||
}
|
||||
): Response
|
||||
|
||||
@Serializable
|
||||
data class UnsubscribeResponse(
|
||||
val error: Int? = null,
|
||||
val message: String? = null,
|
||||
val success: Boolean? = null,
|
||||
override val error: Int? = null,
|
||||
override val message: String? = null,
|
||||
override val success: Boolean? = null,
|
||||
val removed: Boolean? = null,
|
||||
)
|
||||
): Response
|
||||
|
||||
interface Response {
|
||||
val error: Int?
|
||||
val message: String?
|
||||
val success: Boolean?
|
||||
fun isSuccess() = success == true && error == null
|
||||
fun isFailure() = !isSuccess()
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class PushNotificationMetadata(
|
||||
|
@@ -1,7 +1,10 @@
|
||||
package org.session.libsession.messaging.sending_receiving.notifications
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import nl.komponents.kovenant.Promise
|
||||
import nl.komponents.kovenant.all
|
||||
import nl.komponents.kovenant.functional.map
|
||||
import nl.komponents.kovenant.task
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
@@ -11,6 +14,7 @@ import org.session.libsession.snode.Version
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
import org.session.libsignal.utilities.JsonUtil
|
||||
import org.session.libsignal.utilities.Log
|
||||
import org.session.libsignal.utilities.emptyPromise
|
||||
import org.session.libsignal.utilities.retryIfNeeded
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
@@ -29,62 +33,57 @@ object PushNotificationAPI {
|
||||
Unsubscribe("unsubscribe_closed_group");
|
||||
}
|
||||
|
||||
fun register(token: String? = TextSecurePreferences.getLegacyFCMToken(context)) {
|
||||
Log.d(TAG, "register: $token")
|
||||
|
||||
register(token, TextSecurePreferences.getLocalNumber(context))
|
||||
fun register(
|
||||
token: String? = TextSecurePreferences.getLegacyFCMToken(context)
|
||||
): Promise<*, Exception> = all(
|
||||
register(token, TextSecurePreferences.getLocalNumber(context)),
|
||||
subscribeGroups()
|
||||
}
|
||||
)
|
||||
|
||||
fun register(token: String?, publicKey: String?) {
|
||||
Log.d(TAG, "register($token)")
|
||||
|
||||
token ?: return
|
||||
publicKey ?: return
|
||||
val parameters = mapOf("token" to token, "pubKey" to publicKey)
|
||||
val url = "$legacyServer/register"
|
||||
val body =
|
||||
RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body)
|
||||
fun register(token: String?, publicKey: String?): Promise<Unit, Exception> =
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(request.build(), legacyServer, legacyServerPublicKey, Version.V2)
|
||||
.map { response ->
|
||||
val code = response.info["code"] as? Int
|
||||
if (code != null && code != 0) {
|
||||
TextSecurePreferences.setLegacyFCMToken(context, token)
|
||||
} else {
|
||||
Log.d(TAG, "Couldn't register for FCM due to error: ${response.info["message"] as? String ?: "null"}.")
|
||||
}
|
||||
}.fail { exception ->
|
||||
val parameters = mapOf("token" to token!!, "pubKey" to publicKey!!)
|
||||
val url = "$legacyServer/register"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> throw Exception("error: ${response.info["message"]}.")
|
||||
else -> TextSecurePreferences.setLegacyFCMToken(context, token)
|
||||
}
|
||||
} fail { exception ->
|
||||
Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun unregister() {
|
||||
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: return
|
||||
fun unregister(): Promise<*, Exception> {
|
||||
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: emptyPromise()
|
||||
|
||||
val parameters = mapOf( "token" to token )
|
||||
val url = "$legacyServer/unregister"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||
TextSecurePreferences.clearLegacyFCMToken(context)
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${response.info["message"]}.")
|
||||
return retryIfNeeded(maxRetryCount) {
|
||||
val parameters = mapOf( "token" to token )
|
||||
val url = "$legacyServer/unregister"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
legacyServer,
|
||||
legacyServerPublicKey,
|
||||
Version.V2
|
||||
) success {
|
||||
when (it.info["code"]) {
|
||||
null, 0 -> throw Exception("error: ${it.info["message"]}.")
|
||||
else -> Log.d(TAG, "unregisterV1 success token: $token")
|
||||
}
|
||||
}.fail { exception ->
|
||||
Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
|
||||
TextSecurePreferences.clearLegacyFCMToken(context)
|
||||
} fail {
|
||||
exception -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
|
||||
unsubscribeGroups()
|
||||
}
|
||||
|
||||
// Legacy Closed Groups
|
||||
@@ -97,7 +96,7 @@ object PushNotificationAPI {
|
||||
private fun subscribeGroups(
|
||||
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = closedGroupPublicKeys.forEach { performGroupOperation(ClosedGroupOperation.Subscribe, it, publicKey) }
|
||||
) = closedGroupPublicKeys.map { performGroupOperation(ClosedGroupOperation.Subscribe, it, publicKey) }.let(::all)
|
||||
|
||||
fun unsubscribeGroup(
|
||||
closedGroupPublicKey: String,
|
||||
@@ -107,27 +106,30 @@ object PushNotificationAPI {
|
||||
private fun unsubscribeGroups(
|
||||
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = closedGroupPublicKeys.forEach { performGroupOperation(ClosedGroupOperation.Unsubscribe, it, publicKey) }
|
||||
) = closedGroupPublicKeys.map { performGroupOperation(ClosedGroupOperation.Unsubscribe, it, publicKey) }.let(::all)
|
||||
|
||||
private fun performGroupOperation(
|
||||
operation: ClosedGroupOperation,
|
||||
closedGroupPublicKey: String,
|
||||
publicKey: String
|
||||
) {
|
||||
if (!TextSecurePreferences.isUsingFCM(context)) return
|
||||
|
||||
): Promise<*, Exception> {
|
||||
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
||||
val url = "$legacyServer/${operation.rawValue}"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${response.info["message"]}.")
|
||||
return retryIfNeeded(maxRetryCount) {
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
legacyServer,
|
||||
legacyServerPublicKey,
|
||||
Version.V2
|
||||
) success {
|
||||
when (it.info["code"]) {
|
||||
null, 0 -> throw Exception("${it.info["message"]}")
|
||||
else -> Log.d(TAG, "performGroupOperation success: ${operation.rawValue}")
|
||||
}
|
||||
}.fail { exception ->
|
||||
} fail { exception ->
|
||||
Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user