From 72df864d41e91d184dd20178d48e7b68df13e725 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Wed, 22 Nov 2017 21:00:33 +0100 Subject: send and show read markers in private, non-anonymous groups --- .../de/pixart/messenger/entities/ReadByMarker.java | 166 +++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 src/main/java/de/pixart/messenger/entities/ReadByMarker.java (limited to 'src/main/java/de/pixart/messenger/entities/ReadByMarker.java') diff --git a/src/main/java/de/pixart/messenger/entities/ReadByMarker.java b/src/main/java/de/pixart/messenger/entities/ReadByMarker.java new file mode 100644 index 000000000..1536a7ccd --- /dev/null +++ b/src/main/java/de/pixart/messenger/entities/ReadByMarker.java @@ -0,0 +1,166 @@ +package de.pixart.messenger.entities; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import de.pixart.messenger.xmpp.jid.InvalidJidException; +import de.pixart.messenger.xmpp.jid.Jid; + +public class ReadByMarker { + + private ReadByMarker() { + + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + ReadByMarker marker = (ReadByMarker) o; + + if (fullJid != null ? !fullJid.equals(marker.fullJid) : marker.fullJid != null) + return false; + return realJid != null ? realJid.equals(marker.realJid) : marker.realJid == null; + + } + + @Override + public int hashCode() { + int result = fullJid != null ? fullJid.hashCode() : 0; + result = 31 * result + (realJid != null ? realJid.hashCode() : 0); + return result; + } + + private Jid fullJid; + private Jid realJid; + + public Jid getFullJid() { + return fullJid; + } + + public Jid getRealJid() { + return realJid; + } + + public JSONObject toJson() { + JSONObject jsonObject = new JSONObject(); + if (fullJid != null) { + try { + jsonObject.put("fullJid", fullJid.toPreppedString()); + } catch (JSONException e) { + //ignore + } + } + if (realJid != null) { + try { + jsonObject.put("realJid", realJid.toPreppedString()); + } catch (JSONException e) { + //ignore + } + } + return jsonObject; + } + + public static Set fromJson(JSONArray jsonArray) { + HashSet readByMarkers = new HashSet<>(); + for (int i = 0; i < jsonArray.length(); ++i) { + try { + readByMarkers.add(fromJson(jsonArray.getJSONObject(i))); + } catch (JSONException e) { + //ignored + } + } + return readByMarkers; + } + + public static ReadByMarker from(Jid fullJid, Jid realJid) { + final ReadByMarker marker = new ReadByMarker(); + marker.fullJid = fullJid; + marker.realJid = realJid; + return marker; + } + + public static ReadByMarker from(Message message) { + final ReadByMarker marker = new ReadByMarker(); + marker.fullJid = message.getCounterpart(); + marker.realJid = message.getTrueCounterpart(); + return marker; + } + + public static ReadByMarker from(MucOptions.User user) { + final ReadByMarker marker = new ReadByMarker(); + marker.fullJid = user.getFullJid(); + marker.realJid = user.getRealJid(); + return marker; + } + + public static Set from(Collection users) { + final HashSet markers = new HashSet<>(); + for (MucOptions.User user : users) { + markers.add(from(user)); + } + return markers; + } + + public static ReadByMarker fromJson(JSONObject jsonObject) { + ReadByMarker marker = new ReadByMarker(); + try { + marker.fullJid = Jid.fromString(jsonObject.getString("fullJid"), true); + } catch (JSONException | InvalidJidException e) { + marker.fullJid = null; + } + try { + marker.realJid = Jid.fromString(jsonObject.getString("realJid"), true); + } catch (JSONException | InvalidJidException e) { + marker.realJid = null; + } + return marker; + } + + public static Set fromJsonString(String json) { + try { + return fromJson(new JSONArray(json)); + } catch (JSONException | NullPointerException e) { + return new HashSet<>(); + } + } + + public static JSONArray toJson(Set readByMarkers) { + JSONArray jsonArray = new JSONArray(); + for (ReadByMarker marker : readByMarkers) { + jsonArray.put(marker.toJson()); + } + return jsonArray; + } + + public static boolean contains(ReadByMarker needle, Set readByMarkers) { + for(ReadByMarker marker : readByMarkers) { + if (marker.realJid != null && needle.realJid != null) { + if (marker.realJid.toBareJid().equals(needle.realJid.toBareJid())) { + return true; + } + } else if (marker.fullJid != null && needle.fullJid != null) { + if (marker.fullJid.equals(needle.fullJid)) { + return true; + } + } + } + return false; + } + + public static boolean allUsersRepresented(Collection users, Set markers) { + for(MucOptions.User user : users) { + if (!contains(from(user),markers)) { + return false; + } + } + return true; + } + +} -- cgit v1.2.3