|
|
@ -18,55 +18,18 @@ package io.element.android.libraries.pushproviders.unifiedpush |
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
import io.element.android.libraries.di.ApplicationContext |
|
|
|
import io.element.android.libraries.di.ApplicationContext |
|
|
|
import io.element.android.libraries.matrix.api.MatrixClient |
|
|
|
|
|
|
|
import io.element.android.libraries.pushproviders.api.Distributor |
|
|
|
import io.element.android.libraries.pushproviders.api.Distributor |
|
|
|
import io.element.android.libraries.pushproviders.api.PusherSubscriber |
|
|
|
|
|
|
|
import org.unifiedpush.android.connector.UnifiedPush |
|
|
|
import org.unifiedpush.android.connector.UnifiedPush |
|
|
|
import javax.inject.Inject |
|
|
|
import javax.inject.Inject |
|
|
|
|
|
|
|
|
|
|
|
class RegisterUnifiedPushUseCase @Inject constructor( |
|
|
|
class RegisterUnifiedPushUseCase @Inject constructor( |
|
|
|
@ApplicationContext private val context: Context, |
|
|
|
@ApplicationContext private val context: Context, |
|
|
|
private val pusherSubscriber: PusherSubscriber, |
|
|
|
|
|
|
|
private val unifiedPushStore: UnifiedPushStore, |
|
|
|
|
|
|
|
) { |
|
|
|
) { |
|
|
|
sealed interface RegisterUnifiedPushResult { |
|
|
|
fun execute(distributor: Distributor, clientSecret: String): Result<Unit> { |
|
|
|
data object Success : RegisterUnifiedPushResult |
|
|
|
UnifiedPush.saveDistributor(context, distributor.value) |
|
|
|
data object NeedToAskUserForDistributor : RegisterUnifiedPushResult |
|
|
|
// This will trigger the callback |
|
|
|
data object Error : RegisterUnifiedPushResult |
|
|
|
// VectorUnifiedPushMessagingReceiver.onNewEndpoint |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun execute(matrixClient: MatrixClient, distributor: Distributor, clientSecret: String): RegisterUnifiedPushResult { |
|
|
|
|
|
|
|
val distributorValue = distributor.value |
|
|
|
|
|
|
|
if (distributorValue.isNotEmpty()) { |
|
|
|
|
|
|
|
saveAndRegisterApp(distributorValue, clientSecret) |
|
|
|
|
|
|
|
val endpoint = unifiedPushStore.getEndpoint(clientSecret) ?: return RegisterUnifiedPushResult.Error |
|
|
|
|
|
|
|
val gateway = unifiedPushStore.getPushGateway(clientSecret) ?: return RegisterUnifiedPushResult.Error |
|
|
|
|
|
|
|
pusherSubscriber.registerPusher(matrixClient, endpoint, gateway) |
|
|
|
|
|
|
|
return RegisterUnifiedPushResult.Success |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO Below should never happen? |
|
|
|
|
|
|
|
if (UnifiedPush.getDistributor(context).isNotEmpty()) { |
|
|
|
|
|
|
|
registerApp(clientSecret) |
|
|
|
|
|
|
|
return RegisterUnifiedPushResult.Success |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val distributors = UnifiedPush.getDistributors(context) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return if (distributors.size == 1) { |
|
|
|
|
|
|
|
saveAndRegisterApp(distributors.first(), clientSecret) |
|
|
|
|
|
|
|
RegisterUnifiedPushResult.Success |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
RegisterUnifiedPushResult.NeedToAskUserForDistributor |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun saveAndRegisterApp(distributor: String, clientSecret: String) { |
|
|
|
|
|
|
|
UnifiedPush.saveDistributor(context, distributor) |
|
|
|
|
|
|
|
registerApp(clientSecret) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun registerApp(clientSecret: String) { |
|
|
|
|
|
|
|
UnifiedPush.registerApp(context = context, instance = clientSecret) |
|
|
|
UnifiedPush.registerApp(context = context, instance = clientSecret) |
|
|
|
|
|
|
|
return Result.success(Unit) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|