session-android/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushNotificationAPI.kt

110 lines
4.9 KiB
Kotlin
Raw Normal View History

package org.session.libsession.messaging.sending_receiving.notifications
import android.annotation.SuppressLint
import nl.komponents.kovenant.functional.map
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.RequestBody
2021-04-26 03:14:45 +02:00
import org.session.libsession.messaging.MessagingModuleConfiguration
2021-04-23 08:09:47 +02:00
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
import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsignal.utilities.JsonUtil
2021-05-18 01:12:33 +02:00
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.retryIfNeeded
@SuppressLint("StaticFieldLeak")
object PushNotificationAPI {
2023-06-09 10:40:06 +02:00
private const val TAG = "PushNotificationAPI"
2021-04-26 03:14:45 +02:00
val context = MessagingModuleConfiguration.shared.context
2023-06-09 07:20:45 +02:00
const val server = "https://push.getsession.org"
const val serverPublicKey: String = "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b"
private const val legacyServer = "https://live.apns.getsession.org"
private const val legacyServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
private const val maxRetryCount = 4
2023-06-09 07:20:45 +02:00
private enum class ClosedGroupOperation(val rawValue: String) {
Subscribe("subscribe_closed_group"),
Unsubscribe("unsubscribe_closed_group");
}
2023-06-09 07:20:45 +02:00
fun register(token: String? = TextSecurePreferences.getFCMToken(context)) {
2023-06-09 10:40:06 +02:00
Log.d(TAG, "register: $token")
2023-06-09 07:20:45 +02:00
token?.let(::unregisterV1)
subscribeGroups()
}
2023-06-09 07:20:45 +02:00
@JvmStatic
fun unregister(token: String) {
2023-06-09 10:40:06 +02:00
Log.d(TAG, "unregister: $token")
2023-06-09 07:20:45 +02:00
unregisterV1(token)
unsubscribeGroups()
}
private fun unregisterV1(token: String) {
val parameters = mapOf( "token" to token )
2023-06-09 10:40:06 +02:00
val url = "$legacyServer/unregister"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
2023-06-09 10:40:06 +02:00
val request = Request.Builder().url(url).post(body).build()
retryIfNeeded(maxRetryCount) {
2023-06-09 10:40:06 +02:00
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
2023-06-09 07:20:45 +02:00
when (response.info["code"]) {
2023-06-10 04:14:43 +02:00
null, 0 -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${response.info["message"]}.")
2023-06-09 10:40:06 +02:00
else -> Log.d(TAG, "unregisterV1 success token: $token")
}
}.fail { exception ->
2023-06-10 04:14:43 +02:00
Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
}
}
}
2023-06-09 07:20:45 +02:00
// Legacy Closed Groups
fun subscribeGroup(
closedGroupPublicKey: String,
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
2023-06-09 08:26:24 +02:00
) = performGroupOperation(ClosedGroupOperation.Subscribe, closedGroupPublicKey, publicKey)
2023-06-09 07:20:45 +02:00
private fun subscribeGroups(
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
2023-06-09 08:26:24 +02:00
) = closedGroupPublicKeys.forEach { performGroupOperation(ClosedGroupOperation.Subscribe, it, publicKey) }
2023-06-09 07:20:45 +02:00
fun unsubscribeGroup(
closedGroupPublicKey: String,
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
2023-06-09 08:26:24 +02:00
) = performGroupOperation(ClosedGroupOperation.Unsubscribe, closedGroupPublicKey, publicKey)
2023-06-09 07:20:45 +02:00
private fun unsubscribeGroups(
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
2023-06-09 08:26:24 +02:00
) = closedGroupPublicKeys.forEach { performGroupOperation(ClosedGroupOperation.Unsubscribe, it, publicKey) }
2023-06-09 07:20:45 +02:00
private fun performGroupOperation(
operation: ClosedGroupOperation,
closedGroupPublicKey: String,
publicKey: String
) {
if (!TextSecurePreferences.isUsingFCM(context)) return
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))
2023-06-09 10:40:06 +02:00
val request = Request.Builder().url(url).post(body).build()
2023-06-09 07:20:45 +02:00
retryIfNeeded(maxRetryCount) {
2023-06-09 10:40:06 +02:00
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
2023-06-09 07:20:45 +02:00
when (response.info["code"]) {
2023-06-09 10:40:06 +02:00
null, 0 -> Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${response.info["message"]}.")
else -> Log.d(TAG, "performGroupOperation success: ${operation.rawValue}")
}
}.fail { exception ->
2023-06-09 10:40:06 +02:00
Log.d(TAG, "Couldn't ${operation.rawValue}: $closedGroupPublicKey due to error: ${exception}.")
}
}
}
}