Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sync): replace publish date of an article with the current time if it is a future date #638

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ abstract class AbstractRssRepository(
supervisorScope {
coroutineWorker.setProgress(SyncWorker.setIsSyncing(true))
val preTime = System.currentTimeMillis()
val preDate = Date(preTime)
val accountId = context.currentAccountId
feedDao.queryAll(accountId)
.chunked(16)
.forEach {
it.map { feed -> async { syncFeed(feed) } }
it.map { feed -> async { syncFeed(feed, preDate) } }
.awaitAll()
.forEach {
if (it.feed.isNotification) {
Expand Down Expand Up @@ -165,9 +166,9 @@ abstract class AbstractRssRepository(
articleDao.markAsStarredByArticleId(accountId, articleId, isStarred)
}

private suspend fun syncFeed(feed: Feed): FeedWithArticle {
private suspend fun syncFeed(feed: Feed, preDate: Date = Date()): FeedWithArticle {
val latest = articleDao.queryLatestByFeedId(context.currentAccountId, feed.id)
val articles = rssHelper.queryRssXml(feed, latest?.link)
val articles = rssHelper.queryRssXml(feed, latest?.link, preDate)
if (feed.icon == null) {
val iconLink = rssHelper.queryRssIconLink(feed.url)
if (iconLink != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import me.ash.reader.infrastructure.rss.provider.fever.FeverDTO
import me.ash.reader.ui.ext.currentAccountId
import me.ash.reader.ui.ext.decodeHTML
import me.ash.reader.ui.ext.dollarLast
import me.ash.reader.ui.ext.isFuture
import me.ash.reader.ui.ext.showToast
import me.ash.reader.ui.ext.spacerDollar
import java.util.Date
Expand Down Expand Up @@ -137,6 +138,7 @@ class FeverRssService @Inject constructor(

try {
val preTime = System.currentTimeMillis()
val preDate = Date(preTime)
val accountId = context.currentAccountId
val account = accountDao.queryById(accountId)!!
val feverAPI = getFeverAPI()
Expand Down Expand Up @@ -192,7 +194,10 @@ class FeverRssService @Inject constructor(
*itemsBody.items?.map {
Article(
id = accountId.spacerDollar(it.id!!),
date = it.created_on_time?.run { Date(this * 1000) } ?: Date(),
date = it.created_on_time
?.run { Date(this * 1000) }
?.takeIf { !it.isFuture(preDate) }
?: preDate,
title = it.title.decodeHTML() ?: context.getString(R.string.empty),
author = it.author,
rawDescription = it.html ?: "",
Expand All @@ -204,7 +209,7 @@ class FeverRssService @Inject constructor(
accountId = accountId,
isUnread = (it.is_read ?: 0) <= 0,
isStarred = (it.is_saved ?: 0) > 0,
updateAt = Date(),
updateAt = preDate,
).also {
sinceId = it.id.dollarLast()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import me.ash.reader.infrastructure.rss.provider.greader.GoogleReaderDTO
import me.ash.reader.ui.ext.currentAccountId
import me.ash.reader.ui.ext.decodeHTML
import me.ash.reader.ui.ext.dollarLast
import me.ash.reader.ui.ext.isFuture
import me.ash.reader.ui.ext.showToast
import me.ash.reader.ui.ext.spacerDollar
import java.util.Calendar
Expand Down Expand Up @@ -405,7 +406,10 @@ class GoogleReaderRssService @Inject constructor(
val articleId = it.id!!.ofItemStreamIdToId()
Article(
id = accountId.spacerDollar(articleId),
date = it.published?.run { Date(this * 1000) } ?: preDate,
date = it.published
?.run { Date(this * 1000) }
?.takeIf { !it.isFuture(preDate) }
?: preDate,
title = it.title.decodeHTML() ?: context.getString(R.string.empty),
author = it.author,
rawDescription = it.summary?.content ?: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import me.ash.reader.infrastructure.di.IODispatcher
import me.ash.reader.infrastructure.html.Readability
import me.ash.reader.ui.ext.currentAccountId
import me.ash.reader.ui.ext.decodeHTML
import me.ash.reader.ui.ext.isFuture
import me.ash.reader.ui.ext.spacerDollar
import okhttp3.OkHttpClient
import okhttp3.Request
Expand Down Expand Up @@ -67,6 +68,7 @@ class RssHelper @Inject constructor(
suspend fun queryRssXml(
feed: Feed,
latestLink: String?,
preDate: Date = Date(),
): List<Article> =
try {
val accountId = context.currentAccountId
Expand All @@ -76,7 +78,7 @@ class RssHelper @Inject constructor(
.entries
.asSequence()
.takeWhile { latestLink == null || latestLink != it.link }
.map { buildArticleFromSyndEntry(feed, accountId, it) }
.map { buildArticleFromSyndEntry(feed, accountId, it, preDate) }
.toList()
}
} catch (e: Exception) {
Expand All @@ -89,6 +91,7 @@ class RssHelper @Inject constructor(
feed: Feed,
accountId: Int,
syndEntry: SyndEntry,
preDate: Date = Date(),
): Article {
val desc = syndEntry.description?.value
val content = syndEntry.contents
Expand All @@ -108,15 +111,15 @@ class RssHelper @Inject constructor(
id = accountId.spacerDollar(UUID.randomUUID().toString()),
accountId = accountId,
feedId = feed.id,
date = syndEntry.publishedDate ?: syndEntry.updatedDate ?: Date(),
date = (syndEntry.publishedDate ?: syndEntry.updatedDate).takeIf { !it.isFuture(preDate) } ?: preDate,
title = syndEntry.title.decodeHTML() ?: feed.name,
author = syndEntry.author,
rawDescription = (content ?: desc) ?: "",
shortDescription = Readability.parseToText(desc ?: content, syndEntry.link).take(110),
fullContent = content,
img = findImg((content ?: desc) ?: ""),
link = syndEntry.link ?: "",
updateAt = Date(),
updateAt = preDate,
)
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/me/ash/reader/ui/ext/DateExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ private fun String.parseToDate(
}
return null
}

fun Date.isFuture(staticDate: Date = Date()): Boolean = this.time > staticDate.time
Loading