Refactoring some names for the parsers.

This commit is contained in:
Vitor Pamplona
2024-02-21 16:22:17 -05:00
parent 90175198f0
commit 96f29fc5ca
12 changed files with 154 additions and 134 deletions

View File

@@ -20,12 +20,12 @@
*/
package com.vitorpamplona.amethyst.commons
class ExpandableTextParser {
class ExpandableTextCutOffCalculator {
companion object {
private const val SHORT_TEXT_LENGTH = 350
private const val SHORTEN_AFTER_LINES = 10
fun computeWhereToCutIfPostIsTooLong(content: String): Int {
fun indexToCutOff(content: String): Int {
// Cuts the text in the first space or new line after SHORT_TEXT_LENGTH characters
val firstSpaceAfterCut =
content.indexOf(' ', SHORT_TEXT_LENGTH).let { if (it < 0) content.length else it }

View File

@@ -24,33 +24,35 @@ import androidx.compose.runtime.Immutable
import java.io.File
@Immutable
abstract class ZoomableContent(
abstract class BaseMediaContent(
val description: String? = null,
val dim: String? = null,
val blurhash: String? = null,
)
@Immutable
abstract class ZoomableUrlContent(
abstract class MediaUrlContent(
val url: String,
description: String? = null,
val hash: String? = null,
dim: String? = null,
blurhash: String? = null,
val uri: String? = null,
) : ZoomableContent(description, dim)
) : BaseMediaContent(description, dim, blurhash)
@Immutable
class ZoomableUrlImage(
class MediaUrlImage(
url: String,
description: String? = null,
hash: String? = null,
val blurhash: String? = null,
blurhash: String? = null,
dim: String? = null,
uri: String? = null,
val contentWarning: String? = null,
) : ZoomableUrlContent(url, description, hash, dim, uri)
) : MediaUrlContent(url, description, hash, dim, blurhash, uri)
@Immutable
class ZoomableUrlVideo(
class MediaUrlVideo(
url: String,
description: String? = null,
hash: String? = null,
@@ -58,41 +60,43 @@ class ZoomableUrlVideo(
uri: String? = null,
val artworkUri: String? = null,
val authorName: String? = null,
val blurhash: String? = null,
blurhash: String? = null,
val contentWarning: String? = null,
) : ZoomableUrlContent(url, description, hash, dim, uri)
) : MediaUrlContent(url, description, hash, dim, blurhash, uri)
@Immutable
abstract class ZoomablePreloadedContent(
abstract class MediaPreloadedContent(
val localFile: File?,
description: String? = null,
val mimeType: String? = null,
val isVerified: Boolean? = null,
dim: String? = null,
blurhash: String? = null,
val uri: String,
) : ZoomableContent(description, dim) {
) : BaseMediaContent(description, dim, blurhash) {
fun localFileExists() = localFile != null && localFile.exists()
}
@Immutable
class ZoomableLocalImage(
class MediaLocalImage(
localFile: File?,
mimeType: String? = null,
description: String? = null,
val blurhash: String? = null,
dim: String? = null,
blurhash: String? = null,
isVerified: Boolean? = null,
uri: String,
) : ZoomablePreloadedContent(localFile, description, mimeType, isVerified, dim, uri)
) : MediaPreloadedContent(localFile, description, mimeType, isVerified, dim, blurhash, uri)
@Immutable
class ZoomableLocalVideo(
class MediaLocalVideo(
localFile: File?,
mimeType: String? = null,
description: String? = null,
dim: String? = null,
blurhash: String? = null,
isVerified: Boolean? = null,
uri: String,
val artworkUri: String? = null,
val authorName: String? = null,
) : ZoomablePreloadedContent(localFile, description, mimeType, isVerified, dim, uri)
) : MediaPreloadedContent(localFile, description, mimeType, isVerified, dim, blurhash, uri)

View File

@@ -44,13 +44,13 @@ class RichTextParser() {
fun parseMediaUrl(
fullUrl: String,
eventTags: ImmutableListOfLists<String>,
): ZoomableUrlContent? {
): MediaUrlContent? {
val removedParamsFromUrl = removeQueryParamsForExtensionComparison(fullUrl)
return if (imageExtensions.any { removedParamsFromUrl.endsWith(it) }) {
val frags = Nip54().parse(fullUrl)
val tags = Nip92().parse(fullUrl, eventTags.lists)
ZoomableUrlImage(
MediaUrlImage(
url = fullUrl,
description = frags[FileHeaderEvent.ALT] ?: tags[FileHeaderEvent.ALT],
hash = frags[FileHeaderEvent.HASH] ?: tags[FileHeaderEvent.HASH],
@@ -61,7 +61,7 @@ class RichTextParser() {
} else if (videoExtensions.any { removedParamsFromUrl.endsWith(it) }) {
val frags = Nip54().parse(fullUrl)
val tags = Nip92().parse(fullUrl, eventTags.lists)
ZoomableUrlVideo(
MediaUrlVideo(
url = fullUrl,
description = frags[FileHeaderEvent.ALT] ?: tags[FileHeaderEvent.ALT],
hash = frags[FileHeaderEvent.HASH] ?: tags[FileHeaderEvent.HASH],
@@ -289,7 +289,9 @@ class RichTextParser() {
val hashTagsPattern: Pattern =
Pattern.compile("#([^\\s!@#\$%^&*()=+./,\\[{\\]};:'\"?><]+)(.*)", Pattern.CASE_INSENSITIVE)
fun removeQueryParamsForExtensionComparison(fullUrl: String): String {
val acceptedNIP19schemes = listOf("npub1", "naddr1", "note1", "nprofile1", "nevent1")
private fun removeQueryParamsForExtensionComparison(fullUrl: String): String {
return if (fullUrl.contains("?")) {
fullUrl.split("?")[0].lowercase()
} else if (fullUrl.contains("#")) {
@@ -327,24 +329,24 @@ class RichTextParser() {
}
}
fun parseImageOrVideo(fullUrl: String): ZoomableContent {
fun parseImageOrVideo(fullUrl: String): BaseMediaContent {
val removedParamsFromUrl = removeQueryParamsForExtensionComparison(fullUrl)
val isImage = imageExtensions.any { removedParamsFromUrl.endsWith(it) }
val isVideo = videoExtensions.any { removedParamsFromUrl.endsWith(it) }
return if (isImage) {
ZoomableUrlImage(fullUrl)
MediaUrlImage(fullUrl)
} else if (isVideo) {
ZoomableUrlVideo(fullUrl)
MediaUrlVideo(fullUrl)
} else {
ZoomableUrlImage(fullUrl)
MediaUrlImage(fullUrl)
}
}
fun startsWithNIP19Scheme(word: String): Boolean {
val cleaned = word.lowercase().removePrefix("@").removePrefix("nostr:").removePrefix("@")
return listOf("npub1", "naddr1", "note1", "nprofile1", "nevent1").any { cleaned.startsWith(it) }
return acceptedNIP19schemes.any { cleaned.startsWith(it) }
}
fun isUrlWithoutScheme(url: String) = noProtocolUrlValidator.matcher(url).matches()

View File

@@ -28,8 +28,8 @@ import kotlinx.collections.immutable.ImmutableSet
@Immutable
data class RichTextViewerState(
val urlSet: ImmutableSet<String>,
val imagesForPager: ImmutableMap<String, ZoomableUrlContent>,
val imageList: ImmutableList<ZoomableUrlContent>,
val imagesForPager: ImmutableMap<String, MediaUrlContent>,
val imageList: ImmutableList<MediaUrlContent>,
val customEmoji: ImmutableMap<String, String>,
val paragraphs: ImmutableList<ParagraphState>,
)