use stanza-id for display markers in group chats

This commit is contained in:
Daniel Gultsch 2020-04-28 08:25:21 +02:00
parent 27bf871472
commit 8183c54ba0
4 changed files with 1056 additions and 1029 deletions

View file

@ -51,16 +51,16 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
public static final String ATTRIBUTE_ALWAYS_NOTIFY = "always_notify";
public static final String ATTRIBUTE_PUSH_NODE = "push_node";
public static final String ATTRIBUTE_LAST_CLEAR_HISTORY = "last_clear_history";
public static final String ATTRIBUTE_FORMERLY_PRIVATE_NON_ANONYMOUS = "formerly_private_non_anonymous";
static final String ATTRIBUTE_MUC_PASSWORD = "muc_password";
static final String ATTRIBUTE_MEMBERS_ONLY = "members_only";
static final String ATTRIBUTE_MODERATED = "moderated";
static final String ATTRIBUTE_NON_ANONYMOUS = "non_anonymous";
private static final String ATTRIBUTE_NEXT_MESSAGE = "next_message";
private static final String ATTRIBUTE_NEXT_MESSAGE_TIMESTAMP = "next_message_timestamp";
private static final String ATTRIBUTE_CRYPTO_TARGETS = "crypto_targets";
private static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption";
private static final String ATTRIBUTE_CORRECTING_MESSAGE = "correcting_message";
static final String ATTRIBUTE_MEMBERS_ONLY = "members_only";
static final String ATTRIBUTE_MODERATED = "moderated";
static final String ATTRIBUTE_NON_ANONYMOUS = "non_anonymous";
public static final String ATTRIBUTE_FORMERLY_PRIVATE_NON_ANONYMOUS = "formerly_private_non_anonymous";
protected final ArrayList<Message> messages = new ArrayList<>();
public AtomicBoolean messagesLoaded = new AtomicBoolean(true);
protected Account account = null;
@ -118,6 +118,33 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
cursor.getString(cursor.getColumnIndex(ATTRIBUTES)));
}
public static Message getLatestMarkableMessage(final List<Message> messages, boolean isPrivateAndNonAnonymousMuc) {
for (int i = messages.size() - 1; i >= 0; --i) {
final Message message = messages.get(i);
if (message.getStatus() <= Message.STATUS_RECEIVED
&& (message.markable || isPrivateAndNonAnonymousMuc)
&& !message.isPrivateMessage()) {
return message;
}
}
return null;
}
private static boolean suitableForOmemoByDefault(final Conversation conversation) {
if (conversation.getJid().asBareJid().equals(Config.BUG_REPORTS)) {
return false;
}
if (conversation.getContact().isOwnServer()) {
return false;
}
final String contact = conversation.getJid().getDomain();
final String account = conversation.getAccount().getServer();
if (Config.OMEMO_EXCEPTIONS.CONTACT_DOMAINS.contains(contact) || Config.OMEMO_EXCEPTIONS.ACCOUNT_DOMAINS.contains(account)) {
return false;
}
return conversation.isSingleOrPrivateAndNonAnonymous() || conversation.getBooleanAttribute(ATTRIBUTE_FORMERLY_PRIVATE_NON_ANONYMOUS, false);
}
public boolean hasMessagesLeftOnServer() {
return messagesLeftOnServer;
}
@ -161,7 +188,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
}
}
}
for(Message result : results) {
for (Message result : results) {
onMessageFound.onMessageFound(result);
}
}
@ -175,7 +202,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
}
}
}
for(Message result : results) {
for (Message result : results) {
onMessageFound.onMessageFound(result);
}
}
@ -197,7 +224,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
boolean deleted = false;
final PgpDecryptionService pgpDecryptionService = account.getPgpDecryptionService();
synchronized (this.messages) {
for(Message message : this.messages) {
for (Message message : this.messages) {
if (uuids.contains(message.getUuid())) {
message.setDeleted(true);
deleted = true;
@ -214,8 +241,8 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
boolean changed = false;
final PgpDecryptionService pgpDecryptionService = account.getPgpDecryptionService();
synchronized (this.messages) {
for(Message message : this.messages) {
for(final DatabaseBackend.FilePathInfo file : files)
for (Message message : this.messages) {
for (final DatabaseBackend.FilePathInfo file : files)
if (file.uuid.toString().equals(message.getUuid())) {
message.setDeleted(file.deleted);
changed = true;
@ -285,7 +312,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
}
}
}
for(Message result : results) {
for (Message result : results) {
onMessageFound.onMessageFound(result);
}
}
@ -351,6 +378,17 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
return null;
}
public Message findMessageWithServerMsgId(String id) {
synchronized (this.messages) {
for (Message message : this.messages) {
if (id != null && id.equals(message.getServerMsgId())) {
return message;
}
}
}
return null;
}
public boolean hasMessageWithCounterpart(Jid counterpart) {
synchronized (this.messages) {
for (Message message : this.messages) {
@ -428,7 +466,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
}
public boolean setCorrectingMessage(Message correctingMessage) {
setAttribute(ATTRIBUTE_CORRECTING_MESSAGE,correctingMessage == null ? null : correctingMessage.getUuid());
setAttribute(ATTRIBUTE_CORRECTING_MESSAGE, correctingMessage == null ? null : correctingMessage.getUuid());
return correctingMessage == null && draftMessage != null;
}
@ -484,18 +522,6 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
return unread;
}
public static Message getLatestMarkableMessage(final List<Message> messages, boolean isPrivateAndNonAnonymousMuc) {
for (int i = messages.size() - 1; i >= 0; --i) {
final Message message = messages.get(i);
if (message.getStatus() <= Message.STATUS_RECEIVED
&& (message.markable || isPrivateAndNonAnonymousMuc)
&& !message.isPrivateMessage()) {
return message;
}
}
return null;
}
public Message getLatestMessage() {
synchronized (this.messages) {
if (this.messages.size() == 0) {
@ -509,7 +535,8 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
}
}
public @NonNull CharSequence getName() {
public @NonNull
CharSequence getName() {
if (getMode() == MODE_MULTI) {
final String roomName = getMucOptions().getName();
final String subject = getMucOptions().getSubject();
@ -648,21 +675,6 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
}
}
private static boolean suitableForOmemoByDefault(final Conversation conversation) {
if (conversation.getJid().asBareJid().equals(Config.BUG_REPORTS)) {
return false;
}
if (conversation.getContact().isOwnServer()) {
return false;
}
final String contact = conversation.getJid().getDomain();
final String account = conversation.getAccount().getServer();
if (Config.OMEMO_EXCEPTIONS.CONTACT_DOMAINS.contains(contact) || Config.OMEMO_EXCEPTIONS.ACCOUNT_DOMAINS.contains(account)) {
return false;
}
return conversation.isSingleOrPrivateAndNonAnonymous() || conversation.getBooleanAttribute(ATTRIBUTE_FORMERLY_PRIVATE_NON_ANONYMOUS, false);
}
public boolean setNextEncryption(int encryption) {
return this.setAttribute(ATTRIBUTE_NEXT_ENCRYPTION, encryption);
}
@ -750,7 +762,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
return false;
}
synchronized (this.messages) {
for(Message message : this.messages) {
for (Message message : this.messages) {
if (serverMsgId.equals(message.getServerMsgId()) || remoteMsgId.equals(message.getRemoteMsgId())) {
return true;
}

View file

@ -12,6 +12,7 @@ import eu.siacs.conversations.crypto.axolotl.AxolotlService;
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlMessage;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Conversational;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.http.P1S3UrlStreamHandler;
import eu.siacs.conversations.services.XmppConnectionService;
@ -160,15 +161,23 @@ public class MessageGenerator extends AbstractGenerator {
return packet;
}
public MessagePacket confirm(final Account account, final Jid to, final String id, final Jid counterpart, final boolean groupChat) {
MessagePacket packet = new MessagePacket();
public MessagePacket confirm(final Message message) {
final boolean groupChat = message.getConversation().getMode() == Conversational.MODE_MULTI;
final Jid to = message.getCounterpart();
final MessagePacket packet = new MessagePacket();
packet.setType(groupChat ? MessagePacket.TYPE_GROUPCHAT : MessagePacket.TYPE_CHAT);
packet.setTo(groupChat ? to.asBareJid() : to);
packet.setFrom(account.getJid());
Element displayed = packet.addChild("displayed", "urn:xmpp:chat-markers:0");
displayed.setAttribute("id", id);
if (groupChat && counterpart != null) {
displayed.setAttribute("sender", counterpart.toString());
final Element displayed = packet.addChild("displayed", "urn:xmpp:chat-markers:0");
if (groupChat) {
final String stanzaId = message.getServerMsgId();
if (stanzaId != null) {
displayed.setAttribute("id", stanzaId);
} else {
displayed.setAttribute("sender", to.toString());
displayed.setAttribute("id", message.getRemoteMsgId());
}
} else {
displayed.setAttribute("id", message.getRemoteMsgId());
}
packet.addChild("store", "urn:xmpp:hints");
return packet;

View file

@ -927,9 +927,17 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
activateGracePeriod(account);
}
} else if (isTypeGroupChat) {
Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
if (conversation != null && id != null && sender != null) {
Message message = conversation.findMessageWithRemoteId(id, sender);
final Conversation conversation = mXmppConnectionService.find(account, counterpart.asBareJid());
final Message message;
if (conversation != null && id != null) {
if (sender != null) {
message = conversation.findMessageWithRemoteId(id, sender);
} else {
message = conversation.findMessageWithServerMsgId(id);
}
} else {
message = null;
}
if (message != null) {
final Jid fallback = conversation.getMucOptions().getTrueCounterpart(counterpart);
final Jid trueJid = getTrueCounterpart((query != null && query.safeToExtractTrueCounterpart()) ? mucUserElement : null, fallback);
@ -945,7 +953,6 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece
}
}
}
}
} else {
final Message displayedMessage = mXmppConnectionService.markMessage(account, from.asBareJid(), id, Message.STATUS_SEND_DISPLAYED);
Message message = displayedMessage == null ? null : displayedMessage.prev();

View file

@ -4062,6 +4062,7 @@ public class XmppConnectionService extends Service {
}
};
mDatabaseWriterExecutor.execute(runnable);
updateConversationUi();
updateUnreadCountBadge();
return readMessages;
} else {
@ -4094,11 +4095,9 @@ public class XmppConnectionService extends Service {
&& (markable.trusted() || isPrivateAndNonAnonymousMuc)
&& markable.getRemoteMsgId() != null) {
Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": sending read marker to " + markable.getCounterpart().toString());
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(), markable.getCounterpart(), groupChat);
this.sendMessagePacket(conversation.getAccount(), packet);
final Account account = conversation.getAccount();
final MessagePacket packet = mMessageGenerator.confirm(markable);
this.sendMessagePacket(account, packet);
}
}