mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-13 11:38:29 +00:00
cleanup
This commit is contained in:
@@ -36,7 +36,6 @@ import org.session.libsession.avatars.AvatarHelper;
|
||||
import org.session.libsession.database.MessageDataProvider;
|
||||
import org.session.libsession.messaging.MessagingModuleConfiguration;
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.MessageNotifier;
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI;
|
||||
import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPollerV2;
|
||||
import org.session.libsession.messaging.sending_receiving.pollers.Poller;
|
||||
import org.session.libsession.snode.SnodeModule;
|
||||
|
@@ -7,8 +7,6 @@ import org.session.libsession.messaging.messages.signal.IncomingTextMessage
|
||||
import org.session.libsession.messaging.messages.signal.OutgoingTextMessage
|
||||
import org.session.libsession.messaging.open_groups.OpenGroup
|
||||
import org.session.libsession.messaging.open_groups.OpenGroupApi
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPollerV2
|
||||
import org.session.libsession.utilities.Address
|
||||
import org.session.libsession.utilities.GroupUtil
|
||||
import org.session.libsession.utilities.recipients.Recipient
|
||||
@@ -21,7 +19,6 @@ import org.thoughtcrime.securesms.crypto.KeyPairUtilities
|
||||
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
|
||||
import org.thoughtcrime.securesms.groups.GroupManager
|
||||
import java.security.SecureRandom
|
||||
import java.util.*
|
||||
import kotlin.random.asKotlinRandom
|
||||
|
||||
object MockDataGenerator {
|
||||
|
@@ -18,7 +18,7 @@ import nl.komponents.kovenant.functional.map
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationMetadata
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.Response
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.SubscriptionRequest
|
||||
@@ -63,6 +63,8 @@ class FirebasePushManager (private val context: Context): PushManager {
|
||||
}
|
||||
|
||||
fun decrypt(encPayload: ByteArray): ByteArray? {
|
||||
Log.d(TAG, "decrypt() called")
|
||||
|
||||
val encKey = getOrCreateNotificationKey()
|
||||
val nonce = encPayload.take(AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES).toByteArray()
|
||||
val payload = encPayload.drop(AEAD.XCHACHA20POLY1305_IETF_NPUBBYTES).toByteArray()
|
||||
@@ -90,19 +92,23 @@ class FirebasePushManager (private val context: Context): PushManager {
|
||||
|
||||
@Synchronized
|
||||
override fun refresh(force: Boolean) {
|
||||
Log.d(TAG, "refresh() called with: force = $force")
|
||||
|
||||
firebaseInstanceIdJob?.apply {
|
||||
if (force) cancel() else if (isActive) return
|
||||
}
|
||||
|
||||
firebaseInstanceIdJob = getFcmInstanceId { task ->
|
||||
when {
|
||||
task.isSuccessful -> task.result?.token?.let { refresh(it, force).get() }
|
||||
task.isSuccessful -> try { task.result?.token?.let { refresh(it, force).get() } } catch(e: Exception) { Log.d(TAG, "refresh() failed", e) }
|
||||
else -> Log.w(TAG, "getFcmInstanceId failed." + task.exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun refresh(token: String, force: Boolean): Promise<*, Exception> {
|
||||
Log.d(TAG, "refresh() called with: token = $token, force = $force")
|
||||
|
||||
val userPublicKey = getLocalNumber(context) ?: return emptyPromise()
|
||||
val userEdKey = KeyPairUtilities.getUserED25519KeyPair(context) ?: return emptyPromise()
|
||||
|
||||
@@ -137,11 +143,12 @@ class FirebasePushManager (private val context: Context): PushManager {
|
||||
publicKey: String,
|
||||
userEd25519Key: KeyPair,
|
||||
namespaces: List<Int> = listOf(Namespace.DEFAULT)
|
||||
): Promise<*, Exception> = PushNotificationAPI.register(token) and getSubscription(
|
||||
): Promise<*, Exception> = LegacyGroupsPushManager.register(token, publicKey) and getSubscription(
|
||||
token, publicKey, userEd25519Key, namespaces
|
||||
) fail {
|
||||
Log.e(TAG, "Couldn't register for FCM due to error: $it.", it)
|
||||
} success {
|
||||
Log.d(TAG, "register() success!!")
|
||||
tokenManager.fcmToken = token
|
||||
}
|
||||
|
||||
@@ -149,7 +156,7 @@ class FirebasePushManager (private val context: Context): PushManager {
|
||||
token: String,
|
||||
userPublicKey: String,
|
||||
userEdKey: KeyPair
|
||||
): Promise<*, Exception> = PushNotificationAPI.unregister() and getUnsubscription(
|
||||
): Promise<*, Exception> = LegacyGroupsPushManager.unregister() and getUnsubscription(
|
||||
token, userPublicKey, userEdKey
|
||||
) fail {
|
||||
Log.e(TAG, "Couldn't unregister for FCM due to error: ${it}.", it)
|
||||
@@ -212,14 +219,14 @@ class FirebasePushManager (private val context: Context): PushManager {
|
||||
retryIfNeeded(maxRetryCount) { getResponseBody(path, requestParameters) }
|
||||
|
||||
private inline fun <reified T: Response> getResponseBody(path: String, requestParameters: String): Promise<T, Exception> {
|
||||
val url = "${PushNotificationAPI.server}/$path"
|
||||
val url = "${LegacyGroupsPushManager.server}/$path"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), requestParameters)
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
return OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
PushNotificationAPI.server,
|
||||
PushNotificationAPI.serverPublicKey,
|
||||
LegacyGroupsPushManager.server,
|
||||
LegacyGroupsPushManager.serverPublicKey,
|
||||
Version.V4
|
||||
).map { response ->
|
||||
response.body!!.inputStream()
|
||||
|
@@ -8,13 +8,15 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import org.session.libsession.messaging.jobs.BatchMessageReceiveJob
|
||||
import org.session.libsession.messaging.jobs.JobQueue
|
||||
import org.session.libsession.messaging.jobs.MessageReceiveParameters
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager
|
||||
import org.session.libsession.messaging.utilities.MessageWrapper
|
||||
import org.session.libsession.utilities.TextSecurePreferences
|
||||
import org.session.libsignal.utilities.Base64
|
||||
import org.session.libsignal.utilities.Log
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val TAG = "PushNotificationService"
|
||||
|
||||
@AndroidEntryPoint
|
||||
class PushNotificationService : FirebaseMessagingService() {
|
||||
|
||||
@@ -22,27 +24,26 @@ class PushNotificationService : FirebaseMessagingService() {
|
||||
|
||||
override fun onNewToken(token: String) {
|
||||
super.onNewToken(token)
|
||||
Log.d("Loki", "New FCM token: $token.")
|
||||
Log.d(TAG, "New FCM token: $token.")
|
||||
TextSecurePreferences.getLocalNumber(this) ?: return
|
||||
if (TextSecurePreferences.getLocalNumber(this) != token) {
|
||||
if (TextSecurePreferences.getFCMToken(this) != token) {
|
||||
pushManager.refresh(true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMessageReceived(message: RemoteMessage) {
|
||||
Log.d("Loki", "Received a push notification.")
|
||||
Log.d(TAG, "Received a push notification.")
|
||||
val data: ByteArray? = if (message.data.containsKey("spns")) {
|
||||
// this is a v2 push notification
|
||||
try {
|
||||
pushManager.decrypt(Base64.decode(message.data["enc_payload"]))
|
||||
} catch(e: Exception) {
|
||||
Log.e("Loki", "Invalid push notification: ${e.message}")
|
||||
Log.e(TAG, "Invalid push notification: ${e.message}")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// old v1 push notification; we still need this for receiving legacy closed group notifications
|
||||
val base64EncodedData = message.data?.get("ENCRYPTED_DATA")
|
||||
base64EncodedData?.let { Base64.decode(it) }
|
||||
message.data?.get("ENCRYPTED_DATA")?.let(Base64::decode)
|
||||
}
|
||||
if (data != null) {
|
||||
try {
|
||||
@@ -50,10 +51,10 @@ class PushNotificationService : FirebaseMessagingService() {
|
||||
val job = BatchMessageReceiveJob(listOf(MessageReceiveParameters(envelopeAsData)), null)
|
||||
JobQueue.shared.add(job)
|
||||
} catch (e: Exception) {
|
||||
Log.d("Loki", "Failed to unwrap data for message due to error: $e.")
|
||||
Log.d(TAG, "Failed to unwrap data for message due to error: $e.")
|
||||
}
|
||||
} else {
|
||||
Log.d("Loki", "Failed to decode data for message.")
|
||||
Log.d(TAG, "Failed to decode data for message.")
|
||||
val builder = NotificationCompat.Builder(this, NotificationChannels.OTHER)
|
||||
.setSmallIcon(network.loki.messenger.R.drawable.ic_notification)
|
||||
.setColor(resources.getColor(network.loki.messenger.R.color.textsecure_primary))
|
||||
@@ -66,8 +67,8 @@ class PushNotificationService : FirebaseMessagingService() {
|
||||
}
|
||||
|
||||
override fun onDeletedMessages() {
|
||||
Log.d("Loki", "Called onDeletedMessages.")
|
||||
Log.d(TAG, "Called onDeletedMessages.")
|
||||
super.onDeletedMessages()
|
||||
PushNotificationAPI.register()
|
||||
LegacyGroupsPushManager.register()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user