Fix individual group subs

This commit is contained in:
andrew 2023-06-21 10:32:29 +09:30
parent 42cfce0c3e
commit e3f60eb5f2
5 changed files with 25 additions and 25 deletions

View File

@ -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<Int> = 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)

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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<String> = 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<String>): 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}.")
}
}
}

View File

@ -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 <V> Promise<V, Exception>.timeout(millis: Long): Promise<V, Exception> {
if (!deferred.promise.isDone()) { deferred.reject(it) }
}
return deferred.promise
}
}
infix fun <V, E: Exception> Promise<V, E>.sideEffect(
callback: (value: V) -> Unit
) = map {
callback(it)
it
}