From e5eaa63f6860b1983ea5a1008e482fd7a995abc7 Mon Sep 17 00:00:00 2001 From: Ash Date: Fri, 8 Mar 2024 12:38:35 +0800 Subject: [PATCH] fix(sync): replace publish date of an article with the current time if it is a future date --- .../ash/reader/domain/service/AbstractRssRepository.kt | 7 ++++--- .../java/me/ash/reader/domain/service/FeverRssService.kt | 9 +++++++-- .../ash/reader/domain/service/GoogleReaderRssService.kt | 6 +++++- .../java/me/ash/reader/infrastructure/rss/RssHelper.kt | 9 ++++++--- app/src/main/java/me/ash/reader/ui/ext/DateExt.kt | 2 ++ 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/me/ash/reader/domain/service/AbstractRssRepository.kt b/app/src/main/java/me/ash/reader/domain/service/AbstractRssRepository.kt index 0a2a76bf8..566f2511e 100644 --- a/app/src/main/java/me/ash/reader/domain/service/AbstractRssRepository.kt +++ b/app/src/main/java/me/ash/reader/domain/service/AbstractRssRepository.kt @@ -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) { diff --git a/app/src/main/java/me/ash/reader/domain/service/FeverRssService.kt b/app/src/main/java/me/ash/reader/domain/service/FeverRssService.kt index ec7aa99e9..a2972dc5a 100644 --- a/app/src/main/java/me/ash/reader/domain/service/FeverRssService.kt +++ b/app/src/main/java/me/ash/reader/domain/service/FeverRssService.kt @@ -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() } diff --git a/app/src/main/java/me/ash/reader/domain/service/GoogleReaderRssService.kt b/app/src/main/java/me/ash/reader/domain/service/GoogleReaderRssService.kt index 7510ee2c9..cf3e474bd 100644 --- a/app/src/main/java/me/ash/reader/domain/service/GoogleReaderRssService.kt +++ b/app/src/main/java/me/ash/reader/domain/service/GoogleReaderRssService.kt @@ -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 ?: "", diff --git a/app/src/main/java/me/ash/reader/infrastructure/rss/RssHelper.kt b/app/src/main/java/me/ash/reader/infrastructure/rss/RssHelper.kt index a2db73f73..e35dcf646 100644 --- a/app/src/main/java/me/ash/reader/infrastructure/rss/RssHelper.kt +++ b/app/src/main/java/me/ash/reader/infrastructure/rss/RssHelper.kt @@ -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
= 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, ) } diff --git a/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt b/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt index 0717a280e..00126b363 100644 --- a/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt +++ b/app/src/main/java/me/ash/reader/ui/ext/DateExt.kt @@ -86,3 +86,5 @@ private fun String.parseToDate( } return null } + +fun Date.isFuture(staticDate: Date = Date()): Boolean = this.time > staticDate.time