From 340bf45095d0551dcd72545f77bb7e82420b3058 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 31 Mar 2023 15:36:57 +0200 Subject: [PATCH] do not show fallback in embedded message --- .../database/model/MessageEmbedded.java | 3 ++ .../model/MessageWithContentReactions.java | 26 +++++++--------- .../android/util/TextContents.java | 31 +++++++++++++++++++ 3 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/im/conversations/android/util/TextContents.java diff --git a/app/src/main/java/im/conversations/android/database/model/MessageEmbedded.java b/app/src/main/java/im/conversations/android/database/model/MessageEmbedded.java index ade15e5f2..8cdd4e2ef 100644 --- a/app/src/main/java/im/conversations/android/database/model/MessageEmbedded.java +++ b/app/src/main/java/im/conversations/android/database/model/MessageEmbedded.java @@ -16,6 +16,9 @@ public class MessageEmbedded { public Long latestVersion; + public int inReplyToFallbackStart; + public int inReplyToFallbackEnd; + @Relation( entity = MessageContentEntity.class, parentColumn = "latestVersion", diff --git a/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java b/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java index 4ba8dbc68..e303b8026 100644 --- a/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java +++ b/app/src/main/java/im/conversations/android/database/model/MessageWithContentReactions.java @@ -2,7 +2,6 @@ package im.conversations.android.database.model; import androidx.room.Relation; import com.google.common.base.Objects; -import com.google.common.base.Strings; import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Iterables; @@ -13,6 +12,7 @@ import im.conversations.android.database.entity.MessageContentEntity; import im.conversations.android.database.entity.MessageEntity; import im.conversations.android.database.entity.MessageReactionEntity; import im.conversations.android.database.entity.MessageStateEntity; +import im.conversations.android.util.TextContents; import java.time.Instant; import java.util.Arrays; import java.util.Collection; @@ -97,18 +97,11 @@ public final class MessageWithContentReactions } public String textContent() { - final var textContent = - Iterables.getFirst( - Iterables.filter(this.contents, c -> c.type == PartType.TEXT), null); - final var body = Strings.nullToEmpty(textContent == null ? null : textContent.body); - ; - if (inReplyToMessageEntityId != null - && inReplyToFallbackEnd > inReplyToFallbackStart - && inReplyToFallbackEnd <= body.length()) { - return body.substring(0, inReplyToFallbackStart) + body.substring(inReplyToFallbackEnd); - } else { - return body; - } + return TextContents.toText( + this.contents, + inReplyToMessageEntityId != null, + inReplyToFallbackStart, + inReplyToFallbackEnd); } public boolean hasPreview() { @@ -143,8 +136,11 @@ public final class MessageWithContentReactions if (inReplyTo == null) { return null; } - final var content = Iterables.getFirst(inReplyTo.contents, null); - return Strings.nullToEmpty(content == null ? null : content.body); + return TextContents.toText( + inReplyTo.contents, + true, + inReplyTo.inReplyToFallbackStart, + inReplyTo.inReplyToFallbackEnd); } public AddressWithName getAddressWithName() { diff --git a/app/src/main/java/im/conversations/android/util/TextContents.java b/app/src/main/java/im/conversations/android/util/TextContents.java new file mode 100644 index 000000000..f3ac68c5b --- /dev/null +++ b/app/src/main/java/im/conversations/android/util/TextContents.java @@ -0,0 +1,31 @@ +package im.conversations.android.util; + +import com.google.common.base.Strings; +import com.google.common.collect.Collections2; +import com.google.common.collect.Iterables; +import im.conversations.android.database.model.MessageContent; +import im.conversations.android.database.model.PartType; +import java.util.List; + +public final class TextContents { + + private TextContents() {} + + public static String toText( + final List messageContents, + final boolean removeFallback, + final int inReplyToFallbackStart, + final int inReplyToFallbackEnd) { + final var textContents = Collections2.filter(messageContents, c -> c.type == PartType.TEXT); + if (textContents.size() == 1 && removeFallback) { + final String body = Strings.nullToEmpty(Iterables.getOnlyElement(textContents).body); + if (inReplyToFallbackEnd > inReplyToFallbackStart + && inReplyToFallbackEnd <= body.length()) { + return body.substring(0, inReplyToFallbackStart) + + body.substring(inReplyToFallbackEnd); + } + } + final var anyTextContent = Iterables.getFirst(textContents, null); + return anyTextContent == null ? null : anyTextContent.body; + } +}