diff --git a/husky/app/src/main/java/com/keylesspalace/tusky/network/InstanceSwitchAuthInterceptor.java b/husky/app/src/main/java/com/keylesspalace/tusky/network/InstanceSwitchAuthInterceptor.java deleted file mode 100644 index cd44f6d2..00000000 --- a/husky/app/src/main/java/com/keylesspalace/tusky/network/InstanceSwitchAuthInterceptor.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Husky -- A Pleroma client for Android - * - * Copyright (C) 2022 The Husky Developers - * Copyright (C) 2017 charlag - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.keylesspalace.tusky.network; - -import androidx.annotation.NonNull; -import com.keylesspalace.tusky.db.AccountEntity; -import com.keylesspalace.tusky.db.AccountManager; -import java.io.IOException; -import okhttp3.HttpUrl; -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; - -public final class InstanceSwitchAuthInterceptor implements Interceptor { - - private final AccountManager accountManager; - - public InstanceSwitchAuthInterceptor(AccountManager accountManager) { - this.accountManager = accountManager; - } - - @NonNull - @Override - public Response intercept(@NonNull Chain chain) throws IOException { - Request originalRequest = chain.request(); - - // Only switch domains if the request comes from retrofit - if(originalRequest.url().host().equals(MastodonApi.PLACEHOLDER_DOMAIN)) { - AccountEntity currentAccount = accountManager.getActiveAccount(); - - Request.Builder builder = originalRequest.newBuilder(); - - String instanceHeader = originalRequest.header(MastodonApi.DOMAIN_HEADER); - if(instanceHeader != null) { - // use domain explicitly specified in custom header - builder.url(swapHost(originalRequest.url(), instanceHeader)); - builder.removeHeader(MastodonApi.DOMAIN_HEADER); - } else if(currentAccount != null) { - //use domain of current account - builder.url(swapHost(originalRequest.url(), currentAccount.getDomain())) - .header("Authorization", - String.format("Bearer %s", currentAccount.getAccessToken())); - } - Request newRequest = builder.build(); - - return chain.proceed(newRequest); - } else { - return chain.proceed(originalRequest); - } - } - - @NonNull - private static HttpUrl swapHost(@NonNull HttpUrl url, @NonNull String host) { - return url.newBuilder().host(host).build(); - } -} diff --git a/husky/app/src/main/java/com/keylesspalace/tusky/network/InstanceSwitchAuthInterceptor.kt b/husky/app/src/main/java/com/keylesspalace/tusky/network/InstanceSwitchAuthInterceptor.kt new file mode 100644 index 00000000..7f7c349c --- /dev/null +++ b/husky/app/src/main/java/com/keylesspalace/tusky/network/InstanceSwitchAuthInterceptor.kt @@ -0,0 +1,59 @@ +/* + * Husky -- A Pleroma client for Android + * + * Copyright (C) 2022 The Husky Developers + * Copyright (C) 2017 charlag + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.keylesspalace.tusky.network + +import com.keylesspalace.tusky.db.AccountManager +import okhttp3.HttpUrl +import okhttp3.Interceptor +import okhttp3.Response + +class InstanceSwitchAuthInterceptor( + private val accountManager: AccountManager +) : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val originalRequest = chain.request() + + return if (originalRequest.url.host == MastodonApi.PLACEHOLDER_DOMAIN) { + val activeAccount = accountManager.activeAccount + val builder = originalRequest.newBuilder() + val instanceHeader = originalRequest.header(MastodonApi.DOMAIN_HEADER) + if (instanceHeader != null) { + builder.url(swapHost(originalRequest.url, instanceHeader)) + .removeHeader(MastodonApi.DOMAIN_HEADER) + } else if (activeAccount != null) { + builder.url(swapHost(originalRequest.url, activeAccount.domain)) + .header( + "Authorization", + String.format("Bearer %s", activeAccount.accessToken) + ) + } + + chain.proceed(builder.build()) + } else { + chain.proceed(originalRequest) + } + } + + private fun swapHost(url: HttpUrl, host: String): HttpUrl { + return url.newBuilder().host(host).build() + } +} diff --git a/husky/app/src/main/java/com/keylesspalace/tusky/viewmodel/SplashViewModel.kt b/husky/app/src/main/java/com/keylesspalace/tusky/viewmodel/SplashViewModel.kt index 7e22fb66..248f16c1 100644 --- a/husky/app/src/main/java/com/keylesspalace/tusky/viewmodel/SplashViewModel.kt +++ b/husky/app/src/main/java/com/keylesspalace/tusky/viewmodel/SplashViewModel.kt @@ -8,6 +8,7 @@ import com.keylesspalace.tusky.viewmodel.viewstate.SplashViewState import com.keylesspalace.tusky.viewmodel.viewstate.SplashViewState.NO_USER import com.keylesspalace.tusky.viewmodel.viewstate.SplashViewState.USER import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -25,7 +26,10 @@ class SplashViewModel( private fun loadInstanceSettings() { viewModelScope.launch { - instanceRepository.getInstanceInfo().collect { + instanceRepository.getInstanceInfo() + .catch { + _splashViewState.update { NO_USER } + }.collect { _splashViewState.update { accountManager.activeAccount?.let { USER } ?: NO_USER }