From 5d0901f68652ed0f7b382433bc8a90a1d5f414b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BBerko?= Date: Mon, 25 Sep 2023 15:15:45 +0200 Subject: [PATCH 1/2] fix: ignore negative mention index --- .../home/conversations/model/MessageTypes.kt | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt b/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt index 9198e7142f3..ef5ced6dacc 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt @@ -46,7 +46,6 @@ import com.wire.android.ui.home.conversations.model.messagetypes.image.ImageMess import com.wire.android.ui.home.conversations.model.messagetypes.image.ImageMessageParams import com.wire.android.ui.home.conversations.model.messagetypes.image.ImportedImageMessage import com.wire.android.ui.markdown.DisplayMention -import com.wire.android.ui.markdown.MarkdownConsts.MENTION_MARK import com.wire.android.ui.markdown.MarkdownDocument import com.wire.android.ui.markdown.NodeData import com.wire.android.ui.theme.wireColorScheme @@ -63,6 +62,7 @@ import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension import org.commonmark.ext.gfm.tables.TablesExtension import org.commonmark.node.Document import org.commonmark.parser.Parser +import com.wire.android.ui.markdown.MarkdownConsts.MENTION_MARK // TODO: Here we actually need to implement some logic that will distinguish MentionLabel with Body of the message, // waiting for the backend to implement mapping logic for the MessageBody @@ -179,17 +179,20 @@ internal fun MessageGenericAsset( private fun mapToDisplayMentions(uiText: UIText, resources: Resources): Pair, String> { return if (uiText is UIText.DynamicString) { val stringBuilder: StringBuilder = StringBuilder(uiText.value) - val mentions = uiText.mentions.sortedBy { it.start }.reversed() - val mentionList = mentions.mapNotNull { + val mentions = uiText.mentions + .filter { it.start >= 0 && it.length > 0 } + .sortedBy { it.start } + .reversed() + val mentionList = mentions.mapNotNull { mention -> // secured crash for mentions caused by web when text without mentions contains mention data - if (it.start + it.length < uiText.value.length && uiText.value.elementAt(it.start) == '@') { - val mentionName = uiText.value.substring(it.start, it.start + it.length) - stringBuilder.insert(it.start + it.length, MENTION_MARK) - stringBuilder.insert(it.start, MENTION_MARK) + if (mention.start + mention.length <= uiText.value.length && uiText.value.elementAt(mention.start) == '@') { + val mentionName = uiText.value.substring(mention.start, mention.start + mention.length) + stringBuilder.insert(mention.start + mention.length, MENTION_MARK) + stringBuilder.insert(mention.start, MENTION_MARK) DisplayMention( - it.userId, - it.length, - it.isSelfMention, + mention.userId, + mention.length, + mention.isSelfMention, mentionName ) } else { From 6eb2554df65dc76dd948cd6bd7a92633cdf5dbbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C5=BBerko?= Date: Tue, 26 Sep 2023 17:03:06 +0200 Subject: [PATCH 2/2] imports fix --- .../wire/android/ui/home/conversations/model/MessageTypes.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt b/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt index ef5ced6dacc..b7c1327d319 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/conversations/model/MessageTypes.kt @@ -46,6 +46,7 @@ import com.wire.android.ui.home.conversations.model.messagetypes.image.ImageMess import com.wire.android.ui.home.conversations.model.messagetypes.image.ImageMessageParams import com.wire.android.ui.home.conversations.model.messagetypes.image.ImportedImageMessage import com.wire.android.ui.markdown.DisplayMention +import com.wire.android.ui.markdown.MarkdownConsts.MENTION_MARK import com.wire.android.ui.markdown.MarkdownDocument import com.wire.android.ui.markdown.NodeData import com.wire.android.ui.theme.wireColorScheme @@ -62,7 +63,6 @@ import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension import org.commonmark.ext.gfm.tables.TablesExtension import org.commonmark.node.Document import org.commonmark.parser.Parser -import com.wire.android.ui.markdown.MarkdownConsts.MENTION_MARK // TODO: Here we actually need to implement some logic that will distinguish MentionLabel with Body of the message, // waiting for the backend to implement mapping logic for the MessageBody