diff --git a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt index d6b738d8c4..25fed8373b 100644 --- a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt +++ b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/FirebasePushManager.kt @@ -73,7 +73,7 @@ class FirebasePushManager( } private fun refresh(token: String, force: Boolean): Promise<*, Exception> { - Log.d(TAG, "refresh() called with: token = $token, force = $force") + Log.d(TAG, "refresh() called") val userPublicKey = getLocalNumber(context) ?: return emptyPromise() val userEdKey = KeyPairUtilities.getUserED25519KeyPair(context) ?: return emptyPromise() @@ -109,7 +109,10 @@ class FirebasePushManager( publicKey: String, userEd25519Key: KeyPair, namespaces: List = listOf(Namespace.DEFAULT) - ): Promise<*, Exception> = PushManagerV1.register(token, publicKey) and pushManagerV2.register( + ): Promise<*, Exception> = PushManagerV1.register( + token = token, + publicKey = publicKey + ) and pushManagerV2.register( token, publicKey, userEd25519Key, namespaces ) fail { Log.e(TAG, "Couldn't register for FCM due to error: $it.", it) diff --git a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushNotificationService.kt b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushNotificationService.kt index 0a73d0e428..4c6ce9f4ea 100644 --- a/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushNotificationService.kt +++ b/app/src/play/kotlin/org/thoughtcrime/securesms/notifications/PushNotificationService.kt @@ -24,7 +24,6 @@ class PushNotificationService : FirebaseMessagingService() { override fun onNewToken(token: String) { super.onNewToken(token) - Log.d(TAG, "New FCM token: $token.") TextSecurePreferences.getLocalNumber(this) ?: return if (TextSecurePreferences.getFCMToken(this) != token) { pushManager.refresh(true) @@ -69,6 +68,6 @@ class PushNotificationService : FirebaseMessagingService() { override fun onDeletedMessages() { Log.d(TAG, "Called onDeletedMessages.") super.onDeletedMessages() - PushManagerV1.register() + pushManager.refresh(true) } } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt index 2f8fdd3629..dec50f0206 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt @@ -772,7 +772,7 @@ fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, grou storage.setActive(groupID, false) storage.removeMember(groupID, Address.fromSerialized(userPublicKey)) // Notify the PN server - PushManagerV1.register(publicKey = userPublicKey) + PushManagerV1.unsubscribeGroup(groupPublicKey, publicKey = userPublicKey) // Stop polling ClosedGroupPollerV2.shared.stopPolling(groupPublicKey) } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt index 3e2bb3a844..6e4c61fee8 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/notifications/PushManagerV1.kt @@ -14,6 +14,7 @@ import org.session.libsignal.utilities.JsonUtil import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.emptyPromise import org.session.libsignal.utilities.retryIfNeeded +import org.session.libsignal.utilities.sideEffect @SuppressLint("StaticFieldLeak") object PushManagerV1 { @@ -27,12 +28,14 @@ object PushManagerV1 { private const val maxRetryCount = 4 fun register( + isUsingFCM: Boolean = TextSecurePreferences.isUsingFCM(context), token: String? = TextSecurePreferences.getFCMToken(context), publicKey: String? = TextSecurePreferences.getLocalNumber(context), legacyGroupPublicKeys: Collection = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys() ): Promise<*, Exception> = retryIfNeeded(maxRetryCount) { - doRegister(token, publicKey, legacyGroupPublicKeys) + if (isUsingFCM) doRegister(token, publicKey, legacyGroupPublicKeys) + else emptyPromise() } fail { exception -> Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.") } success { @@ -40,13 +43,11 @@ object PushManagerV1 { } private fun doRegister(token: String?, publicKey: String?, legacyGroupPublicKeys: Collection): Promise<*, Exception> { - Log.d(TAG, "doRegister() called $token, $publicKey, $legacyGroupPublicKeys") + Log.d(TAG, "doRegister() called") token ?: return emptyPromise() publicKey ?: return emptyPromise() - legacyGroupPublicKeys.takeIf { it.isNotEmpty() } ?: return emptyPromise() - - Log.d(TAG, "actually registering...") + legacyGroupPublicKeys.takeIf { it.isNotEmpty() } ?: return unregister() val parameters = mapOf( "token" to token, @@ -63,10 +64,6 @@ object PushManagerV1 { null, 0 -> throw Exception("error: ${response.info["message"]}.") else -> Log.d(TAG, "register() success!!") } - } success { - Log.d(TAG, "onion request success") - } fail { - Log.d(TAG, "onion fail: $it") } } @@ -90,14 +87,10 @@ object PushManagerV1 { legacyServerPublicKey, Version.V2 ) success { - Log.d(TAG, "unregister() success!!") - when (it.info["code"]) { null, 0 -> throw Exception("error: ${it.info["message"]}.") - else -> Log.d(TAG, "unregisterV1 success token: $token") + else -> Log.d(TAG, "unregisterV1 success") } - } fail { - Log.d(TAG, "Couldn't disable FCM with token: $token due to error: $it.") } } } @@ -130,13 +123,10 @@ object PushManagerV1 { legacyServer, legacyServerPublicKey, Version.V2 - ) success { + ) sideEffect { when (it.info["code"]) { - null, 0 -> throw Exception("${it.info["message"]}") - else -> Log.d(TAG, "performGroupOperation success: $operation") + 0, null -> throw Exception("${it.info["message"]}") } - } fail { exception -> - Log.d(TAG, "performGroupOperation fail: $operation: $closedGroupPublicKey due to error: ${exception}.") } } } diff --git a/libsignal/src/main/java/org/session/libsignal/utilities/PromiseUtilities.kt b/libsignal/src/main/java/org/session/libsignal/utilities/PromiseUtilities.kt index 8095b44597..fdf8f107b9 100644 --- a/libsignal/src/main/java/org/session/libsignal/utilities/PromiseUtilities.kt +++ b/libsignal/src/main/java/org/session/libsignal/utilities/PromiseUtilities.kt @@ -3,6 +3,7 @@ package org.session.libsignal.utilities import nl.komponents.kovenant.Promise import nl.komponents.kovenant.deferred +import nl.komponents.kovenant.functional.map import nl.komponents.kovenant.task import java.util.concurrent.TimeoutException @@ -58,4 +59,11 @@ fun Promise.timeout(millis: Long): Promise { if (!deferred.promise.isDone()) { deferred.reject(it) } } return deferred.promise -} \ No newline at end of file +} + +infix fun Promise.sideEffect( + callback: (value: V) -> Unit +) = map { + callback(it) + it +}