diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-04-13 21:44:50 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-04-13 21:44:50 +0200 |
commit | bc03a692264de9e6b06539a8abf17b69cc65a2ff (patch) | |
tree | 0a44340ca40635a78c4cd3b3b3e881ecda1a9e62 /src/main/java/de/pixart | |
parent | c42d2759f89a39d6caada419653b2b05bf38ef3d (diff) |
be more careful with corner cases when mentioning multiple people
Diffstat (limited to 'src/main/java/de/pixart')
3 files changed, 53 insertions, 5 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/Conversation.java b/src/main/java/de/pixart/messenger/entities/Conversation.java index a3811a751..f654902b4 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 boolean hasMessageWithCounterpart(Jid counterpart) { + synchronized (this.messages) { + for (Message message : this.messages) { + if (counterpart.equals(message.getCounterpart())) { + return true; + } + } + } + return false; + } + public void populateWithMessages(final List<Message> messages) { synchronized (this.messages) { messages.clear(); diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index c94e6f4f0..50b93cf5e 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -44,6 +44,7 @@ import net.java.otr4j.session.SessionStatus; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; @@ -74,6 +75,7 @@ import de.pixart.messenger.ui.adapter.MessageAdapter.OnContactPictureClicked; import de.pixart.messenger.ui.adapter.MessageAdapter.OnContactPictureLongClicked; import de.pixart.messenger.ui.widget.ListSelectionManager; import de.pixart.messenger.utils.GeoHelper; +import de.pixart.messenger.utils.NickValidityChecker; import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.utils.XmppUri; import de.pixart.messenger.xmpp.XmppConnection; @@ -879,13 +881,15 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa editable.insert(pos, nick + ": "); } else { if (pos > 2 && editable.subSequence(pos - 2, pos).toString().equals(": ")) { - editable.insert(pos - 2, ", " + nick); - } else { - editable.insert(pos, (Character.isWhitespace(before) ? "" : " ") + nick + (Character.isWhitespace(after) ? "" : " ")); - if (Character.isWhitespace(after)) { - mEditMessage.setSelection(mEditMessage.getSelectionStart() + 1); + if (NickValidityChecker.check(conversation, Arrays.asList(editable.subSequence(0, pos - 2).toString().split(", ")))) { + editable.insert(pos - 2, ", " + nick); + return; } } + editable.insert(pos, (Character.isWhitespace(before) ? "" : " ") + nick + (Character.isWhitespace(after) ? "" : " ")); + if (Character.isWhitespace(after)) { + mEditMessage.setSelection(mEditMessage.getSelectionStart() + 1); + } } } } diff --git a/src/main/java/de/pixart/messenger/utils/NickValidityChecker.java b/src/main/java/de/pixart/messenger/utils/NickValidityChecker.java new file mode 100644 index 000000000..0b4df771f --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/NickValidityChecker.java @@ -0,0 +1,33 @@ +package de.pixart.messenger.utils; + +import java.util.HashSet; +import java.util.Set; +import java.util.List; + +import de.pixart.messenger.entities.Conversation; +import de.pixart.messenger.xmpp.jid.InvalidJidException; +import de.pixart.messenger.xmpp.jid.Jid; + +public class NickValidityChecker { + + private static boolean check(final Conversation conversation, final String nick) { + Jid room = conversation.getJid(); + try { + Jid full = Jid.fromParts(room.getLocalpart(), room.getDomainpart(), nick); + return conversation.hasMessageWithCounterpart(full) + || conversation.getMucOptions().findUserByFullJid(full) != null; + } catch (InvalidJidException e) { + return false; + } + } + + public static boolean check(final Conversation conversation, final List<String> nicks) { + Set<String> previousNicks = new HashSet<>(nicks); + for(String previousNick : previousNicks) { + if (!NickValidityChecker.check(conversation,previousNick)) { + return false; + } + } + return true; + } +}
\ No newline at end of file |