1package eu.siacs.conversations.entities;
2
3import org.json.JSONArray;
4import org.json.JSONException;
5import org.json.JSONObject;
6
7import java.util.ArrayList;
8import java.util.List;
9
10public class Edited {
11
12 private final String editedId;
13 private final String serverMsgId;
14
15 public Edited(String editedId, String serverMsgId) {
16 this.editedId = editedId;
17 this.serverMsgId = serverMsgId;
18 }
19
20 public static String toJson(List<Edited> edits) throws JSONException {
21 JSONArray jsonArray = new JSONArray();
22 for (Edited edited : edits) {
23 jsonArray.put(edited.toJson());
24 }
25 return jsonArray.toString();
26 }
27
28 public static boolean wasPreviouslyEditedRemoteMsgId(List<Edited> editeds, String remoteMsgId) {
29 for (Edited edited : editeds) {
30 if (edited.editedId != null && edited.editedId.equals(remoteMsgId)) {
31 return true;
32 }
33 }
34 return false;
35 }
36
37 public static boolean wasPreviouslyEditedServerMsgId(List<Edited> editeds, String serverMsgId) {
38 for (Edited edited : editeds) {
39 if (edited.serverMsgId != null && edited.serverMsgId.equals(serverMsgId)) {
40 return true;
41 }
42 }
43 return false;
44 }
45
46 public static Edited fromJson(JSONObject jsonObject) throws JSONException {
47 String edited = jsonObject.getString("edited_id");
48 String serverMsgId = jsonObject.getString("server_msg_id");
49 return new Edited(edited, serverMsgId);
50 }
51
52 public static List<Edited> fromJson(String input) {
53 ArrayList<Edited> list = new ArrayList<>();
54 if (input == null) {
55 return list;
56 }
57 try {
58 JSONArray jsonArray = new JSONArray(input);
59 for (int i = 0; i < jsonArray.length(); ++i) {
60 list.add(fromJson(jsonArray.getJSONObject(i)));
61 }
62
63 } catch (JSONException e) {
64 list = new ArrayList<>();
65 list.add(new Edited(input, null));
66 }
67 return list;
68 }
69
70 public JSONObject toJson() throws JSONException {
71 JSONObject jsonObject = new JSONObject();
72 jsonObject.put("edited_id", editedId);
73 jsonObject.put("server_msg_id", serverMsgId);
74 return jsonObject;
75 }
76
77 public String getEditedId() {
78 return editedId;
79 }
80}