diff --git a/core-models/src/main/java/com/anytypeio/anytype/core_models/chats/Chat.kt b/core-models/src/main/java/com/anytypeio/anytype/core_models/chats/Chat.kt index 1c66fbdb22..4e28968cb2 100644 --- a/core-models/src/main/java/com/anytypeio/anytype/core_models/chats/Chat.kt +++ b/core-models/src/main/java/com/anytypeio/anytype/core_models/chats/Chat.kt @@ -67,6 +67,7 @@ sealed class Chat { id: Id, text: String, attachments: List = emptyList(), + marks: List ) : Message = Message( id = id, createdAt = 0L, @@ -77,7 +78,7 @@ sealed class Chat { replyToMessageId = "", content = Content( text = text, - marks = emptyList(), + marks = marks, style = Block.Content.Text.Style.P ), order = "" diff --git a/core-utils/src/main/java/com/anytypeio/anytype/core_utils/tools/Regex.kt b/core-utils/src/main/java/com/anytypeio/anytype/core_utils/tools/Regex.kt new file mode 100644 index 0000000000..2bbb0bb94d --- /dev/null +++ b/core-utils/src/main/java/com/anytypeio/anytype/core_utils/tools/Regex.kt @@ -0,0 +1,4 @@ +package com.anytypeio.anytype.core_utils.tools + + +const val DEFAULT_URL_REGEX = """(?:https?://|www\.)\S+|\b[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(/\S*)?\b""" \ No newline at end of file diff --git a/feature-chats/src/main/java/com/anytypeio/anytype/feature_chats/presentation/ChatViewModel.kt b/feature-chats/src/main/java/com/anytypeio/anytype/feature_chats/presentation/ChatViewModel.kt index 02c124480c..e6e6a4eb18 100644 --- a/feature-chats/src/main/java/com/anytypeio/anytype/feature_chats/presentation/ChatViewModel.kt +++ b/feature-chats/src/main/java/com/anytypeio/anytype/feature_chats/presentation/ChatViewModel.kt @@ -11,6 +11,7 @@ import com.anytypeio.anytype.core_models.primitives.Space import com.anytypeio.anytype.core_models.primitives.SpaceId import com.anytypeio.anytype.core_ui.text.splitByMarks import com.anytypeio.anytype.core_utils.common.DefaultFileInfo +import com.anytypeio.anytype.core_utils.tools.DEFAULT_URL_REGEX import com.anytypeio.anytype.domain.auth.interactor.GetAccount import com.anytypeio.anytype.domain.base.AppCoroutineDispatchers import com.anytypeio.anytype.domain.base.onFailure @@ -342,6 +343,28 @@ class ChatViewModel @Inject constructor( Timber.d("DROID-2635 OnMessageSent, markup: $markup}") } viewModelScope.launch { + + val urlRegex = Regex(DEFAULT_URL_REGEX) + val parsedUrls = buildList { + urlRegex.findAll(msg).forEach { match -> + val range = match.range + val url = match.value + + // Check if a LINK markup already exists in the same range + if (markup.none { it.range == range && it.type == Block.Content.Text.Mark.Type.LINK }) { + add( + Block.Content.Text.Mark( + range = range, + type = Block.Content.Text.Mark.Type.LINK, + param = url + ) + ) + } + } + } + + val normalizedMarkup = (markup + parsedUrls).sortedBy { it.range.first } + chatBoxMode.value = chatBoxMode.value.updateIsSendingBlocked(isBlocked = true) val attachments = buildList { val currAttachments = chatBoxAttachments.value @@ -456,7 +479,7 @@ class ChatViewModel @Inject constructor( message = Chat.Message.new( text = msg.trim(), attachments = attachments, - marks = markup + marks = normalizedMarkup ) ) ).onSuccess { (id, payload) -> @@ -479,7 +502,8 @@ class ChatViewModel @Inject constructor( message = Chat.Message.updated( id = mode.msg, text = msg.trim(), - attachments = editedMessage?.attachments.orEmpty() + attachments = editedMessage?.attachments.orEmpty(), + marks = normalizedMarkup ) ) ).onSuccess { @@ -500,7 +524,7 @@ class ChatViewModel @Inject constructor( text = msg.trim(), replyToMessageId = mode.msg, attachments = attachments, - marks = markup + marks = normalizedMarkup ) ) ).onSuccess { (id, payload) ->