Compatibility for Lemmy 0.19

Closes #197
This commit is contained in:
Balazs Toldi 2023-10-06 12:54:57 +02:00
parent 5d2d43eebc
commit 28617cf9a8
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
5 changed files with 62 additions and 3 deletions

View File

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

View File

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

View File

@ -20,13 +20,16 @@ public class SiteInfo {
private boolean enable_nsfw;
private boolean community_creation_admin_only;
private String version;
private List<BasicUserInfo> 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<BasicUserInfo> 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<BasicUserInfo> 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;

View File

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

View File

@ -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";