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

This commit is contained in:
Ash 2024-03-08 12:40:29 +08:00 committed by GitHub
parent f06d8ce05e
commit d40743d5ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 9 deletions

View File

@ -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) {
@ -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) {

View File

@ -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
@ -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()
@ -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 ?: "",
@ -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()
}

View File

@ -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
@ -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 ?: "",

View File

@ -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
@ -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
@ -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) {
@ -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
@ -108,7 +111,7 @@ 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) ?: "",
@ -116,7 +119,7 @@ class RssHelper @Inject constructor(
fullContent = content,
img = findImg((content ?: desc) ?: ""),
link = syndEntry.link ?: "",
updateAt = Date(),
updateAt = preDate,
)
}

View File

@ -86,3 +86,5 @@ private fun String.parseToDate(
}
return null
}
fun Date.isFuture(staticDate: Date = Date()): Boolean = this.time > staticDate.time