Skip to content

Commit

Permalink
Always use absolute img url
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Sep 10, 2024
1 parent eda7e09 commit a5a2912
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion capy/src/main/assets/stylesheet.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions capy/src/main/java/com/jocmp/capy/Article.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ data class Article(
val feedName: String = "",
val faviconURL: String? = null,
val feedURL: String? = null,
val siteURL: String? = null,
val enableStickyFullContent: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.jocmp.capy.persistence.TaggingRecords
import com.jocmp.feedfinder.DefaultFeedFinder
import com.jocmp.feedfinder.FeedFinder
import com.prof18.rssparser.model.RssItem
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -153,7 +152,7 @@ class LocalAccountDelegate(
val updated = updatedAt.toEpochSecond()

val publishedAt = item.pubDate?.toDateTime?.toEpochSecond() ?: updated
val url = cleanedArticleLink(item.link, feed.siteURL)
val url = cleanedURL(item.link, feed.siteURL)
val withinCutoff = cutoffDate == null || publishedAt > cutoffDate.toEpochSecond()

if (url != null && withinCutoff) {
Expand All @@ -166,7 +165,7 @@ class LocalAccountDelegate(
url = url.toString(),
summary = item.summary,
extracted_content_url = null,
image_url = item.image,
image_url = cleanedURL(item.image, siteURL = feed.siteURL)?.toString(),
published_at = publishedAt,
)

Expand Down Expand Up @@ -238,8 +237,8 @@ internal val RssItem.summary: String?
return Jsoup.parse(it).text()
}

internal fun cleanedArticleLink(articleURL: String?, siteURL: String): URL? {
val url = articleURL.orEmpty()
internal fun cleanedURL(inputURL: String?, siteURL: String): URL? {
val url = inputURL.orEmpty()

if (url.isBlank()) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ArticleRenderer(
).renderedText

val document = Jsoup.parse(html).apply {
article.feedURL?.let { setBaseUri(it) }
article.siteURL?.let { setBaseUri(it) }
}

cleanLinks(document)
Expand All @@ -53,6 +53,7 @@ class ArticleRenderer(
private fun cleanLinks(document: Document) {
document.getElementsByTag("img").forEach { element ->
element.attr("loading", "lazy")
element.attr("src", element.absUrl("src"))
}

document.select("img[data-src]").forEach { element ->
Expand Down
2 changes: 1 addition & 1 deletion capy/src/main/java/com/jocmp/capy/common/OptionalURL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.jocmp.capy.common
import java.net.MalformedURLException
import java.net.URL

fun optionalURL(string: String?): URL? {
fun optionalURL(string: String?, baseURL: String? = null): URL? {
if (string.isNullOrBlank()) {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal fun articleMapper(
faviconURL: String?,
enableStickyContent: Boolean = false,
feedURL: String?,
siteURL: String?,
updatedAt: Long?,
starred: Boolean?,
read: Boolean?,
Expand All @@ -35,6 +36,7 @@ internal fun articleMapper(
url = URL(url),
imageURL = imageURL,
feedURL = feedURL,
siteURL = siteURL,
summary = summary ?: "",
updatedAt = updatedAt!!.toDateTimeFromSeconds,
publishedAt = publishedAt!!.toDateTimeFromSeconds,
Expand Down Expand Up @@ -70,6 +72,7 @@ internal fun listMapper(
contentHtml = "",
extractedContentURL = extractedContentURL,
feedURL = null,
siteURL = null,
url = url,
feedTitle = feedTitle,
imageURL = imageURL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ internal class ArticleRecords internal constructor(
}
}

internal fun cutoffDate(): ZonedDateTime {
private fun cutoffDate(): ZonedDateTime {
return nowUTC().minusMonths(3)
}
}
Expand Down
3 changes: 0 additions & 3 deletions capy/src/main/res/raw/template.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions capy/src/main/sqldelight/com/jocmp/capy/db/articles.sq
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ SELECT
feeds.favicon_url,
feeds.enable_sticky_full_content,
feeds.feed_url,
feeds.site_url,
article_statuses.updated_at,
article_statuses.starred,
article_statuses.read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ class LocalAccountDelegateTest {
override val name: String,
override val feedURL: URL,
override val siteURL: URL? = null,
override val faviconURL: URL? = null
override val faviconURL: URL? = null,
override val items: List<RssItem> = listOf()
) : Feed {
override fun isValid() = true
}
Expand All @@ -294,42 +295,42 @@ class LocalAccountDelegateTest {
fun cleanedArticleLink_whenPresent() {
val url = "https://example.com/article"

val result = cleanedArticleLink(articleURL = url, siteURL = "")
val result = cleanedURL(inputURL = url, siteURL = "")

assertEquals(expected = url, actual = result.toString())
}

@Test
fun cleanedArticleLink_whenNull() {
val result = cleanedArticleLink(articleURL = null, siteURL = "")
val result = cleanedURL(inputURL = null, siteURL = "")

assertEquals(expected = null, actual = result)
}

@Test
fun cleanedArticleLink_whenBlank() {
val result = cleanedArticleLink(articleURL = "", siteURL = "")
val result = cleanedURL(inputURL = "", siteURL = "")

assertEquals(expected = null, actual = result)
}

@Test
fun cleanedArticleLink_withRelativePathMissingSiteURL() {
val result = cleanedArticleLink(articleURL = "/article", siteURL = "")
val result = cleanedURL(inputURL = "/article", siteURL = "")

assertEquals(expected = null, actual = result)
}

@Test
fun cleanedArticleLink_withRelativePathAndInvalidSiteURL() {
val result = cleanedArticleLink(articleURL = "/article", siteURL = "wrong")
val result = cleanedURL(inputURL = "/article", siteURL = "wrong")

assertEquals(expected = null, actual = result)
}

@Test
fun cleanedArticleLink_withRelativePathAndValidSiteURL() {
val result = cleanedArticleLink(articleURL = "/article", siteURL = "https://example.com")
val result = cleanedURL(inputURL = "/article", siteURL = "https://example.com")

assertEquals(expected = "https://example.com/article", actual = result.toString())
}
Expand Down

0 comments on commit a5a2912

Please sign in to comment.