feat(LocalFeedRepository): don't fetch ChannelInfo if latest channel video is already known

This commit is contained in:
Bnyro 2025-01-13 17:05:05 +01:00
parent c15352a33d
commit c9e7af52f0
2 changed files with 15 additions and 4 deletions

View File

@ -14,6 +14,9 @@ interface SubscriptionsFeedDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(feedItems: List<SubscriptionsFeedItem>)
@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)

View File

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