Skip to content

Commit

Permalink
Fall back to article ID when URL is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Oct 27, 2024
1 parent adb3bae commit aa86b14
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ class LocalAccountDelegate(

val withinCutoff = cutoffDate == null || publishedAt > cutoffDate.toEpochSecond()

if (parsedItem.url != null && withinCutoff) {
if (parsedItem.id != null && withinCutoff) {
database.articlesQueries.create(
id = item.link!!,
id = parsedItem.id,
feed_id = feed.id,
title = parsedItem.title,
author = item.author,
Expand All @@ -179,7 +179,7 @@ class LocalAccountDelegate(
)

database.articlesQueries.updateStatus(
article_id = item.link!!,
article_id = parsedItem.id,
updated_at = updatedAtSeconds,
read = false
)
Expand Down
8 changes: 5 additions & 3 deletions capy/src/main/java/com/jocmp/capy/accounts/ParsedItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import java.net.URI
import java.net.URL

internal class ParsedItem(private val item: RssItem, private val siteURL: String?) {
val url: String? = cleanedURL(item.link)?.toString()

val id: String? = url ?: item.guid

val contentHTML: String?
get() {
val currentContent = item.content.orEmpty().ifBlank {
Expand All @@ -27,12 +31,10 @@ internal class ParsedItem(private val item: RssItem, private val siteURL: String
Jsoup.parse(it).text()
}
}

val title: String
get() = Jsoup.parse(item.title.orEmpty()).text()

val url: String? = cleanedURL(item.link)?.toString()

val imageURL: String?
get() = cleanedURL(item.image)?.toString()

Expand Down
28 changes: 28 additions & 0 deletions capy/src/test/java/com/jocmp/capy/accounts/ParsedItemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.jocmp.capy.accounts
import com.prof18.rssparser.model.RssItem
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class ParsedItemTest {
@Test
Expand Down Expand Up @@ -31,6 +32,33 @@ class ParsedItemTest {
assertEquals(expected = "", actual = parsedItem.title)
}

@Test
fun id_whenUrlIsPresent() {
val url = "https://example.com/article"
val item = RssItem.Builder().link(url).build()
val parsedItem = ParsedItem(item, siteURL = "")

assertEquals(expected = url, actual = parsedItem.id)
}

@Test
fun id_whenUrlIsMissing() {
val id = "https://example.com/article"
val item = RssItem.Builder().guid(id).build()
val parsedItem = ParsedItem(item, siteURL = "")

assertEquals(expected = id, actual = parsedItem.id)
}

@Test
fun id_whenURLAndGuidAreMissing() {
val item = RssItem.Builder().build()
val parsedItem = ParsedItem(item, siteURL = "")

assertNull(parsedItem.id)
}


@Test
fun url_whenPresent() {
val url = "https://example.com/article"
Expand Down

0 comments on commit aa86b14

Please sign in to comment.