aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities/Edited.java
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2019-09-13 21:38:18 +0200
committerChristian Schneppe <christian@pix-art.de>2019-09-13 21:38:18 +0200
commiteb34cdc3e350a50ce98959bbf05f83813a6c2e12 (patch)
treee6257be6472dfb8bf6a91ac3c6ab52dfd3614bf8 /src/main/java/de/pixart/messenger/entities/Edited.java
parent1b45c333933f9e847439adfa6220ee4cb88ea7b9 (diff)
keep track of previously edited ids
Diffstat (limited to 'src/main/java/de/pixart/messenger/entities/Edited.java')
-rw-r--r--src/main/java/de/pixart/messenger/entities/Edited.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/Edited.java b/src/main/java/de/pixart/messenger/entities/Edited.java
new file mode 100644
index 000000000..a992e6c97
--- /dev/null
+++ b/src/main/java/de/pixart/messenger/entities/Edited.java
@@ -0,0 +1,80 @@
+package de.pixart.messenger.entities;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Edited {
+
+ private final String editedId;
+ private final String serverMsgId;
+
+ public Edited(String editedId, String serverMsgId) {
+ this.editedId = editedId;
+ this.serverMsgId = serverMsgId;
+ }
+
+ public static String toJson(List<Edited> edits) throws JSONException {
+ JSONArray jsonArray = new JSONArray();
+ for (Edited edited : edits) {
+ jsonArray.put(edited.toJson());
+ }
+ return jsonArray.toString();
+ }
+
+ public static boolean wasPreviouslyEditedRemoteMsgId(List<Edited> editeds, String remoteMsgId) {
+ for (Edited edited : editeds) {
+ if (edited.editedId != null && edited.editedId.equals(remoteMsgId)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static boolean wasPreviouslyEditedServerMsgId(List<Edited> editeds, String serverMsgId) {
+ for (Edited edited : editeds) {
+ if (edited.serverMsgId != null && edited.serverMsgId.equals(serverMsgId)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static Edited fromJson(JSONObject jsonObject) throws JSONException {
+ String edited = jsonObject.getString("edited_id");
+ String serverMsgId = jsonObject.getString("server_msg_id");
+ return new Edited(edited, serverMsgId);
+ }
+
+ public static List<Edited> fromJson(String input) {
+ ArrayList<Edited> list = new ArrayList<>();
+ if (input == null) {
+ return list;
+ }
+ try {
+ JSONArray jsonArray = new JSONArray(input);
+ for (int i = 0; i < jsonArray.length(); ++i) {
+ list.add(fromJson(jsonArray.getJSONObject(i)));
+ }
+
+ } catch (JSONException e) {
+ list = new ArrayList<>();
+ list.add(new Edited(input, null));
+ }
+ return list;
+ }
+
+ public JSONObject toJson() throws JSONException {
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("edited_id", editedId);
+ jsonObject.put("server_msg_id", serverMsgId);
+ return jsonObject;
+ }
+
+ public String getEditedId() {
+ return editedId;
+ }
+} \ No newline at end of file