|
|
|
@ -2,12 +2,12 @@ package org.session.libsession.messaging.sending_receiving.notifications
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
|
import nl.komponents.kovenant.Promise
|
|
|
|
|
import nl.komponents.kovenant.functional.map
|
|
|
|
|
import okhttp3.MediaType
|
|
|
|
|
import okhttp3.Request
|
|
|
|
|
import okhttp3.RequestBody
|
|
|
|
|
import org.session.libsession.messaging.MessagingModuleConfiguration
|
|
|
|
|
import org.session.libsession.snode.OnionRequestAPI
|
|
|
|
|
import org.session.libsession.snode.OnionResponse
|
|
|
|
|
import org.session.libsession.snode.Version
|
|
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
|
|
|
|
import org.session.libsignal.utilities.JsonUtil
|
|
|
|
@ -21,12 +21,10 @@ object PushManagerV1 {
|
|
|
|
|
private const val TAG = "PushManagerV1"
|
|
|
|
|
|
|
|
|
|
val context = MessagingModuleConfiguration.shared.context
|
|
|
|
|
const val server = "https://push.getsession.org"
|
|
|
|
|
const val serverPublicKey: String = "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b"
|
|
|
|
|
private const val legacyServer = "https://dev.apns.getsession.org"
|
|
|
|
|
private const val legacyServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
|
|
|
|
|
private const val maxRetryCount = 4
|
|
|
|
|
|
|
|
|
|
private val server = Server.LEGACY
|
|
|
|
|
|
|
|
|
|
fun register(
|
|
|
|
|
isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context),
|
|
|
|
|
token: String? = TextSecurePreferences.getFCMToken(context),
|
|
|
|
@ -38,17 +36,14 @@ object PushManagerV1 {
|
|
|
|
|
} else retryIfNeeded(maxRetryCount) {
|
|
|
|
|
doRegister(token, publicKey, legacyGroupPublicKeys)
|
|
|
|
|
} fail { exception ->
|
|
|
|
|
Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.")
|
|
|
|
|
} success {
|
|
|
|
|
Log.d(TAG, "register success")
|
|
|
|
|
Log.d(TAG, "Couldn't register for FCM due to error: $exception.")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun doRegister(token: String?, publicKey: String?, legacyGroupPublicKeys: Collection<String>): Promise<*, Exception> {
|
|
|
|
|
Log.d(TAG, "doRegister() called")
|
|
|
|
|
Log.d(TAG, "registerV1 requested")
|
|
|
|
|
|
|
|
|
|
token ?: return emptyPromise()
|
|
|
|
|
publicKey ?: return emptyPromise()
|
|
|
|
|
legacyGroupPublicKeys.takeIf { it.isNotEmpty() } ?: return unregister()
|
|
|
|
|
|
|
|
|
|
val parameters = mapOf(
|
|
|
|
|
"token" to token,
|
|
|
|
@ -56,15 +51,16 @@ object PushManagerV1 {
|
|
|
|
|
"legacyGroupPublicKeys" to legacyGroupPublicKeys
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
val url = "$legacyServer/register_legacy_groups_only"
|
|
|
|
|
val url = "${server.url}/register_legacy_groups_only"
|
|
|
|
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
|
|
|
|
val request = Request.Builder().url(url).post(body).build()
|
|
|
|
|
|
|
|
|
|
return OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
|
|
|
|
when (response.info["code"]) {
|
|
|
|
|
null, 0 -> throw Exception("error: ${response.info["message"]}.")
|
|
|
|
|
else -> Log.d(TAG, "doRegister success")
|
|
|
|
|
return sendOnionRequest(request) sideEffect { response ->
|
|
|
|
|
when (response.code) {
|
|
|
|
|
null, 0 -> throw Exception("error: ${response.message}.")
|
|
|
|
|
}
|
|
|
|
|
} success {
|
|
|
|
|
Log.d(TAG, "registerV1 success")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -72,24 +68,19 @@ object PushManagerV1 {
|
|
|
|
|
* Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager.
|
|
|
|
|
*/
|
|
|
|
|
fun unregister(): Promise<*, Exception> {
|
|
|
|
|
Log.d(TAG, "unregister() called")
|
|
|
|
|
Log.d(TAG, "unregisterV1 requested")
|
|
|
|
|
|
|
|
|
|
val token = TextSecurePreferences.getFCMToken(context) ?: emptyPromise()
|
|
|
|
|
|
|
|
|
|
return retryIfNeeded(maxRetryCount) {
|
|
|
|
|
val parameters = mapOf( "token" to token )
|
|
|
|
|
val url = "$legacyServer/unregister"
|
|
|
|
|
val url = "${server.url}/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"]}.")
|
|
|
|
|
sendOnionRequest(request) success {
|
|
|
|
|
when (it.code) {
|
|
|
|
|
null, 0 -> throw Exception("error: ${it.message}.")
|
|
|
|
|
else -> Log.d(TAG, "unregisterV1 success")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -120,21 +111,23 @@ object PushManagerV1 {
|
|
|
|
|
publicKey: String
|
|
|
|
|
): Promise<*, Exception> {
|
|
|
|
|
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
|
|
|
|
val url = "$legacyServer/$operation"
|
|
|
|
|
val url = "${server.url}/$operation"
|
|
|
|
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
|
|
|
|
val request = Request.Builder().url(url).post(body).build()
|
|
|
|
|
|
|
|
|
|
return retryIfNeeded(maxRetryCount) {
|
|
|
|
|
OnionRequestAPI.sendOnionRequest(
|
|
|
|
|
request,
|
|
|
|
|
legacyServer,
|
|
|
|
|
legacyServerPublicKey,
|
|
|
|
|
Version.V2
|
|
|
|
|
) sideEffect {
|
|
|
|
|
when (it.info["code"]) {
|
|
|
|
|
0, null -> throw Exception("${it.info["message"]}")
|
|
|
|
|
sendOnionRequest(request) sideEffect {
|
|
|
|
|
when (it.code) {
|
|
|
|
|
0, null -> throw Exception(it.message)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun sendOnionRequest(request: Request): Promise<OnionResponse, Exception> = OnionRequestAPI.sendOnionRequest(
|
|
|
|
|
request,
|
|
|
|
|
server.url,
|
|
|
|
|
server.publicKey,
|
|
|
|
|
Version.V2
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|