mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-27 10:04:45 +01:00
parent
5d2d43eebc
commit
28617cf9a8
@ -1,7 +1,10 @@
|
|||||||
package eu.toldi.infinityforlemmy;
|
package eu.toldi.infinityforlemmy;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import eu.toldi.infinityforlemmy.network.SortTypeConverterFactory;
|
import eu.toldi.infinityforlemmy.network.SortTypeConverterFactory;
|
||||||
import eu.toldi.infinityforlemmy.utils.APIUtils;
|
import eu.toldi.infinityforlemmy.utils.APIUtils;
|
||||||
|
import okhttp3.Interceptor;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
import retrofit2.adapter.guava.GuavaCallAdapterFactory;
|
import retrofit2.adapter.guava.GuavaCallAdapterFactory;
|
||||||
@ -12,8 +15,14 @@ public class RetrofitHolder {
|
|||||||
|
|
||||||
private Retrofit retrofit;
|
private Retrofit retrofit;
|
||||||
private OkHttpClient okHttpClient;
|
private OkHttpClient okHttpClient;
|
||||||
|
|
||||||
|
private OkHttpClient okHttpClientBase;
|
||||||
private String baseURL = APIUtils.API_BASE_URI;
|
private String baseURL = APIUtils.API_BASE_URI;
|
||||||
|
|
||||||
|
private String accessToken = null;
|
||||||
|
|
||||||
|
private Interceptor oAuthInterceptor;
|
||||||
|
|
||||||
public Retrofit getRetrofit() {
|
public Retrofit getRetrofit() {
|
||||||
return retrofit;
|
return retrofit;
|
||||||
}
|
}
|
||||||
@ -33,6 +42,7 @@ public class RetrofitHolder {
|
|||||||
|
|
||||||
public RetrofitHolder(OkHttpClient okHttpClient) {
|
public RetrofitHolder(OkHttpClient okHttpClient) {
|
||||||
this.okHttpClient = okHttpClient;
|
this.okHttpClient = okHttpClient;
|
||||||
|
this.okHttpClientBase = okHttpClient;
|
||||||
this.retrofit = createRetrofit(okHttpClient, APIUtils.API_BASE_URI);
|
this.retrofit = createRetrofit(okHttpClient, APIUtils.API_BASE_URI);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,4 +56,18 @@ public class RetrofitHolder {
|
|||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
.build();
|
.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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package eu.toldi.infinityforlemmy.asynctasks;
|
|||||||
|
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
@ -25,18 +26,31 @@ public class SwitchAccount {
|
|||||||
.putString(SharedPreferencesUtils.ACCESS_TOKEN, account.getAccessToken())
|
.putString(SharedPreferencesUtils.ACCESS_TOKEN, account.getAccessToken())
|
||||||
.putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getDisplay_name())
|
.putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getDisplay_name())
|
||||||
.putString(SharedPreferencesUtils.ACCOUNT_QUALIFIED_NAME, account.getAccountName())
|
.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();
|
.putString(SharedPreferencesUtils.ACCOUNT_IMAGE_URL, account.getProfileImageUrl()).apply();
|
||||||
retrofitHolder.setBaseURL(account.getInstance_url());
|
retrofitHolder.setBaseURL(account.getInstance_url());
|
||||||
|
retrofitHolder.setAccessToken(null);
|
||||||
FetchSiteInfo.fetchSiteInfo(retrofitHolder.getRetrofit(), account.getAccessToken(), new FetchSiteInfo.FetchSiteInfoListener() {
|
FetchSiteInfo.fetchSiteInfo(retrofitHolder.getRetrofit(), account.getAccessToken(), new FetchSiteInfo.FetchSiteInfoListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
|
public void onFetchSiteInfoSuccess(SiteInfo siteInfo) {
|
||||||
boolean canDownvote = siteInfo.isEnable_downvotes();
|
boolean canDownvote = siteInfo.isEnable_downvotes();
|
||||||
currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, canDownvote).apply();
|
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
|
@Override
|
||||||
public void onFetchSiteInfoFailed() {
|
public void onFetchSiteInfoFailed() {
|
||||||
|
Log.e("SwitchAccount", "Failed to fetch site info");
|
||||||
currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply();
|
currentAccountSharedPreferences.edit().putBoolean(SharedPreferencesUtils.CAN_DOWNVOTE, true).apply();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -20,13 +20,16 @@ public class SiteInfo {
|
|||||||
private boolean enable_nsfw;
|
private boolean enable_nsfw;
|
||||||
private boolean community_creation_admin_only;
|
private boolean community_creation_admin_only;
|
||||||
|
|
||||||
|
private String version;
|
||||||
|
|
||||||
private List<BasicUserInfo> admins;
|
private List<BasicUserInfo> admins;
|
||||||
|
|
||||||
SiteStatistics siteStatistics;
|
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.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.version = version;
|
||||||
this.sidebar = sidebar;
|
this.sidebar = sidebar;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.enable_downvotes = enable_downvotes;
|
this.enable_downvotes = enable_downvotes;
|
||||||
@ -72,6 +75,10 @@ public class SiteInfo {
|
|||||||
return siteStatistics;
|
return siteStatistics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
public static SiteInfo parseSiteInfo(String siteInfoJson) {
|
public static SiteInfo parseSiteInfo(String siteInfoJson) {
|
||||||
try {
|
try {
|
||||||
JSONObject siteInfo = new JSONObject(siteInfoJson);
|
JSONObject siteInfo = new JSONObject(siteInfoJson);
|
||||||
@ -84,6 +91,7 @@ public class SiteInfo {
|
|||||||
String sidebar = null;
|
String sidebar = null;
|
||||||
if (site.has("sidebar"))
|
if (site.has("sidebar"))
|
||||||
sidebar = site.getString("sidebar");
|
sidebar = site.getString("sidebar");
|
||||||
|
String version = siteInfo.getString("version");
|
||||||
|
|
||||||
String description = null;
|
String description = null;
|
||||||
if (site.has("description"))
|
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) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
|
@ -5,7 +5,9 @@ import android.util.Base64;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import okhttp3.Interceptor;
|
||||||
import okhttp3.MediaType;
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.Request;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,4 +155,13 @@ public class APIUtils {
|
|||||||
params.put(APIUtils.USER_AGENT_KEY, APIUtils.USER_AGENT);
|
params.put(APIUtils.USER_AGENT_KEY, APIUtils.USER_AGENT);
|
||||||
return params;
|
return params;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Interceptor getOAuthInterceptor(String accessToken) {
|
||||||
|
return chain -> {
|
||||||
|
Request newRequest = chain.request().newBuilder()
|
||||||
|
.addHeader("Authorization", "Bearer " + accessToken)
|
||||||
|
.build();
|
||||||
|
return chain.proceed(newRequest);
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE = "eu.toldi.infinityforlemmy.current_account";
|
||||||
public static final String ACCOUNT_NAME = "account_name";
|
public static final String ACCOUNT_NAME = "account_name";
|
||||||
public static final String ACCESS_TOKEN = "access_token";
|
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 ACCOUNT_IMAGE_URL = "account_image_url";
|
||||||
public static final String REDGIFS_ACCESS_TOKEN = "redgifs_access_token";
|
public static final String REDGIFS_ACCESS_TOKEN = "redgifs_access_token";
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user