diff options
Diffstat (limited to 'src/main/java/de/pixart/messenger/entities')
4 files changed, 242 insertions, 14 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/Conversation.java b/src/main/java/de/pixart/messenger/entities/Conversation.java index 2d4ab926d..e5a2ec171 100644 --- a/src/main/java/de/pixart/messenger/entities/Conversation.java +++ b/src/main/java/de/pixart/messenger/entities/Conversation.java @@ -303,6 +303,17 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl return null; } + public Message findMessageWithRemoteId(String id) { + synchronized (this.messages) { + for (Message message : this.messages) { + if (id.equals(message.getRemoteMsgId()) || id.equals(message.getUuid())) { + return message; + } + } + } + return null; + } + public boolean hasMessageWithCounterpart(Jid counterpart) { synchronized (this.messages) { for (Message message : this.messages) { diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index c51fa1638..2fa9d7c3a 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -9,6 +9,10 @@ import com.vdurmont.emoji.EmojiManager; import java.net.MalformedURLException; import java.net.URL; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import de.pixart.messenger.Config; import de.pixart.messenger.crypto.axolotl.FingerprintStatus; @@ -65,6 +69,7 @@ public class Message extends AbstractEntity { public static final String FINGERPRINT = "axolotl_fingerprint"; public static final String READ = "read"; public static final String ERROR_MESSAGE = "errorMsg"; + public static final String READ_BY_MARKERS = "readByMarkers"; public static final String ME_COMMAND = "/me "; @@ -91,12 +96,14 @@ public class Message extends AbstractEntity { private Message mPreviousMessage = null; private String axolotlFingerprint = null; private String errorMessage = null; + protected Set<ReadByMarker> readByMarkers = new HashSet<>(); private Boolean isGeoUri = null; private Boolean isXmppUri = null; private Boolean isEmojisOnly = null; private Boolean treatAsDownloadable = null; private FileParams fileParams = null; + private List<MucOptions.User> counterparts; private Message(Conversation conversation) { this.conversation = conversation; @@ -124,6 +131,7 @@ public class Message extends AbstractEntity { true, null, false, + null, null); } @@ -132,7 +140,7 @@ public class Message extends AbstractEntity { final int encryption, final int status, final int type, final boolean carbon, final String remoteMsgId, final String relativeFilePath, final String serverMsgId, final String fingerprint, final boolean read, - final String edited, final boolean oob, final String errorMessage) { + final String edited, final boolean oob, final String errorMessage, final Set<ReadByMarker> readByMarkers) { this.conversation = conversation; this.uuid = uuid; this.conversationUuid = conversationUUid; @@ -152,6 +160,7 @@ public class Message extends AbstractEntity { this.edited = edited; this.oob = oob; this.errorMessage = errorMessage; + this.readByMarkers = new HashSet<>(); } public static Message fromCursor(Cursor cursor, Conversation conversation) { @@ -197,7 +206,8 @@ public class Message extends AbstractEntity { cursor.getInt(cursor.getColumnIndex(READ)) > 0, cursor.getString(cursor.getColumnIndex(EDITED)), cursor.getInt(cursor.getColumnIndex(OOB)) > 0, - cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE))); + cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE)), + ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndex(READ_BY_MARKERS)))); } public static Message createStatusMessage(Conversation conversation, String body) { @@ -252,6 +262,7 @@ public class Message extends AbstractEntity { values.put(EDITED, edited); values.put(OOB, oob ? 1 : 0); values.put(ERROR_MESSAGE, errorMessage); + values.put(READ_BY_MARKERS, ReadByMarker.toJson(readByMarkers).toString()); return values; } @@ -420,6 +431,25 @@ public class Message extends AbstractEntity { this.transferable = transferable; } + public boolean addReadByMarker(ReadByMarker readByMarker) { + if (readByMarker.getRealJid() != null) { + if (readByMarker.getRealJid().toBareJid().equals(trueCounterpart)) { + Log.d(Config.LOGTAG, "trying to add read marker by " + readByMarker.getRealJid() + " to " + body); + return false; + } + } else if (readByMarker.getFullJid() != null) { + if (readByMarker.getFullJid().equals(counterpart)) { + Log.d(Config.LOGTAG, "trying to add read marker by " + readByMarker.getFullJid() + " to " + body); + return false; + } + } + return this.readByMarkers.add(readByMarker); + } + + public Set<ReadByMarker> getReadByMarkers() { + return Collections.unmodifiableSet(this.readByMarkers); + } + public boolean similar(Message message) { if (type != TYPE_PRIVATE && this.serverMsgId != null && message.getServerMsgId() != null) { return this.serverMsgId.equals(message.getServerMsgId()); @@ -522,7 +552,8 @@ public class Message extends AbstractEntity { !this.isXmppUri() && !message.isXmppUri() && ((this.axolotlFingerprint == null && message.axolotlFingerprint == null) || this.axolotlFingerprint.equals(message.getFingerprint())) && - UIHelper.sameDay(message.getTimeSent(), this.getTimeSent()) + UIHelper.sameDay(message.getTimeSent(), this.getTimeSent()) && + this.getReadByMarkers().equals(message.getReadByMarkers()) ); } @@ -536,6 +567,14 @@ public class Message extends AbstractEntity { ); } + public void setCounterparts(List<MucOptions.User> counterparts) { + this.counterparts = counterparts; + } + + public List<MucOptions.User> getCounterparts() { + return this.counterparts; + } + public static class MergeSeparator { } diff --git a/src/main/java/de/pixart/messenger/entities/MucOptions.java b/src/main/java/de/pixart/messenger/entities/MucOptions.java index f60c2643c..50ea4c2fb 100644 --- a/src/main/java/de/pixart/messenger/entities/MucOptions.java +++ b/src/main/java/de/pixart/messenger/entities/MucOptions.java @@ -11,6 +11,7 @@ import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.utils.JidHelper; import de.pixart.messenger.utils.Namespace; +import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.xmpp.chatstate.ChatState; import de.pixart.messenger.xmpp.forms.Data; import de.pixart.messenger.xmpp.forms.Field; @@ -279,6 +280,10 @@ public class MucOptions { return options.getAccount(); } + public Conversation getConversation() { + return options.getConversation(); + } + public Jid getFullJid() { return fullJid; } @@ -518,6 +523,21 @@ public class MucOptions { return null; } + public User findUser(ReadByMarker readByMarker) { + if (readByMarker.getRealJid() != null) { + User user = findUserByRealJid(readByMarker.getRealJid().toBareJid()); + if (user == null) { + user = new User(this, readByMarker.getFullJid()); + user.setRealJid(readByMarker.getRealJid()); + } + return user; + } else if (readByMarker.getFullJid() != null) { + return findUserByFullJid(readByMarker.getFullJid()); + } else { + return null; + } + } + public boolean isContactInRoom(Contact contact) { return findUserByRealJid(contact.getJid().toBareJid()) != null; } @@ -661,17 +681,9 @@ public class MucOptions { if (builder.length() != 0) { builder.append(", "); } - Contact contact = user.getContact(); - if (contact != null && !contact.getDisplayName().isEmpty()) { - builder.append(contact.getDisplayName().split("\\s+")[0]); - } else { - final String name = user.getName(); - final Jid jid = user.getRealJid(); - if (name != null) { - builder.append(name.split("\\s+")[0]); - } else if (jid != null) { - builder.append(jid.getLocalpart()); - } + String name = UIHelper.getDisplayName(user); + if (name != null) { + builder.append(name.split("\\s+")[0]); } } return builder.toString(); 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<ReadByMarker> fromJson(JSONArray jsonArray) { + HashSet<ReadByMarker> 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<ReadByMarker> from(Collection<MucOptions.User> users) { + final HashSet<ReadByMarker> 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<ReadByMarker> fromJsonString(String json) { + try { + return fromJson(new JSONArray(json)); + } catch (JSONException | NullPointerException e) { + return new HashSet<>(); + } + } + + public static JSONArray toJson(Set<ReadByMarker> readByMarkers) { + JSONArray jsonArray = new JSONArray(); + for (ReadByMarker marker : readByMarkers) { + jsonArray.put(marker.toJson()); + } + return jsonArray; + } + + public static boolean contains(ReadByMarker needle, Set<ReadByMarker> 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<MucOptions.User> users, Set<ReadByMarker> markers) { + for(MucOptions.User user : users) { + if (!contains(from(user),markers)) { + return false; + } + } + return true; + } + +} |