test message correction

This commit is contained in:
Daniel Gultsch 2023-02-12 11:59:23 +01:00
parent 4c09b20aa4
commit 7e2bff9d03
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
4 changed files with 87 additions and 7 deletions

View file

@ -9,8 +9,10 @@ import eu.siacs.conversations.xmpp.Jid;
import im.conversations.android.IDs; import im.conversations.android.IDs;
import im.conversations.android.database.ConversationsDatabase; import im.conversations.android.database.ConversationsDatabase;
import im.conversations.android.database.entity.AccountEntity; import im.conversations.android.database.entity.AccountEntity;
import im.conversations.android.database.model.Modification;
import im.conversations.android.transformer.Transformation; import im.conversations.android.transformer.Transformation;
import im.conversations.android.transformer.Transformer; import im.conversations.android.transformer.Transformer;
import im.conversations.android.xmpp.model.correction.Replace;
import im.conversations.android.xmpp.model.jabber.Body; import im.conversations.android.xmpp.model.jabber.Body;
import im.conversations.android.xmpp.model.reactions.Reaction; import im.conversations.android.xmpp.model.reactions.Reaction;
import im.conversations.android.xmpp.model.reactions.Reactions; import im.conversations.android.xmpp.model.reactions.Reactions;
@ -77,4 +79,73 @@ public class TransformationTest {
Assert.assertEquals("Y", onlyReaction.reaction); Assert.assertEquals("Y", onlyReaction.reaction);
Assert.assertEquals(REMOTE, onlyReaction.reactionBy); Assert.assertEquals(REMOTE, onlyReaction.reactionBy);
} }
@Test
public void correctionBeforeOriginal() {
final var messageCorrection = new Message();
messageCorrection.setId("2");
messageCorrection.setTo(ACCOUNT);
messageCorrection.setFrom(REMOTE.withResource("junit"));
messageCorrection.addExtension(new Body()).setContent("Hi example!");
messageCorrection.addExtension(new Replace()).setId("1");
this.transformer.transform(
Transformation.of(messageCorrection, Instant.now(), REMOTE, "stanza-a", null));
// the correction should not show up as a message
Assert.assertEquals(0, database.messageDao().getMessages(1L).size());
final var messageWithTypo = new Message();
messageWithTypo.setId("1");
messageWithTypo.setTo(ACCOUNT);
messageWithTypo.setFrom(REMOTE.withResource("junit"));
messageWithTypo.addExtension(new Body()).setContent("Hii example!");
this.transformer.transform(
Transformation.of(messageWithTypo, Instant.now(), REMOTE, "stanza-b", null));
final var messages = database.messageDao().getMessages(1L);
Assert.assertEquals(1, messages.size());
final var message = Iterables.getOnlyElement(messages);
final var onlyContent = Iterables.getOnlyElement(message.contents);
Assert.assertEquals(Modification.EDIT, message.modification);
Assert.assertEquals("Hi example!", onlyContent.body);
}
@Test
public void correctionAfterOriginal() {
final var messageWithTypo = new Message();
messageWithTypo.setId("1");
messageWithTypo.setTo(ACCOUNT);
messageWithTypo.setFrom(REMOTE.withResource("junit"));
messageWithTypo.addExtension(new Body()).setContent("Hii example!");
this.transformer.transform(
Transformation.of(messageWithTypo, Instant.now(), REMOTE, "stanza-a", null));
Assert.assertEquals(1, database.messageDao().getMessages(1L).size());
final var messageCorrection = new Message();
messageCorrection.setId("2");
messageCorrection.setTo(ACCOUNT);
messageCorrection.setFrom(REMOTE.withResource("junit"));
messageCorrection.addExtension(new Body()).setContent("Hi example!");
messageCorrection.addExtension(new Replace()).setId("1");
this.transformer.transform(
Transformation.of(messageCorrection, Instant.now(), REMOTE, "stanza-b", null));
final var messages = database.messageDao().getMessages(1L);
Assert.assertEquals(1, messages.size());
final var message = Iterables.getOnlyElement(messages);
final var onlyContent = Iterables.getOnlyElement(message.contents);
Assert.assertEquals(Modification.EDIT, message.modification);
Assert.assertEquals("Hi example!", onlyContent.body);
}
} }

