2020-12-30 15:37:09 +01:00
|
|
|
package app.fedilab.fedilabtube.client;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
|
|
|
|
import app.fedilab.fedilabtube.MainActivity;
|
|
|
|
import app.fedilab.fedilabtube.R;
|
|
|
|
import app.fedilab.fedilabtube.client.data.AccountData;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Error;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Oauth;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.OauthParams;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Token;
|
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
|
|
|
import app.fedilab.fedilabtube.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.fedilabtube.sqlite.Sqlite;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Response;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
|
|
|
public class RetrofitMastodonAPI {
|
|
|
|
|
|
|
|
private final String finalUrl;
|
|
|
|
private final Context _context;
|
2020-12-31 11:19:43 +01:00
|
|
|
private String instance;
|
2020-12-30 15:37:09 +01:00
|
|
|
private String token;
|
|
|
|
|
|
|
|
|
|
|
|
public RetrofitMastodonAPI(Context context, String instance, String token) {
|
|
|
|
_context = context;
|
|
|
|
this.instance = instance;
|
|
|
|
this.token = token;
|
|
|
|
finalUrl = "https://" + instance + "/api/v1/";
|
|
|
|
}
|
|
|
|
|
2020-12-31 11:19:43 +01:00
|
|
|
public void updateCredential(Activity activity, String client_id, String client_secret, String refresh_token, String software) {
|
2020-12-30 15:37:09 +01:00
|
|
|
new Thread(() -> {
|
|
|
|
AccountData.Account account;
|
|
|
|
try {
|
|
|
|
account = new RetrofitMastodonAPI(activity, instance, token).verifyCredentials();
|
|
|
|
} catch (Error error) {
|
|
|
|
Error.displayError(activity, error);
|
|
|
|
error.printStackTrace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
//At the state the instance can be encoded
|
|
|
|
instance = URLDecoder.decode(instance, "utf-8");
|
|
|
|
} catch (UnsupportedEncodingException ignored) {
|
|
|
|
}
|
|
|
|
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
account.setToken(token);
|
|
|
|
account.setClient_id(client_id);
|
|
|
|
account.setClient_secret(client_secret);
|
|
|
|
account.setRefresh_token(refresh_token);
|
|
|
|
account.setHost(instance);
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
boolean userExists = new AccountDAO(activity, db).userExist(account);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.PREF_KEY_ID, account.getId());
|
|
|
|
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
|
|
|
|
editor.apply();
|
|
|
|
if (userExists)
|
|
|
|
new AccountDAO(activity, db).updateAccountCredential(account);
|
|
|
|
else {
|
|
|
|
if (account.getUsername() != null && account.getCreatedAt() != null)
|
|
|
|
new AccountDAO(activity, db).insertAccount(account, software);
|
|
|
|
}
|
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
Runnable myRunnable = () -> {
|
|
|
|
Intent mainActivity = new Intent(activity, MainActivity.class);
|
|
|
|
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.ADD_USER_INTENT);
|
|
|
|
activity.startActivity(mainActivity);
|
|
|
|
activity.finish();
|
|
|
|
};
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private MastodonService init() {
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
.baseUrl(finalUrl)
|
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
|
|
|
.build();
|
|
|
|
SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
if (token == null) {
|
|
|
|
token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
|
|
|
}
|
|
|
|
return retrofit.create(MastodonService.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Oauth
|
|
|
|
*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public Oauth oauthClient(String client_name, String redirect_uris, String scopes, String website) {
|
|
|
|
MastodonService mastodonService = init();
|
|
|
|
try {
|
|
|
|
Call<Oauth> oauth;
|
|
|
|
oauth = mastodonService.getOauth(client_name, redirect_uris, scopes, website);
|
|
|
|
Response<Oauth> response = oauth.execute();
|
2020-12-31 11:19:43 +01:00
|
|
|
|
2020-12-30 15:37:09 +01:00
|
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
|
|
return response.body();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Verifiy credential of the authenticated user *synchronously*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public AccountData.Account verifyCredentials() throws Error {
|
|
|
|
MastodonService mastodonService = init();
|
|
|
|
Call<AccountData.Account> accountCall = mastodonService.verifyCredentials("Bearer " + token);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<AccountData.Account> response = accountCall.execute();
|
|
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
|
|
return response.body();
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.errorBody().string());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Verifiy credential of the authenticated user *synchronously*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public Token manageToken(OauthParams oauthParams) throws Error {
|
|
|
|
MastodonService mastodonService = init();
|
2020-12-31 11:19:43 +01:00
|
|
|
Call<Token> createToken = mastodonService.createToken(
|
|
|
|
oauthParams.getGrant_type(),
|
|
|
|
oauthParams.getClient_id(),
|
|
|
|
oauthParams.getClient_secret(),
|
|
|
|
oauthParams.getRedirect_uri(),
|
|
|
|
oauthParams.getCode()
|
|
|
|
);
|
2020-12-30 15:37:09 +01:00
|
|
|
if (createToken != null) {
|
|
|
|
try {
|
|
|
|
Response<Token> response = createToken.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
return response.body();
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.errorBody().string());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|