2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.client;
|
2017-05-05 16:36:04 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2017-05-05 16:36:04 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-05-05 16:36:04 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
2017-05-05 16:36:04 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
2019-01-10 11:51:25 +01:00
|
|
|
import android.content.Intent;
|
2017-05-05 16:36:04 +02:00
|
|
|
import android.content.SharedPreferences;
|
2018-12-20 18:17:08 +01:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2019-01-10 11:51:25 +01:00
|
|
|
import android.os.Bundle;
|
2019-06-20 16:33:29 +02:00
|
|
|
import android.util.Log;
|
2019-06-15 12:49:38 +02:00
|
|
|
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2018-10-30 14:54:58 +01:00
|
|
|
|
2019-03-24 16:38:12 +01:00
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonObject;
|
2018-10-30 14:54:58 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2018-11-17 14:31:03 +01:00
|
|
|
|
2018-04-22 18:02:00 +02:00
|
|
|
import java.io.ByteArrayInputStream;
|
2018-08-17 18:55:38 +02:00
|
|
|
import java.io.IOException;
|
2017-11-21 18:12:57 +01:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLEncoder;
|
2018-08-17 18:55:38 +02:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2018-08-23 10:52:50 +02:00
|
|
|
import java.text.ParseException;
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.util.ArrayList;
|
2018-10-17 14:34:29 +02:00
|
|
|
import java.util.Date;
|
2017-11-18 08:27:25 +01:00
|
|
|
import java.util.HashMap;
|
2018-08-18 17:37:53 +02:00
|
|
|
import java.util.Iterator;
|
2018-10-24 15:44:57 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.util.List;
|
2019-06-15 11:43:32 +02:00
|
|
|
import java.util.Locale;
|
2018-08-18 17:37:53 +02:00
|
|
|
import java.util.Map;
|
2019-05-18 11:10:30 +02:00
|
|
|
|
|
|
|
import app.fedilab.android.R;
|
2019-06-15 12:49:38 +02:00
|
|
|
import app.fedilab.android.activities.LoginActivity;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.activities.MainActivity;
|
2019-06-18 18:50:03 +02:00
|
|
|
import app.fedilab.android.asynctasks.PostAdminActionAsyncTask;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.asynctasks.RetrieveOpenCollectiveAsyncTask;
|
|
|
|
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
2019-06-18 16:22:53 +02:00
|
|
|
import app.fedilab.android.client.Entities.AccountAdmin;
|
2019-06-15 11:43:32 +02:00
|
|
|
import app.fedilab.android.client.Entities.AccountCreation;
|
2019-06-18 18:50:03 +02:00
|
|
|
import app.fedilab.android.client.Entities.AdminAction;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.Entities.Application;
|
|
|
|
import app.fedilab.android.client.Entities.Attachment;
|
|
|
|
import app.fedilab.android.client.Entities.Card;
|
|
|
|
import app.fedilab.android.client.Entities.Conversation;
|
|
|
|
import app.fedilab.android.client.Entities.Emojis;
|
|
|
|
import app.fedilab.android.client.Entities.Error;
|
|
|
|
import app.fedilab.android.client.Entities.Filters;
|
|
|
|
import app.fedilab.android.client.Entities.HowToVideo;
|
|
|
|
import app.fedilab.android.client.Entities.Instance;
|
|
|
|
import app.fedilab.android.client.Entities.InstanceNodeInfo;
|
2019-06-14 14:36:13 +02:00
|
|
|
import app.fedilab.android.client.Entities.InstanceReg;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.Entities.InstanceSocial;
|
|
|
|
import app.fedilab.android.client.Entities.Mention;
|
|
|
|
import app.fedilab.android.client.Entities.NodeInfo;
|
|
|
|
import app.fedilab.android.client.Entities.Notification;
|
|
|
|
import app.fedilab.android.client.Entities.Peertube;
|
|
|
|
import app.fedilab.android.client.Entities.Poll;
|
|
|
|
import app.fedilab.android.client.Entities.PollOptions;
|
|
|
|
import app.fedilab.android.client.Entities.Relationship;
|
2019-06-18 17:44:11 +02:00
|
|
|
import app.fedilab.android.client.Entities.Report;
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.Entities.Results;
|
|
|
|
import app.fedilab.android.client.Entities.Schedule;
|
|
|
|
import app.fedilab.android.client.Entities.Status;
|
|
|
|
import app.fedilab.android.client.Entities.StoredStatus;
|
|
|
|
import app.fedilab.android.client.Entities.Tag;
|
|
|
|
import app.fedilab.android.fragments.DisplayNotificationsFragment;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.android.sqlite.Sqlite;
|
|
|
|
import app.fedilab.android.sqlite.TimelineCacheDAO;
|
2019-03-30 16:43:03 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 23/04/2017.
|
|
|
|
* Manage Calls to the REST API
|
2017-11-18 09:55:09 +01:00
|
|
|
* Modify the 16/11/2017 with httpsurlconnection
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class API {
|
|
|
|
|
|
|
|
|
2017-05-28 14:10:19 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
private Account account;
|
|
|
|
private Context context;
|
|
|
|
private Results results;
|
|
|
|
private Attachment attachment;
|
|
|
|
private List<Account> accounts;
|
|
|
|
private List<Status> statuses;
|
2018-10-29 15:49:13 +01:00
|
|
|
private List<Conversation> conversations;
|
2017-05-05 16:36:04 +02:00
|
|
|
private int tootPerPage, accountPerPage, notificationPerPage;
|
|
|
|
private int actionCode;
|
2017-05-28 14:10:19 +02:00
|
|
|
private String instance;
|
2017-05-28 17:16:31 +02:00
|
|
|
private String prefKeyOauthTokenT;
|
2017-06-03 18:18:27 +02:00
|
|
|
private APIResponse apiResponse;
|
|
|
|
private Error APIError;
|
2018-09-26 18:30:08 +02:00
|
|
|
private List<String> domains;
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2019-03-31 12:40:51 +02:00
|
|
|
public enum searchType{
|
|
|
|
TAGS,
|
|
|
|
STATUSES,
|
|
|
|
ACCOUNTS
|
|
|
|
}
|
|
|
|
|
2019-06-18 16:22:53 +02:00
|
|
|
|
|
|
|
public enum adminAction{
|
|
|
|
ENABLE,
|
|
|
|
APPROVE,
|
|
|
|
REJECT,
|
2019-06-22 11:01:01 +02:00
|
|
|
NONE,
|
|
|
|
SILENCE,
|
|
|
|
UNDISABLE,
|
|
|
|
DISABLE,
|
2019-06-18 16:22:53 +02:00
|
|
|
UNSILENCE,
|
2019-06-22 11:01:01 +02:00
|
|
|
SUSPEND,
|
2019-06-18 16:22:53 +02:00
|
|
|
UNSUSPEND,
|
|
|
|
ASSIGN_TO_SELF,
|
|
|
|
UNASSIGN,
|
|
|
|
REOPEN,
|
|
|
|
RESOLVE,
|
|
|
|
GET_ACCOUNTS,
|
|
|
|
GET_ONE_ACCOUNT,
|
|
|
|
GET_REPORTS,
|
|
|
|
GET_ONE_REPORT
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:33:29 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
public enum StatusAction{
|
|
|
|
FAVOURITE,
|
|
|
|
UNFAVOURITE,
|
|
|
|
REBLOG,
|
|
|
|
UNREBLOG,
|
|
|
|
MUTE,
|
2017-12-17 11:43:29 +01:00
|
|
|
MUTE_NOTIFICATIONS,
|
2017-05-05 16:36:04 +02:00
|
|
|
UNMUTE,
|
2019-06-06 18:16:43 +02:00
|
|
|
MUTE_CONVERSATION,
|
|
|
|
UNMUTE_CONVERSATION,
|
2017-05-05 16:36:04 +02:00
|
|
|
BLOCK,
|
|
|
|
UNBLOCK,
|
|
|
|
FOLLOW,
|
|
|
|
UNFOLLOW,
|
|
|
|
CREATESTATUS,
|
|
|
|
UNSTATUS,
|
2017-06-07 11:12:40 +02:00
|
|
|
AUTHORIZE,
|
|
|
|
REJECT,
|
2017-08-22 17:34:26 +02:00
|
|
|
REPORT,
|
2017-09-12 17:29:43 +02:00
|
|
|
REMOTE_FOLLOW,
|
|
|
|
PIN,
|
2018-09-04 19:27:26 +02:00
|
|
|
UNPIN,
|
|
|
|
ENDORSE,
|
|
|
|
UNENDORSE,
|
|
|
|
SHOW_BOOST,
|
2018-09-26 19:07:22 +02:00
|
|
|
HIDE_BOOST,
|
2019-01-04 16:41:47 +01:00
|
|
|
BLOCK_DOMAIN,
|
|
|
|
RATEVIDEO,
|
|
|
|
PEERTUBECOMMENT,
|
|
|
|
PEERTUBEREPLY,
|
2019-01-12 17:36:24 +01:00
|
|
|
PEERTUBEDELETECOMMENT,
|
2019-01-20 15:20:07 +01:00
|
|
|
PEERTUBEDELETEVIDEO,
|
|
|
|
UPDATESERVERSCHEDULE,
|
2019-03-26 19:07:25 +01:00
|
|
|
DELETESCHEDULED,
|
|
|
|
REFRESHPOLL
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2019-06-20 16:33:29 +02:00
|
|
|
|
|
|
|
|
2018-03-14 16:34:03 +01:00
|
|
|
public enum accountPrivacy {
|
|
|
|
PUBLIC,
|
|
|
|
LOCKED
|
|
|
|
}
|
2019-06-20 16:33:29 +02:00
|
|
|
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
public API(Context context) {
|
|
|
|
this.context = context;
|
2019-02-03 15:00:27 +01:00
|
|
|
if( context == null) {
|
|
|
|
APIError = new Error();
|
|
|
|
return;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
tootPerPage = sharedpreferences.getInt(Helper.SET_TOOTS_PER_PAGE, 40);
|
|
|
|
accountPerPage = sharedpreferences.getInt(Helper.SET_ACCOUNTS_PER_PAGE, 40);
|
2017-07-01 16:43:53 +02:00
|
|
|
notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15);
|
2019-01-19 19:41:55 +01:00
|
|
|
this.prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
2018-12-20 18:17:08 +01:00
|
|
|
if( Helper.getLiveInstance(context) != null)
|
|
|
|
this.instance = Helper.getLiveInstance(context);
|
|
|
|
else {
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2019-02-08 15:07:46 +01:00
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context));
|
|
|
|
Account account = new AccountDAO(context, db).getUniqAccount(userId, instance);
|
2019-01-19 19:41:55 +01:00
|
|
|
if( account == null) {
|
|
|
|
APIError = new Error();
|
|
|
|
APIError.setError(context.getString(R.string.toast_error));
|
|
|
|
return;
|
|
|
|
}
|
2018-12-20 18:17:08 +01:00
|
|
|
this.instance = account.getInstance().trim();
|
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse = new APIResponse();
|
|
|
|
APIError = null;
|
2017-05-28 14:10:19 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 16:22:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute admin get actions
|
|
|
|
* @param action type of the action
|
|
|
|
* @param id String can for an account or a status
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2019-06-20 16:33:29 +02:00
|
|
|
public APIResponse adminGet(adminAction action, String id, AdminAction adminAction){
|
2019-06-18 16:22:53 +02:00
|
|
|
apiResponse = new APIResponse();
|
2019-06-20 16:33:29 +02:00
|
|
|
HashMap<String, String> params = null;
|
2019-06-18 16:22:53 +02:00
|
|
|
String endpoint = null;
|
|
|
|
switch (action){
|
|
|
|
case GET_ACCOUNTS:
|
2019-06-20 18:48:46 +02:00
|
|
|
params = new HashMap<>();
|
|
|
|
if( adminAction.isLocal())
|
|
|
|
params.put("local", String.valueOf(adminAction.isLocal()));
|
|
|
|
if( adminAction.isRemote() )
|
|
|
|
params.put("remote", String.valueOf(adminAction.isRemote()));
|
|
|
|
if( adminAction.isActive() )
|
|
|
|
params.put("active", String.valueOf(adminAction.isActive()));
|
|
|
|
if( adminAction.isPending() )
|
|
|
|
params.put("pending", String.valueOf(adminAction.isPending()));
|
|
|
|
if( adminAction.isDisabled() )
|
|
|
|
params.put("disabled", String.valueOf(adminAction.isDisabled()));
|
|
|
|
if( adminAction.isSilenced() )
|
|
|
|
params.put("silenced", String.valueOf(adminAction.isSilenced()));
|
|
|
|
if( adminAction.isSuspended() )
|
|
|
|
params.put("suspended", String.valueOf(adminAction.isSuspended()));
|
2019-06-18 16:22:53 +02:00
|
|
|
endpoint = "/admin/accounts";
|
|
|
|
break;
|
|
|
|
case GET_ONE_ACCOUNT:
|
|
|
|
endpoint = String.format("/admin/accounts/%s", id);
|
|
|
|
break;
|
|
|
|
case GET_REPORTS:
|
|
|
|
endpoint = "/admin/reports";
|
2019-06-20 16:33:29 +02:00
|
|
|
if( !adminAction.isUnresolved()) {
|
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("resolved", "present");
|
|
|
|
}
|
2019-06-18 16:22:53 +02:00
|
|
|
break;
|
|
|
|
case GET_ONE_REPORT:
|
|
|
|
endpoint = String.format("/admin/reports/%s", id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl(endpoint), 60, params, prefKeyOauthTokenT);
|
2019-06-18 18:18:12 +02:00
|
|
|
switch (action){
|
|
|
|
case GET_ACCOUNTS:
|
|
|
|
List<AccountAdmin> accountAdmins = parseAccountAdminResponse(new JSONArray(response));
|
|
|
|
apiResponse.setAccountAdmins(accountAdmins);
|
|
|
|
break;
|
|
|
|
case GET_ONE_ACCOUNT:
|
|
|
|
AccountAdmin accountAdmin = parseAccountAdminResponse(context, new JSONObject(response));
|
|
|
|
accountAdmins = new ArrayList<>();
|
|
|
|
accountAdmins.add(accountAdmin);
|
|
|
|
apiResponse.setAccountAdmins(accountAdmins);
|
|
|
|
break;
|
|
|
|
case GET_REPORTS:
|
|
|
|
List<Report> reports = parseReportAdminResponse(new JSONArray(response));
|
|
|
|
apiResponse.setReports(reports);
|
|
|
|
break;
|
|
|
|
case GET_ONE_REPORT:
|
|
|
|
reports = new ArrayList<>();
|
|
|
|
Report report = parseReportAdminResponse(context, new JSONObject(response));
|
|
|
|
reports.add(report);
|
|
|
|
apiResponse.setReports(reports);
|
|
|
|
break;
|
|
|
|
}
|
2019-06-18 16:22:53 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2019-06-19 11:04:44 +02:00
|
|
|
e.printStackTrace();
|
2019-06-18 18:18:12 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2019-06-18 16:22:53 +02:00
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute admin post actions
|
|
|
|
* @param action type of the action
|
|
|
|
* @param id String can for an account or a status
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2019-06-18 18:50:03 +02:00
|
|
|
public APIResponse adminDo(adminAction action, String id, AdminAction adminAction){
|
2019-06-18 16:22:53 +02:00
|
|
|
apiResponse = new APIResponse();
|
|
|
|
String endpoint = null;
|
2019-06-18 18:50:03 +02:00
|
|
|
HashMap<String, String> params = null;
|
2019-06-18 16:22:53 +02:00
|
|
|
switch (action){
|
|
|
|
case ENABLE:
|
|
|
|
endpoint = String.format("/admin/accounts/%s/enable", id);
|
|
|
|
break;
|
|
|
|
case APPROVE:
|
|
|
|
endpoint = String.format("/admin/accounts/%s/approve", id);
|
|
|
|
break;
|
|
|
|
case REJECT:
|
|
|
|
endpoint = String.format("/admin/accounts/%s/reject", id);
|
|
|
|
break;
|
|
|
|
case UNSILENCE:
|
|
|
|
endpoint = String.format("/admin/accounts/%s/unsilence", id);
|
|
|
|
break;
|
|
|
|
case UNSUSPEND:
|
|
|
|
endpoint = String.format("/admin/accounts/%s/unsuspend", id);
|
|
|
|
break;
|
|
|
|
case ASSIGN_TO_SELF:
|
2019-06-18 18:18:12 +02:00
|
|
|
endpoint = String.format("/admin/reports/%s/assign_to_self", id);
|
2019-06-18 16:22:53 +02:00
|
|
|
break;
|
|
|
|
case UNASSIGN:
|
2019-06-18 18:18:12 +02:00
|
|
|
endpoint = String.format("/admin/reports/%s/unassign", id);
|
2019-06-18 16:22:53 +02:00
|
|
|
break;
|
|
|
|
case REOPEN:
|
2019-06-18 18:18:12 +02:00
|
|
|
endpoint = String.format("/admin/reports/%s/reopen", id);
|
2019-06-18 16:22:53 +02:00
|
|
|
break;
|
|
|
|
case RESOLVE:
|
2019-06-18 18:18:12 +02:00
|
|
|
endpoint = String.format("/admin/reports/%s/resolve", id);
|
2019-06-18 16:22:53 +02:00
|
|
|
break;
|
2019-06-22 11:01:01 +02:00
|
|
|
case NONE:
|
2019-06-18 18:50:03 +02:00
|
|
|
params = new HashMap<>();
|
2019-06-22 11:01:01 +02:00
|
|
|
params.put("type","none");
|
|
|
|
endpoint = String.format("/admin/accounts/%s/action", id);
|
|
|
|
params.put("send_email_notification", String.valueOf(adminAction.isSend_email_notification()));
|
|
|
|
if( adminAction.getText() != null) {
|
|
|
|
params.put("text", adminAction.getText());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DISABLE:
|
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("type","disable");
|
|
|
|
endpoint = String.format("/admin/accounts/%s/action", id);
|
|
|
|
params.put("send_email_notification", String.valueOf(adminAction.isSend_email_notification()));
|
|
|
|
if( adminAction.getText() != null) {
|
|
|
|
params.put("text", adminAction.getText());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SILENCE:
|
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("type","silence");
|
|
|
|
endpoint = String.format("/admin/accounts/%s/action", id);
|
|
|
|
params.put("send_email_notification", String.valueOf(adminAction.isSend_email_notification()));
|
|
|
|
if( adminAction.getText() != null) {
|
|
|
|
params.put("text", adminAction.getText());
|
2019-06-18 18:50:03 +02:00
|
|
|
}
|
2019-06-22 11:01:01 +02:00
|
|
|
break;
|
|
|
|
case SUSPEND:
|
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("type","suspend");
|
2019-06-18 16:22:53 +02:00
|
|
|
endpoint = String.format("/admin/accounts/%s/action", id);
|
2019-06-18 18:50:03 +02:00
|
|
|
params.put("send_email_notification", String.valueOf(adminAction.isSend_email_notification()));
|
|
|
|
if( adminAction.getText() != null) {
|
|
|
|
params.put("text", adminAction.getText());
|
|
|
|
}
|
2019-06-18 16:22:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl(endpoint), 60, params, prefKeyOauthTokenT);
|
2019-06-18 18:18:12 +02:00
|
|
|
switch (action){
|
|
|
|
case ENABLE:
|
|
|
|
case APPROVE:
|
|
|
|
case REJECT:
|
|
|
|
case UNSILENCE:
|
|
|
|
case UNSUSPEND:
|
|
|
|
List<AccountAdmin> accountAdmins = null;
|
|
|
|
try {
|
|
|
|
AccountAdmin accountAdmin = parseAccountAdminResponse(context, new JSONObject(response));
|
2019-06-22 11:01:01 +02:00
|
|
|
accountAdmin.setAction(action);
|
2019-06-18 18:18:12 +02:00
|
|
|
accountAdmins = new ArrayList<>();
|
|
|
|
accountAdmins.add(accountAdmin);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setAccountAdmins(accountAdmins);
|
|
|
|
break;
|
|
|
|
case ASSIGN_TO_SELF:
|
|
|
|
case UNASSIGN:
|
|
|
|
case REOPEN:
|
|
|
|
case RESOLVE:
|
|
|
|
List<Report> reports = null;
|
|
|
|
Report report;
|
|
|
|
try {
|
|
|
|
reports = new ArrayList<>();
|
|
|
|
report = parseReportAdminResponse(context, new JSONObject(response));
|
|
|
|
reports.add(report);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setReports(reports);
|
|
|
|
|
|
|
|
break;
|
2019-06-22 11:01:01 +02:00
|
|
|
case NONE:
|
|
|
|
break;
|
|
|
|
case DISABLE:
|
|
|
|
accountAdmins = new ArrayList<>();
|
|
|
|
AccountAdmin accountAdmin = new AccountAdmin();
|
|
|
|
accountAdmin.setDisabled(true);
|
|
|
|
accountAdmin.setAction(action);
|
|
|
|
accountAdmins.add(accountAdmin);
|
|
|
|
apiResponse.setAccountAdmins(accountAdmins);
|
|
|
|
break;
|
|
|
|
case SILENCE:
|
|
|
|
accountAdmins = new ArrayList<>();
|
|
|
|
accountAdmin = new AccountAdmin();
|
|
|
|
accountAdmin.setSilenced(true);
|
|
|
|
accountAdmin.setAction(action);
|
|
|
|
accountAdmins.add(accountAdmin);
|
|
|
|
apiResponse.setAccountAdmins(accountAdmins);
|
|
|
|
break;
|
|
|
|
case SUSPEND:
|
|
|
|
accountAdmins = new ArrayList<>();
|
|
|
|
accountAdmin = new AccountAdmin();
|
|
|
|
accountAdmin.setSuspended(true);
|
|
|
|
accountAdmin.setAction(action);
|
|
|
|
accountAdmins.add(accountAdmin);
|
|
|
|
apiResponse.setAccountAdmins(accountAdmins);
|
2019-06-18 18:18:12 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-06-18 16:22:53 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2019-02-13 12:55:39 +01:00
|
|
|
public InstanceNodeInfo getNodeInfo(String domain){
|
2019-05-24 15:13:28 +02:00
|
|
|
|
|
|
|
//Try to guess URL scheme for the onion instance
|
|
|
|
String scheme = "https";
|
|
|
|
if( domain.endsWith(".onion")){
|
|
|
|
try {
|
|
|
|
new HttpsConnection(context, domain).get("http://" + domain, 30, null, null);
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.SET_ONION_SCHEME + domain, "http");
|
|
|
|
scheme = "http";
|
|
|
|
editor.apply();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.SET_ONION_SCHEME + domain, "https");
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}
|
2019-02-13 12:55:39 +01:00
|
|
|
String response;
|
|
|
|
InstanceNodeInfo instanceNodeInfo = new InstanceNodeInfo();
|
|
|
|
try {
|
2019-05-24 15:13:28 +02:00
|
|
|
response = new HttpsConnection(context, domain).get(scheme+"://" + domain + "/.well-known/nodeinfo", 30, null, null);
|
2019-02-13 12:55:39 +01:00
|
|
|
JSONArray jsonArray = new JSONObject(response).getJSONArray("links");
|
|
|
|
ArrayList<NodeInfo> nodeInfos = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
NodeInfo nodeInfo = new NodeInfo();
|
|
|
|
nodeInfo.setHref(resobj.getString("href"));
|
|
|
|
nodeInfo.setRel(resobj.getString("rel"));
|
|
|
|
i++;
|
|
|
|
nodeInfos.add(nodeInfo);
|
|
|
|
}
|
|
|
|
if( nodeInfos.size() > 0){
|
2019-06-07 14:09:51 +02:00
|
|
|
NodeInfo nodeInfo = nodeInfos.get(nodeInfos.size()-1);
|
2019-05-23 07:46:34 +02:00
|
|
|
response = new HttpsConnection(context, this.instance).get(nodeInfo.getHref(), 30, null, null);
|
2019-02-13 12:55:39 +01:00
|
|
|
JSONObject resobj = new JSONObject(response);
|
|
|
|
JSONObject jsonObject = resobj.getJSONObject("software");
|
2019-06-07 14:09:51 +02:00
|
|
|
String name= null;
|
|
|
|
if( resobj.has("metadata") && resobj.getJSONObject("metadata").has("features") ) {
|
|
|
|
JSONArray features = resobj.getJSONObject("metadata").getJSONArray("features");
|
|
|
|
if( features != null && features.length() > 0){
|
|
|
|
for( int counter = 0; counter < features.length(); counter++ ) {
|
|
|
|
if( features.getString(counter).toUpperCase().equals("MASTODON_API") ) {
|
|
|
|
name = "MASTODON";
|
|
|
|
break;
|
|
|
|
}
|
2019-06-06 19:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 14:09:51 +02:00
|
|
|
if( name == null) {
|
|
|
|
name = jsonObject.getString("name").toUpperCase();
|
|
|
|
if (jsonObject.getString("name") != null) {
|
|
|
|
switch (jsonObject.getString("name").toUpperCase()) {
|
|
|
|
case "PLEROMA":
|
|
|
|
name = "MASTODON";
|
|
|
|
break;
|
|
|
|
case "GNUSOCIAL":
|
|
|
|
case "HUBZILLA":
|
|
|
|
case "REDMATRIX":
|
|
|
|
case "FRIENDICA":
|
|
|
|
name = "GNU";
|
|
|
|
break;
|
|
|
|
}
|
2019-02-13 12:55:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
instanceNodeInfo.setName(name);
|
|
|
|
instanceNodeInfo.setVersion(jsonObject.getString("version"));
|
|
|
|
instanceNodeInfo.setOpenRegistrations(resobj.getBoolean("openRegistrations"));
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
try {
|
2019-05-24 15:13:28 +02:00
|
|
|
response = new HttpsConnection(context, this.instance).get(scheme+"://" + domain + "/api/v1/instance", 30, null, null);
|
2019-02-13 12:55:39 +01:00
|
|
|
JSONObject jsonObject = new JSONObject(response);
|
|
|
|
instanceNodeInfo.setName("MASTODON");
|
|
|
|
instanceNodeInfo.setVersion(jsonObject.getString("version"));
|
|
|
|
instanceNodeInfo.setOpenRegistrations(true);
|
|
|
|
} catch (IOException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (KeyManagementException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e1) {
|
|
|
|
instanceNodeInfo.setName("GNU");
|
|
|
|
instanceNodeInfo.setVersion("unknown");
|
|
|
|
instanceNodeInfo.setOpenRegistrations(true);
|
2019-02-13 14:47:28 +01:00
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (JSONException e1) {
|
|
|
|
|
2019-02-13 12:55:39 +01:00
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return instanceNodeInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-28 17:16:31 +02:00
|
|
|
public API(Context context, String instance, String token) {
|
2017-05-28 14:10:19 +02:00
|
|
|
this.context = context;
|
2018-01-13 10:41:56 +01:00
|
|
|
if( context == null) {
|
|
|
|
apiResponse = new APIResponse();
|
|
|
|
APIError = new Error();
|
|
|
|
return;
|
|
|
|
}
|
2017-05-28 14:10:19 +02:00
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
tootPerPage = sharedpreferences.getInt(Helper.SET_TOOTS_PER_PAGE, 40);
|
|
|
|
accountPerPage = sharedpreferences.getInt(Helper.SET_ACCOUNTS_PER_PAGE, 40);
|
2017-07-01 16:43:53 +02:00
|
|
|
notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15);
|
2017-05-28 14:10:19 +02:00
|
|
|
if( instance != null)
|
|
|
|
this.instance = instance;
|
|
|
|
else
|
|
|
|
this.instance = Helper.getLiveInstance(context);
|
2017-05-28 17:16:31 +02:00
|
|
|
|
|
|
|
if( token != null)
|
|
|
|
this.prefKeyOauthTokenT = token;
|
|
|
|
else
|
|
|
|
this.prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse = new APIResponse();
|
|
|
|
APIError = null;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 14:10:19 +02:00
|
|
|
|
2019-06-18 16:22:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-05 11:57:02 +02:00
|
|
|
/***
|
|
|
|
* Get info on the current Instance *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getInstance() {
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/instance"), 30, null, prefKeyOauthTokenT);
|
2017-11-18 09:55:09 +01:00
|
|
|
Instance instanceEntity = parseInstance(new JSONObject(response));
|
2017-11-18 08:27:25 +01:00
|
|
|
apiResponse.setInstance(instanceEntity);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-05 11:57:02 +02:00
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2018-04-22 18:02:00 +02:00
|
|
|
|
2019-06-14 14:36:13 +02:00
|
|
|
/***
|
|
|
|
* Get instance for registering an account *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getInstanceReg(String category) {
|
2019-06-14 16:01:58 +02:00
|
|
|
apiResponse = new APIResponse();
|
2019-06-14 14:36:13 +02:00
|
|
|
try {
|
|
|
|
String response = new HttpsConnection(context, null).get(String.format("https://api.joinmastodon.org/servers?category=%s", category));
|
|
|
|
List<InstanceReg> instanceRegs = parseInstanceReg(new JSONArray(response));
|
|
|
|
apiResponse.setInstanceRegs(instanceRegs);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-22 18:02:00 +02:00
|
|
|
|
2017-06-05 11:57:02 +02:00
|
|
|
/***
|
|
|
|
* Update credential of the authenticated user *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2019-05-08 17:50:17 +02:00
|
|
|
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy, HashMap<String, String> customFields, boolean sensitive) {
|
2017-06-05 11:57:02 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> requestParams = new HashMap<>();
|
2017-06-05 11:57:02 +02:00
|
|
|
if( display_name != null)
|
2017-12-16 10:34:07 +01:00
|
|
|
try {
|
|
|
|
requestParams.put("display_name",URLEncoder.encode(display_name, "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
requestParams.put("display_name",display_name);
|
|
|
|
}
|
2017-06-05 11:57:02 +02:00
|
|
|
if( note != null)
|
2017-12-16 10:34:07 +01:00
|
|
|
try {
|
|
|
|
requestParams.put("note",URLEncoder.encode(note, "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
requestParams.put("note",note);
|
|
|
|
}
|
2018-04-22 13:16:52 +02:00
|
|
|
if( privacy != null)
|
|
|
|
requestParams.put("locked",privacy==accountPrivacy.LOCKED?"true":"false");
|
2018-08-18 17:37:53 +02:00
|
|
|
int i = 0;
|
|
|
|
if( customFields != null && customFields.size() > 0){
|
|
|
|
Iterator it = customFields.entrySet().iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Map.Entry pair = (Map.Entry)it.next();
|
|
|
|
requestParams.put("fields_attributes["+i+"][name]",(String)pair.getKey());
|
|
|
|
requestParams.put("fields_attributes["+i+"][value]",(String)pair.getValue());
|
|
|
|
it.remove();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 05:04:14 +02:00
|
|
|
requestParams.put("source[sensitive]", String.valueOf(sensitive));
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
new HttpsConnection(context, this.instance).patch(getAbsoluteUrl("/accounts/update_credentials"), 60, requestParams, avatar, avatarName, header, headerName, prefKeyOauthTokenT);
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
2017-12-16 10:34:07 +01:00
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
2017-12-16 10:34:07 +01:00
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-05 11:57:02 +02:00
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/***
|
|
|
|
* Verifiy credential of the authenticated user *synchronously*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public Account verifyCredentials() {
|
|
|
|
account = new Account();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-03-02 18:48:25 +01:00
|
|
|
if( context == null) {
|
|
|
|
setError(500, new Throwable("An error occured!"));
|
|
|
|
return null;
|
|
|
|
}
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/accounts/verify_credentials"), 60, null, prefKeyOauthTokenT);
|
2017-11-18 08:27:25 +01:00
|
|
|
account = parseAccountResponse(context, new JSONObject(response));
|
2019-01-30 09:50:07 +01:00
|
|
|
if( account.getSocial().equals("PLEROMA")){
|
|
|
|
isPleromaAdmin(account.getAcct());
|
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
2019-04-17 15:53:33 +02:00
|
|
|
e.printStackTrace();
|
2019-04-06 16:10:20 +02:00
|
|
|
if( e.getStatusCode() == 401 || e.getStatusCode() == 403){
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
Account targetedAccount = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
|
2019-04-16 17:07:51 +02:00
|
|
|
if( targetedAccount == null)
|
|
|
|
return null;
|
2019-04-06 16:10:20 +02:00
|
|
|
HashMap<String, String> values = refreshToken(targetedAccount.getClient_id(), targetedAccount.getClient_secret(), targetedAccount.getRefresh_token());
|
|
|
|
if( values.containsKey("access_token") && values.get("access_token") != null) {
|
|
|
|
targetedAccount.setToken(values.get("access_token"));
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
|
|
|
//This account is currently logged in, the token is updated
|
|
|
|
if( prefKeyOauthTokenT.equals(token)){
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, targetedAccount.getToken());
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
}if( values.containsKey("refresh_token") && values.get("refresh_token") != null)
|
|
|
|
targetedAccount.setRefresh_token(values.get("refresh_token"));
|
2019-05-08 17:50:17 +02:00
|
|
|
new AccountDAO(context, db).updateAccountCredential(targetedAccount);
|
2019-04-06 16:10:20 +02:00
|
|
|
String response;
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/accounts/verify_credentials"), 60, null, targetedAccount.getToken());
|
2019-04-06 16:10:20 +02:00
|
|
|
account = parseAccountResponse(context, new JSONObject(response));
|
|
|
|
if( account.getSocial().equals("PLEROMA")){
|
|
|
|
isPleromaAdmin(account.getAcct());
|
|
|
|
}
|
|
|
|
} catch (IOException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (KeyManagementException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (JSONException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e1) {
|
|
|
|
e1.printStackTrace();
|
|
|
|
setError(e.getStatusCode(), e);
|
2019-03-26 18:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
2018-01-24 16:27:52 +01:00
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:09:39 +01:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Verifiy credential of the authenticated user *synchronously*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
private HashMap<String, String> refreshToken(String client_id, String client_secret, String refresh_token) {
|
|
|
|
account = new Account();
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
HashMap<String, String> newValues = new HashMap<>();
|
|
|
|
params.put("grant_type", "refresh_token");
|
|
|
|
params.put("client_id", client_id);
|
|
|
|
params.put("client_secret", client_secret);
|
|
|
|
params.put("refresh_token", refresh_token);
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/oauth/token"), 60, params, null);
|
2019-03-26 18:09:39 +01:00
|
|
|
JSONObject resobj = new JSONObject(response);
|
|
|
|
String token = resobj.get("access_token").toString();
|
|
|
|
if( resobj.has("refresh_token"))
|
|
|
|
refresh_token = resobj.get("refresh_token").toString();
|
|
|
|
newValues.put("access_token",token);
|
|
|
|
newValues.put("refresh_token",refresh_token);
|
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return newValues;
|
|
|
|
}
|
|
|
|
|
2019-06-15 11:43:32 +02:00
|
|
|
public APIResponse createAccount(AccountCreation accountCreation){
|
|
|
|
apiResponse = new APIResponse();
|
2019-06-15 12:49:38 +02:00
|
|
|
|
2019-06-15 11:43:32 +02:00
|
|
|
try {
|
2019-06-15 12:49:38 +02:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put(Helper.CLIENT_NAME, Helper.CLIENT_NAME_VALUE);
|
|
|
|
params.put(Helper.REDIRECT_URIS, Helper.REDIRECT_CONTENT);
|
|
|
|
params.put(Helper.SCOPES, Helper.OAUTH_SCOPES);
|
|
|
|
params.put(Helper.WEBSITE, Helper.WEBSITE_VALUE);
|
|
|
|
String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/apps"), 30, params, null);
|
|
|
|
JSONObject resobj = new JSONObject(response);
|
|
|
|
String client_id = resobj.getString("client_id");
|
|
|
|
String client_secret = resobj.getString("client_secret");
|
|
|
|
|
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("grant_type", "client_credentials");
|
|
|
|
params.put("client_id", client_id);
|
|
|
|
params.put("client_secret", client_secret);
|
|
|
|
params.put("scope", "read write");
|
|
|
|
response = new HttpsConnection(context, this.instance).post("https://" + this.instance + "/oauth/token", 30, params, null);
|
|
|
|
JSONObject res = new JSONObject(response);
|
|
|
|
String app_token = res.getString("access_token");
|
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("username", accountCreation.getUsername());
|
|
|
|
params.put("email", accountCreation.getEmail());
|
|
|
|
params.put("password", accountCreation.getPassword());
|
|
|
|
params.put("agreement", "true");
|
|
|
|
params.put("locale", Locale.getDefault().getLanguage());
|
2019-06-15 13:52:46 +02:00
|
|
|
response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/accounts"), 60, params, app_token);
|
|
|
|
|
2019-06-15 14:12:48 +02:00
|
|
|
/*res = new JSONObject(response);
|
2019-06-15 13:52:46 +02:00
|
|
|
String access_token = res.getString("access_token");
|
2019-06-15 14:12:48 +02:00
|
|
|
prefKeyOauthTokenT = access_token;
|
2019-06-15 13:52:46 +02:00
|
|
|
|
2019-06-15 14:12:48 +02:00
|
|
|
response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/accounts/verify_credentials"), 60, null, prefKeyOauthTokenT);
|
|
|
|
account = parseAccountResponse(context, new JSONObject(response));
|
|
|
|
if( account.getSocial().equals("PLEROMA")){
|
|
|
|
isPleromaAdmin(account.getAcct());
|
|
|
|
}
|
2019-06-15 13:52:46 +02:00
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
account.setToken(access_token);
|
|
|
|
account.setClient_id(client_id);
|
|
|
|
account.setClient_secret(client_secret);
|
|
|
|
account.setRefresh_token(null);
|
|
|
|
account.setInstance(instance);
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
boolean userExists = new AccountDAO(context, db).userExist(account);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.PREF_KEY_ID, account.getId());
|
|
|
|
editor.putBoolean(Helper.PREF_IS_MODERATOR, account.isModerator());
|
|
|
|
editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, account.isAdmin());
|
|
|
|
editor.putString(Helper.PREF_INSTANCE, instance);
|
|
|
|
editor.apply();
|
|
|
|
if( userExists)
|
|
|
|
new AccountDAO(context, db).updateAccountCredential(account);
|
|
|
|
else {
|
|
|
|
if( account.getUsername() != null && account.getCreated_at() != null)
|
|
|
|
new AccountDAO(context, db).insertAccount(account);
|
2019-06-15 14:12:48 +02:00
|
|
|
}*/
|
2019-06-15 11:43:32 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
2019-06-15 12:49:38 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2019-06-15 11:43:32 +02:00
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2019-03-26 18:09:39 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Returns an account
|
|
|
|
* @param accountId String account fetched
|
|
|
|
* @return Account entity
|
|
|
|
*/
|
|
|
|
public Account getAccount(String accountId) {
|
|
|
|
|
|
|
|
account = new Account();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl(String.format("/accounts/%s",accountId)), 60, null, prefKeyOauthTokenT);
|
2017-11-18 08:27:25 +01:00
|
|
|
account = parseAccountResponse(context, new JSONObject(response));
|
2019-01-30 09:50:07 +01:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
if( account.getSocial().equals("PLEROMA") && accountId.equals(userId)){
|
|
|
|
isPleromaAdmin(account.getAcct());
|
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a relationship between the authenticated account and an account
|
|
|
|
* @param accountId String account fetched
|
|
|
|
* @return Relationship entity
|
|
|
|
*/
|
|
|
|
public Relationship getRelationship(String accountId) {
|
|
|
|
|
2017-11-18 17:53:30 +01:00
|
|
|
List<Relationship> relationships;
|
|
|
|
Relationship relationship = null;
|
2017-11-18 12:22:41 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
params.put("id",accountId);
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/accounts/relationships"), 60, params, prefKeyOauthTokenT);
|
2017-11-18 17:53:30 +01:00
|
|
|
relationships = parseRelationshipResponse(new JSONArray(response));
|
|
|
|
if( relationships != null && relationships.size() > 0)
|
|
|
|
relationship = relationships.get(0);
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return relationship;
|
|
|
|
}
|
|
|
|
|
2017-09-30 08:36:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-03-24 16:38:12 +01:00
|
|
|
|
2017-09-17 11:50:00 +02:00
|
|
|
/**
|
|
|
|
* Returns a relationship between the authenticated account and an account
|
|
|
|
* @param accounts ArrayList<Account> accounts fetched
|
|
|
|
* @return Relationship entity
|
|
|
|
*/
|
|
|
|
public APIResponse getRelationship(List<Account> accounts) {
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-09-17 11:50:00 +02:00
|
|
|
if( accounts != null && accounts.size() > 0 ) {
|
2017-11-19 08:46:53 +01:00
|
|
|
StringBuilder parameters = new StringBuilder();
|
|
|
|
for(Account account: accounts)
|
|
|
|
parameters.append("id[]=").append(account.getId()).append("&");
|
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(5));
|
|
|
|
params.put("id[]", parameters.toString());
|
2018-11-07 18:00:00 +01:00
|
|
|
List<Relationship> relationships = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-11-07 18:00:00 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/accounts/relationships"), 60, params, prefKeyOauthTokenT);
|
|
|
|
relationships = parseRelationshipResponse(new JSONArray(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setRelationships(relationships);
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2018-11-07 18:00:00 +01:00
|
|
|
|
2017-09-17 11:50:00 +02:00
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves status for the account *synchronously*
|
|
|
|
*
|
|
|
|
* @param accountId String Id of the account
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getStatus(String accountId) {
|
2017-09-15 19:51:41 +02:00
|
|
|
return getStatus(accountId, false, false, false, null, null, tootPerPage);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves status for the account *synchronously*
|
|
|
|
*
|
|
|
|
* @param accountId String Id of the account
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getStatus(String accountId, String max_id) {
|
2017-09-15 19:51:41 +02:00
|
|
|
return getStatus(accountId, false, false, false, max_id, null, tootPerPage);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 14:38:47 +02:00
|
|
|
/**
|
|
|
|
* Retrieves status with media for the account *synchronously*
|
|
|
|
*
|
|
|
|
* @param accountId String Id of the account
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getStatusWithMedia(String accountId, String max_id) {
|
2017-09-15 19:51:41 +02:00
|
|
|
return getStatus(accountId, true, false, false, max_id, null, tootPerPage);
|
2017-08-03 14:38:47 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 17:29:43 +02:00
|
|
|
/**
|
|
|
|
* Retrieves pinned status(es) *synchronously*
|
|
|
|
*
|
|
|
|
* @param accountId String Id of the account
|
2017-09-15 19:51:41 +02:00
|
|
|
* @param max_id String id max
|
2017-09-12 17:29:43 +02:00
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2017-09-15 19:51:41 +02:00
|
|
|
public APIResponse getPinnedStatuses(String accountId, String max_id) {
|
|
|
|
return getStatus(accountId, false, true, false, max_id, null, tootPerPage);
|
2017-09-12 17:29:43 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 10:19:07 +02:00
|
|
|
/**
|
|
|
|
* Retrieves replies status(es) *synchronously*
|
|
|
|
*
|
|
|
|
* @param accountId String Id of the account
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getAccountTLStatuses(String accountId, String max_id, boolean exclude_replies) {
|
|
|
|
return getStatus(accountId, false, false, exclude_replies, max_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves status for the account *synchronously*
|
|
|
|
*
|
|
|
|
* @param accountId String Id of the account
|
|
|
|
* @param onlyMedia boolean only with media
|
|
|
|
* @param exclude_replies boolean excludes replies
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
2017-06-07 11:12:40 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2017-09-15 19:51:41 +02:00
|
|
|
private APIResponse getStatus(String accountId, boolean onlyMedia, boolean pinned,
|
2017-05-05 16:36:04 +02:00
|
|
|
boolean exclude_replies, String max_id, String since_id, int limit) {
|
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if (since_id != null)
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if (0 < limit || limit > 40)
|
|
|
|
limit = 40;
|
|
|
|
if( onlyMedia)
|
|
|
|
params.put("only_media", Boolean.toString(true));
|
2017-09-15 19:51:41 +02:00
|
|
|
if( pinned)
|
|
|
|
params.put("pinned", Boolean.toString(true));
|
2018-09-05 10:19:07 +02:00
|
|
|
params.put("exclude_replies", Boolean.toString(exclude_replies));
|
2017-05-05 16:36:04 +02:00
|
|
|
params.put("limit", String.valueOf(limit));
|
2019-03-01 18:50:52 +01:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String tag = sharedpreferences.getString(Helper.SET_FEATURED_TAG_ACTION, null);
|
|
|
|
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON && tag != null){
|
2019-03-01 18:57:19 +01:00
|
|
|
params.put("tagged", tag.toLowerCase());
|
2019-02-27 19:17:39 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
statuses = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/statuses", accountId)), 60, params, prefKeyOauthTokenT);
|
2018-11-17 14:31:03 +01:00
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
2017-11-18 08:27:25 +01:00
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-01-06 17:13:18 +01:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
2018-01-06 17:13:18 +01:00
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-06 19:50:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves accounts that reblogged the status *synchronously*
|
|
|
|
*
|
|
|
|
* @param statusId String Id of the status
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("SameParameterValue")
|
|
|
|
public APIResponse getRebloggedBy(String statusId, String max_id) {
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
params.put("limit", "80");
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-11-06 19:50:09 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/reblogged_by", statusId)), 60, params, prefKeyOauthTokenT);
|
|
|
|
accounts = parseAccountResponse(new JSONArray(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setAccounts(accounts);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves accounts that favourited the status *synchronously*
|
|
|
|
*
|
|
|
|
* @param statusId String Id of the status
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("SameParameterValue")
|
|
|
|
public APIResponse getFavouritedBy(String statusId, String max_id) {
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
params.put("limit", "80");
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-11-06 19:50:09 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/favourited_by", statusId)), 60, params, prefKeyOauthTokenT);
|
|
|
|
accounts = parseAccountResponse(new JSONArray(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setAccounts(accounts);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves one status *synchronously*
|
|
|
|
*
|
|
|
|
* @param statusId String Id of the status
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getStatusbyId(String statusId) {
|
2017-05-05 16:36:04 +02:00
|
|
|
statuses = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s", statusId)), 60, null, prefKeyOauthTokenT);
|
|
|
|
Status status = parseStatuses(context, new JSONObject(response));
|
|
|
|
statuses.add(status);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 17:35:14 +02:00
|
|
|
/**
|
|
|
|
* Retrieves one status *synchronously*
|
|
|
|
*
|
|
|
|
* @param statusId String Id of the status
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getStatusbyIdAndCache(String statusId) {
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-05-12 17:35:14 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s", statusId)), 60, null, prefKeyOauthTokenT);
|
|
|
|
Status status = parseStatuses(context, new JSONObject(response));
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2019-05-31 17:25:17 +02:00
|
|
|
Account account = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
|
|
|
|
new TimelineCacheDAO(context, db).update(status.getId(), response, account.getId(), account.getInstance());
|
2019-05-12 17:35:14 +02:00
|
|
|
statuses.add(status);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the context of status with replies *synchronously*
|
|
|
|
*
|
|
|
|
* @param statusId Id of the status
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
2019-05-18 11:10:30 +02:00
|
|
|
public app.fedilab.android.client.Entities.Context getStatusContext(String statusId) {
|
|
|
|
app.fedilab.android.client.Entities.Context statusContext = new app.fedilab.android.client.Entities.Context();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/context", statusId)), 60, null, prefKeyOauthTokenT);
|
|
|
|
statusContext = parseContext(new JSONObject(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return statusContext;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:15:02 +02:00
|
|
|
|
2018-10-10 08:33:36 +02:00
|
|
|
/**
|
|
|
|
* Retrieves direct timeline for the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getDirectTimeline( String max_id) {
|
|
|
|
return getDirectTimeline(max_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
|
2018-10-29 15:49:13 +01:00
|
|
|
/**
|
|
|
|
* Retrieves conversation timeline for the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getConversationTimeline( String max_id) {
|
|
|
|
return getConversationTimeline(max_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:22:39 +01:00
|
|
|
/**
|
|
|
|
* Retrieves direct timeline for the account since an Id value *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getConversationTimelineSinceId(String since_id) {
|
|
|
|
return getConversationTimeline(null, since_id, tootPerPage);
|
|
|
|
}
|
|
|
|
|
2018-10-29 15:49:13 +01:00
|
|
|
/**
|
|
|
|
* Retrieves conversation timeline for the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
private APIResponse getConversationTimeline(String max_id, String since_id, int limit) {
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if (since_id != null)
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if (0 > limit || limit > 80)
|
|
|
|
limit = 80;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
conversations = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-29 15:49:13 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/conversations"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
conversations = parseConversations(new JSONArray(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setConversations(conversations);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2018-10-10 11:25:54 +02:00
|
|
|
/**
|
|
|
|
* Retrieves direct timeline for the account since an Id value *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getDirectTimelineSinceId(String since_id) {
|
|
|
|
return getDirectTimeline(null, since_id, tootPerPage);
|
|
|
|
}
|
2018-10-10 08:33:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves direct timeline for the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
private APIResponse getDirectTimeline(String max_id, String since_id, int limit) {
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if (since_id != null)
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if (0 > limit || limit > 80)
|
|
|
|
limit = 80;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-10 08:33:36 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/timelines/direct"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
2018-11-17 14:31:03 +01:00
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
2018-10-10 08:33:36 +02:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:15:02 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves home timeline for the account *synchronously*
|
2017-05-20 15:15:02 +02:00
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getHomeTimeline( String max_id) {
|
2018-10-30 14:54:58 +01:00
|
|
|
return getHomeTimeline(max_id, null, null, tootPerPage);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
2017-05-20 19:40:46 +02:00
|
|
|
* Retrieves home timeline for the account since an Id value *synchronously*
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getHomeTimelineSinceId(String since_id) {
|
2018-10-30 14:54:58 +01:00
|
|
|
return getHomeTimeline(null, since_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves home timeline for the account from a min Id value *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getHomeTimelineMinId(String min_id) {
|
|
|
|
return getHomeTimeline(null, null, min_id, tootPerPage);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
|
2019-05-12 09:52:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves home timeline from cache the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getHomeTimelineCache(String max_id) {
|
|
|
|
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
statuses = new TimelineCacheDAO(context, db).get(max_id);
|
2019-05-19 18:52:36 +02:00
|
|
|
|
2019-05-25 18:58:31 +02:00
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true);
|
|
|
|
if( remember_position_home ){
|
|
|
|
if( statuses != null){
|
|
|
|
Iterator<Status> i = statuses.iterator();
|
|
|
|
List<String> ids = new ArrayList<>();
|
|
|
|
while (i.hasNext()) {
|
|
|
|
Status s = i.next();
|
|
|
|
if( ids.contains(s.getId())) {
|
|
|
|
i.remove();
|
|
|
|
new TimelineCacheDAO(context, db).remove(s.getId());
|
|
|
|
}else{
|
|
|
|
ids.add(s.getId());
|
|
|
|
}
|
2019-05-19 18:52:36 +02:00
|
|
|
}
|
2019-05-19 13:16:21 +02:00
|
|
|
}
|
2019-05-25 18:58:31 +02:00
|
|
|
if( statuses == null){
|
|
|
|
return getHomeTimeline(max_id);
|
|
|
|
}else{
|
|
|
|
if( statuses.size() > 0) {
|
|
|
|
if( statuses.get(0).getId().matches("\\d+")){
|
|
|
|
apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1));
|
|
|
|
apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1));
|
|
|
|
}else{
|
|
|
|
apiResponse.setSince_id(statuses.get(0).getId());
|
|
|
|
apiResponse.setMax_id(statuses.get(statuses.size() - 1).getId());
|
|
|
|
}
|
2019-05-23 18:51:40 +02:00
|
|
|
}
|
2019-05-25 18:58:31 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
2019-05-12 09:52:56 +02:00
|
|
|
}
|
2019-05-25 18:58:31 +02:00
|
|
|
}else{
|
|
|
|
return getHomeTimeline(max_id);
|
2019-05-12 09:52:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves home timeline for the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
2017-06-07 11:12:40 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2018-10-30 14:54:58 +01:00
|
|
|
private APIResponse getHomeTimeline(String max_id, String since_id, String min_id, int limit) {
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if (since_id != null)
|
|
|
|
params.put("since_id", since_id);
|
2018-10-30 14:54:58 +01:00
|
|
|
if (min_id != null)
|
|
|
|
params.put("min_id", min_id);
|
2017-09-27 17:52:23 +02:00
|
|
|
if (0 > limit || limit > 80)
|
|
|
|
limit = 80;
|
2017-05-05 16:36:04 +02:00
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
statuses = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/timelines/home"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
2019-05-31 17:25:17 +02:00
|
|
|
statuses = parseStatusesForCache(context, new JSONArray(response));
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2019-03-02 15:43:40 +01:00
|
|
|
if( apiResponse == null)
|
|
|
|
apiResponse = new APIResponse();
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-11 19:58:41 +02:00
|
|
|
/**
|
|
|
|
* Retrieves public GNU timeline for the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getGNUTimeline(String remoteInstance, String max_id) {
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-05-11 19:58:41 +02:00
|
|
|
String response = httpsConnection.get("https://"+remoteInstance+"/api/statuses/public_timeline.json", 60, params, prefKeyOauthTokenT);
|
|
|
|
statuses = GNUAPI.parseStatuses(context, new JSONArray(response));
|
|
|
|
if( statuses.size() > 0) {
|
|
|
|
apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1));
|
|
|
|
apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1));
|
|
|
|
}
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2018-12-26 15:43:36 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves public pixelfed timeline for the account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getPixelfedTimeline(String remoteInstance, String max_id) {
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("page", max_id);
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-01-16 11:33:18 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrlRemote(remoteInstance, "/timelines/public/"), 60, params, prefKeyOauthTokenT);
|
2018-12-26 15:43:36 +01:00
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2019-01-16 11:33:18 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
2019-01-14 16:03:10 +01:00
|
|
|
e.printStackTrace();
|
2018-12-26 15:43:36 +01:00
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2018-10-22 17:09:25 +02:00
|
|
|
/**
|
|
|
|
* Retrieves Peertube videos from an instance *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getPeertubeChannel(String instance, String name) {
|
|
|
|
|
|
|
|
List<Account> accounts = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-22 17:09:25 +02:00
|
|
|
String response = httpsConnection.get(String.format("https://"+instance+"/api/v1/accounts/%s/video-channels", name), 60, null, null);
|
|
|
|
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
|
|
|
|
accounts = parseAccountResponsePeertube(context, instance, jsonArray);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2019-06-07 18:50:49 +02:00
|
|
|
e.printStackTrace();
|
2018-10-22 17:09:25 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setAccounts(accounts);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2018-10-22 19:19:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves Peertube videos from an instance *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getPeertubeChannelVideos(String instance, String name) {
|
|
|
|
|
|
|
|
List<Peertube> peertubes = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-22 19:19:39 +02:00
|
|
|
String response = httpsConnection.get(String.format("https://"+instance+"/api/v1/video-channels/%s/videos", name), 60, null, null);
|
|
|
|
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
|
|
|
|
peertubes = parsePeertube(instance, jsonArray);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setPeertubes(peertubes);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2018-10-07 10:45:34 +02:00
|
|
|
/**
|
|
|
|
* Retrieves Peertube videos from an instance *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getPeertube(String instance, String max_id) {
|
|
|
|
|
|
|
|
List<Peertube> peertubes = new ArrayList<>();
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if( max_id == null)
|
|
|
|
max_id = "0";
|
|
|
|
params.put("start", max_id);
|
2019-03-23 18:15:29 +01:00
|
|
|
params.put("filter","local");
|
|
|
|
params.put("sort","-publishedAt");
|
|
|
|
params.put("count", "20");
|
|
|
|
|
2018-10-07 10:45:34 +02:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-07 10:45:34 +02:00
|
|
|
String response = httpsConnection.get("https://"+instance+"/api/v1/videos", 60, params, null);
|
|
|
|
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
|
2018-10-17 14:34:29 +02:00
|
|
|
peertubes = parsePeertube(instance, jsonArray);
|
2018-10-07 10:45:34 +02:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setPeertubes(peertubes);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2018-09-29 09:58:46 +02:00
|
|
|
|
2018-10-14 16:17:25 +02:00
|
|
|
/**
|
|
|
|
* Retrieves Peertube videos from an instance *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getSinglePeertube(String instance, String videoId) {
|
|
|
|
|
|
|
|
|
|
|
|
Peertube peertube = null;
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-14 16:17:25 +02:00
|
|
|
String response = httpsConnection.get(String.format("https://"+instance+"/api/v1/videos/%s", videoId), 60, null, null);
|
|
|
|
JSONObject jsonObject = new JSONObject(response);
|
2018-10-17 14:34:29 +02:00
|
|
|
peertube = parseSinglePeertube(context, instance, jsonObject);
|
2018-10-14 16:17:25 +02:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
List<Peertube> peertubes = new ArrayList<>();
|
|
|
|
peertubes.add(peertube);
|
|
|
|
apiResponse.setPeertubes(peertubes);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2018-10-20 12:22:35 +02:00
|
|
|
/**
|
|
|
|
* Retrieves peertube search *synchronously*
|
|
|
|
*
|
|
|
|
* @param query String search
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse searchPeertube(String instance, String query) {
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put("count", "50");
|
2019-05-25 12:37:50 +02:00
|
|
|
if( query == null)
|
|
|
|
return null;
|
2018-10-26 10:22:33 +02:00
|
|
|
try {
|
|
|
|
params.put("search", URLEncoder.encode(query, "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
params.put("search", query);
|
|
|
|
}
|
2018-10-20 12:22:35 +02:00
|
|
|
List<Peertube> peertubes = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-20 12:22:35 +02:00
|
|
|
String response = httpsConnection.get("https://"+instance+"/api/v1/search/videos", 60, params, null);
|
|
|
|
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
|
|
|
|
peertubes = parsePeertube(instance, jsonArray);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setPeertubes(peertubes);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2018-10-17 11:32:16 +02:00
|
|
|
/**
|
|
|
|
* Retrieves Peertube videos from an instance *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getSinglePeertubeComments(String instance, String videoId) {
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-17 11:32:16 +02:00
|
|
|
String response = httpsConnection.get(String.format("https://"+instance+"/api/v1/videos/%s/comment-threads", videoId), 60, null, null);
|
|
|
|
JSONObject jsonObject = new JSONObject(response);
|
|
|
|
statuses = parseSinglePeertubeComments(context, instance, jsonObject);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2018-10-14 16:17:25 +02:00
|
|
|
|
2018-09-29 09:58:46 +02:00
|
|
|
/**
|
|
|
|
* Retrieves home timeline for the account *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getHowTo() {
|
|
|
|
|
|
|
|
List<HowToVideo> howToVideos = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-10-07 10:45:34 +02:00
|
|
|
String response = httpsConnection.get("https://peertube.social/api/v1/video-channels/mastalab_channel/videos", 60, null, null);
|
2018-09-29 09:58:46 +02:00
|
|
|
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
|
|
|
|
howToVideos = parseHowTos(jsonArray);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setHowToVideos(howToVideos);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2018-12-29 12:09:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves Peertube videos from an instance *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getMisskey(String instance, String max_id) {
|
|
|
|
|
2018-12-29 19:21:39 +01:00
|
|
|
JSONObject params = new JSONObject();
|
|
|
|
try {
|
|
|
|
params.put("file", false);
|
|
|
|
if( max_id != null)
|
|
|
|
params.put("untilId",max_id);
|
|
|
|
params.put("local",true);
|
|
|
|
params.put("poll",false);
|
|
|
|
params.put("renote",false);
|
|
|
|
params.put("reply",false);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-12-29 12:09:02 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
statuses = new ArrayList<>();
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-12-29 19:21:39 +01:00
|
|
|
String response = httpsConnection.postMisskey("https://"+instance+"/api/notes", 60, params, null);
|
2018-12-29 12:09:02 +01:00
|
|
|
statuses = parseNotes(context, instance, new JSONArray(response));
|
2018-12-29 19:21:39 +01:00
|
|
|
if( statuses != null && statuses.size() > 0){
|
|
|
|
apiResponse.setSince_id(statuses.get(0).getId());
|
|
|
|
apiResponse.setMax_id(statuses.get(statuses.size() -1).getId());
|
|
|
|
}
|
2018-12-29 12:09:02 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2018-08-20 19:00:20 +02:00
|
|
|
/**
|
|
|
|
* Retrieves public timeline for the account *synchronously*
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getPublicTimeline(String instanceName, boolean local, String max_id){
|
|
|
|
return getPublicTimeline(local, instanceName, max_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves public timeline for the account *synchronously*
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getPublicTimeline(boolean local, String max_id){
|
2018-08-20 19:00:20 +02:00
|
|
|
return getPublicTimeline(local, null, max_id, null, tootPerPage);
|
2017-11-15 18:38:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves public timeline for the account since an Id value *synchronously*
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param since_id String id since
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getPublicTimelineSinceId(boolean local, String since_id) {
|
2018-08-20 19:00:20 +02:00
|
|
|
return getPublicTimeline(local, null, null, since_id, tootPerPage);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-10-07 08:34:50 +02:00
|
|
|
|
2018-08-23 16:10:57 +02:00
|
|
|
/**
|
|
|
|
* Retrieves instance timeline since an Id value *synchronously*
|
|
|
|
* @param instanceName String instance name
|
|
|
|
* @param since_id String id since
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getInstanceTimelineSinceId(String instanceName, String since_id) {
|
2018-09-08 11:30:58 +02:00
|
|
|
return getPublicTimeline(true, instanceName, null, since_id, tootPerPage);
|
2018-08-23 16:10:57 +02:00
|
|
|
}
|
2017-10-07 08:34:50 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves public timeline for the account *synchronously*
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
2017-06-07 11:12:40 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2018-08-20 19:00:20 +02:00
|
|
|
private APIResponse getPublicTimeline(boolean local, String instanceName, String max_id, String since_id, int limit){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if( local)
|
|
|
|
params.put("local", Boolean.toString(true));
|
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if( since_id != null )
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if( 0 > limit || limit > 40)
|
|
|
|
limit = 40;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
statuses = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-08-20 19:00:20 +02:00
|
|
|
String url;
|
|
|
|
if( instanceName == null)
|
|
|
|
url = getAbsoluteUrl("/timelines/public");
|
|
|
|
else
|
|
|
|
url = getAbsoluteUrlRemoteInstance(instanceName);
|
|
|
|
String response = httpsConnection.get(url, 60, params, prefKeyOauthTokenT);
|
2017-11-18 08:27:25 +01:00
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
2018-11-17 14:31:03 +01:00
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2019-05-25 14:20:17 +02:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2018-12-05 12:04:49 +01:00
|
|
|
|
2019-06-08 18:55:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves news coming from Fedilab's account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getNews(String max_id){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
|
|
|
|
params.put("exclude_replies", "true");
|
2019-06-09 17:08:25 +02:00
|
|
|
params.put("reblogs","false");
|
2019-06-08 18:55:30 +02:00
|
|
|
params.put("limit", "40");
|
|
|
|
params.put("tagged", "Fedilab");
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
String instance = Helper.getLiveInstance(context);
|
|
|
|
|
|
|
|
String accountID = sharedpreferences.getString(Helper.NEWS_ACCOUNT_ID+userId+instance, null);
|
|
|
|
if( accountID == null){
|
2019-06-11 17:48:30 +02:00
|
|
|
HashMap<String, String> params2 = new HashMap<>();
|
2019-06-09 17:08:25 +02:00
|
|
|
try {
|
2019-06-11 17:48:30 +02:00
|
|
|
params2.put("q", URLEncoder.encode("https://framapiaf.org/@fedilab", "UTF-8"));
|
2019-06-09 17:08:25 +02:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
2019-06-11 17:48:30 +02:00
|
|
|
params2.put("q", "https://framapiaf.org/@fedilab");
|
2019-06-09 17:08:25 +02:00
|
|
|
}
|
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
|
|
|
try {
|
2019-06-11 17:48:30 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/search"), 60, params2, prefKeyOauthTokenT);
|
2019-06-09 17:08:25 +02:00
|
|
|
Results res = parseResultsResponse(new JSONObject(response));
|
|
|
|
if( res != null && res.getAccounts() != null && res.getAccounts().size() > 0 ){
|
|
|
|
accountID = res.getAccounts().get(0).getId();
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.NEWS_ACCOUNT_ID+userId+instance, accountID);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2019-06-08 18:55:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/statuses", accountID)), 60, params, prefKeyOauthTokenT);
|
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2019-01-15 15:41:30 +01:00
|
|
|
/**
|
|
|
|
* Retrieves discover timeline for the account *synchronously*
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getDiscoverTimeline(boolean local, String max_id){
|
|
|
|
return getDiscoverTimeline(local, max_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves discover timeline for the account *synchronously*
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
private APIResponse getDiscoverTimeline(boolean local, String max_id, String since_id, int limit){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if( local)
|
|
|
|
params.put("local", Boolean.toString(true));
|
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if( since_id != null )
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if( 0 > limit || limit > 40)
|
|
|
|
limit = 40;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-01-15 15:41:30 +01:00
|
|
|
String url;
|
|
|
|
url = getAbsoluteUr2l("/discover/posts");
|
|
|
|
String response = httpsConnection.get(url, 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-05 12:04:49 +01:00
|
|
|
|
2018-12-27 17:49:50 +01:00
|
|
|
public APIResponse getCustomArtTimeline(boolean local, String tag, String max_id, List<String> any, List<String> all, List<String> none){
|
|
|
|
return getArtTimeline(local, tag, max_id, null, any, all, none);
|
2018-12-15 12:21:03 +01:00
|
|
|
}
|
2018-12-05 12:04:49 +01:00
|
|
|
|
2018-12-27 17:49:50 +01:00
|
|
|
public APIResponse getArtTimeline(boolean local, String max_id, List<String> any, List<String> all, List<String> none){
|
|
|
|
return getArtTimeline(local, null, max_id, null, any, all, none);
|
2018-12-16 18:03:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 17:49:50 +01:00
|
|
|
public APIResponse getCustomArtTimelineSinceId(boolean local, String tag, String since_id, List<String> any, List<String> all, List<String> none){
|
|
|
|
return getArtTimeline(local, tag, null, since_id, any, all, none);
|
2018-12-16 18:03:39 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 17:49:50 +01:00
|
|
|
public APIResponse getArtTimelineSinceId(boolean local, String since_id, List<String> any, List<String> all, List<String> none){
|
|
|
|
return getArtTimeline(local, null, null, since_id, any, all, none);
|
2018-12-15 12:21:03 +01:00
|
|
|
}
|
2018-12-05 12:04:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieves art timeline
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-12-27 17:49:50 +01:00
|
|
|
private APIResponse getArtTimeline(boolean local, String tag, String max_id, String since_id, List<String> any, List<String> all, List<String> none){
|
2018-12-15 12:21:03 +01:00
|
|
|
if( tag == null)
|
|
|
|
tag = "mastoart";
|
2019-05-25 12:37:50 +02:00
|
|
|
APIResponse apiResponse = getPublicTimelineTag(tag, local, true, max_id, since_id, tootPerPage, any, all, none, null);
|
2018-12-05 12:04:49 +01:00
|
|
|
APIResponse apiResponseReply = new APIResponse();
|
|
|
|
if( apiResponse != null){
|
|
|
|
apiResponseReply.setMax_id(apiResponse.getMax_id());
|
|
|
|
apiResponseReply.setSince_id(apiResponse.getSince_id());
|
|
|
|
apiResponseReply.setStatuses(new ArrayList<>());
|
|
|
|
if( apiResponse.getStatuses() != null && apiResponse.getStatuses().size() > 0){
|
|
|
|
for( Status status: apiResponse.getStatuses()){
|
2018-12-07 17:57:53 +01:00
|
|
|
if( status.getMedia_attachments() != null ) {
|
2018-12-16 17:14:39 +01:00
|
|
|
String statusSerialized = Helper.statusToStringStorage(status);
|
|
|
|
for (Attachment attachment : status.getMedia_attachments()) {
|
|
|
|
Status newStatus = Helper.restoreStatusFromString(statusSerialized);
|
|
|
|
if (newStatus == null)
|
|
|
|
break;
|
|
|
|
newStatus.setArt_attachment(attachment);
|
|
|
|
apiResponseReply.getStatuses().add(newStatus);
|
2018-12-05 12:04:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiResponseReply;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves public tag timeline *synchronously*
|
|
|
|
* @param tag String
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2018-12-27 17:49:50 +01:00
|
|
|
public APIResponse getPublicTimelineTag(String tag, boolean local, String max_id, List<String> any, List<String> all, List<String> none){
|
2019-05-25 12:37:50 +02:00
|
|
|
return getPublicTimelineTag(tag, local, false, max_id, null, tootPerPage, any, all, none, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves public tag timeline *synchronously*
|
|
|
|
* @param tag String
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("SameParameterValue")
|
|
|
|
public APIResponse getPublicTimelineTag(String tag, boolean local, String max_id, String instance){
|
|
|
|
return getPublicTimelineTag(tag, local, false, max_id, null, tootPerPage, null, null, null, instance);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2018-12-16 18:03:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves public tag timeline *synchronously*
|
|
|
|
* @param tag String
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param since_id String since id
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("SameParameterValue")
|
2018-12-27 17:49:50 +01:00
|
|
|
public APIResponse getPublicTimelineTagSinceId(String tag, boolean local, String since_id, List<String> any, List<String> all, List<String> none){
|
2019-05-25 12:37:50 +02:00
|
|
|
return getPublicTimelineTag(tag, local, false, null, since_id, tootPerPage, any, all, none, null);
|
2018-12-16 18:03:39 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves public tag timeline *synchronously*
|
|
|
|
* @param tag String
|
|
|
|
* @param local boolean only local timeline
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
2017-06-07 11:12:40 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2019-05-25 12:37:50 +02:00
|
|
|
private APIResponse getPublicTimelineTag(String tag, boolean local, boolean onlymedia, String max_id, String since_id, int limit, List<String> any, List<String> all, List<String> none, String instance){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if( local)
|
|
|
|
params.put("local", Boolean.toString(true));
|
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if( since_id != null )
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if( 0 > limit || limit > 40)
|
|
|
|
limit = 40;
|
2018-12-27 17:49:50 +01:00
|
|
|
if( onlymedia)
|
|
|
|
params.put("only_media", Boolean.toString(true));
|
|
|
|
|
2019-04-27 10:03:33 +02:00
|
|
|
|
2018-12-27 17:49:50 +01:00
|
|
|
if( any != null && any.size() > 0) {
|
|
|
|
StringBuilder parameters = new StringBuilder();
|
2019-02-08 17:30:55 +01:00
|
|
|
for (String a : any) {
|
|
|
|
try {
|
|
|
|
a = URLEncoder.encode(a, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException ignored) {}
|
2018-12-27 17:49:50 +01:00
|
|
|
parameters.append("any[]=").append(a).append("&");
|
2019-02-08 17:30:55 +01:00
|
|
|
}
|
2018-12-27 17:49:50 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(6));
|
|
|
|
params.put("any[]", parameters.toString());
|
|
|
|
}
|
|
|
|
if( all != null && all.size() > 0) {
|
|
|
|
StringBuilder parameters = new StringBuilder();
|
2019-02-08 17:30:55 +01:00
|
|
|
for (String a : all) {
|
|
|
|
try {
|
|
|
|
a = URLEncoder.encode(a, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException ignored) {}
|
2018-12-27 17:49:50 +01:00
|
|
|
parameters.append("all[]=").append(a).append("&");
|
2019-02-08 17:30:55 +01:00
|
|
|
}
|
2018-12-27 17:49:50 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(6));
|
|
|
|
params.put("all[]", parameters.toString());
|
|
|
|
}
|
|
|
|
if( none != null && none.size() > 0) {
|
|
|
|
StringBuilder parameters = new StringBuilder();
|
2019-02-08 17:30:55 +01:00
|
|
|
for (String a : none) {
|
|
|
|
try {
|
|
|
|
a = URLEncoder.encode(a, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException ignored) {}
|
2018-12-27 17:49:50 +01:00
|
|
|
parameters.append("none[]=").append(a).append("&");
|
2019-02-08 17:30:55 +01:00
|
|
|
}
|
2018-12-27 17:49:50 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(7));
|
|
|
|
params.put("none[]", parameters.toString());
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
statuses = new ArrayList<>();
|
2018-10-10 08:33:36 +02:00
|
|
|
if( tag == null)
|
|
|
|
return null;
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-02-01 18:03:18 +01:00
|
|
|
String query = tag.trim();
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-02-01 18:03:18 +01:00
|
|
|
if( MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
|
|
|
|
try {
|
|
|
|
query = URLEncoder.encode(query, "UTF-8");
|
|
|
|
} catch (UnsupportedEncodingException ignored) {}
|
2019-05-25 12:37:50 +02:00
|
|
|
String response;
|
|
|
|
if( instance == null)
|
|
|
|
response = httpsConnection.get(getAbsoluteUrl(String.format("/timelines/tag/%s",query)), 60, params, prefKeyOauthTokenT);
|
|
|
|
else
|
|
|
|
response = httpsConnection.get(getAbsoluteUrlRemote(instance, String.format("/timelines/tag/%s",query)), 60, params, null);
|
2017-11-18 08:27:25 +01:00
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
2018-11-17 14:31:03 +01:00
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2019-04-26 19:05:09 +02:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves muted users by the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getMuted(String max_id){
|
2017-05-05 16:36:04 +02:00
|
|
|
return getAccounts("/mutes", max_id, null, accountPerPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves blocked users by the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getBlocks(String max_id){
|
2017-05-05 16:36:04 +02:00
|
|
|
return getAccounts("/blocks", max_id, null, accountPerPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves following for the account specified by targetedId *synchronously*
|
|
|
|
* @param targetedId String targetedId
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getFollowing(String targetedId, String max_id){
|
2017-05-05 16:36:04 +02:00
|
|
|
return getAccounts(String.format("/accounts/%s/following",targetedId),max_id, null, accountPerPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves followers for the account specified by targetedId *synchronously*
|
|
|
|
* @param targetedId String targetedId
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getFollowers(String targetedId, String max_id){
|
2017-05-05 16:36:04 +02:00
|
|
|
return getAccounts(String.format("/accounts/%s/followers",targetedId),max_id, null, accountPerPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves blocked users by the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
2017-06-07 11:12:40 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2017-06-03 18:18:27 +02:00
|
|
|
private APIResponse getAccounts(String action, String max_id, String since_id, int limit){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if( since_id != null )
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if( 0 > limit || limit > 40)
|
|
|
|
limit = 40;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
accounts = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
accounts = parseAccountResponse(new JSONArray(response));
|
2018-09-02 11:21:37 +02:00
|
|
|
if( accounts != null && accounts.size() == 1 ){
|
|
|
|
if(accounts.get(0).getAcct() == null){
|
|
|
|
Throwable error = new Throwable(context.getString(R.string.toast_error));
|
|
|
|
setError(500, error);
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setAccounts(accounts);
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-07 19:47:53 +01:00
|
|
|
/**
|
|
|
|
* Retrieves opencollective accounts *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public Results getOpencollectiveAccounts(RetrieveOpenCollectiveAsyncTask.Type type){
|
|
|
|
|
|
|
|
results = new Results();
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-02-07 19:47:53 +01:00
|
|
|
String response = httpsConnection.get("https://opencollective.com/mastalab/members/all.json", 60, null, prefKeyOauthTokenT);
|
|
|
|
accounts = parseOpencollectiveAccountResponse(context, type, new JSONArray(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
results.setAccounts(accounts);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-26 18:30:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves blocked domains for the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("SameParameterValue")
|
|
|
|
public APIResponse getBlockedDomain(String max_id){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
params.put("limit","80");
|
|
|
|
domains = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-09-26 18:30:08 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/domain_blocks"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
domains = parseDomains(new JSONArray(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setDomains(domains);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a blocked domains for the authenticated account *synchronously*
|
|
|
|
* @param domain String domain name
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("SameParameterValue")
|
|
|
|
public int deleteBlockedDomain(String domain){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put("domain",domain);
|
|
|
|
domains = new ArrayList<>();
|
|
|
|
HttpsConnection httpsConnection;
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
httpsConnection = new HttpsConnection(context, this.instance);
|
2018-09-26 18:30:08 +02:00
|
|
|
httpsConnection.delete(getAbsoluteUrl("/domain_blocks"), 60, params, prefKeyOauthTokenT);
|
|
|
|
actionCode = httpsConnection.getActionCode();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return actionCode;
|
|
|
|
}
|
|
|
|
|
2017-06-07 11:12:40 +02:00
|
|
|
/**
|
|
|
|
* Retrieves follow requests for the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getFollowRequest(String max_id){
|
|
|
|
return getFollowRequest(max_id, null, accountPerPage);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Retrieves follow requests for the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2017-06-07 11:12:40 +02:00
|
|
|
private APIResponse getFollowRequest(String max_id, String since_id, int limit){
|
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-06-07 11:12:40 +02:00
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if( since_id != null )
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if( 0 > limit || limit > 40)
|
|
|
|
limit = 40;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
accounts = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/follow_requests"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
accounts = parseAccountResponse(new JSONArray(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-07 11:12:40 +02:00
|
|
|
apiResponse.setAccounts(accounts);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves favourited status for the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getFavourites(String max_id){
|
2017-05-05 16:36:04 +02:00
|
|
|
return getFavourites(max_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Retrieves favourited status for the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
2017-06-07 11:12:40 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2017-06-03 18:18:27 +02:00
|
|
|
private APIResponse getFavourites(String max_id, String since_id, int limit){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if( since_id != null )
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if( 0 > limit || limit > 40)
|
|
|
|
limit = 40;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
statuses = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/favourites"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
2018-11-17 14:31:03 +01:00
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
2017-11-18 08:27:25 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 08:27:25 +01:00
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the post action for a status
|
|
|
|
* @param statusAction Enum
|
|
|
|
* @param targetedId String id of the targeted Id *can be this of a status or an account*
|
|
|
|
* @return in status code - Should be equal to 200 when action is done
|
|
|
|
*/
|
|
|
|
public int postAction(StatusAction statusAction, String targetedId){
|
|
|
|
return postAction(statusAction, targetedId, null, null);
|
|
|
|
}
|
|
|
|
|
2017-12-17 11:43:29 +01:00
|
|
|
/**
|
|
|
|
* Makes the post action for a status
|
|
|
|
* @param targetedId String id of the targeted Id *can be this of a status or an account*
|
|
|
|
* @param muteNotifications - boolean - notifications should be also muted
|
|
|
|
* @return in status code - Should be equal to 200 when action is done
|
|
|
|
*/
|
|
|
|
public int muteNotifications(String targetedId, boolean muteNotifications){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-12-17 13:04:17 +01:00
|
|
|
params.put("notifications", Boolean.toString(muteNotifications));
|
2017-12-17 11:43:29 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-12-17 11:43:29 +01:00
|
|
|
httpsConnection.post(getAbsoluteUrl(String.format("/accounts/%s/mute", targetedId)), 60, params, prefKeyOauthTokenT);
|
|
|
|
actionCode = httpsConnection.getActionCode();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2017-12-17 13:04:17 +01:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
2017-12-17 13:04:17 +01:00
|
|
|
e.printStackTrace();
|
2017-12-17 11:43:29 +01:00
|
|
|
}
|
|
|
|
return actionCode;
|
|
|
|
}
|
2017-06-07 11:12:40 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Makes the post action
|
|
|
|
* @param status Status object related to the status
|
|
|
|
* @param comment String comment for the report
|
|
|
|
* @return in status code - Should be equal to 200 when action is done
|
|
|
|
*/
|
|
|
|
public int reportAction(Status status, String comment){
|
|
|
|
return postAction(API.StatusAction.REPORT, null, status, comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int statusAction(Status status){
|
|
|
|
return postAction(StatusAction.CREATESTATUS, null, status, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the post action
|
|
|
|
* @param statusAction Enum
|
|
|
|
* @param targetedId String id of the targeted Id *can be this of a status or an account*
|
|
|
|
* @param status Status object related to the status
|
|
|
|
* @param comment String comment for the report
|
|
|
|
* @return in status code - Should be equal to 200 when action is done
|
|
|
|
*/
|
|
|
|
private int postAction(StatusAction statusAction, String targetedId, Status status, String comment ){
|
|
|
|
|
|
|
|
String action;
|
2017-11-18 09:55:09 +01:00
|
|
|
HashMap<String, String> params = null;
|
2017-05-05 16:36:04 +02:00
|
|
|
switch (statusAction){
|
|
|
|
case FAVOURITE:
|
|
|
|
action = String.format("/statuses/%s/favourite", targetedId);
|
|
|
|
break;
|
|
|
|
case UNFAVOURITE:
|
|
|
|
action = String.format("/statuses/%s/unfavourite", targetedId);
|
|
|
|
break;
|
|
|
|
case REBLOG:
|
|
|
|
action = String.format("/statuses/%s/reblog", targetedId);
|
|
|
|
break;
|
|
|
|
case UNREBLOG:
|
|
|
|
action = String.format("/statuses/%s/unreblog", targetedId);
|
|
|
|
break;
|
|
|
|
case FOLLOW:
|
|
|
|
action = String.format("/accounts/%s/follow", targetedId);
|
|
|
|
break;
|
2017-08-22 18:15:08 +02:00
|
|
|
case REMOTE_FOLLOW:
|
2017-08-22 18:24:41 +02:00
|
|
|
action = "/follows";
|
2017-11-18 09:55:09 +01:00
|
|
|
params = new HashMap<>();
|
2017-08-22 18:15:08 +02:00
|
|
|
params.put("uri", targetedId);
|
|
|
|
break;
|
2017-05-05 16:36:04 +02:00
|
|
|
case UNFOLLOW:
|
|
|
|
action = String.format("/accounts/%s/unfollow", targetedId);
|
|
|
|
break;
|
|
|
|
case BLOCK:
|
|
|
|
action = String.format("/accounts/%s/block", targetedId);
|
|
|
|
break;
|
2018-09-26 19:07:22 +02:00
|
|
|
case BLOCK_DOMAIN:
|
2018-09-26 19:18:54 +02:00
|
|
|
action = "/domain_blocks";
|
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("domain", targetedId);
|
2018-09-26 19:07:22 +02:00
|
|
|
break;
|
2017-05-05 16:36:04 +02:00
|
|
|
case UNBLOCK:
|
|
|
|
action = String.format("/accounts/%s/unblock", targetedId);
|
|
|
|
break;
|
|
|
|
case MUTE:
|
|
|
|
action = String.format("/accounts/%s/mute", targetedId);
|
|
|
|
break;
|
|
|
|
case UNMUTE:
|
|
|
|
action = String.format("/accounts/%s/unmute", targetedId);
|
|
|
|
break;
|
2019-06-06 18:16:43 +02:00
|
|
|
case MUTE_CONVERSATION:
|
|
|
|
action = String.format("/statuses/%s/mute", targetedId);
|
|
|
|
break;
|
|
|
|
case UNMUTE_CONVERSATION:
|
|
|
|
action = String.format("/statuses/%s/unmute", targetedId);
|
|
|
|
break;
|
2017-09-12 17:29:43 +02:00
|
|
|
case PIN:
|
|
|
|
action = String.format("/statuses/%s/pin", targetedId);
|
|
|
|
break;
|
|
|
|
case UNPIN:
|
2017-09-13 19:04:56 +02:00
|
|
|
action = String.format("/statuses/%s/unpin", targetedId);
|
2017-09-12 17:29:43 +02:00
|
|
|
break;
|
2018-09-04 19:27:26 +02:00
|
|
|
case ENDORSE:
|
|
|
|
action = String.format("/accounts/%s/pin", targetedId);
|
|
|
|
break;
|
|
|
|
case UNENDORSE:
|
|
|
|
action = String.format("/accounts/%s/unpin", targetedId);
|
|
|
|
break;
|
|
|
|
case SHOW_BOOST:
|
2018-09-05 17:08:57 +02:00
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("reblogs","true");
|
2018-09-04 19:27:26 +02:00
|
|
|
action = String.format("/accounts/%s/follow", targetedId);
|
|
|
|
break;
|
|
|
|
case HIDE_BOOST:
|
2018-09-05 17:08:57 +02:00
|
|
|
params = new HashMap<>();
|
|
|
|
params.put("reblogs","false");
|
2018-09-04 19:27:26 +02:00
|
|
|
action = String.format("/accounts/%s/follow", targetedId);
|
|
|
|
break;
|
2017-05-05 16:36:04 +02:00
|
|
|
case UNSTATUS:
|
|
|
|
action = String.format("/statuses/%s", targetedId);
|
|
|
|
break;
|
2017-06-07 11:12:40 +02:00
|
|
|
case AUTHORIZE:
|
|
|
|
action = String.format("/follow_requests/%s/authorize", targetedId);
|
|
|
|
break;
|
|
|
|
case REJECT:
|
|
|
|
action = String.format("/follow_requests/%s/reject", targetedId);
|
|
|
|
break;
|
2017-05-05 16:36:04 +02:00
|
|
|
case REPORT:
|
|
|
|
action = "/reports";
|
2017-11-18 09:55:09 +01:00
|
|
|
params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
params.put("account_id", status.getAccount().getId());
|
|
|
|
params.put("comment", comment);
|
|
|
|
params.put("status_ids[]", status.getId());
|
|
|
|
break;
|
|
|
|
case CREATESTATUS:
|
2017-11-18 09:55:09 +01:00
|
|
|
params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
action = "/statuses";
|
2017-11-21 18:12:57 +01:00
|
|
|
try {
|
|
|
|
params.put("status", URLEncoder.encode(status.getContent(), "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
params.put("status", status.getContent());
|
|
|
|
}
|
2019-01-19 19:41:55 +01:00
|
|
|
if( status.getScheduled_at() != null)
|
|
|
|
params.put("scheduled_at", status.getScheduled_at());
|
2017-05-05 16:36:04 +02:00
|
|
|
if( status.getIn_reply_to_id() != null)
|
|
|
|
params.put("in_reply_to_id", status.getIn_reply_to_id());
|
|
|
|
if( status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0 ) {
|
2017-11-19 08:46:53 +01:00
|
|
|
StringBuilder parameters = new StringBuilder();
|
|
|
|
for(Attachment attachment: status.getMedia_attachments())
|
|
|
|
parameters.append("media_ids[]=").append(attachment.getId()).append("&");
|
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(12));
|
|
|
|
params.put("media_ids[]", parameters.toString());
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
if( status.isSensitive())
|
|
|
|
params.put("sensitive", Boolean.toString(status.isSensitive()));
|
|
|
|
if( status.getSpoiler_text() != null)
|
2017-11-21 18:12:57 +01:00
|
|
|
try {
|
|
|
|
params.put("spoiler_text", URLEncoder.encode(status.getSpoiler_text(), "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
params.put("spoiler_text", status.getSpoiler_text());
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
params.put("visibility", status.getVisibility());
|
2019-01-20 09:45:48 +01:00
|
|
|
break;
|
2017-05-05 16:36:04 +02:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(statusAction != StatusAction.UNSTATUS ) {
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-01-10 11:51:25 +01:00
|
|
|
String resp = httpsConnection.post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT);
|
2017-11-18 09:55:09 +01:00
|
|
|
actionCode = httpsConnection.getActionCode();
|
2019-01-10 11:51:25 +01:00
|
|
|
if( statusAction == StatusAction.REBLOG || statusAction == StatusAction.UNREBLOG || statusAction == StatusAction.FAVOURITE || statusAction == StatusAction.UNFAVOURITE) {
|
|
|
|
Bundle b = new Bundle();
|
|
|
|
try {
|
|
|
|
Status status1 = parseStatuses(context, new JSONObject(resp));
|
|
|
|
b.putParcelable("status", status1);
|
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
Intent intentBC = new Intent(Helper.RECEIVE_ACTION);
|
|
|
|
intentBC.putExtras(b);
|
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(intentBC);
|
2019-05-18 12:10:08 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
2019-05-31 17:25:17 +02:00
|
|
|
Account account = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
|
2019-06-09 15:14:05 +02:00
|
|
|
Status indb = new TimelineCacheDAO(context, db).getSingle(targetedId);
|
|
|
|
if( indb != null) {
|
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s", targetedId)), 60, null, prefKeyOauthTokenT);
|
|
|
|
new TimelineCacheDAO(context, db).update(targetedId, response, account.getId(), account.getInstance());
|
|
|
|
}
|
2019-01-10 11:51:25 +01:00
|
|
|
}
|
2017-11-18 09:55:09 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 09:55:09 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}else{
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 09:55:09 +01:00
|
|
|
httpsConnection.delete(getAbsoluteUrl(action), 60, null, prefKeyOauthTokenT);
|
|
|
|
actionCode = httpsConnection.getActionCode();
|
2019-05-18 12:10:08 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
new TimelineCacheDAO(context, db).remove(targetedId);
|
2017-11-18 09:55:09 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 09:55:09 +01:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return actionCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-19 19:41:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scheduled action for a status
|
|
|
|
* @param status Status object related to the status
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2019-01-20 15:20:07 +01:00
|
|
|
public APIResponse scheduledAction(String call, Status status, String max_id, String targetedId){
|
2019-01-19 19:41:55 +01:00
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if( call.equals("PUT")){
|
2019-01-20 15:20:07 +01:00
|
|
|
if( status.getScheduled_at() != null)
|
|
|
|
params.put("scheduled_at", status.getScheduled_at());
|
2019-01-19 19:41:55 +01:00
|
|
|
}else if(call.equals("GET")){
|
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
}
|
|
|
|
List<StoredStatus> storedStatus = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-01-19 19:41:55 +01:00
|
|
|
String response = null;
|
2019-01-20 15:20:07 +01:00
|
|
|
int responseCode = -1;
|
2019-01-19 19:41:55 +01:00
|
|
|
if( call.equals("GET"))
|
|
|
|
response = httpsConnection.get(getAbsoluteUrl("/scheduled_statuses/"), 60, null, prefKeyOauthTokenT);
|
|
|
|
else if( call.equals("PUT"))
|
2019-01-20 15:20:07 +01:00
|
|
|
response = httpsConnection.put(getAbsoluteUrl(String.format("/scheduled_statuses/%s", targetedId)), 60, params, prefKeyOauthTokenT);
|
2019-01-19 19:41:55 +01:00
|
|
|
else if( call.equals("DELETE"))
|
2019-01-20 15:20:07 +01:00
|
|
|
responseCode = httpsConnection.delete(getAbsoluteUrl(String.format("/scheduled_statuses/%s",targetedId)), 60, null, prefKeyOauthTokenT);
|
2019-01-19 19:41:55 +01:00
|
|
|
if(call.equals("GET")) {
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
}
|
|
|
|
if (response != null && call.equals("PUT")) {
|
2019-01-20 15:20:07 +01:00
|
|
|
Schedule schedule = parseSimpleSchedule(context, new JSONObject(response));
|
2019-01-19 19:41:55 +01:00
|
|
|
StoredStatus st = new StoredStatus();
|
|
|
|
st.setCreation_date(status.getCreated_at());
|
|
|
|
st.setId(-1);
|
|
|
|
st.setJobId(-1);
|
2019-01-20 15:20:07 +01:00
|
|
|
st.setScheduled_date(schedule.getScheduled_at());
|
2019-01-19 19:41:55 +01:00
|
|
|
st.setStatusReply(null);
|
|
|
|
st.setSent_date(null);
|
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
st.setUserId(userId);
|
2019-01-20 15:20:07 +01:00
|
|
|
st.setStatus(schedule.getStatus());
|
2019-01-19 19:41:55 +01:00
|
|
|
storedStatus.add(st);
|
|
|
|
}else if (response != null && call.equals("GET")) {
|
2019-01-20 09:45:48 +01:00
|
|
|
List<Schedule> scheduleList = parseSchedule(context, new JSONArray(response));
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
for(Schedule schedule: scheduleList){
|
2019-01-19 19:41:55 +01:00
|
|
|
StoredStatus st = new StoredStatus();
|
2019-01-20 09:45:48 +01:00
|
|
|
st.setCreation_date(null);
|
2019-01-20 15:20:07 +01:00
|
|
|
st.setScheduledServerdId(schedule.getId());
|
2019-01-19 19:41:55 +01:00
|
|
|
st.setJobId(-1);
|
2019-01-20 09:45:48 +01:00
|
|
|
st.setScheduled_date(schedule.getScheduled_at());
|
2019-01-19 19:41:55 +01:00
|
|
|
st.setStatusReply(null);
|
|
|
|
st.setSent_date(null);
|
|
|
|
st.setUserId(userId);
|
2019-01-20 09:45:48 +01:00
|
|
|
st.setStatus(schedule.getStatus());
|
2019-01-19 19:41:55 +01:00
|
|
|
storedStatus.add(st);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStoredStatuses(storedStatus);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-24 16:38:12 +01:00
|
|
|
/**
|
|
|
|
* Public api call to submit a vote
|
|
|
|
* @param pollId
|
|
|
|
* @param choices
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Poll submiteVote(String pollId, int[] choices){
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
JsonArray jchoices = new JsonArray();
|
|
|
|
for(int choice : choices){
|
|
|
|
jchoices.add(choice);
|
|
|
|
}
|
|
|
|
jsonObject.add("choices",jchoices);
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-03-24 16:38:12 +01:00
|
|
|
String response = httpsConnection.postJson(getAbsoluteUrl(String.format("/polls/%s/votes", pollId)), 60, jsonObject, prefKeyOauthTokenT);
|
|
|
|
return parsePoll(context, new JSONObject(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2019-03-24 17:21:13 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public api call to refresh a poll
|
2019-03-30 16:43:03 +01:00
|
|
|
* @param status Status
|
|
|
|
* @return Poll
|
2019-03-24 17:21:13 +01:00
|
|
|
*/
|
2019-03-30 16:43:03 +01:00
|
|
|
public Poll getPoll(Status status){
|
2019-03-24 17:21:13 +01:00
|
|
|
try {
|
2019-04-18 18:01:44 +02:00
|
|
|
Poll _p = (status.getReblog() != null)?status.getReblog().getPoll():status.getPoll();
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-04-18 18:01:44 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/polls/%s", _p.getId())), 60, null, prefKeyOauthTokenT);
|
2019-03-30 16:43:03 +01:00
|
|
|
Poll poll = parsePoll(context, new JSONObject(response));
|
|
|
|
Bundle b = new Bundle();
|
2019-03-30 18:48:38 +01:00
|
|
|
status.setPoll(poll);
|
2019-03-30 16:43:03 +01:00
|
|
|
b.putParcelable("status", status);
|
|
|
|
Intent intentBC = new Intent(Helper.RECEIVE_ACTION);
|
|
|
|
intentBC.putExtras(b);
|
2019-05-12 17:35:14 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
Status alreadyCached = new TimelineCacheDAO(context, db).getSingle(status.getId());
|
2019-05-31 17:25:17 +02:00
|
|
|
Account account = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
|
2019-05-12 17:35:14 +02:00
|
|
|
if (alreadyCached != null) {
|
2019-05-31 17:25:17 +02:00
|
|
|
new TimelineCacheDAO(context, db).update(status.getId(), response, account.getId(), account.getInstance());
|
2019-05-12 17:35:14 +02:00
|
|
|
}
|
2019-03-30 16:43:03 +01:00
|
|
|
LocalBroadcastManager.getInstance(context).sendBroadcast(intentBC);
|
2019-03-24 17:21:13 +01:00
|
|
|
return parsePoll(context, new JSONObject(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2019-03-24 16:38:12 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:41:55 +01:00
|
|
|
|
2017-07-22 10:00:15 +02:00
|
|
|
/**
|
|
|
|
* Posts a status
|
|
|
|
* @param status Status object related to the status
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse postStatusAction(Status status){
|
|
|
|
|
2019-03-24 16:38:12 +01:00
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
jsonObject.addProperty("status", status.getContent());
|
2019-01-26 20:01:11 +01:00
|
|
|
if( status.getContentType() != null)
|
2019-03-24 16:38:12 +01:00
|
|
|
jsonObject.addProperty("content_type", status.getContentType());
|
2017-07-22 10:00:15 +02:00
|
|
|
if( status.getIn_reply_to_id() != null)
|
2019-03-24 16:38:12 +01:00
|
|
|
jsonObject.addProperty("in_reply_to_id", status.getIn_reply_to_id());
|
2017-07-22 10:00:15 +02:00
|
|
|
if( status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0 ) {
|
2019-03-24 16:38:12 +01:00
|
|
|
JsonArray mediaArray = new JsonArray();
|
2017-11-19 08:46:53 +01:00
|
|
|
for(Attachment attachment: status.getMedia_attachments())
|
2019-03-24 16:38:12 +01:00
|
|
|
mediaArray.add(attachment.getId());
|
|
|
|
jsonObject.add("media_ids", mediaArray);
|
2017-07-22 10:00:15 +02:00
|
|
|
}
|
2019-01-20 09:45:48 +01:00
|
|
|
if( status.getScheduled_at() != null)
|
2019-03-24 16:38:12 +01:00
|
|
|
jsonObject.addProperty("scheduled_at", status.getScheduled_at());
|
2017-07-22 10:00:15 +02:00
|
|
|
if( status.isSensitive())
|
2019-03-24 16:38:12 +01:00
|
|
|
jsonObject.addProperty("sensitive", Boolean.toString(status.isSensitive()));
|
2017-07-22 10:00:15 +02:00
|
|
|
if( status.getSpoiler_text() != null)
|
2019-03-24 16:38:12 +01:00
|
|
|
jsonObject.addProperty("spoiler_text", status.getSpoiler_text());
|
|
|
|
if( status.getPoll() != null){
|
|
|
|
JsonObject poll = new JsonObject();
|
|
|
|
JsonArray options = new JsonArray();
|
|
|
|
for(PollOptions option: status.getPoll().getOptionsList()){
|
|
|
|
if( !option.getTitle().isEmpty())
|
|
|
|
options.add(option.getTitle());
|
2017-11-21 18:12:57 +01:00
|
|
|
}
|
2019-03-24 16:38:12 +01:00
|
|
|
poll.add("options",options);
|
|
|
|
poll.addProperty("expires_in",status.getPoll().getExpires_in());
|
|
|
|
poll.addProperty("multiple",status.getPoll().isMultiple());
|
|
|
|
jsonObject.add("poll", poll);
|
|
|
|
}
|
|
|
|
jsonObject.addProperty("visibility", status.getVisibility());
|
2017-07-22 10:00:15 +02:00
|
|
|
statuses = new ArrayList<>();
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-03-24 16:38:12 +01:00
|
|
|
String response = httpsConnection.postJson(getAbsoluteUrl("/statuses"), 60, jsonObject, prefKeyOauthTokenT);
|
2017-11-18 09:55:09 +01:00
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
Status statusreturned = parseStatuses(context, new JSONObject(response));
|
|
|
|
statuses.add(statusreturned);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-01-30 10:55:57 +01:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
2018-01-30 10:55:57 +01:00
|
|
|
e.printStackTrace();
|
2017-11-18 09:55:09 +01:00
|
|
|
}
|
2017-07-22 10:00:15 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-30 11:21:55 +02:00
|
|
|
/**
|
|
|
|
* Posts a status
|
|
|
|
* @param notificationId String, the current notification id, if null all notifications are deleted
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse postNoticationAction(String notificationId){
|
|
|
|
|
|
|
|
String action;
|
2017-11-18 09:55:09 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-07-30 11:21:55 +02:00
|
|
|
if( notificationId == null)
|
|
|
|
action = "/notifications/clear";
|
|
|
|
else {
|
2017-11-18 09:55:09 +01:00
|
|
|
params.put("id",notificationId);
|
2017-07-30 11:21:55 +02:00
|
|
|
action = "/notifications/dismiss";
|
|
|
|
}
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
new HttpsConnection(context, this.instance).post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT);
|
2017-11-18 09:55:09 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 09:55:09 +01:00
|
|
|
}
|
2017-07-30 11:21:55 +02:00
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves notifications for the authenticated account since an id*synchronously*
|
|
|
|
* @param since_id String since max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2019-02-03 12:07:43 +01:00
|
|
|
public APIResponse getNotificationsSince(DisplayNotificationsFragment.Type type, String since_id, boolean display){
|
|
|
|
return getNotifications(type, null, since_id, notificationPerPage, display);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 17:52:23 +02:00
|
|
|
/**
|
|
|
|
* Retrieves notifications for the authenticated account since an id*synchronously*
|
|
|
|
* @param since_id String since max
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2019-02-03 12:07:43 +01:00
|
|
|
public APIResponse getNotificationsSince(DisplayNotificationsFragment.Type type, String since_id, int notificationPerPage, boolean display){
|
|
|
|
return getNotifications(type, null, since_id, notificationPerPage, display);
|
2017-09-27 17:52:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves notifications for the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2019-02-03 12:07:43 +01:00
|
|
|
public APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, boolean display){
|
|
|
|
return getNotifications(type, max_id, null, notificationPerPage, display);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2017-11-18 09:55:09 +01:00
|
|
|
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves notifications for the authenticated account *synchronously*
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
2017-06-07 11:12:40 +02:00
|
|
|
* @return APIResponse
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2019-02-03 12:07:43 +01:00
|
|
|
private APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, String since_id, int limit, boolean display){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if( max_id != null )
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if( since_id != null )
|
|
|
|
params.put("since_id", since_id);
|
2017-11-05 20:04:38 +01:00
|
|
|
if( 0 > limit || limit > 30)
|
|
|
|
limit = 30;
|
2017-05-05 16:36:04 +02:00
|
|
|
params.put("limit",String.valueOf(limit));
|
2017-09-30 08:36:07 +02:00
|
|
|
|
2019-02-03 18:29:34 +01:00
|
|
|
if( context == null){
|
|
|
|
apiResponse = new APIResponse();
|
|
|
|
Error error = new Error();
|
|
|
|
apiResponse.setError(error);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2017-09-30 08:36:07 +02:00
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2019-05-04 13:08:23 +02:00
|
|
|
boolean notif_follow, notif_add, notif_mention, notif_share, notif_poll;
|
2017-11-19 08:46:53 +01:00
|
|
|
StringBuilder parameters = new StringBuilder();
|
2019-02-03 12:07:43 +01:00
|
|
|
if( type == DisplayNotificationsFragment.Type.ALL){
|
|
|
|
if( display) {
|
|
|
|
notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW_FILTER, true);
|
|
|
|
notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD_FILTER, true);
|
|
|
|
notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION_FILTER, true);
|
|
|
|
notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE_FILTER, true);
|
2019-05-04 13:08:23 +02:00
|
|
|
notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL_FILTER, true);
|
2019-02-03 12:07:43 +01:00
|
|
|
}else{
|
|
|
|
notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
|
|
|
notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
|
|
|
notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
|
|
|
notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
2019-05-04 13:08:23 +02:00
|
|
|
notif_poll = sharedpreferences.getBoolean(Helper.SET_NOTIF_POLL, true);
|
2019-02-03 12:07:43 +01:00
|
|
|
}
|
|
|
|
|
2017-11-19 08:46:53 +01:00
|
|
|
|
2019-02-03 12:07:43 +01:00
|
|
|
if( !notif_follow )
|
|
|
|
parameters.append("exclude_types[]=").append("follow").append("&");
|
|
|
|
if( !notif_add )
|
|
|
|
parameters.append("exclude_types[]=").append("favourite").append("&");
|
|
|
|
if( !notif_share )
|
|
|
|
parameters.append("exclude_types[]=").append("reblog").append("&");
|
|
|
|
if( !notif_mention )
|
|
|
|
parameters.append("exclude_types[]=").append("mention").append("&");
|
2019-05-04 13:08:23 +02:00
|
|
|
if( !notif_poll )
|
|
|
|
parameters.append("exclude_types[]=").append("poll").append("&");
|
2019-02-03 12:07:43 +01:00
|
|
|
if( parameters.length() > 0) {
|
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
|
|
|
params.put("exclude_types[]", parameters.toString());
|
|
|
|
}
|
|
|
|
}else if(type == DisplayNotificationsFragment.Type.MENTION){
|
|
|
|
parameters.append("exclude_types[]=").append("follow").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("favourite").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("reblog").append("&");
|
2019-04-19 16:03:54 +02:00
|
|
|
parameters.append("exclude_types[]=").append("poll").append("&");
|
2019-02-03 12:07:43 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
|
|
|
params.put("exclude_types[]", parameters.toString());
|
|
|
|
}else if(type == DisplayNotificationsFragment.Type.FAVORITE){
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters.append("exclude_types[]=").append("follow").append("&");
|
2019-02-03 12:07:43 +01:00
|
|
|
parameters.append("exclude_types[]=").append("mention").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("reblog").append("&");
|
2019-04-19 16:03:54 +02:00
|
|
|
parameters.append("exclude_types[]=").append("poll").append("&");
|
2019-02-03 12:07:43 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
|
|
|
params.put("exclude_types[]", parameters.toString());
|
|
|
|
}else if(type == DisplayNotificationsFragment.Type.BOOST){
|
2019-04-19 16:03:54 +02:00
|
|
|
parameters.append("exclude_types[]=").append("follow").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("mention").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("favourite").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("poll").append("&");
|
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
|
|
|
params.put("exclude_types[]", parameters.toString());
|
2019-05-04 10:01:38 +02:00
|
|
|
}else if(type == DisplayNotificationsFragment.Type.POLL){
|
2019-04-19 16:03:54 +02:00
|
|
|
parameters.append("exclude_types[]=").append("reblog").append("&");
|
2019-02-03 12:07:43 +01:00
|
|
|
parameters.append("exclude_types[]=").append("follow").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("mention").append("&");
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters.append("exclude_types[]=").append("favourite").append("&");
|
2019-02-03 12:07:43 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
|
|
|
params.put("exclude_types[]", parameters.toString());
|
|
|
|
}else if(type == DisplayNotificationsFragment.Type.FOLLOW){
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters.append("exclude_types[]=").append("reblog").append("&");
|
|
|
|
parameters.append("exclude_types[]=").append("mention").append("&");
|
2019-02-03 12:07:43 +01:00
|
|
|
parameters.append("exclude_types[]=").append("favourite").append("&");
|
2019-04-19 16:03:54 +02:00
|
|
|
parameters.append("exclude_types[]=").append("poll").append("&");
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
|
|
|
params.put("exclude_types[]", parameters.toString());
|
|
|
|
}
|
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
List<Notification> notifications = new ArrayList<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-10-29 16:53:32 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 09:55:09 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/notifications"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
notifications = parseNotificationResponse(new JSONArray(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-10-29 16:53:32 +01:00
|
|
|
}
|
2017-11-18 09:55:09 +01:00
|
|
|
apiResponse.setNotifications(notifications);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-10-29 16:53:32 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
|
2019-01-19 19:41:55 +01:00
|
|
|
|
2017-10-27 10:54:28 +02:00
|
|
|
/**
|
|
|
|
* Changes media description
|
|
|
|
* @param mediaId String
|
|
|
|
* @param description String
|
|
|
|
* @return Attachment
|
|
|
|
*/
|
|
|
|
public Attachment updateDescription(String mediaId, String description){
|
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-11-21 18:12:57 +01:00
|
|
|
try {
|
|
|
|
params.put("description", URLEncoder.encode(description, "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
params.put("description", description);
|
|
|
|
}
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 09:55:09 +01:00
|
|
|
String response = httpsConnection.put(getAbsoluteUrl(String.format("/media/%s", mediaId)), 240, params, prefKeyOauthTokenT);
|
|
|
|
attachment = parseAttachmentResponse(new JSONObject(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-18 09:55:09 +01:00
|
|
|
}
|
2017-10-27 10:54:28 +02:00
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
2017-05-26 17:20:36 +02:00
|
|
|
* Retrieves Accounts and feeds when searching *synchronously*
|
2017-05-05 16:36:04 +02:00
|
|
|
*
|
|
|
|
* @param query String search
|
2019-01-16 13:11:56 +01:00
|
|
|
* @return Results
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2019-03-31 12:40:51 +02:00
|
|
|
public APIResponse search(String query) {
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2019-03-31 12:40:51 +02:00
|
|
|
apiResponse = new APIResponse();
|
2019-02-01 18:03:18 +01:00
|
|
|
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
|
|
|
|
params.put("q", query);
|
|
|
|
else
|
|
|
|
try {
|
|
|
|
params.put("q", URLEncoder.encode(query, "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
params.put("q", query);
|
|
|
|
}
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 09:55:09 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/search"), 60, params, prefKeyOauthTokenT);
|
|
|
|
results = parseResultsResponse(new JSONObject(response));
|
2019-03-31 12:40:51 +02:00
|
|
|
apiResponse.setResults(results);
|
2017-11-18 09:55:09 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2017-12-28 18:28:34 +01:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
2017-12-28 18:28:34 +01:00
|
|
|
e.printStackTrace();
|
2017-11-18 09:55:09 +01:00
|
|
|
}
|
2019-03-31 12:40:51 +02:00
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves Accounts and feeds when searching *synchronously*
|
|
|
|
*
|
|
|
|
* @param query String search
|
|
|
|
* @return Results
|
|
|
|
*/
|
2019-03-31 15:15:46 +02:00
|
|
|
public APIResponse search2(String query, searchType type, String offset) {
|
2019-03-31 12:40:51 +02:00
|
|
|
apiResponse = new APIResponse();
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE)
|
|
|
|
params.put("q", query);
|
|
|
|
else
|
|
|
|
try {
|
|
|
|
params.put("q", URLEncoder.encode(query, "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
params.put("q", query);
|
|
|
|
}
|
2019-03-31 15:15:46 +02:00
|
|
|
if( offset != null)
|
|
|
|
params.put("offset", offset);
|
2019-03-31 12:40:51 +02:00
|
|
|
switch (type){
|
|
|
|
case TAGS:
|
|
|
|
params.put("type", "hashtags");
|
|
|
|
break;
|
|
|
|
case ACCOUNTS:
|
|
|
|
params.put("type", "accounts");
|
|
|
|
break;
|
|
|
|
case STATUSES:
|
|
|
|
params.put("type", "statuses");
|
|
|
|
break;
|
|
|
|
}
|
2019-03-31 15:15:46 +02:00
|
|
|
params.put("limit", "20");
|
2019-03-31 12:40:51 +02:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-03-31 12:40:51 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUr2l("/search"), 60, params, prefKeyOauthTokenT);
|
|
|
|
results = parseResultsResponse(new JSONObject(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
apiResponse.setResults(results);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 18:04:29 +01:00
|
|
|
|
2017-05-26 17:20:36 +02:00
|
|
|
/**
|
|
|
|
* Retrieves Accounts when searching (ie: via @...) *synchronously*
|
2017-12-13 18:04:29 +01:00
|
|
|
* Not limited to following
|
2017-05-26 17:20:36 +02:00
|
|
|
* @param query String search
|
2017-06-05 11:57:02 +02:00
|
|
|
* @return APIResponse
|
2017-05-26 17:20:36 +02:00
|
|
|
*/
|
2018-02-15 10:28:30 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2017-08-02 12:17:43 +02:00
|
|
|
public APIResponse searchAccounts(String query, int count) {
|
2017-12-13 18:04:29 +01:00
|
|
|
return searchAccounts(query, count, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves Accounts when searching (ie: via @...) *synchronously*
|
|
|
|
* @param query String search
|
2018-01-13 10:41:56 +01:00
|
|
|
* @param count int limit
|
|
|
|
* @param following boolean following only
|
2017-12-13 18:04:29 +01:00
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse searchAccounts(String query, int count, boolean following) {
|
2017-05-26 17:20:36 +02:00
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put("q", query);
|
2017-08-02 12:17:43 +02:00
|
|
|
if( count < 5)
|
|
|
|
count = 5;
|
|
|
|
if( count > 40 )
|
|
|
|
count = 40;
|
2017-12-13 18:04:29 +01:00
|
|
|
if( following)
|
|
|
|
params.put("following", Boolean.toString(true));
|
2017-11-18 09:55:09 +01:00
|
|
|
params.put("limit", String.valueOf(count));
|
2017-05-26 17:20:36 +02:00
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-18 09:55:09 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/accounts/search"), 60, params, prefKeyOauthTokenT);
|
|
|
|
accounts = parseAccountResponse(new JSONArray(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2017-12-15 20:01:58 +01:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
2017-12-15 20:01:58 +01:00
|
|
|
e.printStackTrace();
|
2017-11-18 09:55:09 +01:00
|
|
|
}
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setAccounts(accounts);
|
|
|
|
return apiResponse;
|
2017-05-26 17:20:36 +02:00
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2017-11-24 07:13:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves Accounts when searching (ie: via @...) *synchronously*
|
|
|
|
*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getCustomEmoji() {
|
|
|
|
List<Emojis> emojis = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-11-24 07:13:30 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/custom_emojis"), 60, null, prefKeyOauthTokenT);
|
|
|
|
emojis = parseEmojis(new JSONArray(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-11-24 07:13:30 +01:00
|
|
|
}
|
2019-01-27 14:17:48 +01:00
|
|
|
//Add custom emoji for Pleroma
|
|
|
|
if(MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA){
|
|
|
|
APIResponse apiResponsePleroma = getCustomPleromaEmoji();
|
|
|
|
if( apiResponsePleroma != null && apiResponsePleroma.getEmojis() != null && apiResponsePleroma.getEmojis().size() > 0)
|
|
|
|
emojis.addAll(apiResponsePleroma.getEmojis());
|
|
|
|
}
|
2017-11-24 07:13:30 +01:00
|
|
|
apiResponse.setEmojis(emojis);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:34:37 +01:00
|
|
|
//Pleroma admin calls
|
2019-01-27 14:17:48 +01:00
|
|
|
/**
|
2019-01-30 09:50:07 +01:00
|
|
|
* Check if it's a Pleroma admin account and change in settings *synchronously*
|
2019-01-29 11:34:37 +01:00
|
|
|
*/
|
2019-01-30 09:50:07 +01:00
|
|
|
private void isPleromaAdmin(String nickname) {
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
boolean isAdmin;
|
2019-01-29 11:34:37 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-01-29 11:34:37 +01:00
|
|
|
String response = httpsConnection.get(String.format(Helper.getLiveInstanceWithProtocol(context)+"/api/pleroma/admin/permission_group/%s/admin",nickname), 60, null, prefKeyOauthTokenT);
|
|
|
|
//Call didn't return a 404, so the account is admin
|
2019-01-30 09:50:07 +01:00
|
|
|
isAdmin = true;
|
2019-01-29 11:34:37 +01:00
|
|
|
} catch (Exception e) {
|
2019-01-30 09:50:07 +01:00
|
|
|
isAdmin = false;
|
2019-01-29 11:34:37 +01:00
|
|
|
}
|
2019-01-30 09:50:07 +01:00
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, isAdmin);
|
|
|
|
editor.apply();
|
2019-01-29 11:34:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves Pleroma emoji *synchronously*
|
2019-01-27 14:17:48 +01:00
|
|
|
*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getCustomPleromaEmoji() {
|
|
|
|
List<Emojis> emojis = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2019-01-27 14:17:48 +01:00
|
|
|
String response = httpsConnection.get(Helper.getLiveInstanceWithProtocol(context)+"/api/pleroma/emoji", 60, null, prefKeyOauthTokenT);
|
|
|
|
emojis = parsePleromaEmojis(new JSONObject(response));
|
|
|
|
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setEmojis(emojis);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
2017-12-13 15:55:50 +01:00
|
|
|
|
2018-09-05 17:52:13 +02:00
|
|
|
/**
|
|
|
|
* Get filters for the user
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getFilters(){
|
|
|
|
|
2018-09-06 18:45:43 +02:00
|
|
|
List<Filters> filters = null;
|
2018-09-05 17:52:13 +02:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/filters"), 60, null, prefKeyOauthTokenT);
|
2018-09-05 17:52:13 +02:00
|
|
|
filters = parseFilters(new JSONArray(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setFilters(filters);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a Filter by its id
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public APIResponse getFilters(String filterId){
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
List<Filters> filters = new ArrayList<>();
|
|
|
|
Filters filter;
|
2018-09-05 17:52:13 +02:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl(String.format("/filters/%s", filterId)), 60, null, prefKeyOauthTokenT);
|
2018-09-05 17:52:13 +02:00
|
|
|
filter = parseFilter(new JSONObject(response));
|
|
|
|
filters.add(filter);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setFilters(filters);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a filter
|
|
|
|
* @param filter Filter
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse addFilters(Filters filter){
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2018-09-06 18:45:43 +02:00
|
|
|
params.put("phrase", filter.getPhrase());
|
|
|
|
StringBuilder parameters = new StringBuilder();
|
2018-09-05 17:52:13 +02:00
|
|
|
for(String context: filter.getContext())
|
2018-09-06 18:45:43 +02:00
|
|
|
parameters.append("context[]=").append(context).append("&");
|
|
|
|
if( parameters.length() > 0) {
|
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(10));
|
|
|
|
params.put("context[]", parameters.toString());
|
|
|
|
}
|
|
|
|
params.put("irreversible", String.valueOf(filter.isIrreversible()));
|
|
|
|
params.put("whole_word", String.valueOf(filter.isWhole_word()));
|
|
|
|
params.put("expires_in", String.valueOf(filter.getExpires_in()));
|
|
|
|
ArrayList<Filters> filters = new ArrayList<>();
|
2018-09-05 17:52:13 +02:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/filters"), 60, params, prefKeyOauthTokenT);
|
2018-09-06 18:45:43 +02:00
|
|
|
Filters resfilter = parseFilter(new JSONObject(response));
|
|
|
|
filters.add(resfilter);
|
2018-09-05 17:52:13 +02:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
2018-09-06 18:45:43 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2018-09-05 17:52:13 +02:00
|
|
|
}
|
2018-09-06 18:45:43 +02:00
|
|
|
apiResponse.setFilters(filters);
|
2018-09-05 17:52:13 +02:00
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a filter
|
|
|
|
* @param filter Filter
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-09-05 19:00:58 +02:00
|
|
|
public int deleteFilters(Filters filter){
|
2018-09-05 17:52:13 +02:00
|
|
|
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-09-06 18:45:43 +02:00
|
|
|
httpsConnection.delete(getAbsoluteUrl(String.format("/filters/%s", filter.getId())), 60, null, prefKeyOauthTokenT);
|
2018-09-05 19:00:58 +02:00
|
|
|
actionCode = httpsConnection.getActionCode();
|
2018-09-05 17:52:13 +02:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-09-05 19:00:58 +02:00
|
|
|
return actionCode;
|
2018-09-05 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a filter
|
|
|
|
* @param filter Filter
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse updateFilters(Filters filter){
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2018-09-06 18:45:43 +02:00
|
|
|
params.put("phrase", filter.getPhrase());
|
|
|
|
StringBuilder parameters = new StringBuilder();
|
2018-09-05 17:52:13 +02:00
|
|
|
for(String context: filter.getContext())
|
2018-09-06 18:45:43 +02:00
|
|
|
parameters.append("context[]=").append(context).append("&");
|
|
|
|
if( parameters.length() > 0) {
|
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(10));
|
|
|
|
params.put("context[]", parameters.toString());
|
|
|
|
}
|
|
|
|
params.put("irreversible", String.valueOf(filter.isIrreversible()));
|
|
|
|
params.put("whole_word", String.valueOf(filter.isWhole_word()));
|
|
|
|
params.put("expires_in", String.valueOf(filter.getExpires_in()));
|
|
|
|
ArrayList<Filters> filters = new ArrayList<>();
|
2018-09-05 17:52:13 +02:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).put(getAbsoluteUrl(String.format("/filters/%s", filter.getId())), 60, params, prefKeyOauthTokenT);
|
2018-09-06 18:45:43 +02:00
|
|
|
Filters resfilter = parseFilter(new JSONObject(response));
|
|
|
|
filters.add(resfilter);
|
2018-09-05 17:52:13 +02:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
2018-09-06 18:45:43 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2018-09-05 17:52:13 +02:00
|
|
|
}
|
2018-09-06 18:45:43 +02:00
|
|
|
apiResponse.setFilters(filters);
|
2018-09-05 17:52:13 +02:00
|
|
|
return apiResponse;
|
|
|
|
}
|
2017-12-13 16:03:54 +01:00
|
|
|
|
2017-12-13 16:06:28 +01:00
|
|
|
/**
|
|
|
|
* Get lists for the user
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getLists(){
|
2019-04-21 18:50:56 +02:00
|
|
|
apiResponse = new APIResponse();
|
2019-05-18 11:10:30 +02:00
|
|
|
List<app.fedilab.android.client.Entities.List> lists = new ArrayList<>();
|
2017-12-13 16:06:28 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/lists"), 60, null, prefKeyOauthTokenT);
|
2017-12-14 15:16:40 +01:00
|
|
|
lists = parseLists(new JSONArray(response));
|
2017-12-13 16:07:53 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-12-13 16:07:53 +01:00
|
|
|
}
|
|
|
|
apiResponse.setLists(lists);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get lists for a user by its id
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-02-15 10:28:30 +01:00
|
|
|
@SuppressWarnings("unused")
|
2017-12-13 16:07:53 +01:00
|
|
|
public APIResponse getLists(String userId){
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
List<app.fedilab.android.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
app.fedilab.android.client.Entities.List list;
|
2017-12-13 16:07:53 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl(String.format("/accounts/%s/lists", userId)), 60, null, prefKeyOauthTokenT);
|
2017-12-13 16:07:53 +01:00
|
|
|
list = parseList(new JSONObject(response));
|
2017-12-13 16:06:28 +01:00
|
|
|
lists.add(list);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-12-13 16:06:28 +01:00
|
|
|
}
|
|
|
|
apiResponse.setLists(lists);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:49:57 +01:00
|
|
|
/**
|
|
|
|
* Retrieves list timeline *synchronously*
|
|
|
|
* @param list_id String id of the list
|
|
|
|
* @param max_id String id max
|
|
|
|
* @param since_id String since the id
|
|
|
|
* @param limit int limit - max value 40
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getListTimeline(String list_id, String max_id, String since_id, int limit) {
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if (max_id != null)
|
|
|
|
params.put("max_id", max_id);
|
|
|
|
if (since_id != null)
|
|
|
|
params.put("since_id", since_id);
|
|
|
|
if (0 > limit || limit > 80)
|
|
|
|
limit = 80;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-12-14 16:49:57 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/timelines/list/%s",list_id)), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
2018-11-17 14:31:03 +01:00
|
|
|
statuses = parseStatuses(context, new JSONArray(response));
|
2017-12-14 16:49:57 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2017-12-14 18:28:06 +01:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
2017-12-14 18:28:06 +01:00
|
|
|
e.printStackTrace();
|
2017-12-14 16:49:57 +01:00
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:03:54 +01:00
|
|
|
|
2017-12-13 16:14:38 +01:00
|
|
|
/**
|
2017-12-13 16:22:06 +01:00
|
|
|
* Get accounts in a list for a user
|
2017-12-13 16:14:38 +01:00
|
|
|
* @param listId String, id of the list
|
|
|
|
* @param limit int, limit of results
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-02-15 10:28:30 +01:00
|
|
|
@SuppressWarnings("SameParameterValue")
|
2017-12-13 16:14:38 +01:00
|
|
|
public APIResponse getAccountsInList(String listId, int limit){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
if( limit < 0)
|
|
|
|
limit = 0;
|
|
|
|
if( limit > 50 )
|
|
|
|
limit = 50;
|
|
|
|
params.put("limit",String.valueOf(limit));
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-12-13 16:14:38 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/lists/%s/accounts", listId)), 60, params, prefKeyOauthTokenT);
|
|
|
|
accounts = parseAccountResponse(new JSONArray(response));
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-12-13 16:14:38 +01:00
|
|
|
}
|
2017-12-15 18:53:17 +01:00
|
|
|
apiResponse.setAccounts(accounts);
|
2017-12-13 16:14:38 +01:00
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-13 16:03:54 +01:00
|
|
|
/**
|
|
|
|
* Get a list
|
|
|
|
* @param id String, id of the list
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-02-15 10:28:30 +01:00
|
|
|
@SuppressWarnings("unused")
|
2017-12-13 16:03:54 +01:00
|
|
|
public APIResponse getList(String id){
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
List<app.fedilab.android.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
app.fedilab.android.client.Entities.List list;
|
2017-12-13 16:03:54 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl(String.format("/lists/%s",id)), 60, null, prefKeyOauthTokenT);
|
2017-12-13 16:03:54 +01:00
|
|
|
list = parseList(new JSONObject(response));
|
|
|
|
lists.add(list);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-12-13 16:03:54 +01:00
|
|
|
}
|
|
|
|
apiResponse.setLists(lists);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-12-13 16:22:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an account in a list
|
|
|
|
* @param id String, id of the list
|
|
|
|
* @param account_ids String, account to add
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
//TODO: it is unclear what is returned here
|
|
|
|
//TODO: improves doc https://github.com/tootsuite/documentation/blob/4bb149c73f40fa58fd7265a336703dd2d83efb1c/Using-the-API/API.md#addingremoving-accounts-tofrom-a-list
|
2017-12-13 16:48:01 +01:00
|
|
|
public APIResponse addAccountToList(String id, String[] account_ids){
|
2017-12-13 16:22:06 +01:00
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
2017-12-13 16:48:01 +01:00
|
|
|
StringBuilder parameters = new StringBuilder();
|
|
|
|
for(String val: account_ids)
|
|
|
|
parameters.append("account_ids[]=").append(val).append("&");
|
|
|
|
if( parameters.length() > 0) {
|
2017-12-15 18:53:17 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(14));
|
2017-12-13 16:48:01 +01:00
|
|
|
params.put("account_ids[]", parameters.toString());
|
|
|
|
}
|
2017-12-13 16:22:06 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
new HttpsConnection(context, this.instance).post(getAbsoluteUrl(String.format("/lists/%s/accounts", id)), 60, params, prefKeyOauthTokenT);
|
2017-12-13 16:22:06 +01:00
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
2017-12-13 16:22:06 +01:00
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete an account from a list
|
|
|
|
* @param id String, the id of the list
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2017-12-13 16:48:01 +01:00
|
|
|
public int deleteAccountFromList(String id, String[] account_ids){
|
2017-12-13 16:22:06 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-12-13 16:48:01 +01:00
|
|
|
StringBuilder parameters = new StringBuilder();
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
for(String val: account_ids)
|
|
|
|
parameters.append("account_ids[]=").append(val).append("&");
|
|
|
|
if( parameters.length() > 0) {
|
2017-12-15 18:53:17 +01:00
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(14));
|
2017-12-13 16:48:01 +01:00
|
|
|
params.put("account_ids[]", parameters.toString());
|
|
|
|
}
|
|
|
|
httpsConnection.delete(getAbsoluteUrl(String.format("/lists/%s/accounts", id)), 60, params, prefKeyOauthTokenT);
|
2017-12-13 16:22:06 +01:00
|
|
|
actionCode = httpsConnection.getActionCode();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2017-12-15 18:53:17 +01:00
|
|
|
e.printStackTrace();
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
2017-12-15 18:53:17 +01:00
|
|
|
e.printStackTrace();
|
2017-12-13 16:22:06 +01:00
|
|
|
}
|
|
|
|
return actionCode;
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:55:50 +01:00
|
|
|
/**
|
|
|
|
* Posts a list
|
|
|
|
* @param title String, the title of the list
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse createList(String title){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put("title",title);
|
2019-05-18 11:10:30 +02:00
|
|
|
List<app.fedilab.android.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
app.fedilab.android.client.Entities.List list;
|
2017-12-13 15:55:50 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/lists"), 60, params, prefKeyOauthTokenT);
|
2017-12-13 15:55:50 +01:00
|
|
|
list = parseList(new JSONObject(response));
|
|
|
|
lists.add(list);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-12-13 15:55:50 +01:00
|
|
|
}
|
|
|
|
apiResponse.setLists(lists);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-20 13:57:28 +01:00
|
|
|
|
2017-12-13 15:57:52 +01:00
|
|
|
/**
|
|
|
|
* Update a list by its id
|
|
|
|
* @param id String, the id of the list
|
|
|
|
* @param title String, the title of the list
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse updateList(String id, String title){
|
|
|
|
|
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put("title",title);
|
2019-05-18 11:10:30 +02:00
|
|
|
List<app.fedilab.android.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
app.fedilab.android.client.Entities.List list;
|
2017-12-13 15:57:52 +01:00
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
String response = new HttpsConnection(context, this.instance).put(getAbsoluteUrl(String.format("/lists/%s", id)), 60, params, prefKeyOauthTokenT);
|
2017-12-13 15:57:52 +01:00
|
|
|
list = parseList(new JSONObject(response));
|
|
|
|
lists.add(list);
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-08-17 18:55:38 +02:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2017-12-13 15:57:52 +01:00
|
|
|
}
|
|
|
|
apiResponse.setLists(lists);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:55:50 +01:00
|
|
|
|
2017-12-13 16:01:50 +01:00
|
|
|
/**
|
|
|
|
* Delete a list by its id
|
|
|
|
* @param id String, the id of the list
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public int deleteList(String id){
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2017-12-13 16:01:50 +01:00
|
|
|
httpsConnection.delete(getAbsoluteUrl(String.format("/lists/%s", id)), 60, null, prefKeyOauthTokenT);
|
|
|
|
actionCode = httpsConnection.getActionCode();
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
}catch (Exception e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-12-13 16:01:50 +01:00
|
|
|
}
|
|
|
|
return actionCode;
|
|
|
|
}
|
|
|
|
|
2018-09-10 17:39:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves list from Communitywiki *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-09-10 19:21:42 +02:00
|
|
|
public ArrayList<String> getCommunitywikiList() {
|
2018-09-10 17:39:37 +02:00
|
|
|
ArrayList<String> list = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-09-10 17:39:37 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrlCommunitywiki("/list"), 60, null, prefKeyOauthTokenT);
|
|
|
|
|
|
|
|
JSONArray jsonArray = new JSONArray(response);
|
|
|
|
int len = jsonArray.length();
|
|
|
|
for (int i=0;i<len;i++){
|
|
|
|
list.add(jsonArray.get(i).toString());
|
|
|
|
}
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves list from Communitywiki *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-09-10 19:21:42 +02:00
|
|
|
public ArrayList<String> getCommunitywikiList(String name) {
|
2018-09-10 17:39:37 +02:00
|
|
|
ArrayList<String> list = new ArrayList<>();
|
|
|
|
try {
|
2019-05-23 07:46:34 +02:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
|
2018-09-10 17:39:37 +02:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrlCommunitywiki(String.format("/list/%s", name)), 60, null, prefKeyOauthTokenT);
|
|
|
|
|
|
|
|
JSONArray jsonArray = new JSONArray(response);
|
|
|
|
for(int i = 0; i < jsonArray.length(); i++){
|
|
|
|
try {
|
2018-09-10 19:21:42 +02:00
|
|
|
list.add(jsonArray.getJSONObject(i).getString("acct"));
|
2018-09-10 17:39:37 +02:00
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
}
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (KeyManagementException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Parse json response an unique account
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
private Results parseResultsResponse(JSONObject resobj){
|
|
|
|
|
|
|
|
Results results = new Results();
|
|
|
|
try {
|
|
|
|
results.setAccounts(parseAccountResponse(resobj.getJSONArray("accounts")));
|
2018-11-17 14:31:03 +01:00
|
|
|
results.setStatuses(parseStatuses(context, resobj.getJSONArray("statuses")));
|
2017-05-26 20:24:44 +02:00
|
|
|
results.setHashtags(parseTags(resobj.getJSONArray("hashtags")));
|
2017-05-05 16:36:04 +02:00
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
2017-05-26 20:24:44 +02:00
|
|
|
|
2017-12-20 13:57:28 +01:00
|
|
|
/**
|
|
|
|
* Parse json response an unique Car
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Card
|
|
|
|
*/
|
2018-11-07 08:04:04 +01:00
|
|
|
private static Card parseCardResponse(JSONObject resobj){
|
2017-12-20 13:57:28 +01:00
|
|
|
|
|
|
|
Card card = new Card();
|
|
|
|
try {
|
|
|
|
card.setUrl(resobj.get("url").toString());
|
|
|
|
card.setTitle(resobj.get("title").toString());
|
|
|
|
card.setDescription(resobj.get("description").toString());
|
|
|
|
card.setImage(resobj.get("image").toString());
|
2019-01-30 09:50:07 +01:00
|
|
|
|
2017-12-20 13:57:28 +01:00
|
|
|
card.setType(resobj.get("type").toString());
|
2018-11-07 18:00:00 +01:00
|
|
|
try {
|
|
|
|
card.setAuthor_name(resobj.get("author_name").toString());
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setAuthor_name(null);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
card.setAuthor_url(resobj.get("author_url").toString());
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setAuthor_url(null);
|
|
|
|
}
|
2019-01-30 09:50:07 +01:00
|
|
|
try {
|
|
|
|
card.setHtml(resobj.get("html").toString());
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setHtml(null);
|
|
|
|
}
|
2018-11-07 18:00:00 +01:00
|
|
|
try {
|
|
|
|
card.setEmbed_url(resobj.get("embed_url").toString());
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setEmbed_url(null);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
card.setProvider_name(resobj.get("provider_name").toString());
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setProvider_name(null);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
card.setProvider_url(resobj.get("provider_url").toString());
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setProvider_url(null);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
card.setHeight(Integer.parseInt(resobj.get("height").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setHeight(0);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
card.setWidth(Integer.parseInt(resobj.get("width").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
card.setWidth(0);
|
|
|
|
}
|
2017-12-20 13:57:28 +01:00
|
|
|
} catch (JSONException e) {
|
|
|
|
card = null;
|
|
|
|
}
|
|
|
|
return card;
|
|
|
|
}
|
2017-11-24 18:49:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response an unique instance social result
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return InstanceSocial
|
|
|
|
*/
|
|
|
|
public static InstanceSocial parseInstanceSocialResponse(Context context, JSONObject resobj){
|
|
|
|
|
|
|
|
InstanceSocial instanceSocial = new InstanceSocial();
|
|
|
|
try {
|
2018-08-23 10:52:50 +02:00
|
|
|
|
2017-11-24 18:49:02 +01:00
|
|
|
|
|
|
|
instanceSocial.setUptime(Float.parseFloat(resobj.get("uptime").toString()));
|
|
|
|
instanceSocial.setUp(Boolean.parseBoolean(resobj.get("up").toString()));
|
|
|
|
|
|
|
|
instanceSocial.setConnections(Long.parseLong(resobj.get("connections").toString()));
|
|
|
|
instanceSocial.setDead(Boolean.parseBoolean(resobj.get("dead").toString()));
|
2018-08-23 10:52:50 +02:00
|
|
|
|
|
|
|
|
2017-11-24 18:49:02 +01:00
|
|
|
instanceSocial.setId(resobj.get("id").toString());
|
|
|
|
|
|
|
|
instanceSocial.setInfo(resobj.get("info").toString());
|
|
|
|
instanceSocial.setVersion(resobj.get("version").toString());
|
|
|
|
instanceSocial.setName(resobj.get("name").toString());
|
|
|
|
instanceSocial.setObs_rank(resobj.get("obs_rank").toString());
|
|
|
|
instanceSocial.setThumbnail(resobj.get("thumbnail").toString());
|
|
|
|
instanceSocial.setIpv6(Boolean.parseBoolean(resobj.get("ipv6").toString()));
|
|
|
|
instanceSocial.setObs_score(Integer.parseInt(resobj.get("obs_score").toString()));
|
|
|
|
instanceSocial.setOpen_registrations(Boolean.parseBoolean(resobj.get("open_registrations").toString()));
|
|
|
|
|
|
|
|
instanceSocial.setUsers(Long.parseLong(resobj.get("users").toString()));
|
|
|
|
instanceSocial.setStatuses(Long.parseLong(resobj.get("statuses").toString()));
|
|
|
|
|
2018-08-23 10:52:50 +02:00
|
|
|
instanceSocial.setHttps_rank(resobj.get("https_rank").toString());
|
|
|
|
instanceSocial.setHttps_score(Integer.parseInt(resobj.get("https_score").toString()));
|
|
|
|
instanceSocial.setAdded_at(Helper.mstStringToDate(context, resobj.get("added_at").toString()));
|
|
|
|
instanceSocial.setChecked_at(Helper.mstStringToDate(context, resobj.get("checked_at").toString()));
|
|
|
|
instanceSocial.setUpdated_at(Helper.mstStringToDate(context, resobj.get("updated_at").toString()));
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-11-24 18:49:02 +01:00
|
|
|
return instanceSocial;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-26 18:30:08 +02:00
|
|
|
/**
|
|
|
|
* Parse Domains
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<String> of domains
|
|
|
|
*/
|
|
|
|
private List<String> parseDomains(JSONArray jsonArray){
|
|
|
|
List<String> list_tmp = new ArrayList<>();
|
|
|
|
for(int i = 0; i < jsonArray.length(); i++){
|
|
|
|
try {
|
|
|
|
list_tmp.add(jsonArray.getString(i));
|
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
}
|
|
|
|
return list_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-26 20:24:44 +02:00
|
|
|
/**
|
|
|
|
* Parse Tags
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<String> of tags
|
|
|
|
*/
|
|
|
|
private List<String> parseTags(JSONArray jsonArray){
|
|
|
|
List<String> list_tmp = new ArrayList<>();
|
|
|
|
for(int i = 0; i < jsonArray.length(); i++){
|
|
|
|
try {
|
2019-03-31 14:25:40 +02:00
|
|
|
list_tmp.add(jsonArray.getJSONObject(i).getString("name"));
|
2017-05-26 20:24:44 +02:00
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
}
|
|
|
|
return list_tmp;
|
|
|
|
}
|
2018-09-29 09:58:46 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for several howto
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<HowToVideo>
|
|
|
|
*/
|
|
|
|
private List<HowToVideo> parseHowTos(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<HowToVideo> howToVideos = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
HowToVideo howToVideo = parseHowTo(context, resobj);
|
|
|
|
i++;
|
|
|
|
howToVideos.add(howToVideo);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return howToVideos;
|
|
|
|
}
|
|
|
|
|
2018-10-07 10:45:34 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for several howto
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Peertube>
|
|
|
|
*/
|
2018-10-17 14:34:29 +02:00
|
|
|
private List<Peertube> parsePeertube(String instance, JSONArray jsonArray){
|
2018-10-07 10:45:34 +02:00
|
|
|
|
|
|
|
List<Peertube> peertubes = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
2018-10-17 14:34:29 +02:00
|
|
|
Peertube peertube = parsePeertube(context, instance, resobj);
|
2018-10-07 10:45:34 +02:00
|
|
|
i++;
|
|
|
|
peertubes.add(peertube);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return peertubes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for unique how to
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Peertube
|
|
|
|
*/
|
2018-10-21 18:43:57 +02:00
|
|
|
public static Peertube parsePeertube(Context context, String instance, JSONObject resobj){
|
2018-10-07 10:45:34 +02:00
|
|
|
Peertube peertube = new Peertube();
|
|
|
|
try {
|
|
|
|
peertube.setId(resobj.get("id").toString());
|
2018-10-21 18:43:57 +02:00
|
|
|
peertube.setCache(resobj);
|
2018-10-07 10:45:34 +02:00
|
|
|
peertube.setUuid(resobj.get("uuid").toString());
|
|
|
|
peertube.setName(resobj.get("name").toString());
|
|
|
|
peertube.setDescription(resobj.get("description").toString());
|
|
|
|
peertube.setEmbedPath(resobj.get("embedPath").toString());
|
|
|
|
peertube.setPreviewPath(resobj.get("previewPath").toString());
|
|
|
|
peertube.setThumbnailPath(resobj.get("thumbnailPath").toString());
|
2018-10-17 14:34:29 +02:00
|
|
|
peertube.setAccount(parseAccountResponsePeertube(context, instance, resobj.getJSONObject("account")));
|
|
|
|
peertube.setInstance(instance);
|
|
|
|
peertube.setView(Integer.parseInt(resobj.get("views").toString()));
|
|
|
|
peertube.setLike(Integer.parseInt(resobj.get("likes").toString()));
|
|
|
|
peertube.setDislike(Integer.parseInt(resobj.get("dislikes").toString()));
|
|
|
|
peertube.setDuration(Integer.parseInt(resobj.get("duration").toString()));
|
|
|
|
try {
|
|
|
|
peertube.setCreated_at(Helper.mstStringToDate(context, resobj.get("createdAt").toString()));
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-10-07 10:45:34 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return peertube;
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:17:25 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for unique how to
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Peertube
|
|
|
|
*/
|
2018-10-17 14:34:29 +02:00
|
|
|
private static Peertube parseSinglePeertube(Context context, String instance, JSONObject resobj){
|
2018-10-14 16:17:25 +02:00
|
|
|
Peertube peertube = new Peertube();
|
|
|
|
try {
|
|
|
|
peertube.setId(resobj.get("id").toString());
|
|
|
|
peertube.setUuid(resobj.get("uuid").toString());
|
|
|
|
peertube.setName(resobj.get("name").toString());
|
2018-10-21 18:43:57 +02:00
|
|
|
peertube.setCache(resobj);
|
2018-10-17 14:34:29 +02:00
|
|
|
peertube.setInstance(instance);
|
2018-10-20 14:39:24 +02:00
|
|
|
peertube.setHost(resobj.getJSONObject("account").get("host").toString());
|
2018-10-14 16:17:25 +02:00
|
|
|
peertube.setDescription(resobj.get("description").toString());
|
|
|
|
peertube.setEmbedPath(resobj.get("embedPath").toString());
|
|
|
|
peertube.setPreviewPath(resobj.get("previewPath").toString());
|
|
|
|
peertube.setThumbnailPath(resobj.get("thumbnailPath").toString());
|
2018-10-17 11:32:16 +02:00
|
|
|
peertube.setView(Integer.parseInt(resobj.get("views").toString()));
|
|
|
|
peertube.setLike(Integer.parseInt(resobj.get("likes").toString()));
|
2018-10-20 15:16:57 +02:00
|
|
|
peertube.setCommentsEnabled(Boolean.parseBoolean(resobj.get("commentsEnabled").toString()));
|
2018-10-17 11:32:16 +02:00
|
|
|
peertube.setDislike(Integer.parseInt(resobj.get("dislikes").toString()));
|
|
|
|
peertube.setDuration(Integer.parseInt(resobj.get("duration").toString()));
|
2018-10-17 14:34:29 +02:00
|
|
|
peertube.setAccount(parseAccountResponsePeertube(context, instance, resobj.getJSONObject("account")));
|
2018-10-17 11:32:16 +02:00
|
|
|
try {
|
|
|
|
peertube.setCreated_at(Helper.mstStringToDate(context, resobj.get("createdAt").toString()));
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-10-14 16:17:25 +02:00
|
|
|
JSONArray files = resobj.getJSONArray("files");
|
2018-10-20 10:56:35 +02:00
|
|
|
ArrayList<String> resolutions = new ArrayList<>();
|
2018-10-14 16:17:25 +02:00
|
|
|
for(int j = 0 ; j < files.length() ; j++){
|
|
|
|
JSONObject attObj = files.getJSONObject(j);
|
2018-10-20 10:56:35 +02:00
|
|
|
resolutions.add(attObj.getJSONObject("resolution").get("id").toString());
|
2018-10-14 16:17:25 +02:00
|
|
|
}
|
2018-10-20 10:56:35 +02:00
|
|
|
peertube.setResolution(resolutions);
|
2018-10-14 16:17:25 +02:00
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return peertube;
|
|
|
|
}
|
2018-10-07 10:45:34 +02:00
|
|
|
|
2018-10-17 11:32:16 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for peertube comments
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Peertube
|
|
|
|
*/
|
|
|
|
private static List<Status> parseSinglePeertubeComments(Context context, String instance, JSONObject resobj){
|
|
|
|
List<Status> statuses = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
JSONArray jsonArray = resobj.getJSONArray("data");
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
Status status = new Status();
|
|
|
|
JSONObject comment = jsonArray.getJSONObject(i);
|
|
|
|
status.setId(comment.get("id").toString());
|
|
|
|
status.setUri(comment.get("url").toString());
|
|
|
|
status.setUrl(comment.get("url").toString());
|
|
|
|
status.setSensitive(false);
|
|
|
|
status.setSpoiler_text("");
|
|
|
|
status.setContent(comment.get("text").toString());
|
|
|
|
status.setIn_reply_to_id(comment.get("inReplyToCommentId").toString());
|
|
|
|
status.setAccount(parseAccountResponsePeertube(context, instance, comment.getJSONObject("account")));
|
|
|
|
status.setCreated_at(Helper.mstStringToDate(context, comment.get("createdAt").toString()));
|
|
|
|
status.setMentions(new ArrayList<>());
|
|
|
|
status.setEmojis(new ArrayList<>());
|
|
|
|
status.setMedia_attachments(new ArrayList<>());
|
|
|
|
status.setVisibility("public");
|
|
|
|
i++;
|
|
|
|
statuses.add(status);
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return statuses;
|
|
|
|
}
|
|
|
|
|
2018-09-29 09:58:46 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for unique how to
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return HowToVideo
|
|
|
|
*/
|
|
|
|
private static HowToVideo parseHowTo(Context context, JSONObject resobj){
|
|
|
|
HowToVideo howToVideo = new HowToVideo();
|
|
|
|
try {
|
|
|
|
howToVideo.setId(resobj.get("id").toString());
|
|
|
|
howToVideo.setUuid(resobj.get("uuid").toString());
|
|
|
|
howToVideo.setName(resobj.get("name").toString());
|
|
|
|
howToVideo.setDescription(resobj.get("description").toString());
|
|
|
|
howToVideo.setEmbedPath(resobj.get("embedPath").toString());
|
|
|
|
howToVideo.setPreviewPath(resobj.get("previewPath").toString());
|
|
|
|
howToVideo.setThumbnailPath(resobj.get("thumbnailPath").toString());
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
return howToVideo;
|
|
|
|
}
|
|
|
|
|
2018-10-29 15:49:13 +01:00
|
|
|
/**
|
|
|
|
* Parse json response for several conversations
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Conversation>
|
|
|
|
*/
|
|
|
|
private List<Conversation> parseConversations(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<Conversation> conversations = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Conversation conversation = parseConversation(context, resobj);
|
|
|
|
i++;
|
|
|
|
conversations.add(conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return conversations;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for unique conversation
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Conversation
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("InfiniteRecursion")
|
|
|
|
private Conversation parseConversation(Context context, JSONObject resobj) {
|
|
|
|
Conversation conversation = new Conversation();
|
|
|
|
try {
|
|
|
|
conversation.setId(resobj.get("id").toString());
|
|
|
|
conversation.setUnread(Boolean.parseBoolean(resobj.get("unread").toString()));
|
|
|
|
conversation.setAccounts(parseAccountResponse(resobj.getJSONArray("accounts")));
|
|
|
|
conversation.setLast_status(parseStatuses(context, resobj.getJSONObject("last_status")));
|
|
|
|
}catch (JSONException ignored) {}
|
|
|
|
return conversation;
|
|
|
|
}
|
2019-01-20 09:45:48 +01:00
|
|
|
|
2019-01-20 15:20:07 +01:00
|
|
|
/**
|
|
|
|
* Parse json response for several scheduled toots
|
|
|
|
* @param jsonObject JSONObject
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
|
|
|
private static Schedule parseSimpleSchedule(Context context, JSONObject jsonObject){
|
|
|
|
Schedule schedule = new Schedule();
|
|
|
|
try {
|
|
|
|
JSONObject resobj = jsonObject.getJSONObject("params");
|
|
|
|
Status status = parseSchedule(context, resobj);
|
|
|
|
List<Attachment> attachements = parseAttachmentResponse(jsonObject.getJSONArray("media_attachments"));
|
|
|
|
status.setMedia_attachments((ArrayList<Attachment>) attachements);
|
|
|
|
schedule.setStatus(status);
|
|
|
|
schedule.setAttachmentList(attachements);
|
|
|
|
schedule.setId(jsonObject.get("id").toString());
|
|
|
|
schedule.setScheduled_at(Helper.mstStringToDate(context, jsonObject.get("scheduled_at").toString()));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return schedule;
|
|
|
|
}
|
2019-01-20 09:45:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for several scheduled toots
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
|
|
|
private static List<Schedule> parseSchedule(Context context, JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<Schedule> schedules = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
Schedule schedule = new Schedule();
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i).getJSONObject("params");
|
|
|
|
Status status = parseSchedule(context, resobj);
|
|
|
|
List<Attachment> attachements = parseAttachmentResponse(jsonArray.getJSONObject(i).getJSONArray("media_attachments"));
|
|
|
|
status.setMedia_attachments((ArrayList<Attachment>) attachements);
|
|
|
|
schedule.setStatus(status);
|
|
|
|
schedule.setAttachmentList(attachements);
|
|
|
|
schedules.add(schedule);
|
|
|
|
schedule.setId(jsonArray.getJSONObject(i).get("id").toString());
|
2019-01-20 15:20:07 +01:00
|
|
|
schedule.setScheduled_at(Helper.mstStringToDate(context, jsonArray.getJSONObject(i).get("scheduled_at").toString()));
|
2019-01-20 09:45:48 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
2019-01-20 15:20:07 +01:00
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
2019-01-20 09:45:48 +01:00
|
|
|
}
|
|
|
|
return schedules;
|
|
|
|
}
|
|
|
|
|
2019-05-12 09:36:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for several status
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
2019-05-31 17:25:17 +02:00
|
|
|
private List<Status> parseStatusesForCache(Context context, JSONArray jsonArray){
|
2019-05-12 09:36:28 +02:00
|
|
|
|
|
|
|
List<Status> statuses = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Status status = parseStatuses(context, resobj);
|
2019-05-31 17:25:17 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
Status alreadyCached = new TimelineCacheDAO(context, db).getSingle(status.getId());
|
|
|
|
if (alreadyCached == null) {
|
|
|
|
Account account = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
|
|
|
|
new TimelineCacheDAO(context, db).insert(status.getId(), resobj.toString(), account.getId(), account.getInstance());
|
2019-05-12 09:36:28 +02:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
statuses.add(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return statuses;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for several status
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
2019-01-20 09:45:48 +01:00
|
|
|
private static List<Status> parseStatuses(Context context, JSONArray jsonArray){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
List<Status> statuses = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
2017-06-03 15:54:19 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
2017-08-28 13:53:07 +02:00
|
|
|
Status status = parseStatuses(context, resobj);
|
2017-05-05 16:36:04 +02:00
|
|
|
i++;
|
2019-06-15 18:42:14 +02:00
|
|
|
if( status != null) {
|
|
|
|
statuses.add(status);
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
2018-11-17 14:31:03 +01:00
|
|
|
e.printStackTrace();
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return statuses;
|
|
|
|
}
|
|
|
|
|
2019-03-24 16:38:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a poll
|
|
|
|
* @param context
|
|
|
|
* @param resobj
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static Poll parsePoll(Context context, JSONObject resobj){
|
|
|
|
Poll poll = new Poll();
|
|
|
|
try {
|
|
|
|
poll.setId(resobj.getString("id"));
|
|
|
|
poll.setExpires_at(Helper.mstStringToDate(context, resobj.getString("expires_at")));
|
|
|
|
poll.setExpired(resobj.getBoolean("expired"));
|
|
|
|
poll.setMultiple(resobj.getBoolean("multiple"));
|
|
|
|
poll.setVotes_count(resobj.getInt("votes_count"));
|
|
|
|
poll.setVoted(resobj.getBoolean("voted"));
|
|
|
|
JSONArray options = resobj.getJSONArray("options");
|
|
|
|
List<PollOptions> pollOptions = new ArrayList<>();
|
|
|
|
for(int i = 0; i < options.length() ; i++){
|
|
|
|
JSONObject option = options.getJSONObject(i);
|
|
|
|
PollOptions pollOption = new PollOptions();
|
|
|
|
pollOption.setTitle(option.getString("title"));
|
|
|
|
pollOption.setVotes_count(option.getInt("votes_count"));
|
|
|
|
pollOptions.add(pollOption);
|
|
|
|
}
|
|
|
|
poll.setOptionsList(pollOptions);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return poll;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for unique status
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("InfiniteRecursion")
|
2017-08-28 15:01:45 +02:00
|
|
|
public static Status parseStatuses(Context context, JSONObject resobj){
|
2017-05-05 16:36:04 +02:00
|
|
|
Status status = new Status();
|
|
|
|
try {
|
|
|
|
status.setId(resobj.get("id").toString());
|
2017-10-03 19:16:15 +02:00
|
|
|
status.setUri(resobj.get("uri").toString());
|
2017-05-05 16:36:04 +02:00
|
|
|
status.setCreated_at(Helper.mstStringToDate(context, resobj.get("created_at").toString()));
|
|
|
|
status.setIn_reply_to_id(resobj.get("in_reply_to_id").toString());
|
|
|
|
status.setIn_reply_to_account_id(resobj.get("in_reply_to_account_id").toString());
|
2017-06-07 15:47:05 +02:00
|
|
|
status.setSensitive(Boolean.parseBoolean(resobj.get("sensitive").toString()));
|
2017-05-05 16:36:04 +02:00
|
|
|
status.setSpoiler_text(resobj.get("spoiler_text").toString());
|
2018-12-05 09:58:15 +01:00
|
|
|
try {
|
|
|
|
status.setVisibility(resobj.get("visibility").toString());
|
|
|
|
}catch (Exception e){status.setVisibility("public");}
|
2019-01-22 09:09:01 +01:00
|
|
|
try {
|
|
|
|
status.setLanguage(resobj.get("language").toString());
|
2019-01-22 09:12:35 +01:00
|
|
|
}catch (Exception e){status.setLanguage("ja");}
|
2017-08-04 16:13:28 +02:00
|
|
|
status.setUrl(resobj.get("url").toString());
|
2017-06-24 07:23:15 +02:00
|
|
|
//Retrieves attachments
|
2017-05-05 16:36:04 +02:00
|
|
|
JSONArray arrayAttachement = resobj.getJSONArray("media_attachments");
|
2017-06-26 18:49:39 +02:00
|
|
|
ArrayList<Attachment> attachments = new ArrayList<>();
|
2017-05-05 16:36:04 +02:00
|
|
|
if( arrayAttachement != null){
|
|
|
|
for(int j = 0 ; j < arrayAttachement.length() ; j++){
|
|
|
|
JSONObject attObj = arrayAttachement.getJSONObject(j);
|
|
|
|
Attachment attachment = new Attachment();
|
|
|
|
attachment.setId(attObj.get("id").toString());
|
|
|
|
attachment.setPreview_url(attObj.get("preview_url").toString());
|
|
|
|
attachment.setRemote_url(attObj.get("remote_url").toString());
|
|
|
|
attachment.setType(attObj.get("type").toString());
|
|
|
|
attachment.setText_url(attObj.get("text_url").toString());
|
|
|
|
attachment.setUrl(attObj.get("url").toString());
|
2018-05-11 18:20:07 +02:00
|
|
|
try {
|
|
|
|
attachment.setDescription(attObj.get("description").toString());
|
|
|
|
}catch (JSONException ignore){}
|
2017-05-05 16:36:04 +02:00
|
|
|
attachments.add(attachment);
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 09:09:01 +01:00
|
|
|
|
2018-11-07 08:04:04 +01:00
|
|
|
try {
|
|
|
|
status.setCard(parseCardResponse(resobj.getJSONObject("card")));
|
2018-11-18 16:01:46 +01:00
|
|
|
}catch (Exception e){status.setCard(null);}
|
2018-11-07 08:04:04 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
status.setMedia_attachments(attachments);
|
2017-06-24 07:23:15 +02:00
|
|
|
//Retrieves mentions
|
2017-06-03 15:54:19 +02:00
|
|
|
List<Mention> mentions = new ArrayList<>();
|
|
|
|
JSONArray arrayMention = resobj.getJSONArray("mentions");
|
|
|
|
if( arrayMention != null){
|
|
|
|
for(int j = 0 ; j < arrayMention.length() ; j++){
|
|
|
|
JSONObject menObj = arrayMention.getJSONObject(j);
|
|
|
|
Mention mention = new Mention();
|
|
|
|
mention.setId(menObj.get("id").toString());
|
|
|
|
mention.setUrl(menObj.get("url").toString());
|
|
|
|
mention.setAcct(menObj.get("acct").toString());
|
|
|
|
mention.setUsername(menObj.get("username").toString());
|
|
|
|
mentions.add(mention);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status.setMentions(mentions);
|
2017-06-24 07:23:15 +02:00
|
|
|
//Retrieves tags
|
|
|
|
List<Tag> tags = new ArrayList<>();
|
|
|
|
JSONArray arrayTag = resobj.getJSONArray("tags");
|
|
|
|
if( arrayTag != null){
|
|
|
|
for(int j = 0 ; j < arrayTag.length() ; j++){
|
|
|
|
JSONObject tagObj = arrayTag.getJSONObject(j);
|
|
|
|
Tag tag = new Tag();
|
|
|
|
tag.setName(tagObj.get("name").toString());
|
|
|
|
tag.setUrl(tagObj.get("url").toString());
|
|
|
|
tags.add(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status.setTags(tags);
|
2017-10-20 06:57:53 +02:00
|
|
|
//Retrieves emjis
|
|
|
|
List<Emojis> emojiList = new ArrayList<>();
|
2017-10-20 19:58:46 +02:00
|
|
|
try {
|
|
|
|
JSONArray emojisTag = resobj.getJSONArray("emojis");
|
2018-08-15 14:31:15 +02:00
|
|
|
if( emojisTag != null){
|
2017-10-20 19:58:46 +02:00
|
|
|
for(int j = 0 ; j < emojisTag.length() ; j++){
|
|
|
|
JSONObject emojisObj = emojisTag.getJSONObject(j);
|
2017-11-24 07:13:30 +01:00
|
|
|
Emojis emojis = parseEmojis(emojisObj);
|
2017-10-20 19:58:46 +02:00
|
|
|
emojiList.add(emojis);
|
|
|
|
}
|
2017-10-20 06:57:53 +02:00
|
|
|
}
|
2017-10-20 19:58:46 +02:00
|
|
|
status.setEmojis(emojiList);
|
|
|
|
}catch (Exception e){
|
2018-04-22 18:02:00 +02:00
|
|
|
status.setEmojis(new ArrayList<>());
|
2017-10-20 06:57:53 +02:00
|
|
|
}
|
2018-02-15 10:13:58 +01:00
|
|
|
//Retrieve Application
|
|
|
|
Application application = new Application();
|
2018-02-15 11:18:40 +01:00
|
|
|
try {
|
2018-04-25 13:53:02 +02:00
|
|
|
if(resobj.getJSONObject("application") != null){
|
|
|
|
application.setName(resobj.getJSONObject("application").getString("name"));
|
|
|
|
application.setWebsite(resobj.getJSONObject("application").getString("website"));
|
2018-02-15 11:18:40 +01:00
|
|
|
}
|
2018-04-25 13:53:02 +02:00
|
|
|
}catch (Exception e){
|
|
|
|
application = new Application();
|
|
|
|
}
|
2018-02-15 10:13:58 +01:00
|
|
|
status.setApplication(application);
|
|
|
|
|
2017-08-28 13:53:07 +02:00
|
|
|
status.setAccount(parseAccountResponse(context, resobj.getJSONObject("account")));
|
2017-05-05 16:36:04 +02:00
|
|
|
status.setContent(resobj.get("content").toString());
|
|
|
|
status.setFavourites_count(Integer.valueOf(resobj.get("favourites_count").toString()));
|
|
|
|
status.setReblogs_count(Integer.valueOf(resobj.get("reblogs_count").toString()));
|
2018-08-19 15:21:39 +02:00
|
|
|
try{
|
|
|
|
status.setReplies_count(Integer.valueOf(resobj.get("replies_count").toString()));
|
|
|
|
}catch (Exception e){
|
2018-09-04 18:25:45 +02:00
|
|
|
status.setReplies_count(-1);
|
2018-08-19 15:21:39 +02:00
|
|
|
}
|
2017-09-28 19:34:26 +02:00
|
|
|
try {
|
|
|
|
status.setReblogged(Boolean.valueOf(resobj.get("reblogged").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
status.setReblogged(false);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
status.setFavourited(Boolean.valueOf(resobj.get("favourited").toString()));
|
|
|
|
}catch (Exception e){
|
2018-02-15 11:18:40 +01:00
|
|
|
status.setFavourited(false);
|
2017-09-28 19:34:26 +02:00
|
|
|
}
|
2018-02-15 07:55:24 +01:00
|
|
|
try {
|
|
|
|
status.setMuted(Boolean.valueOf(resobj.get("muted").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
status.setMuted(false);
|
|
|
|
}
|
2017-09-16 10:16:05 +02:00
|
|
|
try {
|
|
|
|
status.setPinned(Boolean.valueOf(resobj.get("pinned").toString()));
|
|
|
|
}catch (JSONException e){
|
|
|
|
status.setPinned(false);
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
try{
|
2017-08-28 13:53:07 +02:00
|
|
|
status.setReblog(parseStatuses(context, resobj.getJSONObject("reblog")));
|
2018-02-15 15:27:46 +01:00
|
|
|
}catch (Exception ignored){}
|
2019-03-24 19:49:17 +01:00
|
|
|
|
|
|
|
if( resobj.has("poll") && !resobj.isNull("poll")){
|
|
|
|
Poll poll = new Poll();
|
|
|
|
poll.setId(resobj.getJSONObject("poll").getString("id"));
|
2019-03-24 19:53:36 +01:00
|
|
|
try {
|
|
|
|
poll.setExpires_at(Helper.mstStringToDate(context, resobj.getJSONObject("poll").getString("expires_at")));
|
|
|
|
}catch (Exception e){
|
|
|
|
poll.setExpires_at(new Date());
|
|
|
|
}
|
2019-03-24 19:49:17 +01:00
|
|
|
poll.setExpired(resobj.getJSONObject("poll").getBoolean("expired"));
|
|
|
|
poll.setMultiple(resobj.getJSONObject("poll").getBoolean("multiple"));
|
|
|
|
poll.setVotes_count(resobj.getJSONObject("poll").getInt("votes_count"));
|
|
|
|
poll.setVoted(resobj.getJSONObject("poll").getBoolean("voted"));
|
|
|
|
JSONArray options = resobj.getJSONObject("poll").getJSONArray("options");
|
|
|
|
List<PollOptions> pollOptions = new ArrayList<>();
|
|
|
|
for(int i = 0; i < options.length() ; i++){
|
|
|
|
JSONObject option = options.getJSONObject(i);
|
|
|
|
PollOptions pollOption = new PollOptions();
|
|
|
|
pollOption.setTitle(option.getString("title"));
|
|
|
|
pollOption.setVotes_count(option.getInt("votes_count"));
|
|
|
|
pollOptions.add(pollOption);
|
|
|
|
}
|
|
|
|
poll.setOptionsList(pollOptions);
|
|
|
|
status.setPoll(poll);
|
|
|
|
}
|
|
|
|
|
2018-08-23 10:52:50 +02:00
|
|
|
} catch (JSONException ignored) {} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:09:02 +01:00
|
|
|
|
2019-01-20 09:45:48 +01:00
|
|
|
/**
|
|
|
|
* Parse json response for unique schedule
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("InfiniteRecursion")
|
|
|
|
private static Status parseSchedule(Context context, JSONObject resobj){
|
|
|
|
Status status = new Status();
|
|
|
|
try {
|
|
|
|
status.setIn_reply_to_id(resobj.get("in_reply_to_id").toString());
|
|
|
|
status.setSensitive(Boolean.parseBoolean(resobj.get("sensitive").toString()));
|
|
|
|
status.setSpoiler_text(resobj.get("spoiler_text").toString());
|
|
|
|
try {
|
|
|
|
status.setVisibility(resobj.get("visibility").toString());
|
|
|
|
}catch (Exception e){status.setVisibility("public");}
|
|
|
|
status.setContent(resobj.get("text").toString());
|
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:09:02 +01:00
|
|
|
/**
|
|
|
|
* Parse json response for several notes (Misskey)
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
|
|
|
public static List<Status> parseNotes(Context context, String instance, JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<Status> statuses = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Status status = parseNotes(context, instance, resobj);
|
|
|
|
i++;
|
|
|
|
statuses.add(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return statuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for unique note (misskey)
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("InfiniteRecursion")
|
|
|
|
public static Status parseNotes(Context context, String instance, JSONObject resobj){
|
|
|
|
Status status = new Status();
|
|
|
|
try {
|
|
|
|
status.setId(resobj.get("id").toString());
|
|
|
|
status.setUri("https://" + instance + "/notes/" + resobj.get("id").toString());
|
|
|
|
status.setCreated_at(Helper.mstStringToDate(context, resobj.get("createdAt").toString()));
|
|
|
|
status.setIn_reply_to_id(resobj.get("replyId").toString());
|
|
|
|
status.setSensitive(false);
|
|
|
|
if(resobj.get("cw") != null && !resobj.get("cw").toString().equals("null"))
|
|
|
|
status.setSpoiler_text(resobj.get("cw").toString());
|
|
|
|
try {
|
|
|
|
status.setVisibility(resobj.get("visibility").toString());
|
|
|
|
}catch (Exception e){status.setVisibility("public"); e.printStackTrace();}
|
|
|
|
status.setUrl("https://" + instance + "/notes/" + resobj.get("id").toString());
|
|
|
|
//Retrieves attachments
|
|
|
|
JSONArray arrayAttachement = resobj.getJSONArray("media");
|
|
|
|
ArrayList<Attachment> attachments = new ArrayList<>();
|
|
|
|
if( arrayAttachement != null){
|
|
|
|
for(int j = 0 ; j < arrayAttachement.length() ; j++){
|
|
|
|
JSONObject attObj = arrayAttachement.getJSONObject(j);
|
|
|
|
Attachment attachment = new Attachment();
|
|
|
|
attachment.setId(attObj.get("id").toString());
|
|
|
|
attachment.setPreview_url(attObj.get("thumbnailUrl").toString());
|
|
|
|
attachment.setRemote_url(attObj.get("url").toString());
|
|
|
|
if( attObj.get("type").toString().contains("/")){
|
|
|
|
attachment.setType(attObj.get("type").toString().split("/")[0]);
|
|
|
|
}else
|
|
|
|
attachment.setType(attObj.get("type").toString());
|
|
|
|
attachment.setText_url(attObj.get("url").toString());
|
|
|
|
attachment.setUrl(attObj.get("url").toString());
|
|
|
|
if(attObj.get("isSensitive").toString().equals("true")){
|
|
|
|
status.setSensitive(true);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
attachment.setDescription(attObj.get("comment").toString());
|
|
|
|
}catch (JSONException ignore){ignore.printStackTrace();}
|
|
|
|
attachments.add(attachment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
status.setCard(parseCardResponse(resobj.getJSONObject("card")));
|
|
|
|
}catch (Exception e){status.setCard(null);}
|
|
|
|
|
|
|
|
status.setMedia_attachments(attachments);
|
|
|
|
//Retrieves mentions
|
|
|
|
List<Mention> mentions = new ArrayList<>();
|
|
|
|
|
|
|
|
status.setAccount(parseMisskeyAccountResponse(context, instance, resobj.getJSONObject("user")));
|
|
|
|
status.setContent(resobj.get("text").toString());
|
|
|
|
try{
|
|
|
|
status.setReplies_count(Integer.valueOf(resobj.get("repliesCount").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
status.setReplies_count(-1);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
status.setFavourited(Boolean.valueOf(resobj.get("isFavorited").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
status.setFavourited(false);
|
|
|
|
}
|
|
|
|
try{
|
|
|
|
if(resobj.getJSONObject("renoteId") != null && !resobj.getJSONObject("renoteId").toString().equals("null"))
|
|
|
|
status.setReblog(parseStatuses(context, resobj.getJSONObject("renote")));
|
|
|
|
}catch (Exception ignored){}
|
|
|
|
|
|
|
|
status.setMentions(mentions);
|
|
|
|
//Retrieves tags
|
|
|
|
List<Tag> tags = new ArrayList<>();
|
|
|
|
JSONArray arrayTag = resobj.getJSONArray("tags");
|
|
|
|
if( arrayTag != null){
|
|
|
|
for(int j = 0 ; j < arrayTag.length() ; j++){
|
|
|
|
JSONObject tagObj = arrayTag.getJSONObject(j);
|
|
|
|
Tag tag = new Tag();
|
|
|
|
tag.setName(tagObj.get("name").toString());
|
|
|
|
tag.setUrl(tagObj.get("url").toString());
|
|
|
|
tags.add(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status.setTags(tags);
|
|
|
|
|
|
|
|
//Retrieves emjis
|
|
|
|
List<Emojis> emojiList = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
JSONArray emojisTag = resobj.getJSONArray("emojis");
|
|
|
|
if( emojisTag != null){
|
|
|
|
for(int j = 0 ; j < emojisTag.length() ; j++){
|
|
|
|
JSONObject emojisObj = emojisTag.getJSONObject(j);
|
2018-12-29 19:21:39 +01:00
|
|
|
Emojis emojis = parseMisskeyEmojis(emojisObj);
|
2018-12-29 12:09:02 +01:00
|
|
|
emojiList.add(emojis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status.setEmojis(emojiList);
|
|
|
|
}catch (Exception e){
|
|
|
|
status.setEmojis(new ArrayList<>());
|
|
|
|
}
|
|
|
|
|
|
|
|
//Retrieve Application
|
|
|
|
Application application = new Application();
|
|
|
|
try {
|
|
|
|
if(resobj.getJSONObject("application") != null){
|
|
|
|
application.setName(resobj.getJSONObject("application").getString("name"));
|
|
|
|
application.setWebsite(resobj.getJSONObject("application").getString("website"));
|
|
|
|
}
|
|
|
|
}catch (Exception e){
|
|
|
|
application = new Application();
|
|
|
|
}
|
|
|
|
status.setApplication(application);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (JSONException ignored) {} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
2017-06-05 11:57:02 +02:00
|
|
|
/**
|
|
|
|
* Parse json response an unique instance
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Instance
|
|
|
|
*/
|
|
|
|
private Instance parseInstance(JSONObject resobj){
|
|
|
|
|
|
|
|
Instance instance = new Instance();
|
|
|
|
try {
|
|
|
|
instance.setUri(resobj.get("uri").toString());
|
|
|
|
instance.setTitle(resobj.get("title").toString());
|
|
|
|
instance.setDescription(resobj.get("description").toString());
|
|
|
|
instance.setEmail(resobj.get("email").toString());
|
|
|
|
instance.setVersion(resobj.get("version").toString());
|
2019-06-04 11:02:15 +02:00
|
|
|
|
|
|
|
if(resobj.has("poll_limits")){
|
|
|
|
HashMap<String, Integer> poll_limits = new HashMap<>();
|
|
|
|
JSONObject polllimits = resobj.getJSONObject("poll_limits");
|
|
|
|
poll_limits.put("min_expiration",polllimits.getInt("min_expiration"));
|
|
|
|
poll_limits.put("max_options",polllimits.getInt("max_options"));
|
|
|
|
poll_limits.put("max_option_chars",polllimits.getInt("max_option_chars"));
|
|
|
|
poll_limits.put("max_expiration",polllimits.getInt("max_expiration"));
|
|
|
|
instance.setPoll_limits(poll_limits);
|
|
|
|
}
|
2017-06-05 11:57:02 +02:00
|
|
|
} catch (JSONException e) {
|
2019-06-04 11:02:15 +02:00
|
|
|
e.printStackTrace();
|
2017-06-05 11:57:02 +02:00
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2019-06-14 14:36:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for several instance reg
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
|
|
|
public List<InstanceReg> parseInstanceReg(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<InstanceReg> instanceRegs = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ){
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
InstanceReg instanceReg = parseInstanceReg(resobj);
|
|
|
|
i++;
|
|
|
|
instanceRegs.add(instanceReg);
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return instanceRegs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response an unique instance for registering
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return InstanceReg
|
|
|
|
*/
|
|
|
|
private InstanceReg parseInstanceReg(JSONObject resobj){
|
|
|
|
InstanceReg instanceReg = new InstanceReg();
|
|
|
|
try {
|
|
|
|
instanceReg.setDomain(resobj.getString("domain"));
|
|
|
|
instanceReg.setVersion(resobj.getString("version"));
|
|
|
|
instanceReg.setDescription(resobj.getString("description"));
|
|
|
|
instanceReg.setLanguage(resobj.getString("language"));
|
|
|
|
instanceReg.setCategory(resobj.getString("category"));
|
|
|
|
instanceReg.setProxied_thumbnail(resobj.getString("proxied_thumbnail"));
|
|
|
|
instanceReg.setTotal_users(resobj.getInt("total_users"));
|
|
|
|
instanceReg.setLast_week_users(resobj.getInt("last_week_users"));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return instanceReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-01-27 14:17:48 +01:00
|
|
|
/**
|
|
|
|
* Parse Pleroma emojis
|
|
|
|
* @param jsonObject JSONObject
|
|
|
|
* @return List<Emojis> of emojis
|
|
|
|
*/
|
|
|
|
private List<Emojis> parsePleromaEmojis(JSONObject jsonObject){
|
|
|
|
List<Emojis> emojis = new ArrayList<>();
|
|
|
|
Iterator<String> iter = jsonObject.keys();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
String shortcode = iter.next();
|
|
|
|
try {
|
|
|
|
String url = (String) jsonObject.get(shortcode);
|
|
|
|
Emojis emojisObj = new Emojis();
|
|
|
|
emojisObj.setVisible_in_picker(true);
|
|
|
|
emojisObj.setShortcode(shortcode);
|
|
|
|
emojisObj.setStatic_url(Helper.getLiveInstanceWithProtocol(context) + url);
|
|
|
|
emojisObj.setUrl(Helper.getLiveInstanceWithProtocol(context) + url);
|
|
|
|
emojis.add(emojisObj);
|
|
|
|
} catch (JSONException ignored) { }
|
|
|
|
}
|
|
|
|
return emojis;
|
|
|
|
}
|
2017-11-24 07:13:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse emojis
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Emojis> of emojis
|
|
|
|
*/
|
|
|
|
private List<Emojis> parseEmojis(JSONArray jsonArray){
|
|
|
|
List<Emojis> emojis = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Emojis emojis1 = parseEmojis(resobj);
|
2019-01-25 11:41:39 +01:00
|
|
|
if( emojis1.isVisible_in_picker())
|
|
|
|
emojis.add(emojis1);
|
2017-11-24 07:13:30 +01:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-11-24 07:13:30 +01:00
|
|
|
}
|
|
|
|
return emojis;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for emoji
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Emojis
|
|
|
|
*/
|
|
|
|
private static Emojis parseEmojis(JSONObject resobj){
|
|
|
|
Emojis emojis = new Emojis();
|
|
|
|
try {
|
|
|
|
emojis.setShortcode(resobj.get("shortcode").toString());
|
|
|
|
emojis.setStatic_url(resobj.get("static_url").toString());
|
|
|
|
emojis.setUrl(resobj.get("url").toString());
|
2019-01-25 11:41:39 +01:00
|
|
|
try {
|
|
|
|
emojis.setVisible_in_picker((resobj.getBoolean("visible_in_picker")));
|
|
|
|
}catch (Exception e){
|
|
|
|
emojis.setVisible_in_picker(true);
|
|
|
|
}
|
2017-12-02 14:54:25 +01:00
|
|
|
}catch (Exception ignored){}
|
2017-11-24 07:13:30 +01:00
|
|
|
return emojis;
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:45:15 +01:00
|
|
|
|
2018-12-29 19:21:39 +01:00
|
|
|
/**
|
|
|
|
* Parse emojis
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Emojis> of emojis
|
|
|
|
*/
|
|
|
|
private List<Emojis> parseMisskeyEmojis(JSONArray jsonArray){
|
|
|
|
List<Emojis> emojis = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Emojis emojis1 = parseMisskeyEmojis(resobj);
|
|
|
|
emojis.add(emojis1);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return emojis;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for emoji
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Emojis
|
|
|
|
*/
|
|
|
|
private static Emojis parseMisskeyEmojis(JSONObject resobj){
|
|
|
|
Emojis emojis = new Emojis();
|
|
|
|
try {
|
|
|
|
emojis.setShortcode(resobj.get("name").toString());
|
|
|
|
emojis.setStatic_url(resobj.get("url").toString());
|
|
|
|
emojis.setUrl(resobj.get("url").toString());
|
|
|
|
}catch (Exception ignored){}
|
|
|
|
return emojis;
|
|
|
|
}
|
|
|
|
|
2018-09-05 17:52:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse Filters
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Filters> of filters
|
|
|
|
*/
|
2018-09-06 18:45:43 +02:00
|
|
|
private List<Filters> parseFilters(JSONArray jsonArray){
|
|
|
|
List<Filters> filters = new ArrayList<>();
|
2018-09-05 17:52:13 +02:00
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
2018-09-06 18:45:43 +02:00
|
|
|
Filters filter = parseFilter(resobj);
|
2018-09-09 09:37:15 +02:00
|
|
|
if( filter != null)
|
|
|
|
filters.add(filter);
|
2018-09-05 17:52:13 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return filters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for filter
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Filter
|
|
|
|
*/
|
2018-09-09 09:37:15 +02:00
|
|
|
private Filters parseFilter(JSONObject resobj){
|
2019-05-18 11:10:30 +02:00
|
|
|
Filters filter = new Filters();
|
2018-09-05 17:52:13 +02:00
|
|
|
try {
|
2018-09-22 19:13:55 +02:00
|
|
|
|
2018-09-06 18:45:43 +02:00
|
|
|
filter.setId(resobj.get("id").toString());
|
2018-09-09 09:37:15 +02:00
|
|
|
if( resobj.get("phrase").toString() == null)
|
|
|
|
return null;
|
2018-09-05 17:52:13 +02:00
|
|
|
filter.setPhrase(resobj.get("phrase").toString());
|
2018-09-22 19:13:55 +02:00
|
|
|
if( resobj.get("expires_at") != null && !resobj.get("expires_at").toString().equals("null"))
|
|
|
|
filter.setSetExpires_at(Helper.mstStringToDate(context, resobj.get("expires_at").toString()));
|
2018-09-05 17:52:13 +02:00
|
|
|
filter.setWhole_word(Boolean.parseBoolean(resobj.get("whole_word").toString()));
|
|
|
|
filter.setIrreversible(Boolean.parseBoolean(resobj.get("irreversible").toString()));
|
|
|
|
String contextString = resobj.get("context").toString();
|
2018-09-05 19:37:03 +02:00
|
|
|
contextString = contextString.replaceAll("\\[","");
|
|
|
|
contextString = contextString.replaceAll("]","");
|
|
|
|
contextString = contextString.replaceAll("\"","");
|
2018-09-05 17:52:13 +02:00
|
|
|
if( contextString != null) {
|
|
|
|
String[] context = contextString.split(",");
|
|
|
|
if( contextString.length() > 0 ){
|
|
|
|
ArrayList<String> finalContext = new ArrayList<>();
|
|
|
|
for(String c: context)
|
|
|
|
finalContext.add(c.trim());
|
|
|
|
filter.setContext(finalContext);
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 18:45:43 +02:00
|
|
|
return filter;
|
|
|
|
}catch (Exception ignored){ return null;}
|
2018-09-05 19:00:58 +02:00
|
|
|
|
2018-09-05 17:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 15:45:15 +01:00
|
|
|
/**
|
|
|
|
* Parse Lists
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<List> of lists
|
|
|
|
*/
|
2019-05-18 11:10:30 +02:00
|
|
|
private List<app.fedilab.android.client.Entities.List> parseLists(JSONArray jsonArray){
|
|
|
|
List<app.fedilab.android.client.Entities.List> lists = new ArrayList<>();
|
2017-12-13 15:45:15 +01:00
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
2019-05-18 11:10:30 +02:00
|
|
|
app.fedilab.android.client.Entities.List list = parseList(resobj);
|
2017-12-13 15:45:15 +01:00
|
|
|
lists.add(list);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-12-13 15:45:15 +01:00
|
|
|
}
|
|
|
|
return lists;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for emoji
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Emojis
|
|
|
|
*/
|
2019-05-18 11:10:30 +02:00
|
|
|
private static app.fedilab.android.client.Entities.List parseList(JSONObject resobj){
|
|
|
|
app.fedilab.android.client.Entities.List list = new app.fedilab.android.client.Entities.List();
|
2017-12-13 15:45:15 +01:00
|
|
|
try {
|
|
|
|
list.setId(resobj.get("id").toString());
|
|
|
|
list.setTitle(resobj.get("title").toString());
|
|
|
|
}catch (Exception ignored){}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-10-22 17:09:25 +02:00
|
|
|
private List<Account> parseAccountResponsePeertube(Context context, String instance, JSONArray jsonArray){
|
|
|
|
List<Account> accounts = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Account account = parseAccountResponsePeertube(context, instance, resobj);
|
|
|
|
accounts.add(account);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
2017-12-13 15:45:15 +01:00
|
|
|
|
2018-10-17 11:32:16 +02:00
|
|
|
/**
|
|
|
|
* Parse json response an unique peertube account
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
private static Account parseAccountResponsePeertube(Context context, String instance, JSONObject resobj){
|
|
|
|
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());
|
2018-10-20 18:13:22 +02:00
|
|
|
account.setHost(resobj.get("host").toString());
|
2018-10-17 14:34:29 +02:00
|
|
|
if( resobj.has("createdAt") )
|
|
|
|
account.setCreated_at(Helper.mstStringToDate(context, resobj.get("createdAt").toString()));
|
|
|
|
else
|
|
|
|
account.setCreated_at(new Date());
|
|
|
|
|
|
|
|
if( resobj.has("followersCount") )
|
|
|
|
account.setFollowers_count(Integer.valueOf(resobj.get("followersCount").toString()));
|
|
|
|
else
|
|
|
|
account.setFollowers_count(0);
|
|
|
|
if( resobj.has("followingCount"))
|
|
|
|
account.setFollowing_count(Integer.valueOf(resobj.get("followingCount").toString()));
|
|
|
|
else
|
|
|
|
account.setFollowing_count(0);
|
2018-10-17 11:32:16 +02:00
|
|
|
account.setStatuses_count(0);
|
2018-10-17 14:34:29 +02:00
|
|
|
if( resobj.has("description") )
|
|
|
|
account.setNote(resobj.get("description").toString());
|
|
|
|
else
|
|
|
|
account.setNote("");
|
2018-10-17 11:32:16 +02:00
|
|
|
account.setUrl(resobj.get("url").toString());
|
2019-01-14 09:48:01 +01:00
|
|
|
account.setSocial("PEERTUBE");
|
2018-10-17 14:34:29 +02:00
|
|
|
if( resobj.has("avatar") && !resobj.get("avatar").toString().equals("null")){
|
2018-10-17 11:32:16 +02:00
|
|
|
account.setAvatar("https://" + instance + resobj.getJSONObject("avatar").get("path"));
|
|
|
|
}else
|
|
|
|
account.setAvatar(null);
|
|
|
|
account.setAvatar_static(resobj.get("avatar").toString());
|
|
|
|
} catch (JSONException ignored) {} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
2019-06-18 16:22:53 +02:00
|
|
|
|
2019-06-18 17:44:11 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for list of reports for admins
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Report>
|
|
|
|
*/
|
|
|
|
private List<Report> parseReportAdminResponse(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<Report> reports = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Report report = parseReportAdminResponse(context, resobj);
|
|
|
|
reports.add(report);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return reports;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response an unique report for admins
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return AccountAdmin
|
|
|
|
*/
|
|
|
|
private static Report parseReportAdminResponse(Context context, JSONObject resobj){
|
|
|
|
|
|
|
|
Report report = new Report();
|
|
|
|
try {
|
|
|
|
report.setId(resobj.getString("id"));
|
2019-06-19 18:42:07 +02:00
|
|
|
report.setAction_taken(resobj.getBoolean("action_taken"));
|
2019-06-18 17:44:11 +02:00
|
|
|
report.setComment(resobj.getString("comment"));
|
|
|
|
report.setCreated_at(Helper.mstStringToDate(context, resobj.getString("created_at")));
|
|
|
|
report.setUpdated_at(Helper.mstStringToDate(context, resobj.getString("updated_at")));
|
2019-06-19 16:05:14 +02:00
|
|
|
if( !resobj.isNull("account")) {
|
2019-06-19 18:42:07 +02:00
|
|
|
report.setAccount(parseAccountAdminResponse(context, resobj.getJSONObject("account")));
|
2019-06-19 16:05:14 +02:00
|
|
|
}
|
|
|
|
if( !resobj.isNull("target_account")) {
|
2019-06-19 18:42:07 +02:00
|
|
|
report.setTarget_account(parseAccountAdminResponse(context, resobj.getJSONObject("target_account")));
|
2019-06-19 16:05:14 +02:00
|
|
|
}
|
|
|
|
if( !resobj.isNull("assigned_account")) {
|
2019-06-19 18:42:07 +02:00
|
|
|
report.setAssigned_account(parseAccountAdminResponse(context, resobj.getJSONObject("assigned_account")));
|
|
|
|
}
|
|
|
|
if( !resobj.isNull("action_taken_by_account")) {
|
|
|
|
report.setAction_taken_by_account(parseAccountAdminResponse(context, resobj.getJSONObject("action_taken_by_account")));
|
2019-06-19 16:05:14 +02:00
|
|
|
}
|
2019-06-18 17:44:11 +02:00
|
|
|
report.setStatuses(parseStatuses(context, resobj.getJSONArray("statuses")));
|
2019-06-19 18:42:07 +02:00
|
|
|
}catch (Exception ignored){ignored.printStackTrace();}
|
2019-06-18 17:44:11 +02:00
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-18 16:22:53 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for list of accounts for admins
|
|
|
|
* @param jsonArray JSONArray
|
2019-06-18 17:44:11 +02:00
|
|
|
* @return List<AccountAdmin>
|
2019-06-18 16:22:53 +02:00
|
|
|
*/
|
|
|
|
private List<AccountAdmin> parseAccountAdminResponse(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<AccountAdmin> accountAdmins = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
AccountAdmin accountAdmin = parseAccountAdminResponse(context, resobj);
|
|
|
|
accountAdmins.add(accountAdmin);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return accountAdmins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response an unique account for admins
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
private static AccountAdmin parseAccountAdminResponse(Context context, JSONObject resobj){
|
|
|
|
|
|
|
|
AccountAdmin accountAdmin = new AccountAdmin();
|
|
|
|
try {
|
|
|
|
accountAdmin.setId(resobj.get("id").toString());
|
|
|
|
accountAdmin.setUsername(resobj.getString("username"));
|
|
|
|
accountAdmin.setCreated_at(Helper.mstStringToDate(context, resobj.getString("created_at")));
|
|
|
|
accountAdmin.setEmail(resobj.getString("email"));
|
|
|
|
accountAdmin.setRole(resobj.getString("role"));
|
|
|
|
accountAdmin.setIp(resobj.getString("ip"));
|
2019-06-22 09:38:19 +02:00
|
|
|
accountAdmin.setDomain(resobj.getString("domain"));
|
2019-06-18 16:22:53 +02:00
|
|
|
accountAdmin.setAccount(parseAccountResponse(context, resobj.getJSONObject("account")));
|
2019-06-19 18:42:07 +02:00
|
|
|
if( !resobj.isNull("confirmed")) {
|
|
|
|
accountAdmin.setConfirmed(resobj.getBoolean("confirmed"));
|
|
|
|
}else{
|
|
|
|
accountAdmin.setConfirmed(true);
|
|
|
|
}
|
|
|
|
if( !resobj.isNull("suspended")) {
|
|
|
|
accountAdmin.setSuspended(resobj.getBoolean("suspended"));
|
|
|
|
}else{
|
|
|
|
accountAdmin.setSuspended(false);
|
|
|
|
}
|
|
|
|
if( !resobj.isNull("silenced")) {
|
|
|
|
accountAdmin.setSilenced(resobj.getBoolean("silenced"));
|
|
|
|
}else{
|
|
|
|
accountAdmin.setSilenced(false);
|
|
|
|
}
|
|
|
|
if( !resobj.isNull("disabled")) {
|
|
|
|
accountAdmin.setDisabled(resobj.getBoolean("disabled"));
|
|
|
|
}else{
|
|
|
|
accountAdmin.setDisabled(false);
|
|
|
|
}
|
2019-06-18 16:22:53 +02:00
|
|
|
}catch (Exception ignored){}
|
|
|
|
return accountAdmin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for list of accounts
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Account>
|
|
|
|
*/
|
|
|
|
private List<Account> parseAccountResponse(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<Account> accounts = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Account account = parseAccountResponse(context, resobj);
|
|
|
|
accounts.add(account);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Parse json response an unique account
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Account
|
|
|
|
*/
|
2017-12-17 08:56:09 +01:00
|
|
|
@SuppressWarnings("InfiniteRecursion")
|
2017-08-28 13:53:07 +02:00
|
|
|
private static Account parseAccountResponse(Context context, JSONObject resobj){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
Account account = new Account();
|
|
|
|
try {
|
|
|
|
account.setId(resobj.get("id").toString());
|
2019-01-03 18:12:24 +01:00
|
|
|
account.setUuid(resobj.get("id").toString());
|
2017-05-05 16:36:04 +02:00
|
|
|
account.setUsername(resobj.get("username").toString());
|
|
|
|
account.setAcct(resobj.get("acct").toString());
|
|
|
|
account.setDisplay_name(resobj.get("display_name").toString());
|
|
|
|
account.setLocked(Boolean.parseBoolean(resobj.get("locked").toString()));
|
|
|
|
account.setCreated_at(Helper.mstStringToDate(context, resobj.get("created_at").toString()));
|
|
|
|
account.setFollowers_count(Integer.valueOf(resobj.get("followers_count").toString()));
|
|
|
|
account.setFollowing_count(Integer.valueOf(resobj.get("following_count").toString()));
|
|
|
|
account.setStatuses_count(Integer.valueOf(resobj.get("statuses_count").toString()));
|
|
|
|
account.setNote(resobj.get("note").toString());
|
2018-12-22 11:08:04 +01:00
|
|
|
try {
|
|
|
|
account.setBot(Boolean.parseBoolean(resobj.get("bot").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
account.setBot(false);
|
|
|
|
}
|
2017-12-17 08:56:09 +01:00
|
|
|
try{
|
2017-12-17 10:09:16 +01:00
|
|
|
account.setMoved_to_account(parseAccountResponse(context, resobj.getJSONObject("moved")));
|
2017-12-17 13:04:17 +01:00
|
|
|
}catch (Exception ignored){account.setMoved_to_account(null);}
|
2017-05-05 16:36:04 +02:00
|
|
|
account.setUrl(resobj.get("url").toString());
|
|
|
|
account.setAvatar(resobj.get("avatar").toString());
|
|
|
|
account.setAvatar_static(resobj.get("avatar_static").toString());
|
|
|
|
account.setHeader(resobj.get("header").toString());
|
|
|
|
account.setHeader_static(resobj.get("header_static").toString());
|
2019-01-14 09:48:01 +01:00
|
|
|
|
|
|
|
try{
|
|
|
|
account.setSocial(resobj.get("software").toString().toUpperCase());
|
|
|
|
}catch (Exception ignored){
|
|
|
|
account.setSocial("MASTODON");
|
|
|
|
}
|
2019-01-27 10:40:09 +01:00
|
|
|
try{
|
2019-01-27 11:02:53 +01:00
|
|
|
if( resobj.has("pleroma") ) {
|
2019-01-27 10:40:09 +01:00
|
|
|
account.setSocial("PLEROMA");
|
2019-01-27 11:02:53 +01:00
|
|
|
try{
|
|
|
|
account.setModerator(resobj.getJSONObject("pleroma").getBoolean("is_moderator"));
|
|
|
|
account.setAdmin(resobj.getJSONObject("pleroma").getBoolean("is_admin"));
|
|
|
|
}catch (Exception ignored){
|
|
|
|
account.setModerator(false);
|
|
|
|
account.setAdmin(false);
|
|
|
|
}
|
|
|
|
}
|
2019-01-27 10:40:09 +01:00
|
|
|
}catch (Exception ignored){}
|
2019-01-27 11:02:53 +01:00
|
|
|
|
2018-05-20 10:34:32 +02:00
|
|
|
try {
|
|
|
|
JSONArray fields = resobj.getJSONArray("fields");
|
2018-10-24 15:44:57 +02:00
|
|
|
LinkedHashMap<String, String> fieldsMap = new LinkedHashMap<>();
|
|
|
|
LinkedHashMap<String, Boolean> fieldsMapVerified = new LinkedHashMap<>();
|
2018-05-20 10:34:32 +02:00
|
|
|
if( fields != null){
|
|
|
|
for(int j = 0 ; j < fields.length() ; j++){
|
|
|
|
fieldsMap.put(fields.getJSONObject(j).getString("name"),fields.getJSONObject(j).getString("value"));
|
2018-09-27 19:47:55 +02:00
|
|
|
try {
|
|
|
|
fieldsMapVerified.put(fields.getJSONObject(j).getString("name"),(fields.getJSONObject(j).getString("verified_at")!= null && !fields.getJSONObject(j).getString("verified_at").equals("null")));
|
|
|
|
}catch (Exception e){
|
|
|
|
fieldsMapVerified.put(fields.getJSONObject(j).getString("name"),false);
|
|
|
|
}
|
|
|
|
|
2018-05-20 10:34:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
account.setFields(fieldsMap);
|
2018-09-27 19:33:22 +02:00
|
|
|
account.setFieldsVerified(fieldsMapVerified);
|
2018-05-20 10:34:32 +02:00
|
|
|
}catch (Exception ignored){}
|
2018-08-15 14:31:15 +02:00
|
|
|
|
2019-05-08 17:50:17 +02:00
|
|
|
|
|
|
|
|
2018-08-15 14:31:15 +02:00
|
|
|
//Retrieves emjis
|
|
|
|
List<Emojis> emojiList = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
JSONArray emojisTag = resobj.getJSONArray("emojis");
|
|
|
|
if( emojisTag != null){
|
|
|
|
for(int j = 0 ; j < emojisTag.length() ; j++){
|
|
|
|
JSONObject emojisObj = emojisTag.getJSONObject(j);
|
|
|
|
Emojis emojis = parseEmojis(emojisObj);
|
|
|
|
emojiList.add(emojis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
account.setEmojis(emojiList);
|
|
|
|
}catch (Exception e){
|
|
|
|
account.setEmojis(new ArrayList<>());
|
|
|
|
}
|
2019-05-08 17:50:17 +02:00
|
|
|
if( resobj.has("source")){
|
|
|
|
JSONObject source = resobj.getJSONObject("source");
|
|
|
|
try{
|
|
|
|
account.setPrivacy(source.getString("privacy"));
|
|
|
|
account.setSensitive(source.getBoolean("sensitive"));
|
|
|
|
}catch (Exception e){
|
|
|
|
account.setPrivacy("public");
|
|
|
|
account.setSensitive(false);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 10:52:50 +02:00
|
|
|
} catch (JSONException ignored) {} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:09:02 +01:00
|
|
|
|
2019-02-07 19:47:53 +01:00
|
|
|
private List<Account> parseOpencollectiveAccountResponse(Context context, RetrieveOpenCollectiveAsyncTask.Type type, JSONArray jsonArray){
|
|
|
|
List<Account> accounts = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Account account = parseOpencollectiveAccountResponse(context, type, resobj);
|
|
|
|
if( type == RetrieveOpenCollectiveAsyncTask.Type.BACKERS && account.getSocial() != null && account.getSocial().equals("OPENCOLLECTIVE_BACKER"))
|
|
|
|
accounts.add(account);
|
|
|
|
else if( type == RetrieveOpenCollectiveAsyncTask.Type.SPONSORS && account.getSocial() != null && account.getSocial().equals("OPENCOLLECTIVE_SPONSOR"))
|
|
|
|
accounts.add(account);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
setDefaultError(e);
|
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response an unique account
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("InfiniteRecursion")
|
|
|
|
private static Account parseOpencollectiveAccountResponse(Context context, RetrieveOpenCollectiveAsyncTask.Type type, JSONObject resobj){
|
|
|
|
|
|
|
|
Account account = new Account();
|
|
|
|
try {
|
|
|
|
account.setId(resobj.get("MemberId").toString());
|
|
|
|
account.setUuid(resobj.get("MemberId").toString());
|
|
|
|
account.setUsername(resobj.get("name").toString());
|
|
|
|
account.setAcct(resobj.get("tier").toString());
|
|
|
|
account.setDisplay_name(resobj.get("name").toString());
|
|
|
|
account.setLocked(false);
|
|
|
|
account.setCreated_at(Helper.opencollectivetStringToDate(context, resobj.get("createdAt").toString()));
|
|
|
|
account.setFollowers_count(0);
|
|
|
|
account.setFollowing_count(0);
|
|
|
|
account.setStatuses_count(0);
|
|
|
|
account.setNote(resobj.get("description").toString());
|
|
|
|
account.setBot(false);
|
|
|
|
account.setMoved_to_account(null);
|
|
|
|
account.setUrl(resobj.get("profile").toString());
|
|
|
|
account.setAvatar(resobj.get("image").toString());
|
|
|
|
account.setAvatar_static(resobj.get("image").toString());
|
|
|
|
account.setHeader(null);
|
|
|
|
account.setHeader_static(null);
|
|
|
|
if(resobj.get("role").toString().equals("BACKER"))
|
|
|
|
account.setSocial("OPENCOLLECTIVE_BACKER");
|
|
|
|
else if(resobj.get("role").toString().equals("SPONSOR"))
|
|
|
|
account.setSocial("OPENCOLLECTIVE_SPONSOR");
|
|
|
|
else
|
|
|
|
account.setSocial("OPENCOLLECTIVE");
|
|
|
|
|
|
|
|
} catch (JSONException ignored) {} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
2018-12-29 12:09:02 +01:00
|
|
|
/**
|
|
|
|
* Parse json response an unique account
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("InfiniteRecursion")
|
|
|
|
private static Account parseMisskeyAccountResponse(Context context, String instance, JSONObject resobj){
|
|
|
|
|
|
|
|
Account account = new Account();
|
|
|
|
try {
|
|
|
|
account.setId(resobj.get("id").toString());
|
|
|
|
account.setUsername(resobj.get("username").toString());
|
|
|
|
String host = resobj.get("host").toString();
|
|
|
|
String acct;
|
|
|
|
if( host == null || host.equals("null"))
|
|
|
|
acct = resobj.get("username").toString();
|
|
|
|
else
|
|
|
|
acct = resobj.get("username").toString() + "@" + host;
|
|
|
|
account.setAcct(acct);
|
|
|
|
account.setDisplay_name(resobj.get("name").toString());
|
|
|
|
account.setCreated_at(new Date());
|
|
|
|
|
|
|
|
account.setUrl("https://" + instance + "/@"+account.getUsername());
|
|
|
|
account.setAvatar(resobj.get("avatarUrl").toString());
|
|
|
|
account.setAvatar_static(resobj.get("avatarUrl").toString());
|
|
|
|
try {
|
|
|
|
account.setBot(Boolean.parseBoolean(resobj.get("isBot").toString()));
|
|
|
|
}catch (Exception e){
|
|
|
|
account.setBot(false);
|
|
|
|
}
|
|
|
|
//Retrieves emjis
|
|
|
|
List<Emojis> emojiList = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
JSONArray emojisTag = resobj.getJSONArray("emojis");
|
|
|
|
if( emojisTag != null){
|
|
|
|
for(int j = 0 ; j < emojisTag.length() ; j++){
|
|
|
|
JSONObject emojisObj = emojisTag.getJSONObject(j);
|
|
|
|
Emojis emojis = parseEmojis(emojisObj);
|
|
|
|
emojiList.add(emojis);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
account.setEmojis(emojiList);
|
|
|
|
}catch (Exception e){
|
|
|
|
account.setEmojis(new ArrayList<>());
|
|
|
|
}
|
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
return account;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
|
2017-06-03 15:54:19 +02:00
|
|
|
|
|
|
|
/**
|
2017-09-17 11:50:00 +02:00
|
|
|
* Parse json response an unique relationship
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Relationship
|
|
|
|
*/
|
|
|
|
private Relationship parseRelationshipResponse(JSONObject resobj){
|
|
|
|
|
|
|
|
Relationship relationship = new Relationship();
|
|
|
|
try {
|
|
|
|
relationship.setId(resobj.get("id").toString());
|
|
|
|
relationship.setFollowing(Boolean.valueOf(resobj.get("following").toString()));
|
|
|
|
relationship.setFollowed_by(Boolean.valueOf(resobj.get("followed_by").toString()));
|
|
|
|
relationship.setBlocking(Boolean.valueOf(resobj.get("blocking").toString()));
|
|
|
|
relationship.setMuting(Boolean.valueOf(resobj.get("muting").toString()));
|
2017-12-17 11:10:49 +01:00
|
|
|
try {
|
|
|
|
relationship.setMuting_notifications(Boolean.valueOf(resobj.get("muting_notifications").toString()));
|
|
|
|
}catch (Exception ignored){
|
|
|
|
relationship.setMuting_notifications(true);
|
|
|
|
}
|
2018-09-04 18:25:45 +02:00
|
|
|
try {
|
|
|
|
relationship.setEndorsed(Boolean.valueOf(resobj.get("endorsed").toString()));
|
|
|
|
}catch (Exception ignored){
|
|
|
|
relationship.setMuting_notifications(false);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
relationship.setShowing_reblogs(Boolean.valueOf(resobj.get("showing_reblogs").toString()));
|
|
|
|
}catch (Exception ignored){
|
|
|
|
relationship.setMuting_notifications(false);
|
|
|
|
}
|
2017-09-17 11:50:00 +02:00
|
|
|
relationship.setRequested(Boolean.valueOf(resobj.get("requested").toString()));
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-09-17 11:50:00 +02:00
|
|
|
}
|
|
|
|
return relationship;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for list of relationship
|
2017-06-03 15:54:19 +02:00
|
|
|
* @param jsonArray JSONArray
|
2017-09-17 11:50:00 +02:00
|
|
|
* @return List<Relationship>
|
2017-06-03 15:54:19 +02:00
|
|
|
*/
|
2017-09-17 11:50:00 +02:00
|
|
|
private List<Relationship> parseRelationshipResponse(JSONArray jsonArray){
|
2017-06-03 15:54:19 +02:00
|
|
|
|
2017-09-17 11:50:00 +02:00
|
|
|
List<Relationship> relationships = new ArrayList<>();
|
2017-06-03 15:54:19 +02:00
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
2017-09-17 11:50:00 +02:00
|
|
|
Relationship relationship = parseRelationshipResponse(resobj);
|
|
|
|
relationships.add(relationship);
|
2017-06-03 15:54:19 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-06-03 15:54:19 +02:00
|
|
|
}
|
2017-09-17 11:50:00 +02:00
|
|
|
return relationships;
|
2017-06-03 15:54:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for the context
|
|
|
|
* @param jsonObject JSONObject
|
2019-05-18 11:10:30 +02:00
|
|
|
* @return Context
|
2017-05-05 16:36:04 +02:00
|
|
|
*/
|
2019-05-18 11:10:30 +02:00
|
|
|
private app.fedilab.android.client.Entities.Context parseContext(JSONObject jsonObject){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
app.fedilab.android.client.Entities.Context context = new app.fedilab.android.client.Entities.Context();
|
2017-05-05 16:36:04 +02:00
|
|
|
try {
|
2018-11-17 14:31:03 +01:00
|
|
|
context.setAncestors(parseStatuses(this.context, jsonObject.getJSONArray("ancestors")));
|
|
|
|
context.setDescendants(parseStatuses(this.context, jsonObject.getJSONArray("descendants")));
|
2017-05-05 16:36:04 +02:00
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2019-01-20 09:45:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for list of relationship
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Relationship>
|
|
|
|
*/
|
|
|
|
private static List<Attachment> parseAttachmentResponse(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<Attachment> attachments = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
Attachment attachment = parseAttachmentResponse(resobj);
|
|
|
|
attachments.add(attachment);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException ignored) { }
|
|
|
|
return attachments;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Parse json response an unique attachment
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Relationship
|
|
|
|
*/
|
2019-06-05 18:23:44 +02:00
|
|
|
public static Attachment parseAttachmentResponse(JSONObject resobj){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
Attachment attachment = new Attachment();
|
|
|
|
try {
|
|
|
|
attachment.setId(resobj.get("id").toString());
|
|
|
|
attachment.setType(resobj.get("type").toString());
|
|
|
|
attachment.setUrl(resobj.get("url").toString());
|
2017-10-27 10:51:00 +02:00
|
|
|
try {
|
|
|
|
attachment.setDescription(resobj.get("description").toString());
|
|
|
|
}catch (JSONException ignore){}
|
2017-05-05 16:36:04 +02:00
|
|
|
try{
|
|
|
|
attachment.setRemote_url(resobj.get("remote_url").toString());
|
|
|
|
}catch (JSONException ignore){}
|
|
|
|
try{
|
|
|
|
attachment.setPreview_url(resobj.get("preview_url").toString());
|
|
|
|
}catch (JSONException ignore){}
|
2018-02-15 10:18:39 +01:00
|
|
|
try{
|
|
|
|
attachment.setMeta(resobj.get("meta").toString());
|
|
|
|
}catch (JSONException ignore){}
|
2017-05-05 16:36:04 +02:00
|
|
|
try{
|
|
|
|
attachment.setText_url(resobj.get("text_url").toString());
|
|
|
|
}catch (JSONException ignore){}
|
|
|
|
|
2017-12-02 14:54:25 +01:00
|
|
|
} catch (JSONException ignored) {}
|
2017-05-05 16:36:04 +02:00
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response an unique notification
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Account
|
|
|
|
*/
|
2017-08-28 13:53:07 +02:00
|
|
|
public static Notification parseNotificationResponse(Context context, JSONObject resobj){
|
2017-05-05 16:36:04 +02:00
|
|
|
|
|
|
|
Notification notification = new Notification();
|
|
|
|
try {
|
|
|
|
notification.setId(resobj.get("id").toString());
|
|
|
|
notification.setType(resobj.get("type").toString());
|
|
|
|
notification.setCreated_at(Helper.mstStringToDate(context, resobj.get("created_at").toString()));
|
2017-08-28 13:53:07 +02:00
|
|
|
notification.setAccount(parseAccountResponse(context, resobj.getJSONObject("account")));
|
2017-05-05 16:36:04 +02:00
|
|
|
try{
|
2017-08-28 13:53:07 +02:00
|
|
|
notification.setStatus(parseStatuses(context, resobj.getJSONObject("status")));
|
2017-05-05 16:36:04 +02:00
|
|
|
}catch (Exception ignored){}
|
|
|
|
notification.setCreated_at(Helper.mstStringToDate(context, resobj.get("created_at").toString()));
|
2018-08-23 10:52:50 +02:00
|
|
|
} catch (JSONException ignored) {} catch (ParseException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response for list of notifications
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Notification>
|
|
|
|
*/
|
|
|
|
private List<Notification> parseNotificationResponse(JSONArray jsonArray){
|
|
|
|
|
|
|
|
List<Notification> notifications = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
2017-06-03 15:54:19 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
2017-08-28 13:53:07 +02:00
|
|
|
Notification notification = parseNotificationResponse(context, resobj);
|
2017-05-05 16:36:04 +02:00
|
|
|
notifications.add(notification);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return notifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the error message
|
|
|
|
* @param statusCode int code
|
|
|
|
* @param error Throwable error
|
|
|
|
*/
|
|
|
|
private void setError(int statusCode, Throwable error){
|
2017-06-03 18:18:27 +02:00
|
|
|
APIError = new Error();
|
2018-01-02 11:42:11 +01:00
|
|
|
APIError.setStatusCode(statusCode);
|
2018-08-17 18:55:38 +02:00
|
|
|
String message = statusCode + " - " + error.getMessage();
|
|
|
|
try {
|
|
|
|
JSONObject jsonObject = new JSONObject(error.getMessage());
|
|
|
|
String errorM = jsonObject.get("error").toString();
|
|
|
|
message = "Error " + statusCode + " : " + errorM;
|
|
|
|
} catch (JSONException e) {
|
2018-12-09 17:12:47 +01:00
|
|
|
if(error.getMessage().split(".").length > 0) {
|
|
|
|
String errorM = error.getMessage().split(".")[0];
|
|
|
|
message = "Error " + statusCode + " : " + errorM;
|
|
|
|
}
|
2018-08-17 18:55:38 +02:00
|
|
|
}
|
|
|
|
APIError.setError(message);
|
2017-06-03 18:18:27 +02:00
|
|
|
apiResponse.setError(APIError);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-02 11:42:11 +01:00
|
|
|
private void setDefaultError(Exception e){
|
2017-11-23 15:51:55 +01:00
|
|
|
APIError = new Error();
|
2018-01-02 11:42:11 +01:00
|
|
|
if( e.getLocalizedMessage() != null && e.getLocalizedMessage().trim().length() > 0)
|
|
|
|
APIError.setError(e.getLocalizedMessage());
|
|
|
|
else if( e.getMessage() != null && e.getMessage().trim().length() > 0)
|
|
|
|
APIError.setError(e.getMessage());
|
|
|
|
else
|
|
|
|
APIError.setError(context.getString(R.string.toast_error));
|
2017-11-23 15:51:55 +01:00
|
|
|
apiResponse.setError(APIError);
|
|
|
|
}
|
2017-10-29 16:53:32 +01:00
|
|
|
|
|
|
|
|
2017-05-30 19:31:44 +02:00
|
|
|
public Error getError(){
|
2017-06-03 18:18:27 +02:00
|
|
|
return APIError;
|
2017-05-30 19:31:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 14:10:19 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
private String getAbsoluteUrl(String action) {
|
2019-05-24 15:13:28 +02:00
|
|
|
return Helper.instanceWithProtocol(this.context, this.instance) + "/api/v1" + action;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2019-01-15 15:41:30 +01:00
|
|
|
private String getAbsoluteUr2l(String action) {
|
2019-05-24 15:13:28 +02:00
|
|
|
return Helper.instanceWithProtocol(this.context, this.instance) + "/api/v2" + action;
|
2019-01-15 15:41:30 +01:00
|
|
|
}
|
2018-12-26 15:43:36 +01:00
|
|
|
private String getAbsoluteUrlRemote(String remote, String action) {
|
|
|
|
return "https://" + remote + "/api/v1" + action;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
2018-08-20 19:00:20 +02:00
|
|
|
private String getAbsoluteUrlRemoteInstance(String instanceName) {
|
|
|
|
return "https://" + instanceName + "/api/v1/timelines/public?local=true";
|
|
|
|
}
|
2018-09-10 17:39:37 +02:00
|
|
|
|
|
|
|
private String getAbsoluteUrlCommunitywiki(String action) {
|
|
|
|
return "https://communitywiki.org/trunk/api/v1" + action;
|
|
|
|
}
|
2018-09-29 09:58:46 +02:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|