1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 13:57:10 +09:00

DROID-3389 Space-level chat | Enhancement | Parse urls when sending message and include it in message markup (#2102)

This commit is contained in:
Evgenii Kozlov 2025-02-14 14:17:42 +01:00 committed by Evgenii Kozlov
parent c79ecd3331
commit f9ac843ca2
3 changed files with 33 additions and 4 deletions

View file

@ -67,6 +67,7 @@ sealed class Chat {
id: Id,
text: String,
attachments: List<Attachment> = emptyList(),
marks: List<Block.Content.Text.Mark>
) : 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 = ""

View file

@ -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"""

View file

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