diff --git a/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java index a2274a16e..3d1e5fb0f 100644 --- a/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java @@ -23,8 +23,6 @@ import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.text.ParseException; @@ -36,16 +34,12 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import app.fedilab.android.R; import app.fedilab.android.client.Entities.Account; import app.fedilab.android.client.Entities.AccountCreation; -import app.fedilab.android.client.Entities.Attachment; -import app.fedilab.android.client.Entities.Conversation; -import app.fedilab.android.client.Entities.Emojis; import app.fedilab.android.client.Entities.Error; -import app.fedilab.android.client.Entities.Filters; -import app.fedilab.android.client.Entities.HowToVideo; import app.fedilab.android.client.Entities.Instance; import app.fedilab.android.client.Entities.InstanceNodeInfo; import app.fedilab.android.client.Entities.InstanceReg; @@ -57,9 +51,6 @@ import app.fedilab.android.client.Entities.PeertubeInformation; import app.fedilab.android.client.Entities.PeertubeNotification; import app.fedilab.android.client.Entities.PeertubeVideoNotification; import app.fedilab.android.client.Entities.Playlist; -import app.fedilab.android.client.Entities.Relationship; -import app.fedilab.android.client.Entities.Results; -import app.fedilab.android.client.Entities.Status; import app.fedilab.android.helper.Helper; import app.fedilab.android.sqlite.AccountDAO; import app.fedilab.android.sqlite.Sqlite; @@ -75,26 +66,18 @@ public class PeertubeAPI { private Account account; private Context context; - private Results results; - private Attachment attachment; - private List accounts; - private List statuses; - private List conversations; - private int tootPerPage, accountPerPage, notificationPerPage; + private int tootPerPage; private int actionCode; private String instance; private String prefKeyOauthTokenT; private APIResponse apiResponse; private Error APIError; - private List domains; public PeertubeAPI(Context context) { this.context = context; SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); tootPerPage = Helper.TOOTS_PER_PAGE; - accountPerPage = Helper.ACCOUNTS_PER_PAGE; - notificationPerPage = Helper.NOTIFICATIONS_PER_PAGE; if (Helper.getLiveInstance(context) != null) this.instance = Helper.getLiveInstance(context); else { @@ -123,8 +106,6 @@ public class PeertubeAPI { } SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); tootPerPage = Helper.TOOTS_PER_PAGE; - accountPerPage = Helper.ACCOUNTS_PER_PAGE; - notificationPerPage = Helper.NOTIFICATIONS_PER_PAGE; if (instance != null) this.instance = instance; else @@ -236,9 +217,7 @@ public class PeertubeAPI { } - } catch (JSONException e) { - e.printStackTrace(); - } catch (ParseException e) { + } catch (JSONException | ParseException e) { e.printStackTrace(); } @@ -251,7 +230,7 @@ public class PeertubeAPI { * @param resobj JSONObject * @return Peertube */ - public static Peertube parsePeertube(Context context, JSONObject resobj) { + private static Peertube parsePeertube(Context context, JSONObject resobj) { Peertube peertube = new Peertube(); if (resobj.has("video")) { try { @@ -407,100 +386,6 @@ public class PeertubeAPI { return peertube; } - /** - * Parse json response for peertube comments - * - * @param resobj JSONObject - * @return Peertube - */ - private static List parseSinglePeertubeComments(Context context, String instance, JSONObject resobj) { - List statuses = new ArrayList<>(); - try { - JSONArray jsonArray = resobj.getJSONArray("data"); - int i = 0; - while (i < jsonArray.length()) { - Status status = new Status(); - JSONObject comment = jsonArray.getJSONObject(i); - status.setId(comment.get("id").toString()); - status.setUri(comment.get("url").toString()); - status.setUrl(comment.get("url").toString()); - status.setSensitive(false); - status.setSpoiler_text(""); - status.setContent(context, comment.get("text").toString()); - status.setIn_reply_to_id(comment.get("inReplyToCommentId").toString()); - status.setAccount(parseAccountResponsePeertube(context, comment.getJSONObject("account"))); - status.setCreated_at(Helper.mstStringToDate(context, comment.get("createdAt").toString())); - status.setMentions(new ArrayList<>()); - status.setEmojis(new ArrayList<>()); - status.setMedia_attachments(new ArrayList<>()); - status.setVisibility("public"); - i++; - statuses.add(status); - } - } catch (JSONException e) { - e.printStackTrace(); - } catch (ParseException e) { - e.printStackTrace(); - } - return statuses; - } - - /** - * Parse json response for unique how to - * - * @param resobj JSONObject - * @return HowToVideo - */ - private static HowToVideo parseHowTo(Context context, JSONObject resobj) { - HowToVideo howToVideo = new HowToVideo(); - try { - howToVideo.setId(resobj.get("id").toString()); - howToVideo.setUuid(resobj.get("uuid").toString()); - howToVideo.setName(resobj.get("name").toString()); - howToVideo.setDescription(resobj.get("description").toString()); - howToVideo.setEmbedPath(resobj.get("embedPath").toString()); - howToVideo.setPreviewPath(resobj.get("previewPath").toString()); - howToVideo.setThumbnailPath(resobj.get("thumbnailPath").toString()); - } catch (JSONException e) { - e.printStackTrace(); - } - - return howToVideo; - } - - /** - * 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()); - } catch (Exception ignored) { - } - return emojis; - } - - /** - * Parse json response for emoji - * - * @param resobj JSONObject - * @return Emojis - */ - private static Emojis parseMisskeyEmojis(JSONObject resobj) { - Emojis emojis = new Emojis(); - try { - emojis.setShortcode(resobj.get("name").toString()); - emojis.setStatic_url(resobj.get("url").toString()); - emojis.setUrl(resobj.get("url").toString()); - } catch (Exception ignored) { - } - return emojis; - } /** * Parse json response for emoji @@ -528,19 +413,11 @@ public class PeertubeAPI { type.put(resobj.getJSONObject("type").getInt("id"), resobj.getJSONObject("type").get("label").toString()); playlist.setType(type); playlist.setPrivacy(privacy); - } catch (Exception ignored) { - ignored.printStackTrace(); - } - - + } catch (Exception ignored) {} try { playlist.setUpdatedAt(Helper.stringToDate(context, resobj.getString("updatedAt"))); - } catch (Exception ignored) { - ignored.printStackTrace(); - } - } catch (Exception ignored) { - ignored.printStackTrace(); - } + } catch (Exception ignored) {} + } catch (Exception ignored) {} return playlist; } @@ -566,11 +443,11 @@ public class PeertubeAPI { else account.setCreated_at(new Date()); if (accountObject.has("followersCount")) - account.setFollowers_count(Integer.valueOf(accountObject.get("followersCount").toString())); + account.setFollowers_count(accountObject.getInt("followersCount")); else account.setFollowers_count(0); if (accountObject.has("followingCount")) - account.setFollowing_count(Integer.valueOf(accountObject.get("followingCount").toString())); + account.setFollowing_count(accountObject.getInt("followingCount")); else account.setFollowing_count(0); account.setStatuses_count(0); @@ -587,52 +464,12 @@ public class PeertubeAPI { account.setHeader("null"); account.setHeader_static("null"); account.setAvatar_static(accountObject.get("avatar").toString()); - } catch (JSONException ignored) { - ignored.printStackTrace(); - } catch (ParseException e) { + } catch (JSONException | ParseException e) { e.printStackTrace(); } return account; } - /** - * Parse json response an unique attachment - * - * @param resobj JSONObject - * @return Relationship - */ - static Attachment parseAttachmentResponse(JSONObject resobj) { - - Attachment attachment = new Attachment(); - try { - attachment.setId(resobj.get("id").toString()); - attachment.setType(resobj.get("type").toString()); - attachment.setUrl(resobj.get("url").toString()); - try { - attachment.setDescription(resobj.get("description").toString()); - } catch (JSONException ignore) { - } - try { - attachment.setRemote_url(resobj.get("remote_url").toString()); - } catch (JSONException ignore) { - } - try { - attachment.setPreview_url(resobj.get("preview_url").toString()); - } catch (JSONException ignore) { - } - try { - attachment.setMeta(resobj.get("meta").toString()); - } catch (JSONException ignore) { - } - try { - attachment.setText_url(resobj.get("text_url").toString()); - } catch (JSONException ignore) { - } - - } catch (JSONException ignored) { - } - return attachment; - } /*** * Get info on the current Instance *synchronously* @@ -645,13 +482,7 @@ public class PeertubeAPI { apiResponse.setInstance(instanceEntity); } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } return apiResponse; @@ -714,11 +545,7 @@ public class PeertubeAPI { } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { e.printStackTrace(); } apiResponse.setPeertubes(peertubes); @@ -901,7 +728,7 @@ public class PeertubeAPI { params.put("username", accountCreation.getUsername()); params.put("email", accountCreation.getEmail()); params.put("password", accountCreation.getPassword()); - String response = new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/users/register"), 30, params, null); + new HttpsConnection(context, this.instance).post(getAbsoluteUrl("/users/register"), 30, params, null); } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { e.printStackTrace(); @@ -953,13 +780,7 @@ public class PeertubeAPI { } 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(); - } catch (JSONException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } return account; @@ -982,13 +803,7 @@ public class PeertubeAPI { return new JSONObject(response).getBoolean(uri); } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } return false; @@ -1043,13 +858,7 @@ public class PeertubeAPI { } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setPeertubes(peertubes); @@ -1094,13 +903,7 @@ public class PeertubeAPI { } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setPeertubes(peertubes); @@ -1134,13 +937,7 @@ public class PeertubeAPI { } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setPeertubes(peertubes); @@ -1192,13 +989,7 @@ public class PeertubeAPI { } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setPeertubeNotifications(peertubeNotifications); @@ -1245,13 +1036,7 @@ public class PeertubeAPI { } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setPeertubes(peertubes); @@ -1275,7 +1060,6 @@ public class PeertubeAPI { SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); if (values.containsKey("access_token") && values.get("access_token") != null) { targetedAccount.setToken(values.get("access_token")); - String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); //This account is currently logged in, the token is updated SharedPreferences.Editor editor = sharedpreferences.edit(); prefKeyOauthTokenT = targetedAccount.getToken(); @@ -1357,33 +1141,7 @@ public class PeertubeAPI { } } - /** - * Retrieves home timeline for the account since an Id value *synchronously* - * - * @return APIResponse - */ - public APIResponse getSubscriptionsTLSinceId(String since_id) { - try { - return getTL("/users/me/subscriptions/videos", null, null, null, since_id, null); - } catch (HttpsConnection.HttpsConnectionException e) { - setError(e.getStatusCode(), e); - return apiResponse; - } - } - /** - * Retrieves home timeline for the account from a min Id value *synchronously* - * - * @return APIResponse - */ - public APIResponse getSubscriptionsTLMinId(String min_id) { - try { - return getTL("/users/me/subscriptions/videos", null, null, null, null, min_id); - } catch (HttpsConnection.HttpsConnectionException e) { - setError(e.getStatusCode(), e); - return apiResponse; - } - } /** * Retrieves home timeline for the account *synchronously* @@ -1427,7 +1185,7 @@ public class PeertubeAPI { for (int i = 0; i < categories.length(); i++) { JSONArray categoriesVideos = categories.getJSONObject(i).getJSONArray("videos"); List peertubeCategories = parsePeertube(categoriesVideos); - if (peertubeCategories != null && peertubeCategories.size() > 0) { + if (peertubeCategories.size() > 0) { peertubeCategories.get(0).setHeaderType("categories"); peertubeCategories.get(0).setHeaderTypeValue(categories.getJSONObject(i).getJSONObject("category").getString("label")); peertubes.addAll(peertubeCategories); @@ -1438,7 +1196,7 @@ public class PeertubeAPI { for (int i = 0; i < channels.length(); i++) { JSONArray channelsVideos = channels.getJSONObject(i).getJSONArray("videos"); List peertubeChannels = parsePeertube(channelsVideos); - if (peertubeChannels != null && peertubeChannels.size() > 0) { + if (peertubeChannels.size() > 0) { peertubeChannels.get(0).setHeaderType("channels"); peertubeChannels.get(0).setHeaderTypeValue(channels.getJSONObject(i).getJSONObject("channel").getString("displayName")); peertubes.addAll(peertubeChannels); @@ -1448,7 +1206,7 @@ public class PeertubeAPI { for (int i = 0; i < tags.length(); i++) { JSONArray tagsVideos = tags.getJSONObject(i).getJSONArray("videos"); List peertubeTags = parsePeertube(tagsVideos); - if (peertubeTags != null && peertubeTags.size() > 0) { + if (peertubeTags.size() > 0) { peertubeTags.get(0).setHeaderType("tags"); peertubeTags.get(0).setHeaderTypeValue(tags.getJSONObject(i).getString("tag")); peertubes.addAll(peertubeTags); @@ -1477,82 +1235,16 @@ public class PeertubeAPI { HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); String response = httpsConnection.get(getAbsoluteUrl(String.format("/accounts/%s/video-channels", name)), 60, null, null); JSONArray jsonArray = new JSONObject(response).getJSONArray("data"); - accounts = parseAccountResponsePeertube(context, instance, jsonArray); + accounts = parseAccountResponsePeertube(context, jsonArray); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setAccounts(accounts); return apiResponse; } - /** - * Retrieves Peertube videos from an instance *synchronously* - * - * @return APIResponse - */ - public APIResponse getPeertubeChannelVideos(String instance, String name) { - - List peertubes = new ArrayList<>(); - try { - HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); - String response = httpsConnection.get(String.format("https://" + instance + "/api/v1/video-channels/%s/videos", name), 60, null, null); - JSONArray jsonArray = new JSONObject(response).getJSONArray("data"); - peertubes = parsePeertube(jsonArray); - } catch (HttpsConnection.HttpsConnectionException e) { - setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { - e.printStackTrace(); - } - apiResponse.setPeertubes(peertubes); - return apiResponse; - } - - /** - * Retrieves Peertube videos from an instance *synchronously* - * - * @return APIResponse - */ - public APIResponse getPeertube(String instance, String max_id) { - - List peertubes = new ArrayList<>(); - HashMap params = new HashMap<>(); - if (max_id == null) - max_id = "0"; - params.put("start", String.valueOf(tootPerPage)); - try { - HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); - String response = httpsConnection.get("https://" + instance + "/api/v1/videos", 60, params, null); - JSONArray jsonArray = new JSONObject(response).getJSONArray("data"); - peertubes = parsePeertube(jsonArray); - } catch (HttpsConnection.HttpsConnectionException e) { - setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { - e.printStackTrace(); - } - apiResponse.setPeertubes(peertubes); - return apiResponse; - } - /** * Retrieves Peertube videos from an instance *synchronously* * @@ -1568,13 +1260,7 @@ public class PeertubeAPI { peertube = parseSinglePeertube(context, instance, jsonObject); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } List peertubes = new ArrayList<>(); @@ -1583,68 +1269,6 @@ public class PeertubeAPI { return apiResponse; } - /** - * Retrieves peertube search *synchronously* - * - * @param query String search - * @return APIResponse - */ - public APIResponse searchPeertube(String instance, String query) { - HashMap params = new HashMap<>(); - params.put("count", String.valueOf(tootPerPage)); - try { - params.put("search", URLEncoder.encode(query, "UTF-8")); - } catch (UnsupportedEncodingException e) { - params.put("search", query); - } - List peertubes = new ArrayList<>(); - try { - HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); - String response = httpsConnection.get("https://" + instance + "/api/v1/search/videos", 60, params, null); - JSONArray jsonArray = new JSONObject(response).getJSONArray("data"); - peertubes = parsePeertube(jsonArray); - } catch (HttpsConnection.HttpsConnectionException e) { - setError(e.getStatusCode(), e); - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { - e.printStackTrace(); - } - apiResponse.setPeertubes(peertubes); - return apiResponse; - } - - /** - * Retrieves Peertube videos from an instance *synchronously* - * - * @return APIResponse - */ - public APIResponse getSinglePeertubeComments(String instance, String videoId) { - statuses = new ArrayList<>(); - try { - HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); - String response = httpsConnection.get(String.format("https://" + instance + "/api/v1/videos/%s/comment-threads", videoId), 60, null, null); - JSONObject jsonObject = new JSONObject(response); - statuses = parseSinglePeertubeComments(context, instance, jsonObject); - } catch (HttpsConnection.HttpsConnectionException e) { - setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { - e.printStackTrace(); - } - apiResponse.setStatuses(statuses); - return apiResponse; - } /** * Retrieves rating of user on a video *synchronously* @@ -1660,13 +1284,7 @@ public class PeertubeAPI { return new JSONObject(response).get("rating").toString(); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } return null; @@ -1756,57 +1374,26 @@ public class PeertubeAPI { } try { HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); - if (actionCall.equals("POST")) - httpsConnection.post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); - else if (actionCall.equals("DELETE")) - httpsConnection.delete(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); - else if (actionCall.equals("PUT")) - httpsConnection.put(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); + switch (actionCall) { + case "POST": + httpsConnection.post(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); + break; + case "DELETE": + httpsConnection.delete(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); + break; + default: + httpsConnection.put(getAbsoluteUrl(action), 60, params, prefKeyOauthTokenT); + break; + } actionCode = httpsConnection.getActionCode(); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { e.printStackTrace(); } return actionCode; } - /** - * Changes media description - * - * @param mediaId String - * @param description String - * @return Attachment - */ - public Attachment updateDescription(String mediaId, String description) { - - HashMap params = new HashMap<>(); - try { - params.put("description", URLEncoder.encode(description, "UTF-8")); - } catch (UnsupportedEncodingException e) { - params.put("description", description); - } - try { - HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); - 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); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { - e.printStackTrace(); - } - return attachment; - } /** * Video is in play lists @@ -1836,13 +1423,7 @@ public class PeertubeAPI { } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse = new APIResponse(); @@ -1863,13 +1444,7 @@ public class PeertubeAPI { playlists = parsePlaylists(context, new JSONObject(response).getJSONArray("data")); } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setPlaylists(playlists); @@ -1889,11 +1464,7 @@ public class PeertubeAPI { actionCode = httpsConnection.getActionCode(); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { e.printStackTrace(); } return actionCode; @@ -1913,11 +1484,7 @@ public class PeertubeAPI { actionCode = httpsConnection.getActionCode(); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { e.printStackTrace(); } return actionCode; @@ -1940,11 +1507,7 @@ public class PeertubeAPI { } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { + } catch (NoSuchAlgorithmException | KeyManagementException | IOException e) { e.printStackTrace(); } return actionCode; @@ -1978,43 +1541,13 @@ public class PeertubeAPI { } 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) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } apiResponse.setPeertubes(peertubes); return apiResponse; } - /** - * Parse json response for several howto - * - * @param jsonArray JSONArray - * @return List - */ - private List parseHowTos(JSONArray jsonArray) { - - List howToVideos = new ArrayList<>(); - try { - int i = 0; - while (i < jsonArray.length()) { - - JSONObject resobj = jsonArray.getJSONObject(i); - HowToVideo howToVideo = parseHowTo(context, resobj); - i++; - howToVideos.add(howToVideo); - } - - } catch (JSONException e) { - setDefaultError(e); - } - return howToVideos; - } /** * Parse json response for peertube notifications @@ -2044,7 +1577,7 @@ public class PeertubeAPI { * @param jsonArray JSONArray * @return List */ - public List parseInstanceReg(JSONArray jsonArray) { + private List parseInstanceReg(JSONArray jsonArray) { List instanceRegs = new ArrayList<>(); try { @@ -2131,111 +1664,6 @@ public class PeertubeAPI { return instance; } - /** - * Parse emojis - * - * @param jsonArray JSONArray - * @return List of emojis - */ - private List parseEmojis(JSONArray jsonArray) { - List 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) { - setDefaultError(e); - } - return emojis; - } - - /** - * Parse emojis - * - * @param jsonArray JSONArray - * @return List of emojis - */ - private List parseMisskeyEmojis(JSONArray jsonArray) { - List emojis = new ArrayList<>(); - try { - int i = 0; - while (i < jsonArray.length()) { - JSONObject resobj = jsonArray.getJSONObject(i); - Emojis emojis1 = parseMisskeyEmojis(resobj); - emojis.add(emojis1); - i++; - } - } catch (JSONException e) { - setDefaultError(e); - } - return emojis; - } - - /** - * Parse Filters - * - * @param jsonArray JSONArray - * @return List of filters - */ - private List parseFilters(JSONArray jsonArray) { - List filters = new ArrayList<>(); - try { - int i = 0; - while (i < jsonArray.length()) { - JSONObject resobj = jsonArray.getJSONObject(i); - Filters filter = parseFilter(resobj); - if (filter != null) - filters.add(filter); - i++; - } - } catch (JSONException e) { - setDefaultError(e); - } - return filters; - } - - /** - * Parse json response for filter - * - * @param resobj JSONObject - * @return Filter - */ - private Filters parseFilter(JSONObject resobj) { - Filters filter = new Filters(); - try { - - filter.setId(resobj.get("id").toString()); - if (resobj.get("phrase").toString() == null) - return null; - filter.setPhrase(resobj.get("phrase").toString()); - if (resobj.get("expires_at") != null && !resobj.get("expires_at").toString().equals("null")) - filter.setSetExpires_at(Helper.mstStringToDate(context, resobj.get("expires_at").toString())); - filter.setWhole_word(Boolean.parseBoolean(resobj.get("whole_word").toString())); - filter.setIrreversible(Boolean.parseBoolean(resobj.get("irreversible").toString())); - String contextString = resobj.get("context").toString(); - contextString = contextString.replaceAll("\\[", ""); - contextString = contextString.replaceAll("]", ""); - contextString = contextString.replaceAll("\"", ""); - if (contextString != null) { - String[] context = contextString.split(","); - if (contextString.length() > 0) { - ArrayList finalContext = new ArrayList<>(); - for (String c : context) - finalContext.add(c.trim()); - filter.setContext(finalContext); - } - } - return filter; - } catch (Exception ignored) { - return null; - } - - } - /** * Parse Playlists * @@ -2258,7 +1686,7 @@ public class PeertubeAPI { return playlists; } - private List parseAccountResponsePeertube(Context context, String instance, JSONArray jsonArray) { + private List parseAccountResponsePeertube(Context context, JSONArray jsonArray) { List accounts = new ArrayList<>(); try { int i = 0; @@ -2274,65 +1702,6 @@ public class PeertubeAPI { return accounts; } - /** - * 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())); - try { - relationship.setMuting_notifications(Boolean.valueOf(resobj.get("muting_notifications").toString())); - } catch (Exception ignored) { - relationship.setMuting_notifications(true); - } - try { - relationship.setEndorsed(Boolean.valueOf(resobj.get("endorsed").toString())); - } catch (Exception ignored) { - relationship.setMuting_notifications(false); - } - try { - relationship.setShowing_reblogs(Boolean.valueOf(resobj.get("showing_reblogs").toString())); - } catch (Exception ignored) { - relationship.setMuting_notifications(false); - } - relationship.setRequested(Boolean.valueOf(resobj.get("requested").toString())); - } catch (JSONException e) { - setDefaultError(e); - } - return relationship; - } - - /** - * Parse json response for list of relationship - * - * @param jsonArray JSONArray - * @return List - */ - private List parseRelationshipResponse(JSONArray jsonArray) { - - List relationships = new ArrayList<>(); - try { - int i = 0; - while (i < jsonArray.length()) { - JSONObject resobj = jsonArray.getJSONObject(i); - Relationship relationship = parseRelationshipResponse(resobj); - relationships.add(relationship); - i++; - } - } catch (JSONException e) { - setDefaultError(e); - } - return relationships; - } /** * Set the error message @@ -2345,7 +1714,7 @@ public class PeertubeAPI { APIError.setStatusCode(statusCode); String message = statusCode + " - " + error.getMessage(); try { - JSONObject jsonObject = new JSONObject(error.getMessage()); + JSONObject jsonObject = new JSONObject(Objects.requireNonNull(error.getMessage())); String errorM = jsonObject.get("error").toString(); message = "Error " + statusCode + " : " + errorM; } catch (JSONException e) { @@ -2379,16 +1748,4 @@ public class PeertubeAPI { return Helper.instanceWithProtocol(this.context, this.instance) + "/api/v1" + action; } - private String getAbsoluteUrlRemote(String remote, String action) { - return "https://" + remote + "/api/v1" + action; - } - - private String getAbsoluteUrlRemoteInstance(String instanceName) { - return "https://" + instanceName + "/api/v1/timelines/public?local=true"; - } - - private String getAbsoluteUrlCommunitywiki(String action) { - return "https://communitywiki.org/trunk/api/v1" + action; - } - }