From 8aab488a94bdf5015f2de1a9c58b8fc66ef90098 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sun, 8 Mar 2020 19:01:27 +0100 Subject: [PATCH] comment #411 - Prepare endpoints + entities --- .../java/app/fedilab/android/client/API.java | 177 +++++++++++++++++- .../fedilab/android/client/APIResponse.java | 10 + .../android/client/Entities/Announcement.java | 130 +++++++++++++ .../android/client/Entities/Reaction.java | 44 +++++ 4 files changed, 356 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/app/fedilab/android/client/Entities/Announcement.java create mode 100644 app/src/main/java/app/fedilab/android/client/Entities/Reaction.java diff --git a/app/src/main/java/app/fedilab/android/client/API.java b/app/src/main/java/app/fedilab/android/client/API.java index 4af1e9b8c..5726bc2da 100644 --- a/app/src/main/java/app/fedilab/android/client/API.java +++ b/app/src/main/java/app/fedilab/android/client/API.java @@ -62,6 +62,7 @@ import app.fedilab.android.client.Entities.Account; import app.fedilab.android.client.Entities.AccountAdmin; import app.fedilab.android.client.Entities.AccountCreation; import app.fedilab.android.client.Entities.AdminAction; +import app.fedilab.android.client.Entities.Announcement; import app.fedilab.android.client.Entities.Application; import app.fedilab.android.client.Entities.Attachment; import app.fedilab.android.client.Entities.Card; @@ -81,6 +82,7 @@ import app.fedilab.android.client.Entities.Notification; import app.fedilab.android.client.Entities.Peertube; import app.fedilab.android.client.Entities.Poll; import app.fedilab.android.client.Entities.PollOptions; +import app.fedilab.android.client.Entities.Reaction; import app.fedilab.android.client.Entities.Relationship; import app.fedilab.android.client.Entities.Report; import app.fedilab.android.client.Entities.Results; @@ -520,6 +522,132 @@ public class API { return statuses; } + + /** + * Parse json response for several reactions + * + * @param jsonArray JSONArray + * @return List + */ + private static List parseReaction(JSONArray jsonArray) { + List reactions = new ArrayList<>(); + try { + int i = 0; + while (i < jsonArray.length()) { + JSONObject resobj = jsonArray.getJSONObject(i); + Reaction reaction = parseReaction(resobj); + i++; + reactions.add(reaction); + } + } catch (JSONException e) { + e.printStackTrace(); + } + return reactions; + } + + /** + * Parse a reaction + * @param resobj JSONObject + * @return Reaction + */ + private static Reaction parseReaction(JSONObject resobj) { + Reaction reaction = new Reaction(); + try { + reaction.setName(resobj.getString("name")); + reaction.setCount(resobj.getInt("count")); + reaction.setMe(resobj.getBoolean("me")); + } catch (JSONException e) { + e.printStackTrace(); + } + return reaction; + } + + /** + * Parse json response for several announcements + * + * @param jsonArray JSONArray + * @return List + */ + private static List parseAnnouncement(Context context, JSONArray jsonArray) { + + List announcements = new ArrayList<>(); + try { + int i = 0; + while (i < jsonArray.length()) { + + JSONObject resobj = jsonArray.getJSONObject(i); + Announcement announcement = parseAnnouncement(context, resobj); + i++; + announcements.add(announcement); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + return announcements; + } + /** + * Parse an annoucement + * @param context Context + * @param resobj JSONObject + * @return Announcement + */ + private static Announcement parseAnnouncement(Context context, JSONObject resobj) { + Announcement announcement = new Announcement(); + try { + announcement.setId(resobj.getString("id")); + announcement.setContent(resobj.getString("content")); + announcement.setPublishedAt(Helper.mstStringToDate(context, resobj.getString("published_at"))); + announcement.setUpdatedAt(Helper.mstStringToDate(context, resobj.getString("updated_at"))); + announcement.setStartAt(Helper.mstStringToDate(context, resobj.getString("starts_at"))); + announcement.setEndAt(Helper.mstStringToDate(context, resobj.getString("ends_at"))); + announcement.setRead(resobj.getBoolean("read")); + + announcement.setReactions(parseReaction(resobj.getJSONArray("reactions"))); + + List mentions = new ArrayList<>(); + JSONArray arrayMention = resobj.getJSONArray("mentions"); + 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); + } + announcement.setMentions(mentions); + + List tags = new ArrayList<>(); + JSONArray arrayTag = resobj.getJSONArray("tags"); + 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); + } + announcement.setTags(tags); + + List emojiList = new ArrayList<>(); + try { + JSONArray emojisTag = resobj.getJSONArray("emojis"); + for (int j = 0; j < emojisTag.length(); j++) { + JSONObject emojisObj = emojisTag.getJSONObject(j); + Emojis emojis = parseEmojis(emojisObj); + emojiList.add(emojis); + } + announcement.setEmojis(emojiList); + } catch (Exception e) { + announcement.setEmojis(new ArrayList<>()); + } + + } catch (JSONException | ParseException e) { + e.printStackTrace(); + } + return announcement; + } + /** * Parse a poll * @param context Context @@ -3889,6 +4017,27 @@ public class API { return apiResponse; } + /** + * Retrieves announcements for the authenticated account *synchronously* + * @return APIResponse + */ + public APIResponse getAnnouncements() { + + List announcements = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); + String response = httpsConnection.get(getAbsoluteUrl("/announcements"), 10, null, null); + announcements = parseAnnouncement(context, new JSONArray(response)); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { + e.printStackTrace(); + } + apiResponse.setAnnouncements(announcements); + return apiResponse; + } + + /** * Retrieves bookmarked status for the authenticated account *synchronously* * @@ -3990,7 +4139,7 @@ public class API { * @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) { + private int postAction(StatusAction statusAction, String targetedId, Status status, String comment) { String action; HashMap params = null; @@ -4078,6 +4227,10 @@ public class API { case UNSTATUS: action = String.format("/statuses/%s", targetedId); break; + case REMOVE_REACTION: + case ADD_REACTION: + action = String.format("announcements/%s/reactions/%s", targetedId, comment); + break; case AUTHORIZE: action = String.format("/follow_requests/%s/authorize", targetedId); break; @@ -4128,7 +4281,7 @@ public class API { default: return -1; } - if (statusAction != StatusAction.UNSTATUS) { + if (statusAction != StatusAction.UNSTATUS && statusAction != StatusAction.ADD_REACTION && statusAction != StatusAction.REMOVE_REACTION) { try { HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); String resp = httpsConnection.post(getAbsoluteUrl(action), 10, params, prefKeyOauthTokenT); @@ -4157,13 +4310,25 @@ public class API { } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { e.printStackTrace(); } + } else if(statusAction == StatusAction.ADD_REACTION){ + try { + HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); + httpsConnection.put(getAbsoluteUrl(action), 10, null, prefKeyOauthTokenT); + actionCode = httpsConnection.getActionCode(); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { + e.printStackTrace(); + } } else { try { HttpsConnection httpsConnection = new HttpsConnection(context, this.instance); httpsConnection.delete(getAbsoluteUrl(action), 10, null, prefKeyOauthTokenT); actionCode = httpsConnection.getActionCode(); - SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - new TimelineCacheDAO(context, db).remove(targetedId); + if( statusAction != StatusAction.REMOVE_REACTION) { + SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + new TimelineCacheDAO(context, db).remove(targetedId); + } } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); } catch (NoSuchAlgorithmException | IOException | KeyManagementException e) { @@ -6000,7 +6165,9 @@ public class API { PEERTUBEDELETEVIDEO, UPDATESERVERSCHEDULE, DELETESCHEDULED, - REFRESHPOLL + REFRESHPOLL, + ADD_REACTION, + REMOVE_REACTION } public enum accountPrivacy { diff --git a/app/src/main/java/app/fedilab/android/client/APIResponse.java b/app/src/main/java/app/fedilab/android/client/APIResponse.java index 4acd133da..9ece26d7c 100644 --- a/app/src/main/java/app/fedilab/android/client/APIResponse.java +++ b/app/src/main/java/app/fedilab/android/client/APIResponse.java @@ -19,6 +19,7 @@ import java.util.List; import app.fedilab.android.client.Entities.Account; import app.fedilab.android.client.Entities.AccountAdmin; +import app.fedilab.android.client.Entities.Announcement; import app.fedilab.android.client.Entities.Context; import app.fedilab.android.client.Entities.Conversation; import app.fedilab.android.client.Entities.Emojis; @@ -54,6 +55,7 @@ public class APIResponse { private List conversations = null; private List notifications = null; private List relationships = null; + private List announcements = null; private String targetedId = null; private Results results = null; private List howToVideos = null; @@ -320,4 +322,12 @@ public class APIResponse { public void setIdentityProofs(List identityProofs) { this.identityProofs = identityProofs; } + + public List getAnnouncements() { + return announcements; + } + + public void setAnnouncements(List announcements) { + this.announcements = announcements; + } } diff --git a/app/src/main/java/app/fedilab/android/client/Entities/Announcement.java b/app/src/main/java/app/fedilab/android/client/Entities/Announcement.java new file mode 100644 index 000000000..721195cd8 --- /dev/null +++ b/app/src/main/java/app/fedilab/android/client/Entities/Announcement.java @@ -0,0 +1,130 @@ +package app.fedilab.android.client.Entities; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of Fedilab + * + * 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. + * + * Fedilab 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 Fedilab; if not, + * see . */ + +import java.util.Date; +import java.util.List; + +public class Announcement { + private String id; + private String content; + private Date startAt; + private Date endAt; + private boolean all_day; + private Date publishedAt; + private Date updatedAt; + private boolean read; + private java.util.List mentions; + private java.util.List tags; + private java.util.List emojis; + private java.util.List reactions; + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public Date getStartAt() { + return startAt; + } + + public void setStartAt(Date startAt) { + this.startAt = startAt; + } + + public Date getEndAt() { + return endAt; + } + + public void setEndAt(Date endAt) { + this.endAt = endAt; + } + + public boolean isAll_day() { + return all_day; + } + + public void setAll_day(boolean all_day) { + this.all_day = all_day; + } + + public Date getPublishedAt() { + return publishedAt; + } + + public void setPublishedAt(Date publishedAt) { + this.publishedAt = publishedAt; + } + + public Date getUpdatedAt() { + return updatedAt; + } + + public void setUpdatedAt(Date updatedAt) { + this.updatedAt = updatedAt; + } + + public boolean isRead() { + return read; + } + + public void setRead(boolean read) { + this.read = read; + } + + public List getMentions() { + return mentions; + } + + public void setMentions(List mentions) { + this.mentions = mentions; + } + + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags = tags; + } + + public List getEmojis() { + return emojis; + } + + public void setEmojis(List emojis) { + this.emojis = emojis; + } + + public List getReactions() { + return reactions; + } + + public void setReactions(List reactions) { + this.reactions = reactions; + } +} diff --git a/app/src/main/java/app/fedilab/android/client/Entities/Reaction.java b/app/src/main/java/app/fedilab/android/client/Entities/Reaction.java new file mode 100644 index 000000000..32632ab45 --- /dev/null +++ b/app/src/main/java/app/fedilab/android/client/Entities/Reaction.java @@ -0,0 +1,44 @@ +package app.fedilab.android.client.Entities; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of Fedilab + * + * 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. + * + * Fedilab 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 Fedilab; if not, + * see . */ +public class Reaction { + private String name; + private int count; + private boolean me; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public boolean isMe() { + return me; + } + + public void setMe(boolean me) { + this.me = me; + } +}