Check custom unifiedpush gateway

This commit is contained in:
sim 2022-02-28 22:53:18 +01:00 committed by Benoit Marty
parent 1069f77bd5
commit f774f46627
2 changed files with 62 additions and 21 deletions

View File

@ -21,16 +21,23 @@ import android.content.pm.PackageManager
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit import androidx.core.content.edit
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import im.vector.app.BuildConfig import im.vector.app.BuildConfig
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.di.DefaultSharedPreferences import im.vector.app.core.di.DefaultSharedPreferences
import im.vector.app.features.settings.BackgroundSyncMode import im.vector.app.features.settings.BackgroundSyncMode
import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.VectorPreferences
import im.vector.app.push.fcm.FcmHelper import im.vector.app.push.fcm.FcmHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import okhttp3.Request
import org.unifiedpush.android.connector.UnifiedPush import org.unifiedpush.android.connector.UnifiedPush
import timber.log.Timber import timber.log.Timber
import java.net.URI
import java.net.URL import java.net.URL
object UnifiedPushHelper { object UnifiedPushHelper {
@ -77,7 +84,7 @@ object UnifiedPushHelper {
* @param context android context * @param context android context
* @param gateway the push gateway to store * @param gateway the push gateway to store
*/ */
fun storePushGateway(context: Context, private fun storePushGateway(context: Context,
gateway: String?) { gateway: String?) {
DefaultSharedPreferences.getInstance(context).edit { DefaultSharedPreferences.getInstance(context).edit {
putString(PREFS_PUSH_GATEWAY, gateway) putString(PREFS_PUSH_GATEWAY, gateway)
@ -191,30 +198,63 @@ object UnifiedPushHelper {
up.unregisterApp(context) up.unregisterApp(context)
} }
fun customOrDefaultGateway(context: Context, endpoint: String?): String { @JsonClass(generateAdapter = true)
internal data class DiscoveryResponse(
val unifiedpush: DiscoveryUnifiedPush = DiscoveryUnifiedPush()
)
@JsonClass(generateAdapter = true)
internal data class DiscoveryUnifiedPush(
val gateway: String = ""
)
fun storeCustomOrDefaultGateway(
context: Context,
endpoint: String,
onDoneRunnable: Runnable? = null) {
// if we use the embedded distributor, // if we use the embedded distributor,
// register app_id type upfcm on sygnal // register app_id type upfcm on sygnal
// the pushkey if FCM key // the pushkey if FCM key
if (up.getDistributor(context) == context.packageName) { if (up.getDistributor(context) == context.packageName) {
context.getString(R.string.pusher_http_url).let { context.getString(R.string.pusher_http_url).let {
storePushGateway(context, it) storePushGateway(context, it)
return it onDoneRunnable?.run()
return
} }
} }
// else, unifiedpush, and pushkey is an endpoint // else, unifiedpush, and pushkey is an endpoint
val default = context.getString(R.string.default_push_gateway_http_url) val gateway = context.getString(R.string.default_push_gateway_http_url)
endpoint?.let { val parsed = URL(endpoint)
val uri = URI(it) val custom = "${parsed.protocol}://${parsed.host}/_matrix/push/v1/notify"
val custom = "${it.split(uri.rawPath)[0]}/_matrix/push/v1/notify"
Timber.i("Testing $custom") Timber.i("Testing $custom")
/** val thread = CoroutineScope(SupervisorJob()).launch {
* TODO: try {
* if GET custom returns """{"unifiedpush":{"gateway":"matrix"}}""" val moshi: Moshi = Moshi.Builder()
* return custom .add(KotlinJsonAdapterFactory())
*/ .build()
val client = OkHttpClient()
val request = Request.Builder()
.url(custom)
.build()
val sResponse = client.newCall(request).execute()
.body?.string() ?: ""
moshi.adapter(DiscoveryResponse::class.java)
.fromJson(sResponse)?.let { response ->
if (response.unifiedpush.gateway == "matrix") {
Timber.d("Using custom gateway")
storePushGateway(context, custom)
onDoneRunnable?.run()
return@launch
} }
storePushGateway(context, default) }
return default } catch (e: Exception) {
Timber.d("Cannot try custom gateway: $e")
}
storePushGateway(context, gateway)
onDoneRunnable?.run()
return@launch
}
thread.start()
} }
fun getExternalDistributors(context: Context): List<String> { fun getExternalDistributors(context: Context): List<String> {

View File

@ -156,14 +156,15 @@ class VectorMessagingReceiver : MessagingReceiver() {
override fun onNewEndpoint(context: Context, endpoint: String, instance: String) { override fun onNewEndpoint(context: Context, endpoint: String, instance: String) {
Timber.tag(loggerTag.value).i("onNewEndpoint: adding $endpoint") Timber.tag(loggerTag.value).i("onNewEndpoint: adding $endpoint")
if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) { if (vectorPreferences.areNotificationEnabledForDevice() && activeSessionHolder.hasActiveSession()) {
val gateway = UnifiedPushHelper.customOrDefaultGateway(context, endpoint)
// If the endpoint has changed // If the endpoint has changed
// or the gateway has changed // or the gateway has changed
if (UnifiedPushHelper.getEndpointOrToken(context) != endpoint || if (UnifiedPushHelper.getEndpointOrToken(context) != endpoint) {
UnifiedPushHelper.getPushGateway(context) != gateway) {
UnifiedPushHelper.storePushGateway(context, gateway)
UnifiedPushHelper.storeUpEndpoint(context, endpoint) UnifiedPushHelper.storeUpEndpoint(context, endpoint)
pushersManager.enqueueRegisterPusher(endpoint, gateway) UnifiedPushHelper.storeCustomOrDefaultGateway(context, endpoint) {
UnifiedPushHelper.getPushGateway(context)?.let {
pushersManager.enqueueRegisterPusher(endpoint, it)
}
}
} else { } else {
Timber.tag(loggerTag.value).i("onNewEndpoint: skipped") Timber.tag(loggerTag.value).i("onNewEndpoint: skipped")
} }