This commit is contained in:
stom79 2019-01-03 10:23:31 +01:00
parent eebe401aaf
commit ede0e4d145
5 changed files with 59 additions and 36 deletions

View File

@ -103,6 +103,7 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveInstanceAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMetaDataAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveRemoteDataAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
@ -210,6 +211,7 @@ public abstract class BaseMainActivity extends BaseActivity
private HashMap<String, Integer> tabPosition = new HashMap<>();
public static HashMap<Integer, RetrieveFeedsAsyncTask.Type> typePosition = new HashMap<>();
private FloatingActionButton federatedTimelines;
private UpdateAccountInfoAsyncTask.SOCIAL social;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -1265,10 +1267,10 @@ public abstract class BaseMainActivity extends BaseActivity
});
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(getApplicationContext()));
Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
Account account = new AccountDAO(getApplicationContext(), db).getAccountByToken(token);
if( account == null){
Helper.logout(getApplicationContext());
Intent myIntent = new Intent(BaseMainActivity.this, LoginActivity.class);
@ -1276,6 +1278,7 @@ public abstract class BaseMainActivity extends BaseActivity
finish();
return;
}
social = (account.getSocial() == null || account.getSocial().equals("MASTODON")? UpdateAccountInfoAsyncTask.SOCIAL.MASTODON: UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE);
//Image loader configuration
final DrawerLayout drawer = findViewById(R.id.drawer_layout);
@ -2030,7 +2033,7 @@ public abstract class BaseMainActivity extends BaseActivity
updateHomeCounter();
//Proceeds to update of the authenticated account
if(Helper.isLoggedIn(getApplicationContext())) {
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new UpdateAccountInfoByIDAsyncTask(getApplicationContext(), social, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
if( lastHomeId != null && homeFragment != null){
homeFragment.retrieveMissingToots(lastHomeId);

View File

@ -60,10 +60,13 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
Account account = null;
if( social == SOCIAL.MASTODON)
if( social == SOCIAL.MASTODON) {
account = new API(this.contextReference.get(), instance, null).verifyCredentials();
else if( social == SOCIAL.PEERTUBE)
account.setSocial("MASTODON");
}else if( social == SOCIAL.PEERTUBE) {
account = new PeertubeAPI(this.contextReference.get(), instance, null).verifyCredentials();
account.setSocial("PEERTUBE");
}
if( account == null)
return null;
@ -75,7 +78,6 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
if( token == null) {
token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
}
account.setToken(token);
account.setInstance(instance);
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();

View File

@ -25,6 +25,7 @@ import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.PeertubeAPI;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
@ -41,10 +42,12 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
private OnUpdateAccountInfoInterface listener;
private WeakReference<Context> contextReference;
private UpdateAccountInfoAsyncTask.SOCIAL social;
public UpdateAccountInfoByIDAsyncTask(Context context, OnUpdateAccountInfoInterface onUpdateAccountInfoInterface){
public UpdateAccountInfoByIDAsyncTask(Context context, UpdateAccountInfoAsyncTask.SOCIAL social, OnUpdateAccountInfoInterface onUpdateAccountInfoInterface){
this.contextReference = new WeakReference<>(context);
this.listener = onUpdateAccountInfoInterface;
this.social = social;
}
@Override
@ -52,7 +55,13 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
SharedPreferences sharedpreferences = this.contextReference.get().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Account account = new API(this.contextReference.get()).getAccount(userId);
Account account = null;
if( social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON)
account = new API(this.contextReference.get()).getAccount(userId);
else if( social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
account = new PeertubeAPI(this.contextReference.get()).verifyCredentials();
if( account == null)
return null;
account.setInstance(Helper.getLiveInstance(contextReference.get()));
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
boolean userExists = new AccountDAO(this.contextReference.get(), db).userExist(account);
@ -64,15 +73,18 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
new AccountDAO(this.contextReference.get(), db).updateAccount(account);
}
}
try {
APIResponse response = new API(contextReference.get()).getCustomEmoji();
if( response != null && response.getEmojis() != null && response.getEmojis().size() > 0){
new CustomEmojiDAO(contextReference.get(), db).removeAll();
for(Emojis emojis: response.getEmojis()){
new CustomEmojiDAO(contextReference.get(), db).insertEmoji(emojis);
if( social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
try {
APIResponse response = new API(contextReference.get()).getCustomEmoji();
if (response != null && response.getEmojis() != null && response.getEmojis().size() > 0) {
new CustomEmojiDAO(contextReference.get(), db).removeAll();
for (Emojis emojis : response.getEmojis()) {
new CustomEmojiDAO(contextReference.get(), db).insertEmoji(emojis);
}
}
} catch (Exception ignored) {
}
}catch (Exception ignored){}
}
return null;
}

View File

@ -240,7 +240,8 @@ public class PeertubeAPI {
account = new Account();
try {
String response = new HttpsConnection(context).get(getAbsoluteUrl("/users/me"), 60, null, prefKeyOauthTokenT);
account = parseAccountResponsePeertube(context, new JSONObject(response));
JSONObject accountObject = new JSONObject(response).getJSONObject("account");
account = parseAccountResponsePeertube(context, accountObject);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
e.printStackTrace();
@ -3534,39 +3535,43 @@ public class PeertubeAPI {
* @param resobj JSONObject
* @return Account
*/
private static Account parseAccountResponsePeertube(Context context, JSONObject resobj){
private static Account parseAccountResponsePeertube(Context context, JSONObject accountObject){
Account account = new Account();
try {
account.setId(resobj.get("id").toString());
account.setUsername(resobj.get("name").toString());
account.setAcct(resobj.get("name").toString() + "@"+ resobj.get("host").toString());
account.setDisplay_name(resobj.get("displayName").toString());
account.setHost(resobj.get("host").toString());
if( resobj.has("createdAt") )
account.setCreated_at(Helper.mstStringToDate(context, resobj.get("createdAt").toString()));
account.setId(accountObject.get("id").toString());
account.setUsername(accountObject.get("name").toString());
account.setAcct(accountObject.get("name").toString()+"@" + accountObject.get("host"));
account.setDisplay_name(accountObject.get("name").toString());
account.setHost(accountObject.get("host").toString());
if( accountObject.has("createdAt") )
account.setCreated_at(Helper.mstStringToDate(context, accountObject.get("createdAt").toString()));
else
account.setCreated_at(new Date());
if( resobj.has("followersCount") )
account.setFollowers_count(Integer.valueOf(resobj.get("followersCount").toString()));
if( accountObject.has("followersCount") )
account.setFollowers_count(Integer.valueOf(accountObject.get("followersCount").toString()));
else
account.setFollowers_count(0);
if( resobj.has("followingCount"))
account.setFollowing_count(Integer.valueOf(resobj.get("followingCount").toString()));
if( accountObject.has("followingCount"))
account.setFollowing_count(Integer.valueOf(accountObject.get("followingCount").toString()));
else
account.setFollowing_count(0);
account.setStatuses_count(0);
if( resobj.has("description") )
account.setNote(resobj.get("description").toString());
if( accountObject.has("description") )
account.setNote(accountObject.get("description").toString());
else
account.setNote("");
account.setUrl(resobj.get("url").toString());
if( resobj.has("avatar") && !resobj.get("avatar").toString().equals("null")){
account.setAvatar(resobj.getJSONObject("avatar").get("path").toString());
account.setUrl(accountObject.get("url").toString());
if( accountObject.has("avatar") && !accountObject.getJSONObject("avatar").toString().equals("null")){
account.setAvatar(accountObject.getJSONObject("avatar").get("path").toString());
}else
account.setAvatar(null);
account.setAvatar_static(resobj.get("avatar").toString());
} catch (JSONException ignored) {} catch (ParseException e) {
account.setHeader("null");
account.setHeader_static("null");
account.setAvatar_static(accountObject.get("avatar").toString());
} catch (JSONException ignored) {ignored.printStackTrace();} catch (ParseException e) {
e.printStackTrace();
}
return account;

View File

@ -78,6 +78,7 @@ public class AccountDAO {
db.insert(Sqlite.TABLE_USER_ACCOUNT, null, values);
}catch (Exception e) {
e.printStackTrace();
return false;
}
return true;