Fix bug with processing and displaying long messages with mentions.

This commit is contained in:
Cody Henthorne 2021-08-06 13:19:44 -04:00 committed by GitHub
parent fc51c4940c
commit 570b4d7150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 21 deletions

View file

@ -132,10 +132,20 @@ public class ConversationMessage {
*/
@WorkerThread
public static @NonNull ConversationMessage createWithUnresolvedData(@NonNull Context context, @NonNull MessageRecord messageRecord) {
return createWithUnresolvedData(context, messageRecord, messageRecord.getDisplayBody(context));
}
/**
* Creates a {@link ConversationMessage} wrapping the provided MessageRecord and body, and will query for potential mentions. If mentions
* are found, the body of the provided message will be updated and modified to match actual mentions. This will perform
* database operations to query for mentions and then to resolve mentions to display names.
*/
@WorkerThread
public static @NonNull ConversationMessage createWithUnresolvedData(@NonNull Context context, @NonNull MessageRecord messageRecord, @NonNull CharSequence body) {
if (messageRecord.isMms()) {
List<Mention> mentions = DatabaseFactory.getMentionDatabase(context).getMentionsForMessage(messageRecord.getId());
if (!mentions.isEmpty()) {
MentionUtil.UpdatedBodyAndMentions updated = MentionUtil.updateBodyAndMentionsWithDisplayNames(context, messageRecord, mentions);
MentionUtil.UpdatedBodyAndMentions updated = MentionUtil.updateBodyAndMentionsWithDisplayNames(context, body, mentions);
return new ConversationMessage(messageRecord, updated.getBody(), updated.getMentions());
}
}

View file

@ -77,7 +77,7 @@ public final class MentionUtil {
for (Mention mention : sortedMentions) {
if (invalidMention(body, mention)) {
return new UpdatedBodyAndMentions(body, Collections.emptyList());
continue;
}
updatedBody.append(body.subSequence(bodyIndex, mention.getStart()));

View file

@ -15,11 +15,9 @@ import org.thoughtcrime.securesms.database.model.MessageRecord;
class LongMessage {
private final ConversationMessage conversationMessage;
private final String fullBody;
LongMessage(@NonNull ConversationMessage conversationMessage, @NonNull String fullBody) {
LongMessage(@NonNull ConversationMessage conversationMessage) {
this.conversationMessage = conversationMessage;
this.fullBody = fullBody;
}
@NonNull MessageRecord getMessageRecord() {
@ -27,6 +25,6 @@ class LongMessage {
}
@NonNull CharSequence getFullBody(@NonNull Context context) {
return !TextUtils.isEmpty(fullBody) ? fullBody : conversationMessage.getDisplayBody(context);
return conversationMessage.getDisplayBody(context);
}
}

View file

@ -54,9 +54,9 @@ class LongMessageRepository {
TextSlide textSlide = record.get().getSlideDeck().getTextSlide();
if (textSlide != null && textSlide.getUri() != null) {
return Optional.of(new LongMessage(ConversationMessageFactory.createWithUnresolvedData(context, record.get()), readFullBody(context, textSlide.getUri())));
return Optional.of(new LongMessage(ConversationMessageFactory.createWithUnresolvedData(context, record.get(), readFullBody(context, textSlide.getUri()))));
} else {
return Optional.of(new LongMessage(ConversationMessageFactory.createWithUnresolvedData(context, record.get()), ""));
return Optional.of(new LongMessage(ConversationMessageFactory.createWithUnresolvedData(context, record.get())));
}
} else {
return Optional.absent();
@ -68,7 +68,7 @@ class LongMessageRepository {
Optional<MessageRecord> record = getSmsMessage(smsDatabase, messageId);
if (record.isPresent()) {
return Optional.of(new LongMessage(ConversationMessageFactory.createWithUnresolvedData(context, record.get()), ""));
return Optional.of(new LongMessage(ConversationMessageFactory.createWithUnresolvedData(context, record.get())));
} else {
return Optional.absent();
}
@ -89,7 +89,7 @@ class LongMessageRepository {
}
}
private String readFullBody(@NonNull Context context, @NonNull Uri uri) {
private @NonNull String readFullBody(@NonNull Context context, @NonNull Uri uri) {
try (InputStream stream = PartAuthority.getAttachmentStream(context, uri)) {
return StreamUtil.readFullyAsString(stream);
} catch (IOException e) {

View file

@ -951,7 +951,6 @@ public final class SignalServiceContent {
for (SignalServiceProtos.DataMessage.BodyRange bodyRange : bodyRanges) {
if (bodyRange.hasMentionUuid()) {
try {
validateBodyRange(body, bodyRange);
mentions.add(new SignalServiceDataMessage.Mention(UuidUtil.parseOrThrow(bodyRange.getMentionUuid()), bodyRange.getStart(), bodyRange.getLength()));
} catch (IllegalArgumentException e) {
throw new ProtocolInvalidMessageException(new InvalidMessageException(e), null, 0);
@ -966,16 +965,6 @@ public final class SignalServiceContent {
return mentions;
}
private static void validateBodyRange(String body, SignalServiceProtos.DataMessage.BodyRange bodyRange) throws ProtocolInvalidMessageException {
int incomingBodyLength = body != null ? body.length() : -1;
int start = bodyRange.hasStart() ? bodyRange.getStart() : -1;
int length = bodyRange.hasLength() ? bodyRange.getLength() : -1;
if (start < 0 || length < 0 || (start + length) > incomingBodyLength) {
throw new ProtocolInvalidMessageException(new InvalidMessageException("Incoming body range has out-of-bound range"), null, 0);
}
}
private static SignalServiceDataMessage.Sticker createSticker(SignalServiceProtos.DataMessage content) throws ProtocolInvalidMessageException {
if (!content.hasSticker() ||
!content.getSticker().hasPackId() ||