From 381fe82b01a38f7bbfd39c2c991af950839fe2b4 Mon Sep 17 00:00:00 2001
From: Daniel Gultsch <daniel@gultsch.de>
Date: Tue, 21 Nov 2017 15:42:46 +0100
Subject: [PATCH] save full jid when sending in muc. check chat markers against
 'sender'

---
 .../eu/siacs/conversations/entities/Conversation.java |  5 +++--
 .../conversations/generator/MessageGenerator.java     |  9 ++++++---
 .../eu/siacs/conversations/parser/MessageParser.java  |  5 +++--
 .../conversations/services/XmppConnectionService.java | 11 ++++++++---
 4 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java
index 44aea6928..ce048c6df 100644
--- a/src/main/java/eu/siacs/conversations/entities/Conversation.java
+++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java
@@ -289,10 +289,11 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
 		return null;
 	}
 
-	public Message findMessageWithRemoteId(String id) {
+	public Message findMessageWithRemoteId(String id, Jid counterpart) {
 		synchronized (this.messages) {
 			for(Message message : this.messages) {
-				if (id.equals(message.getRemoteMsgId()) || id.equals(message.getUuid())) {
+				if (counterpart.equals(message.getCounterpart())
+						&& (id.equals(message.getRemoteMsgId()) || id.equals(message.getUuid()))) {
 					return message;
 				}
 			}
diff --git a/src/main/java/eu/siacs/conversations/generator/MessageGenerator.java b/src/main/java/eu/siacs/conversations/generator/MessageGenerator.java
index 49355b1b1..d82706a40 100644
--- a/src/main/java/eu/siacs/conversations/generator/MessageGenerator.java
+++ b/src/main/java/eu/siacs/conversations/generator/MessageGenerator.java
@@ -173,13 +173,16 @@ public class MessageGenerator extends AbstractGenerator {
 		return packet;
 	}
 
-	public MessagePacket confirm(final Account account, final Jid to, final String id, final boolean groupChat) {
+	public MessagePacket confirm(final Account account, final Jid to, final String id, final Jid counterpart, final boolean groupChat) {
 		MessagePacket packet = new MessagePacket();
 		packet.setType(groupChat ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
 		packet.setTo(groupChat ? to.toBareJid() : to);
 		packet.setFrom(account.getJid());
-		Element received = packet.addChild("displayed","urn:xmpp:chat-markers:0");
-		received.setAttribute("id", id);
+		Element displayed = packet.addChild("displayed","urn:xmpp:chat-markers:0");
+		displayed.setAttribute("id", id);
+		if (groupChat && counterpart != null) {
+			displayed.setAttribute("sender",counterpart.toPreppedString());
+		}
 		packet.addChild("store", "urn:xmpp:hints");
 		return packet;
 	}
diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java
index 0f7c3ce26..606fbff05 100644
--- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java
+++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java
@@ -701,6 +701,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
 		Element displayed = packet.findChild("displayed", "urn:xmpp:chat-markers:0");
 		if (displayed != null) {
 			final String id = displayed.getAttribute("id");
+			final Jid sender = displayed.getAttributeAsJid("sender");
 			if (packet.fromAccount(account)) {
 				Conversation conversation = mXmppConnectionService.find(account, counterpart.toBareJid());
 				if (conversation != null && (query == null || query.isCatchup())) {
@@ -708,8 +709,8 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
 				}
 			} else if (isTypeGroupChat) {
 				Conversation conversation = mXmppConnectionService.find(account, counterpart.toBareJid());
-				if (conversation != null && id != null) {
-					Message message = conversation.findMessageWithRemoteId(id);
+				if (conversation != null && id != null && sender != null) {
+					Message message = conversation.findMessageWithRemoteId(id, sender);
 					if (message != null) {
 						if (conversation.getMucOptions().isSelf(counterpart)) {
 							if (!message.isRead() && (query == null || query.isCatchup())) { //checking if message is unread fixes race conditions with reflections
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
index 74472faec..6f342691c 100644
--- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
@@ -1278,10 +1278,15 @@ public class XmppConnectionService extends Service {
 			}
 		}
 
+
+		boolean mucMessage = conversation.getMode() == Conversation.MODE_MULTI && message.getType() != Message.TYPE_PRIVATE;
+		if (mucMessage) {
+			message.setCounterpart(conversation.getMucOptions().getSelf().getFullJid());
+		}
+
 		if (resend) {
 			if (packet != null && addToConversation) {
-				if (account.getXmppConnection().getFeatures().sm()
-						|| (conversation.getMode() == Conversation.MODE_MULTI && message.getCounterpart().isBareJid())) {
+				if (account.getXmppConnection().getFeatures().sm() || mucMessage) {
 					markMessage(message, Message.STATUS_UNSEND);
 				} else {
 					markMessage(message, Message.STATUS_SEND);
@@ -3400,7 +3405,7 @@ public class XmppConnectionService extends Service {
 			Account account = conversation.getAccount();
 			final Jid to = markable.getCounterpart();
 			final boolean groupChat = conversation.getMode() == Conversation.MODE_MULTI;
-			MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId(), groupChat);
+			MessagePacket packet = mMessageGenerator.confirm(account, to, markable.getRemoteMsgId(), markable.getCounterpart(), groupChat);
 			this.sendMessagePacket(conversation.getAccount(), packet);
 		}
 	}