1package eu.siacs.conversations.entities;
2
3import org.json.JSONArray;
4import org.json.JSONException;
5import org.json.JSONObject;
6
7import java.util.Collection;
8import java.util.HashSet;
9import java.util.List;
10import java.util.Set;
11
12import rocks.xmpp.addr.Jid;
13
14public class ReadByMarker {
15
16 private ReadByMarker() {
17
18 }
19
20 @Override
21 public boolean equals(Object o) {
22 if (this == o) return true;
23 if (o == null || getClass() != o.getClass()) return false;
24
25 ReadByMarker marker = (ReadByMarker) o;
26
27 if (fullJid != null ? !fullJid.equals(marker.fullJid) : marker.fullJid != null)
28 return false;
29 return realJid != null ? realJid.equals(marker.realJid) : marker.realJid == null;
30
31 }
32
33 @Override
34 public int hashCode() {
35 int result = fullJid != null ? fullJid.hashCode() : 0;
36 result = 31 * result + (realJid != null ? realJid.hashCode() : 0);
37 return result;
38 }
39
40 private Jid fullJid;
41 private Jid realJid;
42
43 public Jid getFullJid() {
44 return fullJid;
45 }
46
47 public Jid getRealJid() {
48 return realJid;
49 }
50
51 public JSONObject toJson() {
52 JSONObject jsonObject = new JSONObject();
53 if (fullJid != null) {
54 try {
55 jsonObject.put("fullJid", fullJid.toString());
56 } catch (JSONException e) {
57 //ignore
58 }
59 }
60 if (realJid != null) {
61 try {
62 jsonObject.put("realJid", realJid.toString());
63 } catch (JSONException e) {
64 //ignore
65 }
66 }
67 return jsonObject;
68 }
69
70 public static Set<ReadByMarker> fromJson(JSONArray jsonArray) {
71 HashSet<ReadByMarker> readByMarkers = new HashSet<>();
72 for(int i = 0; i < jsonArray.length(); ++i) {
73 try {
74 readByMarkers.add(fromJson(jsonArray.getJSONObject(i)));
75 } catch (JSONException e) {
76 //ignored
77 }
78 }
79 return readByMarkers;
80 }
81
82 public static ReadByMarker from(Jid fullJid, Jid realJid) {
83 final ReadByMarker marker = new ReadByMarker();
84 marker.fullJid = fullJid;
85 marker.realJid = realJid == null ? null : realJid.asBareJid();
86 return marker;
87 }
88
89 public static ReadByMarker from(Message message) {
90 final ReadByMarker marker = new ReadByMarker();
91 marker.fullJid = message.getCounterpart();
92 marker.realJid = message.getTrueCounterpart();
93 return marker;
94 }
95
96 public static ReadByMarker from(MucOptions.User user) {
97 final ReadByMarker marker = new ReadByMarker();
98 marker.fullJid = user.getFullJid();
99 marker.realJid = user.getRealJid();
100 return marker;
101 }
102
103 public static Set<ReadByMarker> from(Collection<MucOptions.User> users) {
104 final HashSet<ReadByMarker> markers = new HashSet<>();
105 for(MucOptions.User user : users) {
106 markers.add(from(user));
107 }
108 return markers;
109 }
110
111 public static ReadByMarker fromJson(JSONObject jsonObject) {
112 ReadByMarker marker = new ReadByMarker();
113 try {
114 marker.fullJid = Jid.of(jsonObject.getString("fullJid"));
115 } catch (JSONException | IllegalArgumentException e) {
116 marker.fullJid = null;
117 }
118 try {
119 marker.realJid = Jid.of(jsonObject.getString("realJid"));
120 } catch (JSONException | IllegalArgumentException e) {
121 marker.realJid = null;
122 }
123 return marker;
124 }
125
126 public static Set<ReadByMarker> fromJsonString(String json) {
127 try {
128 return fromJson(new JSONArray(json));
129 } catch (JSONException | NullPointerException e) {
130 return new HashSet<>();
131 }
132 }
133
134 public static JSONArray toJson(Set<ReadByMarker> readByMarkers) {
135 JSONArray jsonArray = new JSONArray();
136 for(ReadByMarker marker : readByMarkers) {
137 jsonArray.put(marker.toJson());
138 }
139 return jsonArray;
140 }
141
142 public static boolean contains(ReadByMarker needle, Set<ReadByMarker> readByMarkers) {
143 for(ReadByMarker marker : readByMarkers) {
144 if (marker.realJid != null && needle.realJid != null) {
145 if (marker.realJid.asBareJid().equals(needle.realJid.asBareJid())) {
146 return true;
147 }
148 } else if (marker.fullJid != null && needle.fullJid != null) {
149 if (marker.fullJid.equals(needle.fullJid)) {
150 return true;
151 }
152 }
153 }
154 return false;
155 }
156
157 public static boolean allUsersRepresented(Collection<MucOptions.User> users, Set<ReadByMarker> markers) {
158 for(MucOptions.User user : users) {
159 if (!contains(from(user),markers)) {
160 return false;
161 }
162 }
163 return true;
164 }
165
166 public static boolean allUsersRepresented(Collection<MucOptions.User> users, Set<ReadByMarker> markers, ReadByMarker marker) {
167 HashSet<ReadByMarker> markersCopy = new HashSet<>(markers);
168 markersCopy.add(marker);
169 return allUsersRepresented(users, markersCopy);
170 }
171
172}