Skip to content

Commit

Permalink
wip Update read status on fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
jocmp committed Dec 24, 2024
1 parent 5c40c57 commit fe0201d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.jocmp.capy.common.TimeHelpers.nowUTC
import com.jocmp.capy.common.TimeHelpers.published
import com.jocmp.capy.common.transactionWithErrorHandling
import com.jocmp.capy.db.Database
import com.jocmp.capy.persistence.ArticleRecords
import com.jocmp.capy.persistence.FeedRecords
import com.jocmp.capy.persistence.TaggingRecords
import com.jocmp.feedfinder.DefaultFeedFinder
Expand All @@ -27,6 +28,7 @@ class LocalAccountDelegate(
private val feedFinder: FeedFinder = DefaultFeedFinder(httpClient),
) : AccountDelegate {
private val feedRecords = FeedRecords(database)
private val articleRecords = ArticleRecords(database)
private val taggingRecords = TaggingRecords(database)

override suspend fun refresh(cutoffDate: ZonedDateTime?): Result<Unit> {
Expand Down Expand Up @@ -146,8 +148,6 @@ class LocalAccountDelegate(
updatedAt: ZonedDateTime = nowUTC()
) {
database.transactionWithErrorHandling {
val updatedAtSeconds = updatedAt.toEpochSecond()

items.forEach { item ->
val publishedAt = published(item.pubDate, fallback = updatedAt).toEpochSecond()
val parsedItem = ParsedItem(
Expand All @@ -171,9 +171,9 @@ class LocalAccountDelegate(
published_at = publishedAt,
)

database.articlesQueries.updateStatus(
article_id = parsedItem.id,
updated_at = updatedAtSeconds,
articleRecords.updateStatus(
articleID = parsedItem.id,
updatedAt = updatedAt,
read = false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ internal class ReaderAccountDelegate(
private fun saveItems(items: List<Item>) {
database.transactionWithErrorHandling {
items.forEach { item ->
val updated = TimeHelpers.nowUTC().toEpochSecond()
val updated = TimeHelpers.nowUTC()

database.articlesQueries.create(
id = item.hexID,
Expand All @@ -348,10 +348,10 @@ internal class ReaderAccountDelegate(
published_at = item.published
)

database.articlesQueries.updateStatus(
article_id = item.hexID,
updated_at = updated,
read = true
articleRecords.updateStatus(
articleID = item.hexID,
updatedAt = updated,
read = item.read
)
}
}
Expand Down
19 changes: 18 additions & 1 deletion capy/src/main/java/com/jocmp/capy/persistence/ArticleRecords.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ internal class ArticleRecords internal constructor(
).executeAsOneOrNull()
}

fun updateStatus(articleID: String, updatedAt: ZonedDateTime, read: Boolean) {
val updatedAtSeconds = updatedAt.toEpochSecond()

val lastReadAt = if (read) {
updatedAtSeconds
} else {
null
}

database.articlesQueries.updateStatus(
article_id = articleID,
updated_at = updatedAtSeconds,
last_read_at = lastReadAt,
read = read,
)
}

fun findMissingArticles(): List<String> {
return database
.articlesQueries
Expand All @@ -56,7 +73,7 @@ internal class ArticleRecords internal constructor(
database.articlesQueries.deleteAllArticles()
}

fun deleteOldArticles(before: ZonedDateTime) {
fun deleteOldArticles(before: ZonedDateTime) {
database.transactionWithErrorHandling {
val maxDate = before.toEpochSecond()

Expand Down
8 changes: 7 additions & 1 deletion capy/src/main/sqldelight/com/jocmp/capy/db/articles.sq
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,20 @@ updateStatus:
INSERT INTO article_statuses(
article_id,
updated_at,
last_read_at,
read
)
VALUES (
:article_id,
:updated_at,
:last_read_at,
:read
)
ON CONFLICT(article_id) DO NOTHING;
ON CONFLICT(article_id) DO UPDATE
SET
updated_at = updated_at,
last_read_at = excluded.last_read_at,
read = excluded.read;

upsertUnread:
INSERT INTO article_statuses(
Expand Down
4 changes: 4 additions & 0 deletions readerclient/src/main/java/com/jocmp/readerclient/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ data class Item(
val content: Content? = null,
val author: String? = null,
val enclosure: List<Enclosure>? = null,
val categories: List<Category>? = null
) {
val hexID = id.split("/").last()

val read: Boolean
get() = categories?.any { it.id == Stream.READ.id } ?: true

@JsonClass(generateAdapter = true)
data class Origin(
val streamId: String,
Expand Down

0 comments on commit fe0201d

Please sign in to comment.