diff options
Diffstat (limited to 'app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java')
-rw-r--r-- | app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java | 2239 |
1 files changed, 2239 insertions, 0 deletions
diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java new file mode 100644 index 000000000..22b3efaa7 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java @@ -0,0 +1,2239 @@ +package fr.gouv.etalab.mastodon.client; +/* Copyright 2019 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see <http://www.gnu.org/licenses>. */ + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.activities.MainActivity; +import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask; +import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.Entities.Application; +import fr.gouv.etalab.mastodon.client.Entities.Attachment; +import fr.gouv.etalab.mastodon.client.Entities.Conversation; +import fr.gouv.etalab.mastodon.client.Entities.Error; +import fr.gouv.etalab.mastodon.client.Entities.Mention; +import fr.gouv.etalab.mastodon.client.Entities.Notification; +import fr.gouv.etalab.mastodon.client.Entities.Relationship; +import fr.gouv.etalab.mastodon.client.Entities.Results; +import fr.gouv.etalab.mastodon.client.Entities.Status; +import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment; +import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.sqlite.AccountDAO; +import fr.gouv.etalab.mastodon.sqlite.Sqlite; + +import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_OAUTH_TOKEN; + + +/** + * Created by Thomas on 02/02/2019. + * Manage Calls to the REST API for GNU + */ + +public class GNUAPI { + + + + private Account account; + private Context context; + private Attachment attachment; + private List<Account> accounts; + private List<Status> statuses; + private int tootPerPage, accountPerPage, notificationPerPage; + private int actionCode; + private String instance; + private String prefKeyOauthTokenT; + private APIResponse apiResponse; + private Error APIError; + + public enum accountPrivacy { + PUBLIC, + LOCKED + } + public GNUAPI(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); + notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15); + this.prefKeyOauthTokenT = sharedpreferences.getString(PREF_KEY_OAUTH_TOKEN, null); + if( Helper.getLiveInstance(context) != null) + this.instance = Helper.getLiveInstance(context); + else { + SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(context)); + Account account = new AccountDAO(context, db).getUniqAccount(userId, instance); + if( account == null) { + APIError = new Error(); + APIError.setError(context.getString(R.string.toast_error)); + return; + } + this.instance = account.getInstance().trim(); + } + apiResponse = new APIResponse(); + APIError = null; + } + + public GNUAPI(Context context, String instance, String token) { + this.context = context; + if( context == null) { + apiResponse = new APIResponse(); + APIError = new Error(); + return; + } + 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); + notificationPerPage = sharedpreferences.getInt(Helper.SET_NOTIFICATIONS_PER_PAGE, 15); + if( instance != null) + this.instance = instance; + else + this.instance = Helper.getLiveInstance(context); + + if( token != null) + this.prefKeyOauthTokenT = token; + else + this.prefKeyOauthTokenT = sharedpreferences.getString(PREF_KEY_OAUTH_TOKEN, null); + apiResponse = new APIResponse(); + APIError = null; + } + + + + + /*** + * Update credential of the authenticated user *synchronously* + * @return APIResponse + */ + public APIResponse updateCredential(String display_name, String note, ByteArrayInputStream avatar, String avatarName, ByteArrayInputStream header, String headerName, accountPrivacy privacy, HashMap<String, String> customFields) { + + HashMap<String, String> requestParams = new HashMap<>(); + if( display_name != null) + try { + requestParams.put("name",URLEncoder.encode(display_name, "UTF-8")); + } catch (UnsupportedEncodingException e) { + requestParams.put("name",display_name); + } + if( note != null) + try { + requestParams.put("description",URLEncoder.encode(note, "UTF-8")); + } catch (UnsupportedEncodingException e) { + requestParams.put("description",note); + } + if( privacy != null) + requestParams.put("locked",privacy== accountPrivacy.LOCKED?"true":"false"); + try { + if( requestParams.size() > 0) + new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_profile"), 60, requestParams, avatar, null, null, null, prefKeyOauthTokenT); + if( avatar!= null && avatarName != null) + new HttpsConnection(context).patch(getAbsoluteUrl("/accounts/update_profile_image"), 60, null, avatar, avatarName, null, null, prefKeyOauthTokenT); + + } catch (HttpsConnection.HttpsConnectionException e) { + e.printStackTrace(); + setError(e.getStatusCode(), e); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } + return apiResponse; + } + + /*** + * Verifiy credential of the authenticated user *synchronously* + * @return Account + */ + public Account verifyCredentials() { + account = new Account(); + try { + String response = new HttpsConnection(context).get(getAbsoluteUrl("/account/verify_credentials.json"), 60, null, prefKeyOauthTokenT); + account = parseAccountResponse(context, new JSONObject(response)); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + return account; + } + + /** + * Returns an account + * @param accountId String account fetched + * @return Account entity + */ + public Account getAccount(String accountId) { + + account = new Account(); + HashMap<String, String> params = new HashMap<>(); + params.put("user_id",accountId); + try { + String response = new HttpsConnection(context).get(getAbsoluteUrl("/users/show.json"), 60, params, prefKeyOauthTokenT); + account = parseAccountResponse(context, new JSONObject(response)); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + 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) { + + + Relationship relationship = null; + HashMap<String, String> params = new HashMap<>(); + + try { + String response; + if(MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.GNU) { + params.put("target_id",accountId); + response = new HttpsConnection(context).get(getAbsoluteUrl("/friendships/show.json"), 60, params, prefKeyOauthTokenT); + relationship = parseRelationshipResponse(new JSONObject(response)); + }else if(MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) { + params.put("user_id",accountId); + response = new HttpsConnection(context).get(getAbsoluteUrl("/users/show.json"), 60, params, prefKeyOauthTokenT); + JSONObject resobj = new JSONObject(response); + try { + relationship = new Relationship(); + relationship.setId(resobj.get("id").toString()); + relationship.setFollowing(Boolean.valueOf(resobj.get("following").toString())); + relationship.setFollowed_by(Boolean.valueOf(resobj.get("follow_request_sent").toString())); + relationship.setBlocking(Boolean.valueOf(resobj.get("statusnet_blocking").toString())); + try { + relationship.setMuting(Boolean.valueOf(resobj.get("muting").toString())); + }catch (Exception ignored){ + relationship.setMuting(false); + } + try { + relationship.setMuting_notifications(!Boolean.valueOf(resobj.get("notifications_enabled").toString())); + }catch (Exception ignored){ + relationship.setMuting_notifications(false); + } + relationship.setEndorsed(false); + relationship.setShowing_reblogs(true); + relationship.setRequested(false); + } catch (JSONException e) { + setDefaultError(e); + } + return relationship; + } + + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + return relationship; + } + + + + + /** + * 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) { + HashMap<String, String> params = new HashMap<>(); + if( accounts != null && accounts.size() > 0 ) { + StringBuilder parameters = new StringBuilder(); + for(Account account: accounts) + parameters.append("target_id[]=").append(account.getId()).append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(12)); + params.put("target_id[]", parameters.toString()); + List<Relationship> relationships = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl("/friendships/show.json"), 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); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setRelationships(relationships); + } + + return apiResponse; + } + + /** + * Retrieves status for the account *synchronously* + * + * @param accountId String Id of the account + * @return APIResponse + */ + public APIResponse getStatus(String accountId) { + return getStatus(accountId, false, null, null, tootPerPage); + } + + /** + * Retrieves status for the account *synchronously* + * + * @param accountId String Id of the account + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getStatus(String accountId, String max_id) { + return getStatus(accountId, false, max_id, null, tootPerPage); + } + + /** + * 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) { + return getStatus(accountId, false, max_id, null, tootPerPage); + } + + /** + * Retrieves pinned status(es) *synchronously* + * + * @param accountId String Id of the account + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getPinnedStatuses(String accountId, String max_id) { + return getStatus(accountId, false, max_id, null, tootPerPage); + } + + /** + * Retrieves replies status(es) *synchronously* + * + * @param accountId String Id of the account + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getAccountTLStatuses(String accountId, String max_id, boolean exclude_replies) { + return getStatus(accountId, exclude_replies, max_id, null, tootPerPage); + } + + /** + * Retrieves status for the account *synchronously* + * + * @param accountId String Id of the account + * @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 + * @return APIResponse + */ + @SuppressWarnings("SameParameterValue") + private APIResponse getStatus(String accountId,boolean exclude_replies, 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 > 40) + limit = 40; + params.put("user_id", accountId); + params.put("exclude_replies", Boolean.toString(exclude_replies)); + params.put("count", String.valueOf(limit)); + statuses = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl("/statuses/user_timeline.json"), 60, params, prefKeyOauthTokenT); + statuses = parseStatuses(context, new JSONArray(response)); + if( statuses.size() > 0) { + apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1)); + apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1)); + } + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setStatuses(statuses); + return apiResponse; + } + + + + /** + * Retrieves accounts that reblogged the status *synchronously* + * + * @param statusId String Id of the status + * @param max_id String id max + * @return APIResponse + */ + @SuppressWarnings("SameParameterValue") + public APIResponse getRebloggedBy(String statusId, String max_id) { + + HashMap<String, String> params = new HashMap<>(); + if (max_id != null) + params.put("max_id", max_id); + params.put("limit", "80"); + accounts = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/reblogged_by", statusId)), 60, params, prefKeyOauthTokenT); + accounts = parseAccountResponse(new JSONArray(response)); + apiResponse.setSince_id(httpsConnection.getSince_id()); + apiResponse.setMax_id(httpsConnection.getMax_id()); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setAccounts(accounts); + return apiResponse; + } + + + /** + * Retrieves accounts that favourited the status *synchronously* + * + * @param statusId String Id of the status + * @param max_id String id max + * @return APIResponse + */ + @SuppressWarnings("SameParameterValue") + public APIResponse getFavouritedBy(String statusId, String max_id) { + + HashMap<String, String> params = new HashMap<>(); + if (max_id != null) + params.put("max_id", max_id); + params.put("limit", "80"); + accounts = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/favourited_by", statusId)), 60, params, prefKeyOauthTokenT); + accounts = parseAccountResponse(new JSONArray(response)); + apiResponse.setSince_id(httpsConnection.getSince_id()); + apiResponse.setMax_id(httpsConnection.getMax_id()); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setAccounts(accounts); + return apiResponse; + } + + + /** + * Retrieves one status *synchronously* + * + * @param statusId String Id of the status + * @return APIResponse + */ + public APIResponse getStatusbyId(String statusId) { + statuses = new ArrayList<>(); + + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + 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); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setStatuses(statuses); + return apiResponse; + } + + /** + * 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) { + fr.gouv.etalab.mastodon.client.Entities.Context statusContext = new fr.gouv.etalab.mastodon.client.Entities.Context(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + 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); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + return statusContext; + } + + + /** + * Retrieves direct timeline for the account *synchronously* + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getDirectTimeline( String max_id) { + return getDirectTimeline(max_id, null, tootPerPage); + } + + /** + * Retrieves conversation timeline for the account *synchronously* + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getConversationTimeline( String max_id) { + return getConversationTimeline(max_id, null, tootPerPage); + } + + /** + * Retrieves direct timeline for the account since an Id value *synchronously* + * @return APIResponse + */ + public APIResponse getConversationTimelineSinceId(String since_id) { + return getConversationTimeline(null, since_id, tootPerPage); + } + + /** + * Retrieves conversation timeline for the account *synchronously* + * @param max_id String id max + * @param since_id String since the id + * @param limit int limit - max value 40 + * @return APIResponse + */ + private APIResponse getConversationTimeline(String max_id, String since_id, int limit) { + + HashMap<String, String> params = new HashMap<>(); + if (max_id != null) + params.put("max_id", max_id); + if (since_id != null) + params.put("since_id", since_id); + if (0 > limit || limit > 80) + limit = 80; + params.put("limit",String.valueOf(limit)); + List<Conversation> conversations = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl("/conversations"), 60, params, prefKeyOauthTokenT); + apiResponse.setSince_id(httpsConnection.getSince_id()); + apiResponse.setMax_id(httpsConnection.getMax_id()); + conversations = parseConversations(new JSONArray(response)); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setConversations(conversations); + return apiResponse; + } + + /** + * Retrieves direct timeline for the account since an Id value *synchronously* + * @return APIResponse + */ + public APIResponse getDirectTimelineSinceId(String since_id) { + return getDirectTimeline(null, since_id, tootPerPage); + } + + /** + * Retrieves direct timeline for the account *synchronously* + * @param max_id String id max + * @param since_id String since the id + * @param limit int limit - max value 40 + * @return APIResponse + */ + private APIResponse getDirectTimeline(String max_id, String since_id, int limit) { + + HashMap<String, String> params = new HashMap<>(); + if (max_id != null) + params.put("max_id", max_id); + if (since_id != null) + params.put("since_id", since_id); + if (0 > limit || limit > 80) + limit = 80; + params.put("count",String.valueOf(limit)); + statuses = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl("/direct_messages.json"), 60, params, prefKeyOauthTokenT); + statuses = parseStatuses(context, new JSONArray(response)); + if( statuses.size() > 0) { + apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1)); + apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1)); + } + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setStatuses(statuses); + return apiResponse; + } + + + /** + * Retrieves home timeline for the account *synchronously* + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getHomeTimeline( String max_id) { + return getHomeTimeline(max_id, null, null, tootPerPage); + } + + + /** + * Retrieves home timeline for the account since an Id value *synchronously* + * @return APIResponse + */ + public APIResponse getHomeTimelineSinceId(String since_id) { + return getHomeTimeline(null, since_id, null, tootPerPage); + } + + /** + * Retrieves home timeline for the account from a min Id value *synchronously* + * @return APIResponse + */ + public APIResponse getHomeTimelineMinId(String min_id) { + return getHomeTimeline(null, null, min_id, tootPerPage); + } + + + /** + * 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 + * @return APIResponse + */ + private APIResponse getHomeTimeline(String max_id, String since_id, String min_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 (min_id != null) + params.put("min_id", min_id); + if (0 > limit || limit > 80) + limit = 80; + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + //Current user + statuses = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl("/statuses/home_timeline.json"), 60, params, prefKeyOauthTokenT); + statuses = parseStatuses(context, new JSONArray(response)); + if( statuses.size() > 0) { + apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1)); + apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1)); + } + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setStatuses(statuses); + return apiResponse; + } + + + + + + /** + * Retrieves public timeline for the account *synchronously* + * @param local boolean only local timeline + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getPublicTimeline(String instanceName, boolean local, String max_id){ + return getPublicTimeline(local, instanceName, max_id, null, tootPerPage); + } + + /** + * Retrieves public timeline for the account *synchronously* + * @param local boolean only local timeline + * @param max_id String id max + * @return APIResponse + */ + public APIResponse getPublicTimeline(boolean local, String max_id){ + return getPublicTimeline(local, null, max_id, null, tootPerPage); + } + + /** + * 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, null, since_id, tootPerPage); + } + + /** + * Retrieves instance timeline since an Id value *synchronously* + * @param instanceName String instance name + * @param since_id String id since + * @return APIResponse + */ + public APIResponse getInstanceTimelineSinceId(String instanceName, String since_id) { + return getPublicTimeline(true, instanceName, null, since_id, tootPerPage); + } + + /** + * 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 + * @return APIResponse + */ + private APIResponse getPublicTimeline(boolean local, String instanceName, String max_id, String since_id, int limit){ + + HashMap<String, String> params = new HashMap<>(); + if( local) + params.put("local", Boolean.toString(true)); + if( max_id != null ) + params.put("max_id", max_id); + if( since_id != null ) + params.put("since_id", since_id); + if( 0 > limit || limit > 40) + limit = 40; + params.put("count",String.valueOf(limit)); + statuses = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String url; + if(local) + url = getAbsoluteUrl("/statuses/public_timeline.json"); + else + url = getAbsoluteUrl("/statuses/public_and_external_timeline.json"); + String response = httpsConnection.get(url, 60, params, prefKeyOauthTokenT); + statuses = parseStatuses(context, new JSONArray(response)); + if( statuses.size() > 0) { + apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1)); + apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1)); + } + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setStatuses(statuses); + return apiResponse; + } + + + + + + public APIResponse getCustomArtTimeline(boolean local, String tag, String max_id, List<String> any, List<String> all, List<String> none){ + return getArtTimeline(local, tag, max_id, null, any, all, none); + } + + public APIResponse getArtTimeline(boolean local, String max_id, List<String> any, List<String> all, List<String> none){ + return getArtTimeline(local, null, max_id, null, any, all, none); + } + + public APIResponse getCustomArtTimelineSinceId(boolean local, String tag, String since_id, List<String> any, List<String> all, List<String> none){ + return getArtTimeline(local, tag, null, since_id, any, all, none); + } + + public APIResponse getArtTimelineSinceId(boolean local, String since_id, List<String> any, List<String> all, List<String> none){ + return getArtTimeline(local, null, null, since_id, any, all, none); + } + /** + * Retrieves art timeline + * @param local boolean only local timeline + * @param max_id String id max + * @return APIResponse + */ + private APIResponse getArtTimeline(boolean local, String tag, String max_id, String since_id, List<String> any, List<String> all, List<String> none){ + if( tag == null) + tag = "mastoart"; + APIResponse apiResponse = getPublicTimelineTag(tag, local, true, max_id, since_id, tootPerPage, any, all, none); + APIResponse apiResponseReply = new APIResponse(); + if( apiResponse != null){ + apiResponseReply.setMax_id(apiResponse.getMax_id()); + apiResponseReply.setSince_id(apiResponse.getSince_id()); + apiResponseReply.setStatuses(new ArrayList<>()); + if( apiResponse.getStatuses() != null && apiResponse.getStatuses().size() > 0){ + for( Status status: apiResponse.getStatuses()){ + if( status.getMedia_attachments() != null ) { + String statusSeriali |