2023-01-13 14:39:03 +00:00
|
|
|
package com.vitorpamplona.amethyst.model
|
|
|
|
|
|
|
|
import com.baha.url.preview.BahaUrlPreview
|
|
|
|
import com.baha.url.preview.IUrlPreviewCallback
|
|
|
|
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
|
2023-01-18 19:24:04 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.launch
|
2023-01-13 14:39:03 +00:00
|
|
|
|
|
|
|
object UrlCachedPreviewer {
|
2023-02-03 22:23:47 +00:00
|
|
|
var cache = mapOf<String, UrlInfoItem>()
|
|
|
|
private set
|
|
|
|
var failures = mapOf<String, Throwable>()
|
|
|
|
private set
|
2023-01-13 14:39:03 +00:00
|
|
|
|
|
|
|
fun previewInfo(url: String, callback: IUrlPreviewCallback? = null) {
|
|
|
|
cache[url]?.let {
|
|
|
|
callback?.onComplete(it)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-25 02:09:03 +00:00
|
|
|
failures[url]?.let {
|
|
|
|
callback?.onFailed(it)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-19 13:41:48 +00:00
|
|
|
val scope = CoroutineScope(Job() + Dispatchers.IO)
|
2023-01-18 19:24:04 +00:00
|
|
|
scope.launch {
|
|
|
|
BahaUrlPreview(url, object : IUrlPreviewCallback {
|
|
|
|
override fun onComplete(urlInfo: UrlInfoItem) {
|
2023-02-03 22:23:47 +00:00
|
|
|
cache = cache + Pair(url, urlInfo)
|
2023-01-18 19:24:04 +00:00
|
|
|
callback?.onComplete(urlInfo)
|
|
|
|
}
|
2023-01-13 14:39:03 +00:00
|
|
|
|
2023-01-18 19:24:04 +00:00
|
|
|
override fun onFailed(throwable: Throwable) {
|
2023-02-03 22:23:47 +00:00
|
|
|
failures = failures + Pair(url, throwable)
|
2023-01-18 19:24:04 +00:00
|
|
|
callback?.onFailed(throwable)
|
|
|
|
}
|
|
|
|
}).fetchUrlPreview()
|
|
|
|
}
|
2023-01-13 14:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-03-05 21:42:19 +00:00
|
|
|
note.event?.content()?.let {
|
2023-01-13 14:39:03 +00:00
|
|
|
findUrlsInMessage(it).forEach {
|
2023-03-04 10:27:27 +00:00
|
|
|
val removedParamsFromUrl = it.split("?")[0].lowercase()
|
2023-01-13 14:39:03 +00:00
|
|
|
if (imageExtension.matcher(removedParamsFromUrl).matches()) {
|
|
|
|
// Preload Images? Isn't this too heavy?
|
|
|
|
} else if (videoExtension.matcher(removedParamsFromUrl).matches()) {
|
|
|
|
// Do nothing for now.
|
2023-01-25 02:09:03 +00:00
|
|
|
} else if (isValidURL(removedParamsFromUrl)) {
|
2023-01-13 14:39:03 +00:00
|
|
|
previewInfo(it)
|
2023-01-25 02:09:03 +00:00
|
|
|
} else {
|
|
|
|
previewInfo("https://${it}")
|
2023-01-13 14:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-04 10:27:27 +00:00
|
|
|
}
|