View file

@ -82,23 +82,27 @@ public abstract class MessageDao {
"Found stub for stanzaId '{}' and messageId '{}'", "Found stub for stanzaId '{}' and messageId '{}'",
transformation.stanzaId, transformation.stanzaId,
transformation.messageId); transformation.messageId);
final long messageVersionId = final long originalVersionId =
insert( insert(
MessageVersionEntity.of( MessageVersionEntity.of(
messageIdentifier.id, messageIdentifier.id,
Modification.ORIGINAl, Modification.ORIGINAL,
transformation)); transformation));
final MessageEntity updatedEntity = final MessageEntity updatedEntity =
MessageEntity.of(chatIdentifier.id, transformation); MessageEntity.of(chatIdentifier.id, transformation);
updatedEntity.id = messageIdentifier.id; updatedEntity.id = messageIdentifier.id;
updatedEntity.latestVersion = messageVersionId; updatedEntity.latestVersion = getLatestVersion(messageIdentifier.id);
LOGGER.info(
"Created original version {} and updated latest version to {}",
originalVersionId,
updatedEntity.latestVersion);
update(updatedEntity); update(updatedEntity);
return new MessageIdentifier( return new MessageIdentifier(
updatedEntity.id, updatedEntity.id,
transformation.stanzaId, transformation.stanzaId,
transformation.messageId, transformation.messageId,
transformation.fromBare(), transformation.fromBare(),
messageVersionId); originalVersionId);
} else { } else {
throw new IllegalStateException( throw new IllegalStateException(
String.format( String.format(
@ -114,7 +118,7 @@ public abstract class MessageDao {
final long messageVersionId = final long messageVersionId =
insert( insert(
MessageVersionEntity.of( MessageVersionEntity.of(
messageEntityId, Modification.ORIGINAl, transformation)); messageEntityId, Modification.ORIGINAL, transformation));
setLatestMessageId(messageEntityId, messageVersionId); setLatestMessageId(messageEntityId, messageVersionId);
return new MessageIdentifier( return new MessageIdentifier(
messageEntityId, messageEntityId,
@ -206,7 +210,7 @@ public abstract class MessageDao {
@Query( @Query(
"SELECT id FROM message_version WHERE messageEntityId=:messageEntityId ORDER BY (CASE" "SELECT id FROM message_version WHERE messageEntityId=:messageEntityId ORDER BY (CASE"
+ " modification WHEN 'ORIGINAL' THEN 0 ELSE 1 END),receivedAt DESC LIMIT 1") + " modification WHEN 'ORIGINAL' THEN 1 ELSE 0 END),receivedAt DESC LIMIT 1")
abstract Long getLatestVersion(long messageEntityId); abstract Long getLatestVersion(long messageEntityId);
@Query( @Query(

View file

@ -1,7 +1,7 @@
package im.conversations.android.database.model; package im.conversations.android.database.model;
public enum Modification { public enum Modification {
ORIGINAl, ORIGINAL,
EDIT, // XEP-0308: Last Message Correction EDIT, // XEP-0308: Last Message Correction
RETRACTION, // XEP-0424: Message Retraction RETRACTION, // XEP-0424: Message Retraction
MODERATION // XEP-0425: Message Moderation MODERATION // XEP-0425: Message Moderation

View file

@ -1,5 +1,6 @@
package im.conversations.android.xmpp.model.correction; package im.conversations.android.xmpp.model.correction;
import androidx.annotation.NonNull;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Namespace;
import im.conversations.android.annotation.XmlElement; import im.conversations.android.annotation.XmlElement;
@ -15,4 +16,8 @@ public class Replace extends Extension {
public String getId() { public String getId() {
return Strings.emptyToNull(this.getAttribute("id")); return Strings.emptyToNull(this.getAttribute("id"));
} }
public void setId(@NonNull final String id) {
this.setAttribute("id", id);
}
} }