From c9e7af52f06a7c95721581b7eac1fe216b00c993 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 13 Jan 2025 17:05:05 +0100 Subject: [PATCH] feat(LocalFeedRepository): don't fetch ChannelInfo if latest channel video is already known --- .../libretube/db/dao/SubscriptionsFeedDao.kt | 3 +++ .../github/libretube/repo/LocalFeedRepository.kt | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/dao/SubscriptionsFeedDao.kt b/app/src/main/java/com/github/libretube/db/dao/SubscriptionsFeedDao.kt index 42f95b53a..80443b421 100644 --- a/app/src/main/java/com/github/libretube/db/dao/SubscriptionsFeedDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/SubscriptionsFeedDao.kt @@ -14,6 +14,9 @@ interface SubscriptionsFeedDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun insertAll(feedItems: List) + @Query("SELECT EXISTS (SELECT * FROM feedItem WHERE videoId = :videoId)") + suspend fun contains(videoId: String): Boolean + @Query("DELETE FROM feedItem WHERE uploaded < :olderThan") suspend fun cleanUpOlderThan(olderThan: Long) diff --git a/app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt b/app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt index ffc91bd2d..af96457e0 100644 --- a/app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt +++ b/app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt @@ -8,6 +8,7 @@ import com.github.libretube.constants.PreferenceKeys import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.SubscriptionsFeedItem import com.github.libretube.extensions.parallelMap +import com.github.libretube.extensions.toID import com.github.libretube.helpers.NewPipeExtractorInstance import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_URL @@ -73,10 +74,17 @@ class LocalFeedRepository : FeedRepository { val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}" val feedInfo = FeedInfo.getInfo(channelUrl) - val hasNewerUploads = feedInfo.relatedItems.any { - (it.uploadDate?.offsetDateTime()?.toInstant()?.toEpochMilli() - ?: 0) > minimumDateMillis - } + val mostRecentChannelVideo = feedInfo.relatedItems.maxBy { + it.uploadDate?.offsetDateTime()?.toInstant()?.toEpochMilli() ?: 0 + } ?: return emptyList() + + // check if the channel has at least one video whose upload time is newer than the maximum + // feed ago and which is not yet stored in the database + val mostRecentUploadTime = + mostRecentChannelVideo.uploadDate?.offsetDateTime()?.toInstant()?.toEpochMilli() ?: 0 + val hasNewerUploads = + mostRecentUploadTime > minimumDateMillis && !DatabaseHolder.Database.feedDao() + .contains(mostRecentChannelVideo.url.replace(YOUTUBE_FRONTEND_URL, "").toID()) if (!hasNewerUploads) return emptyList() val channelInfo = ChannelInfo.getInfo(channelUrl)