From 5e6a42c629d3d66a0c5d8604059986a465740ed8 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 11 Jan 2025 11:41:13 +0100 Subject: [PATCH] fix: auto-delete feed videos when unsubscribing from channel when using local feed extraction --- .../github/libretube/db/dao/SubscriptionsFeedDao.kt | 3 +++ .../github/libretube/repo/LocalFeedRepository.kt | 13 +++++++------ 2 files changed, 10 insertions(+), 6 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 45396e615..42f95b53a 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 @@ -17,6 +17,9 @@ interface SubscriptionsFeedDao { @Query("DELETE FROM feedItem WHERE uploaded < :olderThan") suspend fun cleanUpOlderThan(olderThan: Long) + @Query("DELETE FROM feedItem WHERE uploaderUrl NOT IN (:channelUrls)") + suspend fun deleteAllExcept(channelUrls: List) + @Query("DELETE FROM feedItem") suspend fun deleteAll() } \ No newline at end of file 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 e364f7c98..69a1289be 100644 --- a/app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt +++ b/app/src/main/java/com/github/libretube/repo/LocalFeedRepository.kt @@ -23,6 +23,11 @@ class LocalFeedRepository : FeedRepository { override suspend fun getFeed(forceRefresh: Boolean): List { val nowMillis = Instant.now().toEpochMilli() + val minimumDateMillis = nowMillis - Duration.ofDays(MAX_FEED_AGE_DAYS).toMillis() + + val channelIds = SubscriptionHelper.getSubscriptionChannelIds() + // remove videos from channels that are no longer subscribed + DatabaseHolder.Database.feedDao().deleteAllExcept(channelIds.map { id -> "/channel/${id}" }) if (!forceRefresh) { val feed = DatabaseHolder.Database.feedDao().getAll() @@ -37,18 +42,14 @@ class LocalFeedRepository : FeedRepository { } } - val minimumDateMillis = nowMillis - Duration.ofDays(MAX_FEED_AGE_DAYS).toMillis() DatabaseHolder.Database.feedDao().cleanUpOlderThan(minimumDateMillis) - - refreshFeed(minimumDateMillis) + refreshFeed(channelIds, minimumDateMillis) PreferenceHelper.putLong(PreferenceKeys.LAST_FEED_REFRESH_TIMESTAMP_MILLIS, nowMillis) return DatabaseHolder.Database.feedDao().getAll().map(SubscriptionsFeedItem::toStreamItem) } - private suspend fun refreshFeed(minimumDateMillis: Long) { - val channelIds = SubscriptionHelper.getSubscriptionChannelIds() - + private suspend fun refreshFeed(channelIds: List, minimumDateMillis: Long) { for (channelIdChunk in channelIds.chunked(CHUNK_SIZE)) { val collectedFeedItems = channelIdChunk.parallelMap { channelId -> try {