Fixes "author_is_elon" bug

pull/327/head
Vitor Pamplona 2023-03-31 16:59:23 -04:00
rodzic 22adbddd88
commit 737244fb60
4 zmienionych plików z 10 dodań i 41 usunięć

Wyświetl plik

@ -3,10 +3,6 @@ package com.vitorpamplona.amethyst.model
import com.baha.url.preview.BahaUrlPreview import com.baha.url.preview.BahaUrlPreview
import com.baha.url.preview.IUrlPreviewCallback import com.baha.url.preview.IUrlPreviewCallback
import com.baha.url.preview.UrlInfoItem import com.baha.url.preview.UrlInfoItem
import com.vitorpamplona.amethyst.ui.components.imageExtension
import com.vitorpamplona.amethyst.ui.components.isValidURL
import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator
import com.vitorpamplona.amethyst.ui.components.videoExtension
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@ -47,29 +43,4 @@ object UrlCachedPreviewer {
).fetchUrlPreview() ).fetchUrlPreview()
} }
} }
fun findUrlsInMessage(message: String): List<String> {
return message.split('\n').map { paragraph ->
paragraph.split(' ').filter { word: String ->
isValidURL(word) || noProtocolUrlValidator.matcher(word).matches()
}
}.flatten()
}
fun preloadPreviewsFor(note: Note) {
note.event?.content()?.let {
findUrlsInMessage(it).forEach {
val removedParamsFromUrl = it.split("?")[0].lowercase()
if (imageExtension.matcher(removedParamsFromUrl).matches()) {
// Preload Images? Isn't this too heavy?
} else if (videoExtension.matcher(removedParamsFromUrl).matches()) {
// Do nothing for now.
} else if (isValidURL(removedParamsFromUrl)) {
previewInfo(it)
} else {
previewInfo("https://$it")
}
}
}
}
} }

Wyświetl plik

@ -167,7 +167,7 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
if (isValidURL(myUrlPreview)) { if (isValidURL(myUrlPreview)) {
val removedParamsFromUrl = val removedParamsFromUrl =
myUrlPreview.split("?")[0].lowercase() myUrlPreview.split("?")[0].lowercase()
if (imageExtension.matcher(removedParamsFromUrl).matches()) { if (imageExtensions.any { removedParamsFromUrl.endsWith(it, true) }) {
AsyncImage( AsyncImage(
model = myUrlPreview, model = myUrlPreview,
contentDescription = myUrlPreview, contentDescription = myUrlPreview,
@ -182,9 +182,7 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
RoundedCornerShape(15.dp) RoundedCornerShape(15.dp)
) )
) )
} else if (videoExtension.matcher(removedParamsFromUrl) } else if (videoExtensions.any { removedParamsFromUrl.endsWith(it, true) }) {
.matches()
) {
VideoView(myUrlPreview) VideoView(myUrlPreview)
} else { } else {
UrlPreview(myUrlPreview, myUrlPreview) UrlPreview(myUrlPreview, myUrlPreview)

Wyświetl plik

@ -46,8 +46,8 @@ import java.net.URISyntaxException
import java.net.URL import java.net.URL
import java.util.regex.Pattern import java.util.regex.Pattern
val imageExtension = Pattern.compile("(.*/)*.+\\.(png|jpg|gif|bmp|jpeg|webp|svg)$", Pattern.CASE_INSENSITIVE) val imageExtensions = listOf("png", "jpg", "gif", "bmp", "jpeg", "webp", "svg")
val videoExtension = Pattern.compile("(.*/)*.+\\.(mp4|avi|wmv|mpg|amv|webm|mov)$", Pattern.CASE_INSENSITIVE) val videoExtensions = listOf("mp4", "avi", "wmv", "mpg", "amv", "webm", "mov")
// Group 1 = url, group 4 additional chars // Group 1 = url, group 4 additional chars
val noProtocolUrlValidator = Pattern.compile("(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+)*\\/?)(.*)") val noProtocolUrlValidator = Pattern.compile("(([\\w\\d-]+\\.)*[a-zA-Z][\\w-]+[\\.\\:]\\w+([\\/\\?\\=\\&\\#\\.]?[\\w-]+)*\\/?)(.*)")
@ -138,10 +138,10 @@ fun RichTextViewer(
// sequence of images will render in a slideview // sequence of images will render in a slideview
if (isValidURL(word)) { if (isValidURL(word)) {
val removedParamsFromUrl = word.split("?")[0].lowercase() val removedParamsFromUrl = word.split("?")[0].lowercase()
if (imageExtension.matcher(removedParamsFromUrl).matches()) { if (imageExtensions.any { word.endsWith(it, true) }) {
imagesForPager.add(word) imagesForPager.add(word)
} }
if (videoExtension.matcher(removedParamsFromUrl).matches()) { if (videoExtensions.any { word.endsWith(it, true) }) {
imagesForPager.add(word) imagesForPager.add(word)
} }
} }
@ -160,9 +160,9 @@ fun RichTextViewer(
if (isValidURL(word)) { if (isValidURL(word)) {
val removedParamsFromUrl = word.split("?")[0].lowercase() val removedParamsFromUrl = word.split("?")[0].lowercase()
if (imageExtension.matcher(removedParamsFromUrl).matches()) { if (imageExtensions.any { word.endsWith(it, true) }) {
ZoomableImageView(word, imagesForPager) ZoomableImageView(word, imagesForPager)
} else if (videoExtension.matcher(removedParamsFromUrl).matches()) { } else if (videoExtensions.any { word.endsWith(it, true) }) {
ZoomableImageView(word, imagesForPager) ZoomableImageView(word, imagesForPager)
} else { } else {
UrlPreview(word, "$word ") UrlPreview(word, "$word ")

Wyświetl plik

@ -62,7 +62,7 @@ fun ZoomableImageView(word: String, images: List<String> = listOf(word)) {
mutableStateOf<AsyncImagePainter.State?>(null) mutableStateOf<AsyncImagePainter.State?>(null)
} }
if (imageExtension.matcher(word).matches()) { if (imageExtensions.any { word.endsWith(it, true) }) {
AsyncImage( AsyncImage(
model = word, model = word,
contentDescription = word, contentDescription = word,
@ -171,7 +171,7 @@ fun ZoomableImageDialog(imageUrl: String, allImages: List<String> = listOf(image
@Composable @Composable
private fun RenderImageOrVideo(imageUrl: String) { private fun RenderImageOrVideo(imageUrl: String) {
if (imageExtension.matcher(imageUrl).matches()) { if (imageExtensions.any { imageUrl.endsWith(it, true) }) {
AsyncImage( AsyncImage(
model = imageUrl, model = imageUrl,
contentDescription = stringResource(id = R.string.profile_image), contentDescription = stringResource(id = R.string.profile_image),