2017-05-05 16:36:04 +02:00
|
|
|
package fr.gouv.etalab.mastodon.client;
|
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* This file is a part of Mastalab
|
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.
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* Mastalab 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.
|
|
|
|
*
|
2017-08-04 11:11:27 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
2017-05-05 16:36:04 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2017-12-17 13:04:17 +01:00
|
|
|
|
2017-05-05 16:36:04 +02:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2017-11-21 18:12:57 +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;
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.lang.*;
|
2017-11-21 18:12:57 +01:00
|
|
|
import java.net.URLEncoder;
|
2018-08-17 18:55:38 +02:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.util.ArrayList;
|
2017-11-18 08:27:25 +01:00
|
|
|
import java.util.HashMap;
|
2017-05-05 16:36:04 +02:00
|
|
|
import java.util.List;
|
2017-11-23 15:51:55 +01:00
|
|
|
|
|
|
|
import fr.gouv.etalab.mastodon.R;
|
2017-05-30 19:31:44 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.*;
|
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Error;
|
2017-05-05 16:36:04 +02:00
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
2017-11-12 08:33:24 +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;
|
|
|
|
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;
|
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,
|
|
|
|
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,
|
|
|
|
UNPIN
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
2018-03-14 16:34:03 +01:00
|
|
|
public enum accountPrivacy {
|
|
|
|
PUBLIC,
|
|
|
|
LOCKED
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
public API(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
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
|
|
|
this.instance = Helper.getLiveInstance(context);
|
2017-05-28 17:16:31 +02:00
|
|
|
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-28 14:10:19 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).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
|
|
|
|
|
|
|
|
2017-06-05 11:57:02 +02:00
|
|
|
/***
|
|
|
|
* Update credential of the authenticated user *synchronously*
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2018-04-23 07:00:12 +02:00
|
|
|
public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy) {
|
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");
|
|
|
|
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2018-04-23 07:00:12 +02:00
|
|
|
new HttpsConnection(context).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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).get(getAbsoluteUrl("/accounts/verify_credentials"), 60, null, prefKeyOauthTokenT);
|
2017-11-18 08:27:25 +01:00
|
|
|
account = parseAccountResponse(context, new JSONObject(response));
|
|
|
|
} catch (HttpsConnection.HttpsConnectionException e) {
|
|
|
|
setError(e.getStatusCode(), e);
|
2018-01-24 16:27:52 +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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/accounts/%s",accountId)), 60, null, prefKeyOauthTokenT);
|
2017-11-18 08:27:25 +01:00
|
|
|
account = parseAccountResponse(context, 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 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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).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
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2017-09-17 11:50:00 +02:00
|
|
|
}
|
2017-11-18 09:55:09 +01:00
|
|
|
List<Relationship> relationships = new ArrayList<>();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 08:27:25 +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);
|
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-09-17 12:25:21 +02:00
|
|
|
apiResponse.setRelationships(relationships);
|
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
|
|
|
}
|
|
|
|
|
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( exclude_replies)
|
|
|
|
params.put("exclude_replies", 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;
|
|
|
|
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));
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/statuses", accountId)), 60, params, prefKeyOauthTokenT);
|
|
|
|
statuses = parseStatuses(new JSONArray(response));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the context of status with replies *synchronously*
|
|
|
|
*
|
|
|
|
* @param statusId Id of the status
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
|
|
|
public fr.gouv.etalab.mastodon.client.Entities.Context getStatusContext(String statusId) {
|
2017-11-18 09:55:09 +01:00
|
|
|
fr.gouv.etalab.mastodon.client.Entities.Context statusContext = new fr.gouv.etalab.mastodon.client.Entities.Context();
|
2017-11-18 08:27:25 +01:00
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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
|
|
|
|
|
|
|
|
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) {
|
2017-05-20 15:15:02 +02:00
|
|
|
return getHomeTimeline(max_id, 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) {
|
2017-05-20 15:15:02 +02:00
|
|
|
return getHomeTimeline(null, since_id, 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
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
private APIResponse getHomeTimeline(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);
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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());
|
|
|
|
statuses = parseStatuses(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-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
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){
|
2017-05-05 16:36:04 +02:00
|
|
|
return getPublicTimeline(local, 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) {
|
|
|
|
return getPublicTimeline(local, null, since_id, tootPerPage);
|
2017-05-05 16:36:04 +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
|
|
|
*/
|
2017-06-03 18:18:27 +02:00
|
|
|
private APIResponse getPublicTimeline(boolean local, 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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/timelines/public"), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
statuses = parseStatuses(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-03 18:18:27 +02:00
|
|
|
apiResponse.setStatuses(statuses);
|
|
|
|
return apiResponse;
|
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")
|
2017-06-03 18:18:27 +02:00
|
|
|
public APIResponse getPublicTimelineTag(String tag, boolean local, String max_id){
|
2017-05-05 16:36:04 +02:00
|
|
|
return getPublicTimelineTag(tag, local, max_id, null, tootPerPage);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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")
|
2017-06-03 18:18:27 +02:00
|
|
|
private APIResponse getPublicTimelineTag(String tag, boolean local, 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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 08:27:25 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl(String.format("/timelines/tag/%s",tag.trim())), 60, params, prefKeyOauthTokenT);
|
|
|
|
apiResponse.setSince_id(httpsConnection.getSince_id());
|
|
|
|
apiResponse.setMax_id(httpsConnection.getMax_id());
|
|
|
|
statuses = parseStatuses(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-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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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));
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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());
|
|
|
|
statuses = parseStatuses(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-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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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;
|
|
|
|
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;
|
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;
|
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());
|
|
|
|
}
|
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());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(statusAction != StatusAction.UNSTATUS ) {
|
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 09:55:09 +01:00
|
|
|
httpsConnection.post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT);
|
|
|
|
actionCode = httpsConnection.getActionCode();
|
|
|
|
} 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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 09:55:09 +01:00
|
|
|
httpsConnection.delete(getAbsoluteUrl(action), 60, null, prefKeyOauthTokenT);
|
|
|
|
actionCode = httpsConnection.getActionCode();
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
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("status", URLEncoder.encode(status.getContent(), "UTF-8"));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
params.put("status", status.getContent());
|
|
|
|
}
|
2017-07-22 10:00:15 +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-07-22 10:00:15 +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-07-22 10:00:15 +02:00
|
|
|
params.put("visibility", status.getVisibility());
|
|
|
|
statuses = new ArrayList<>();
|
2017-11-18 09:55:09 +01:00
|
|
|
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 09:55:09 +01:00
|
|
|
String response = httpsConnection.post(getAbsoluteUrl("/statuses"), 60, params, prefKeyOauthTokenT);
|
|
|
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
new HttpsConnection(context).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
|
|
|
*/
|
2017-11-01 10:09:04 +01:00
|
|
|
public APIResponse getNotificationsSince(String since_id, boolean display){
|
|
|
|
return getNotifications(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")
|
2017-11-01 10:09:04 +01:00
|
|
|
public APIResponse getNotificationsSince(String since_id, int notificationPerPage, boolean display){
|
|
|
|
return getNotifications(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
|
|
|
*/
|
2017-11-01 10:09:04 +01:00
|
|
|
public APIResponse getNotifications(String max_id, boolean display){
|
|
|
|
return getNotifications(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
|
|
|
*/
|
2017-11-01 10:09:04 +01:00
|
|
|
private APIResponse getNotifications(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
|
|
|
|
|
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2017-11-01 10:09:04 +01:00
|
|
|
boolean notif_follow, notif_add, notif_mention, notif_share;
|
2017-11-05 18:12:58 +01:00
|
|
|
if( display) {
|
2017-11-01 10:09:04 +01:00
|
|
|
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);
|
2017-11-06 07:20:52 +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);
|
2017-11-01 10:09:04 +01:00
|
|
|
}
|
2017-11-19 08:46:53 +01:00
|
|
|
StringBuilder parameters = new StringBuilder();
|
|
|
|
|
2017-11-06 07:20:52 +01:00
|
|
|
if( !notif_follow )
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters.append("exclude_types[]=").append("follow").append("&");
|
2017-11-06 07:20:52 +01:00
|
|
|
if( !notif_add )
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters.append("exclude_types[]=").append("favourite").append("&");
|
2017-11-06 07:20:52 +01:00
|
|
|
if( !notif_share )
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters.append("exclude_types[]=").append("reblog").append("&");
|
2017-11-06 07:20:52 +01:00
|
|
|
if( !notif_mention )
|
2017-11-19 08:46:53 +01:00
|
|
|
parameters.append("exclude_types[]=").append("mention").append("&");
|
|
|
|
if( parameters.length() > 0) {
|
|
|
|
parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16));
|
|
|
|
params.put("exclude_types[]", parameters.toString());
|
|
|
|
}
|
|
|
|
|
2017-09-30 08:36:07 +02:00
|
|
|
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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
|
|
|
|
|
|
|
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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
|
|
|
|
* @return List<Account>
|
|
|
|
*/
|
|
|
|
public Results search(String query) {
|
|
|
|
|
2017-11-18 09:55:09 +01:00
|
|
|
HashMap<String, String> params = new HashMap<>();
|
|
|
|
params.put("q", query);
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
2017-11-18 09:55:09 +01:00
|
|
|
String response = httpsConnection.get(getAbsoluteUrl("/search"), 60, params, prefKeyOauthTokenT);
|
|
|
|
results = parseResultsResponse(new JSONObject(response));
|
|
|
|
} 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
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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
|
|
|
}
|
|
|
|
apiResponse.setEmojis(emojis);
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2017-12-13 15:55:50 +01:00
|
|
|
|
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(){
|
|
|
|
|
|
|
|
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).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){
|
|
|
|
|
|
|
|
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
fr.gouv.etalab.mastodon.client.Entities.List list;
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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());
|
|
|
|
statuses = parseStatuses(new JSONArray(response));
|
|
|
|
} 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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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){
|
|
|
|
|
|
|
|
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
fr.gouv.etalab.mastodon.client.Entities.List list;
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
new HttpsConnection(context).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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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);
|
|
|
|
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
fr.gouv.etalab.mastodon.client.Entities.List list;
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).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
|
|
|
/**
|
|
|
|
* Get card
|
|
|
|
* @param statusId String, the id of the status
|
|
|
|
* @return Card, the card (null if none)
|
|
|
|
*/
|
|
|
|
public Card getCard(String statusId){
|
|
|
|
|
|
|
|
Card card = null;
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).get(getAbsoluteUrl(String.format("/statuses/%s/card", statusId)), 60, null, prefKeyOauthTokenT);
|
2017-12-20 13:57:28 +01:00
|
|
|
card = parseCardResponse(new JSONObject(response));
|
|
|
|
}catch (Exception ignored) {ignored.printStackTrace();}
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
fr.gouv.etalab.mastodon.client.Entities.List list;
|
|
|
|
try {
|
2018-01-19 18:59:30 +01:00
|
|
|
String response = new HttpsConnection(context).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 {
|
2018-01-19 18:59:30 +01:00
|
|
|
HttpsConnection httpsConnection = new HttpsConnection(context);
|
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;
|
|
|
|
}
|
|
|
|
|
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")));
|
|
|
|
results.setStatuses(parseStatuses(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
|
|
|
|
*/
|
|
|
|
private Card parseCardResponse(JSONObject resobj){
|
|
|
|
|
|
|
|
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());
|
2017-12-26 09:09:51 +01:00
|
|
|
card.setHtml(resobj.get("html").toString());
|
2017-12-20 13:57:28 +01:00
|
|
|
card.setType(resobj.get("type").toString());
|
|
|
|
} 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 {
|
|
|
|
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()));
|
|
|
|
|
|
|
|
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()));
|
|
|
|
instanceSocial.setHttps_rank(resobj.get("https_rank").toString());
|
|
|
|
instanceSocial.setHttps_score(Integer.parseInt(resobj.get("https_score").toString()));
|
|
|
|
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()));
|
|
|
|
|
2017-12-02 14:54:25 +01:00
|
|
|
} catch (JSONException ignored) {}
|
2017-11-24 18:49:02 +01:00
|
|
|
return instanceSocial;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
list_tmp.add(jsonArray.getString(i));
|
|
|
|
} catch (JSONException ignored) {}
|
|
|
|
}
|
|
|
|
return list_tmp;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
/**
|
|
|
|
* Parse json response for several status
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<Status>
|
|
|
|
*/
|
|
|
|
private List<Status> parseStatuses(JSONArray jsonArray){
|
|
|
|
|
|
|
|
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++;
|
|
|
|
statuses.add(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return statuses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
status.setVisibility(resobj.get("visibility").toString());
|
2017-07-03 11:30:36 +02:00
|
|
|
status.setLanguage(resobj.get("language").toString());
|
2017-08-04 16:13:28 +02:00
|
|
|
status.setUrl(resobj.get("url").toString());
|
2017-08-19 08:28:04 +02:00
|
|
|
status.setReplies(null);
|
2017-05-05 16:36:04 +02:00
|
|
|
//TODO: replace by the value
|
|
|
|
status.setApplication(new Application());
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2017-10-20 19:58:46 +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-10-20 06:57:53 +02:00
|
|
|
|
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()));
|
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){}
|
|
|
|
} catch (JSONException ignored) {}
|
2017-05-05 16:36:04 +02:00
|
|
|
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());
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-06-05 11:57:02 +02:00
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
2017-05-05 16:36:04 +02:00
|
|
|
|
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);
|
|
|
|
emojis.add(emojis1);
|
|
|
|
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());
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse Lists
|
|
|
|
* @param jsonArray JSONArray
|
|
|
|
* @return List<List> of lists
|
|
|
|
*/
|
|
|
|
private List<fr.gouv.etalab.mastodon.client.Entities.List> parseLists(JSONArray jsonArray){
|
|
|
|
List<fr.gouv.etalab.mastodon.client.Entities.List> lists = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
int i = 0;
|
|
|
|
while (i < jsonArray.length() ) {
|
|
|
|
JSONObject resobj = jsonArray.getJSONObject(i);
|
|
|
|
fr.gouv.etalab.mastodon.client.Entities.List list = parseList(resobj);
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
private static fr.gouv.etalab.mastodon.client.Entities.List parseList(JSONObject resobj){
|
|
|
|
fr.gouv.etalab.mastodon.client.Entities.List list = new fr.gouv.etalab.mastodon.client.Entities.List();
|
|
|
|
try {
|
|
|
|
list.setId(resobj.get("id").toString());
|
|
|
|
list.setTitle(resobj.get("title").toString());
|
|
|
|
}catch (Exception ignored){}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
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());
|
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());
|
2018-05-20 10:34:32 +02:00
|
|
|
try {
|
|
|
|
JSONArray fields = resobj.getJSONArray("fields");
|
|
|
|
HashMap<String, String> fieldsMap = new HashMap<>();
|
|
|
|
if( fields != null){
|
|
|
|
for(int j = 0 ; j < fields.length() ; j++){
|
|
|
|
fieldsMap.put(fields.getJSONObject(j).getString("name"),fields.getJSONObject(j).getString("value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
account.setFields(fieldsMap);
|
|
|
|
}catch (Exception ignored){}
|
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<>());
|
|
|
|
}
|
2017-12-02 14:54:25 +01:00
|
|
|
} catch (JSONException ignored) {}
|
2017-05-05 16:36:04 +02:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2017-08-28 13:53:07 +02:00
|
|
|
Account account = parseAccountResponse(context, resobj);
|
2017-05-05 16:36:04 +02:00
|
|
|
accounts.add(account);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
|
* @return fr.gouv.etalab.mastodon.client.Entities.Context
|
|
|
|
*/
|
|
|
|
private fr.gouv.etalab.mastodon.client.Entities.Context parseContext(JSONObject jsonObject){
|
|
|
|
|
|
|
|
fr.gouv.etalab.mastodon.client.Entities.Context context = new fr.gouv.etalab.mastodon.client.Entities.Context();
|
|
|
|
try {
|
|
|
|
context.setAncestors(parseStatuses(jsonObject.getJSONArray("ancestors")));
|
|
|
|
context.setDescendants(parseStatuses(jsonObject.getJSONArray("descendants")));
|
|
|
|
} catch (JSONException e) {
|
2018-01-02 11:42:11 +01:00
|
|
|
setDefaultError(e);
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse json response an unique attachment
|
|
|
|
* @param resobj JSONObject
|
|
|
|
* @return Relationship
|
|
|
|
*/
|
2017-11-18 09:55:09 +01:00
|
|
|
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()));
|
2017-12-02 14:54:25 +01:00
|
|
|
} catch (JSONException ignored) {}
|
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) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
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) {
|
2018-01-24 15:56:33 +01:00
|
|
|
return Helper.instanceWithProtocol(this.instance) + "/api/v1" + action;
|
2017-05-05 16:36:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|