do not show fallback in embedded message

This commit is contained in:
Daniel Gultsch 2023-03-31 15:36:57 +02:00
parent acfcde8416
commit 340bf45095
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 45 additions and 15 deletions

View file

@ -16,6 +16,9 @@ public class MessageEmbedded {
public Long latestVersion;
public int inReplyToFallbackStart;
public int inReplyToFallbackEnd;
@Relation(
entity = MessageContentEntity.class,
parentColumn = "latestVersion",

View file

@ -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() {

View file

@ -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<MessageContent> 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;
}
}