From 28617cf9a820a41982f8974241b1ddf4c2181493 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Fri, 6 Oct 2023 12:54:57 +0200 Subject: [PATCH] Compatibility for Lemmy 0.19 Closes #197 --- .../infinityforlemmy/RetrofitHolder.java | 24 +++++++++++++++++++ .../asynctasks/SwitchAccount.java | 16 ++++++++++++- .../toldi/infinityforlemmy/site/SiteInfo.java | 12 ++++++++-- .../infinityforlemmy/utils/APIUtils.java | 11 +++++++++ .../utils/SharedPreferencesUtils.java | 2 ++ 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/RetrofitHolder.java b/app/src/main/java/eu/toldi/infinityforlemmy/RetrofitHolder.java index 04f27247..475637a7 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/RetrofitHolder.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/RetrofitHolder.java @@ -1,7 +1,10 @@ package eu.toldi.infinityforlemmy; +import android.util.Log; + import eu.toldi.infinityforlemmy.network.SortTypeConverterFactory; import eu.toldi.infinityforlemmy.utils.APIUtils; +import okhttp3.Interceptor; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.adapter.guava.GuavaCallAdapterFactory; @@ -12,8 +15,14 @@ public class RetrofitHolder { private Retrofit retrofit; private OkHttpClient okHttpClient; + + private OkHttpClient okHttpClientBase; private String baseURL = APIUtils.API_BASE_URI; + private String accessToken = null; + + private Interceptor oAuthInterceptor; + public Retrofit getRetrofit() { return retrofit; } @@ -33,6 +42,7 @@ public class RetrofitHolder { public RetrofitHolder(OkHttpClient okHttpClient) { this.okHttpClient = okHttpClient; + this.okHttpClientBase = okHttpClient; this.retrofit = createRetrofit(okHttpClient, APIUtils.API_BASE_URI); } @@ -46,4 +56,18 @@ public class RetrofitHolder { .addConverterFactory(GsonConverterFactory.create()) .build(); } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + OkHttpClient.Builder builder = okHttpClientBase.newBuilder(); + Log.d("RetrofitHolder", "Access token changed"); + if (accessToken != null && !accessToken.equals("")) { + Log.i("RetrofitHolder", "Setting access token interceptor"); + oAuthInterceptor = APIUtils.getOAuthInterceptor(accessToken); + builder.addInterceptor(oAuthInterceptor); + } + okHttpClient = builder.build(); + retrofit = createRetrofit(okHttpClient, baseURL); + } + } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java b/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java index 9f258980..efe8f65c 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/asynctasks/SwitchAccount.java @@ -2,6 +2,7 @@ package eu.toldi.infinityforlemmy.asynctasks; import android.content.SharedPreferences; import android.os.Handler; +import android.util.Log; import java.util.concurrent.Executor; @@ -25,18 +26,31 @@ public class SwitchAccount { .putString(SharedPreferencesUtils.ACCESS_TOKEN, account.getAccessToken()) .putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getDisplay_name()) .putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, account.getAccountName()) - .putString(SharedPreferencesUtils.ACCOUNT_INSTANCE,account.getInstance_url()) + .putString(SharedPreferencesUtils.ACCOUNT_INSTANCE, account.getInstance_url()) + .putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, false) .putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, account.getProfileImageUrl()).apply(); retrofitHolder.setBaseURL(account.getInstance_url()); + retrofitHolder.setAccessToken(null); FetchSiteInfo.fetchSiteInfo(retrofitHolder.getRetrofit(), account.getAccessToken(), new FetchSiteInfo.FetchSiteInfoListener() { @Override public void onFetchSiteInfoSuccess(SiteInfo siteInfo) { boolean canDownvote = siteInfo.isEnable_downvotes(); currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply(); + String[] version = siteInfo.getVersion().split("\\."); + if (version.length > 0) { + Log.d("SwitchAccount", "Lemmy Version: " + version[0] + "." + version[1]); + int majorVersion = Integer.parseInt(version[0]); + int minorVersion = Integer.parseInt(version[1]); + if (majorVersion > 0 || (majorVersion == 0 && minorVersion >= 19)) { + retrofitHolder.setAccessToken(account.getAccessToken()); + currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.BEARER_TOKEN_AUTH, true).apply(); + } + } } @Override public void onFetchSiteInfoFailed() { + Log.e("SwitchAccount", "Failed to fetch site info"); currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply(); } }); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java index d7ba99b7..e14d98db 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/site/SiteInfo.java @@ -20,13 +20,16 @@ public class SiteInfo { private boolean enable_nsfw; private boolean community_creation_admin_only; + private String version; + private List admins; SiteStatistics siteStatistics; - public SiteInfo(int id, String name, String sidebar, String description, boolean enable_downvotes, boolean enable_nsfw, boolean community_creation_admin_only, List admins, SiteStatistics siteStatistics) { + public SiteInfo(int id, String name, String version, String sidebar, String description, boolean enable_downvotes, boolean enable_nsfw, boolean community_creation_admin_only, List admins, SiteStatistics siteStatistics) { this.id = id; this.name = name; + this.version = version; this.sidebar = sidebar; this.description = description; this.enable_downvotes = enable_downvotes; @@ -72,6 +75,10 @@ public class SiteInfo { return siteStatistics; } + public String getVersion() { + return version; + } + public static SiteInfo parseSiteInfo(String siteInfoJson) { try { JSONObject siteInfo = new JSONObject(siteInfoJson); @@ -84,6 +91,7 @@ public class SiteInfo { String sidebar = null; if (site.has("sidebar")) sidebar = site.getString("sidebar"); + String version = siteInfo.getString("version"); String description = null; if (site.has("description")) @@ -106,7 +114,7 @@ public class SiteInfo { } } - return new SiteInfo(id, name, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only, admins, SiteStatistics.parseSiteStatistics(counts)); + return new SiteInfo(id, name, version, sidebar, description, enable_downvotes, enable_nsfw, community_creation_admin_only, admins, SiteStatistics.parseSiteStatistics(counts)); } catch (JSONException e) { e.printStackTrace(); return null; diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java b/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java index 405d5474..154c3e42 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/utils/APIUtils.java @@ -5,7 +5,9 @@ import android.util.Base64; import java.util.HashMap; import java.util.Map; +import okhttp3.Interceptor; import okhttp3.MediaType; +import okhttp3.Request; import okhttp3.RequestBody; /** @@ -153,4 +155,13 @@ public class APIUtils { params.put(APIUtils.USER_AGENT_KEY, APIUtils.USER_AGENT); return params; } + + public static Interceptor getOAuthInterceptor(String accessToken) { + return chain -> { + Request newRequest = chain.request().newBuilder() + .addHeader("Authorization", "Bearer " + accessToken) + .build(); + return chain.proceed(newRequest); + }; + } } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java b/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java index 1ebb4356..0073a9d2 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/utils/SharedPreferencesUtils.java @@ -359,6 +359,8 @@ public class SharedPreferencesUtils { public static final String CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE = "eu.toldi.infinityforlemmy.current_account"; public static final String ACCOUNT_NAME = "account_name"; public static final String ACCESS_TOKEN = "access_token"; + + public static final String BEARER_TOKEN_AUTH = "bearer_token_auth"; public static final String ACCOUNT_IMAGE_URL = "account_image_url"; public static final String REDGIFS_ACCESS_TOKEN = "redgifs_access_token";