mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-08 05:47:05 +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:
parent
7fe3f6d306
commit
683b06901f
3 changed files with 33 additions and 4 deletions
|
@ -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 = ""
|
||||
|
|
|
@ -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"""
|
|
@ -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) ->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue