Merge branch 'main' of https://github.com/vitorpamplona/amethyst
* 'main' of https://github.com/vitorpamplona/amethyst: feat: add RelayUrlSegment for ws:// and wss:// relay URIs fix: treat multibyte characters as URL terminators in RichTextParser Simplify profile edit screen layout feat: show "Replying to" label in quoted notes fix: suppress parent thread in quoted notes
This commit is contained in:
+131
-80
@@ -20,7 +20,12 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.ui.actions
|
package com.vitorpamplona.amethyst.ui.actions
|
||||||
|
|
||||||
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
|
import androidx.compose.animation.expandVertically
|
||||||
|
import androidx.compose.animation.shrinkVertically
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.Spacer
|
import androidx.compose.foundation.layout.Spacer
|
||||||
import androidx.compose.foundation.layout.consumeWindowInsets
|
import androidx.compose.foundation.layout.consumeWindowInsets
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
@@ -30,16 +35,24 @@ import androidx.compose.foundation.layout.padding
|
|||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.text.KeyboardOptions
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.material.icons.Icons
|
||||||
|
import androidx.compose.material.icons.filled.ExpandLess
|
||||||
|
import androidx.compose.material.icons.filled.ExpandMore
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
|
import androidx.compose.material3.Icon
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.MutableState
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
@@ -61,8 +74,15 @@ fun NewUserMetadataScreen(
|
|||||||
postViewModel.init(accountViewModel)
|
postViewModel.init(accountViewModel)
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
|
val socialExpanded = rememberSaveable { mutableStateOf(false) }
|
||||||
|
|
||||||
LaunchedEffect(postViewModel, accountViewModel) {
|
LaunchedEffect(postViewModel, accountViewModel) {
|
||||||
postViewModel.load()
|
postViewModel.load()
|
||||||
|
|
||||||
|
// Auto-expand social proofs if any have data
|
||||||
|
if (postViewModel.twitter.value.isNotBlank() || postViewModel.mastodon.value.isNotBlank() || postViewModel.github.value.isNotBlank()) {
|
||||||
|
socialExpanded.value = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
@@ -95,8 +115,9 @@ fun NewUserMetadataScreen(
|
|||||||
Column(
|
Column(
|
||||||
modifier = Modifier.padding(10.dp).verticalScroll(rememberScrollState()),
|
modifier = Modifier.padding(10.dp).verticalScroll(rememberScrollState()),
|
||||||
) {
|
) {
|
||||||
|
// -- Profile (always visible) --
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
label = { Text(text = stringRes(R.string.profile_name_with_explainer)) },
|
label = { Text(text = stringRes(R.string.username)) },
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
value = postViewModel.name.value,
|
value = postViewModel.name.value,
|
||||||
onValueChange = { postViewModel.name.value = it },
|
onValueChange = { postViewModel.name.value = it },
|
||||||
@@ -106,6 +127,7 @@ fun NewUserMetadataScreen(
|
|||||||
color = MaterialTheme.colorScheme.placeholderText,
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
prefix = { Text("@") },
|
||||||
keyboardOptions =
|
keyboardOptions =
|
||||||
KeyboardOptions.Default.copy(
|
KeyboardOptions.Default.copy(
|
||||||
capitalization = KeyboardCapitalization.Sentences,
|
capitalization = KeyboardCapitalization.Sentences,
|
||||||
@@ -195,7 +217,7 @@ fun NewUserMetadataScreen(
|
|||||||
SelectSingleFromGallery(
|
SelectSingleFromGallery(
|
||||||
isUploading = postViewModel.isUploadingImageForBanner,
|
isUploading = postViewModel.isUploadingImageForBanner,
|
||||||
tint = MaterialTheme.colorScheme.placeholderText,
|
tint = MaterialTheme.colorScheme.placeholderText,
|
||||||
modifier = Modifier.padding(start = 5.dp).align(Alignment.CenterHorizontally),
|
modifier = Modifier.padding(start = 5.dp),
|
||||||
) {
|
) {
|
||||||
postViewModel.uploadForBanner(it, context, onError = accountViewModel.toastManager::toast)
|
postViewModel.uploadForBanner(it, context, onError = accountViewModel.toastManager::toast)
|
||||||
}
|
}
|
||||||
@@ -206,13 +228,29 @@ fun NewUserMetadataScreen(
|
|||||||
Spacer(modifier = Modifier.height(10.dp))
|
Spacer(modifier = Modifier.height(10.dp))
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
label = { Text(text = stringRes(R.string.pronouns)) },
|
label = { Text(text = stringRes(R.string.lightning_address)) },
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
value = postViewModel.pronouns.value,
|
value = postViewModel.lnAddress.value,
|
||||||
onValueChange = { postViewModel.pronouns.value = it },
|
onValueChange = { postViewModel.lnAddress.value = it },
|
||||||
placeholder = {
|
placeholder = {
|
||||||
Text(
|
Text(
|
||||||
text = "they/them, ...",
|
text = "me@mylightningnode.com",
|
||||||
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
singleLine = true,
|
||||||
|
)
|
||||||
|
|
||||||
|
Spacer(modifier = Modifier.height(10.dp))
|
||||||
|
|
||||||
|
OutlinedTextField(
|
||||||
|
label = { Text(text = stringRes(R.string.nip_05) + " (NIP-05)") },
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
value = postViewModel.nip05.value,
|
||||||
|
onValueChange = { postViewModel.nip05.value = it },
|
||||||
|
placeholder = {
|
||||||
|
Text(
|
||||||
|
text = "_@mywebsite.com",
|
||||||
color = MaterialTheme.colorScheme.placeholderText,
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
@@ -238,94 +276,107 @@ fun NewUserMetadataScreen(
|
|||||||
Spacer(modifier = Modifier.height(10.dp))
|
Spacer(modifier = Modifier.height(10.dp))
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
label = { Text(text = stringRes(R.string.nip_05)) },
|
label = { Text(text = stringRes(R.string.pronouns)) },
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
value = postViewModel.nip05.value,
|
value = postViewModel.pronouns.value,
|
||||||
onValueChange = { postViewModel.nip05.value = it },
|
onValueChange = { postViewModel.pronouns.value = it },
|
||||||
placeholder = {
|
placeholder = {
|
||||||
Text(
|
Text(
|
||||||
text = "_@mywebsite.com",
|
text = "they/them, ...",
|
||||||
color = MaterialTheme.colorScheme.placeholderText,
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
singleLine = true,
|
singleLine = true,
|
||||||
)
|
)
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(10.dp))
|
// -- Social Proofs --
|
||||||
OutlinedTextField(
|
ExpandableSection(
|
||||||
label = { Text(text = stringRes(R.string.ln_address)) },
|
title = "Social proof",
|
||||||
modifier = Modifier.fillMaxWidth(),
|
expanded = socialExpanded,
|
||||||
value = postViewModel.lnAddress.value,
|
) {
|
||||||
onValueChange = { postViewModel.lnAddress.value = it },
|
OutlinedTextField(
|
||||||
placeholder = {
|
label = { Text(text = stringRes(R.string.twitter)) },
|
||||||
Text(
|
modifier = Modifier.fillMaxWidth(),
|
||||||
text = "me@mylightningnode.com",
|
value = postViewModel.twitter.value,
|
||||||
color = MaterialTheme.colorScheme.placeholderText,
|
onValueChange = { postViewModel.twitter.value = it },
|
||||||
)
|
placeholder = {
|
||||||
},
|
Text(
|
||||||
singleLine = true,
|
text = stringRes(R.string.twitter_proof_url_template),
|
||||||
)
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(10.dp))
|
Spacer(modifier = Modifier.height(10.dp))
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
label = { Text(text = stringRes(R.string.ln_url_outdated)) },
|
label = { Text(text = stringRes(R.string.mastodon)) },
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
value = postViewModel.lnURL.value,
|
value = postViewModel.mastodon.value,
|
||||||
onValueChange = { postViewModel.lnURL.value = it },
|
onValueChange = { postViewModel.mastodon.value = it },
|
||||||
placeholder = {
|
placeholder = {
|
||||||
Text(
|
Text(
|
||||||
text = stringRes(R.string.lnurl),
|
text = stringRes(R.string.mastodon_proof_url_template),
|
||||||
color = MaterialTheme.colorScheme.placeholderText,
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(10.dp))
|
Spacer(modifier = Modifier.height(10.dp))
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
label = { Text(text = stringRes(R.string.twitter)) },
|
label = { Text(text = stringRes(R.string.github)) },
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
value = postViewModel.twitter.value,
|
value = postViewModel.github.value,
|
||||||
onValueChange = { postViewModel.twitter.value = it },
|
onValueChange = { postViewModel.github.value = it },
|
||||||
placeholder = {
|
placeholder = {
|
||||||
Text(
|
Text(
|
||||||
text = stringRes(R.string.twitter_proof_url_template),
|
text = stringRes(R.string.github_proof_url_template),
|
||||||
color = MaterialTheme.colorScheme.placeholderText,
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
}
|
||||||
Spacer(modifier = Modifier.height(10.dp))
|
|
||||||
|
|
||||||
OutlinedTextField(
|
|
||||||
label = { Text(text = stringRes(R.string.mastodon)) },
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
value = postViewModel.mastodon.value,
|
|
||||||
onValueChange = { postViewModel.mastodon.value = it },
|
|
||||||
placeholder = {
|
|
||||||
Text(
|
|
||||||
text = stringRes(R.string.mastodon_proof_url_template),
|
|
||||||
color = MaterialTheme.colorScheme.placeholderText,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(10.dp))
|
|
||||||
|
|
||||||
OutlinedTextField(
|
|
||||||
label = { Text(text = stringRes(R.string.github)) },
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
value = postViewModel.github.value,
|
|
||||||
onValueChange = { postViewModel.github.value = it },
|
|
||||||
placeholder = {
|
|
||||||
Text(
|
|
||||||
text = stringRes(R.string.github_proof_url_template),
|
|
||||||
color = MaterialTheme.colorScheme.placeholderText,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
private fun ExpandableSection(
|
||||||
|
title: String,
|
||||||
|
expanded: MutableState<Boolean>,
|
||||||
|
content: @Composable () -> Unit,
|
||||||
|
) {
|
||||||
|
Row(
|
||||||
|
modifier =
|
||||||
|
Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.clickable { expanded.value = !expanded.value }
|
||||||
|
.padding(vertical = 12.dp),
|
||||||
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = title,
|
||||||
|
style = MaterialTheme.typography.titleSmall,
|
||||||
|
fontWeight = FontWeight.Bold,
|
||||||
|
modifier = Modifier.weight(1f),
|
||||||
|
)
|
||||||
|
Icon(
|
||||||
|
imageVector = if (expanded.value) Icons.Default.ExpandLess else Icons.Default.ExpandMore,
|
||||||
|
contentDescription = if (expanded.value) "Collapse" else "Expand",
|
||||||
|
tint = MaterialTheme.colorScheme.placeholderText,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
AnimatedVisibility(
|
||||||
|
visible = expanded.value,
|
||||||
|
enter = expandVertically(),
|
||||||
|
exit = shrinkVertically(),
|
||||||
|
) {
|
||||||
|
Column {
|
||||||
|
content()
|
||||||
|
Spacer(modifier = Modifier.height(10.dp))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -595,6 +595,7 @@ fun DisplayFullNote(
|
|||||||
baseNote = note,
|
baseNote = note,
|
||||||
modifier = MaterialTheme.colorScheme.innerPostModifier,
|
modifier = MaterialTheme.colorScheme.innerPostModifier,
|
||||||
isQuotedNote = true,
|
isQuotedNote = true,
|
||||||
|
unPackReply = false,
|
||||||
quotesLeft = quotesLeft - 1,
|
quotesLeft = quotesLeft - 1,
|
||||||
parentBackgroundColor = backgroundColor,
|
parentBackgroundColor = backgroundColor,
|
||||||
accountViewModel = accountViewModel,
|
accountViewModel = accountViewModel,
|
||||||
@@ -867,6 +868,7 @@ private fun DisplayNoteFromTag(
|
|||||||
baseNote = baseNote,
|
baseNote = baseNote,
|
||||||
modifier = MaterialTheme.colorScheme.innerPostModifier,
|
modifier = MaterialTheme.colorScheme.innerPostModifier,
|
||||||
isQuotedNote = true,
|
isQuotedNote = true,
|
||||||
|
unPackReply = false,
|
||||||
quotesLeft = quotesLeft - 1,
|
quotesLeft = quotesLeft - 1,
|
||||||
parentBackgroundColor = backgroundColor,
|
parentBackgroundColor = backgroundColor,
|
||||||
accountViewModel = accountViewModel,
|
accountViewModel = accountViewModel,
|
||||||
|
|||||||
@@ -116,6 +116,31 @@ fun ReplyInformationChannel(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalLayoutApi::class)
|
||||||
|
@Composable
|
||||||
|
fun ReplyToLabel(
|
||||||
|
replyingDirectlyTo: Note,
|
||||||
|
accountViewModel: AccountViewModel,
|
||||||
|
nav: INav,
|
||||||
|
) {
|
||||||
|
val user = replyingDirectlyTo.author ?: return
|
||||||
|
|
||||||
|
FlowRow {
|
||||||
|
Text(
|
||||||
|
stringRes(id = R.string.replying_to),
|
||||||
|
fontSize = 13.sp,
|
||||||
|
color = MaterialTheme.colorScheme.placeholderText,
|
||||||
|
)
|
||||||
|
|
||||||
|
ReplyInfoMention(
|
||||||
|
user = user,
|
||||||
|
prefix = "",
|
||||||
|
accountViewModel = accountViewModel,
|
||||||
|
onUserTagClick = { nav.nav(routeFor(it)) },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun ReplyInfoMention(
|
private fun ReplyInfoMention(
|
||||||
user: User,
|
user: User,
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ import com.vitorpamplona.amethyst.ui.components.SensitivityWarning
|
|||||||
import com.vitorpamplona.amethyst.ui.components.TranslatableRichTextViewer
|
import com.vitorpamplona.amethyst.ui.components.TranslatableRichTextViewer
|
||||||
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
|
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
|
||||||
import com.vitorpamplona.amethyst.ui.note.LoadDecryptedContent
|
import com.vitorpamplona.amethyst.ui.note.LoadDecryptedContent
|
||||||
|
import com.vitorpamplona.amethyst.ui.note.ReplyInformationChannel
|
||||||
import com.vitorpamplona.amethyst.ui.note.ReplyNoteComposition
|
import com.vitorpamplona.amethyst.ui.note.ReplyNoteComposition
|
||||||
import com.vitorpamplona.amethyst.ui.note.elements.DisplayUncitedHashtags
|
import com.vitorpamplona.amethyst.ui.note.elements.DisplayUncitedHashtags
|
||||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
||||||
@@ -49,10 +50,13 @@ import com.vitorpamplona.amethyst.ui.theme.StdVertSpacer
|
|||||||
import com.vitorpamplona.amethyst.ui.theme.placeholderText
|
import com.vitorpamplona.amethyst.ui.theme.placeholderText
|
||||||
import com.vitorpamplona.quartz.nip01Core.tags.hashtags.hasHashtags
|
import com.vitorpamplona.quartz.nip01Core.tags.hashtags.hasHashtags
|
||||||
import com.vitorpamplona.quartz.nip01Core.tags.people.hasAnyTaggedUser
|
import com.vitorpamplona.quartz.nip01Core.tags.people.hasAnyTaggedUser
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.tags.people.taggedUsers
|
||||||
import com.vitorpamplona.quartz.nip10Notes.BaseThreadedEvent
|
import com.vitorpamplona.quartz.nip10Notes.BaseThreadedEvent
|
||||||
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
|
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
|
||||||
import com.vitorpamplona.quartz.nip14Subject.subject
|
import com.vitorpamplona.quartz.nip14Subject.subject
|
||||||
import com.vitorpamplona.quartz.nip72ModCommunities.definition.CommunityDefinitionEvent
|
import com.vitorpamplona.quartz.nip72ModCommunities.definition.CommunityDefinitionEvent
|
||||||
|
import kotlinx.collections.immutable.persistentListOf
|
||||||
|
import kotlinx.collections.immutable.toImmutableList
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun RenderTextEvent(
|
fun RenderTextEvent(
|
||||||
@@ -98,6 +102,17 @@ fun RenderTextEvent(
|
|||||||
ReplyNoteComposition(replyingDirectlyTo, backgroundColor, accountViewModel, nav)
|
ReplyNoteComposition(replyingDirectlyTo, backgroundColor, accountViewModel, nav)
|
||||||
Spacer(modifier = StdVertSpacer)
|
Spacer(modifier = StdVertSpacer)
|
||||||
}
|
}
|
||||||
|
} else if (!unPackReply && !makeItShort && noteEvent is BaseThreadedEvent && noteEvent.hasAnyTaggedUser()) {
|
||||||
|
val mentions =
|
||||||
|
remember(noteEvent) {
|
||||||
|
noteEvent.taggedUsers().map { it.pubKey }.toImmutableList()
|
||||||
|
}
|
||||||
|
ReplyInformationChannel(
|
||||||
|
replyTo = persistentListOf(note),
|
||||||
|
mentions = mentions,
|
||||||
|
accountViewModel = accountViewModel,
|
||||||
|
nav = nav,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this is an audio-only event (content is just an audio URL with waveform IMeta)
|
// Check if this is an audio-only event (content is just an audio URL with waveform IMeta)
|
||||||
|
|||||||
+10
-11
@@ -175,7 +175,7 @@ class RichTextParser {
|
|||||||
lines.forEach { paragraph ->
|
lines.forEach { paragraph ->
|
||||||
val isRTL = isArabic(paragraph)
|
val isRTL = isArabic(paragraph)
|
||||||
|
|
||||||
val wordList = paragraph.trimEnd().split(' ')
|
val wordList = paragraph.trimEnd().split(wordBoundaryRegex).filter { it.isNotEmpty() }
|
||||||
val segments = ArrayList<Segment>(wordList.size)
|
val segments = ArrayList<Segment>(wordList.size)
|
||||||
wordList.forEach { word ->
|
wordList.forEach { word ->
|
||||||
segments.add(wordIdentifier(word, images, videos, urls, emojis, tags))
|
segments.add(wordIdentifier(word, images, videos, urls, emojis, tags))
|
||||||
@@ -245,6 +245,8 @@ class RichTextParser {
|
|||||||
|
|
||||||
if (videos.contains(word)) return VideoSegment(word)
|
if (videos.contains(word)) return VideoSegment(word)
|
||||||
|
|
||||||
|
if (word.startsWith("ws://", ignoreCase = true) || word.startsWith("wss://", ignoreCase = true)) return RelayUrlSegment(word)
|
||||||
|
|
||||||
if (urls.contains(word)) return LinkSegment(word)
|
if (urls.contains(word)) return LinkSegment(word)
|
||||||
|
|
||||||
if (CustomEmoji.fastMightContainEmoji(word, emojis) && emojis.any { word.contains(it.key) }) return EmojiSegment(word)
|
if (CustomEmoji.fastMightContainEmoji(word, emojis) && emojis.any { word.contains(it.key) }) return EmojiSegment(word)
|
||||||
@@ -334,17 +336,14 @@ class RichTextParser {
|
|||||||
val shortDatePattern: Regex = Regex("^\\d{2}-\\d{2}-\\d{2}$")
|
val shortDatePattern: Regex = Regex("^\\d{2}-\\d{2}-\\d{2}$")
|
||||||
val numberPattern: Regex = Regex("^(-?[\\d.]+)([a-zA-Z%]*)$")
|
val numberPattern: Regex = Regex("^(-?[\\d.]+)([a-zA-Z%]*)$")
|
||||||
|
|
||||||
// Android9 seems to have an issue starting this regex.
|
|
||||||
val noProtocolUrlValidator =
|
val noProtocolUrlValidator =
|
||||||
try {
|
Regex(
|
||||||
Regex(
|
"(([a-zA-Z0-9_-]+\\.)*[a-zA-Z][a-zA-Z0-9_-]+[\\.\\:][a-zA-Z0-9_]+([\\/ \\?\\=\\&\\#\\.]?[a-zA-Z0-9_-]+)*\\/?)(.*)",
|
||||||
"(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+[^\\p{IsHan}\\p{IsHiragana}\\p{IsKatakana}])*\\/?)(.*)",
|
)
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
// Splits at spaces AND at ASCII/multibyte character boundaries
|
||||||
Regex(
|
// e.g. "ああexample.com" -> ["ああ", "example.com"]
|
||||||
"(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+)*\\/?)(.*)",
|
private val wordBoundaryRegex = Regex("(?<=[\\u0000-\\u007F])(?=[\\u0080-\\uFFFF])|(?<=[\\u0080-\\uFFFF])(?=[\\u0000-\\u007F])| +")
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
val additionalUrlSchema =
|
val additionalUrlSchema =
|
||||||
"""^([A-Za-z0-9-_]+(\.[A-Za-z0-9-_]+)+)(:[0-9]+)?(/[^?#]*)?(\?[^#]*)?(#.*)?"""
|
"""^([A-Za-z0-9-_]+(\.[A-Za-z0-9-_]+)+)(:[0-9]+)?(/[^?#]*)?(\?[^#]*)?(#.*)?"""
|
||||||
|
|||||||
+5
@@ -146,6 +146,11 @@ class SchemelessUrlSegment(
|
|||||||
val extras: String?,
|
val extras: String?,
|
||||||
) : Segment(segment)
|
) : Segment(segment)
|
||||||
|
|
||||||
|
@Immutable
|
||||||
|
class RelayUrlSegment(
|
||||||
|
segment: String,
|
||||||
|
) : Segment(segment)
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
class RegularTextSegment(
|
class RegularTextSegment(
|
||||||
segment: String,
|
segment: String,
|
||||||
|
|||||||
+132
@@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Vitor Pamplona
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
* this software and associated documentation files (the "Software"), to deal in
|
||||||
|
* the Software without restriction, including without limitation the rights to use,
|
||||||
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||||
|
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||||
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
package com.vitorpamplona.amethyst.commons.richtext
|
||||||
|
|
||||||
|
import com.vitorpamplona.amethyst.commons.model.EmptyTagList
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class RichTextParserMultibyteTest {
|
||||||
|
@Test
|
||||||
|
fun testFullTextWithMultibyte() {
|
||||||
|
// Multibyte characters around an email address should not produce URL/Link segments
|
||||||
|
val text =
|
||||||
|
"マルチバイト文字テストuser@example.com ほげほげ"
|
||||||
|
|
||||||
|
val state =
|
||||||
|
RichTextParser()
|
||||||
|
.parseText(text, EmptyTagList, null)
|
||||||
|
|
||||||
|
val allSegments =
|
||||||
|
state.paragraphs
|
||||||
|
.flatMap { it.words }
|
||||||
|
|
||||||
|
// user@example.com should be EmailSegment
|
||||||
|
assertTrue(
|
||||||
|
"user@example.com should be EmailSegment",
|
||||||
|
allSegments.any { it is EmailSegment && it.segmentText == "user@example.com" },
|
||||||
|
)
|
||||||
|
|
||||||
|
// user@example.com should NOT be a LinkSegment
|
||||||
|
assertTrue(
|
||||||
|
"user@example.com should not be a LinkSegment",
|
||||||
|
allSegments.none { it is LinkSegment && it.segmentText == "user@example.com" },
|
||||||
|
)
|
||||||
|
|
||||||
|
// user@example.com should not be in urlSet
|
||||||
|
assertTrue(
|
||||||
|
"user@example.com should not be in urlSet",
|
||||||
|
!state.urlSet.contains("user@example.com"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testEmailSegmentStandalone() {
|
||||||
|
val text = "user@example.com"
|
||||||
|
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||||
|
val allSegments = state.paragraphs.flatMap { it.words }
|
||||||
|
assertTrue(
|
||||||
|
"user@example.com should be EmailSegment",
|
||||||
|
allSegments.any { it is EmailSegment && it.segmentText == "user@example.com" },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMultibytePrefix_SchemelessUrl() {
|
||||||
|
// ああexample.com → RegularText(ああ) + SchemelessUrl(example.com)
|
||||||
|
val text = "ああexample.com"
|
||||||
|
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||||
|
val allSegments = state.paragraphs.flatMap { it.words }
|
||||||
|
|
||||||
|
val urlSegments = allSegments.filterIsInstance<SchemelessUrlSegment>()
|
||||||
|
assertTrue("Should have SchemelessUrlSegment", urlSegments.isNotEmpty())
|
||||||
|
assertTrue("URL should be example.com", urlSegments.any { it.url == "example.com" })
|
||||||
|
|
||||||
|
val textSegments = allSegments.filterIsInstance<RegularTextSegment>()
|
||||||
|
assertTrue("Should have prefix ああ", textSegments.any { it.segmentText == "ああ" })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMultibyteSuffix_SchemelessUrl() {
|
||||||
|
// example.comああ → SchemelessUrl(example.com) + RegularText(ああ)
|
||||||
|
val text = "example.comああ"
|
||||||
|
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||||
|
val allSegments = state.paragraphs.flatMap { it.words }
|
||||||
|
|
||||||
|
val urlSegments = allSegments.filterIsInstance<SchemelessUrlSegment>()
|
||||||
|
assertTrue("Should have SchemelessUrlSegment", urlSegments.isNotEmpty())
|
||||||
|
assertTrue("URL should be example.com", urlSegments.any { it.url == "example.com" })
|
||||||
|
|
||||||
|
val textSegments = allSegments.filterIsInstance<RegularTextSegment>()
|
||||||
|
assertTrue("Should have suffix ああ", textSegments.any { it.segmentText == "ああ" })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMultibytePrefix_Email() {
|
||||||
|
// ほむほむuser@example.comほげほげ → RegularText(ほむほむ) + Email(user@example.com) + RegularText(ほげほげ)
|
||||||
|
val text = "ほむほむuser@example.comほげほげ"
|
||||||
|
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||||
|
val allSegments = state.paragraphs.flatMap { it.words }
|
||||||
|
|
||||||
|
val emailSegment = allSegments.filterIsInstance<EmailSegment>()
|
||||||
|
assertTrue("Should have EmailSegment", emailSegment.isNotEmpty())
|
||||||
|
assertTrue("Email should be user@example.com", emailSegment.any { it.segmentText == "user@example.com" })
|
||||||
|
|
||||||
|
val textSegments = allSegments.filterIsInstance<RegularTextSegment>()
|
||||||
|
assertTrue("Should have prefix ほむほむ", textSegments.any { it.segmentText == "ほむほむ" })
|
||||||
|
assertTrue("Should have suffix ほげほげ", textSegments.any { it.segmentText == "ほげほげ" })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testEmailWithSpaceAndMultibyteText() {
|
||||||
|
// user@example.com ふがふが → Email(user@example.com) + RegularText(ふがふが)
|
||||||
|
val text = "user@example.com ふがふが"
|
||||||
|
val state = RichTextParser().parseText(text, EmptyTagList, null)
|
||||||
|
val allSegments = state.paragraphs.flatMap { it.words }
|
||||||
|
|
||||||
|
val emailSegment = allSegments.filterIsInstance<EmailSegment>()
|
||||||
|
assertTrue("Should have EmailSegment", emailSegment.isNotEmpty())
|
||||||
|
assertTrue("Email should be user@example.com", emailSegment.any { it.segmentText == "user@example.com" })
|
||||||
|
|
||||||
|
val textSegments = allSegments.filterIsInstance<RegularTextSegment>()
|
||||||
|
assertTrue("Should have suffix ふがふが", textSegments.any { it.segmentText == "ふがふが" })
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user