youpipe: add Room entities and migration for recommendation preferences, impressions, and channel/keyword prefs
- Commit
70eef9107d266e0024065056181137150e97f716- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-09 17:02 UTC (5 months ago)
- Parents
bbbd590
Diff
youpipe/src/main/java/com/vayunmathur/youpipe/data/ChannelPreference.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/data/ChannelPreference.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/data/ChannelPreference.ktnew file mode 100644index 0000000000..53db86be51--- /dev/null+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/data/ChannelPreference.kt@@ -0,0 +1,43 @@+package com.vayunmathur.youpipe.data++import androidx.room.Dao+import androidx.room.Entity+import androidx.room.PrimaryKey+import androidx.room.Query+import androidx.room.Upsert+import kotlinx.coroutines.flow.Flow++/**+ * A per-channel recommendation preference keyed by the lowercased author name+ * ([channelKey]). [multiplier] scales the channel's candidate scores+ * (>1 boosts, <1 demotes, 1.0 = neutral). [blocked] hard-filters the channel;+ * [pinned] forces its items to the top of the feed.+ */+@Entity+data class ChannelPreference(+ @PrimaryKey val channelKey: String,+ val multiplier: Double = 1.0,+ val blocked: Boolean = false,+ val pinned: Boolean = false,+)++@Dao+interface ChannelPreferenceDao {+ @Query("SELECT * FROM ChannelPreference")+ suspend fun getAll(): List<ChannelPreference>++ @Query("SELECT * FROM ChannelPreference")+ fun getAllFlow(): Flow<List<ChannelPreference>>++ @Query("SELECT * FROM ChannelPreference WHERE channelKey = :channelKey")+ suspend fun get(channelKey: String): ChannelPreference?++ @Upsert+ suspend fun upsert(value: ChannelPreference)++ @Query("DELETE FROM ChannelPreference WHERE channelKey = :channelKey")+ suspend fun delete(channelKey: String)++ @Query("DELETE FROM ChannelPreference")+ suspend fun clearAll()+}
youpipe/src/main/java/com/vayunmathur/youpipe/data/KeywordPreference.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/data/KeywordPreference.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/data/KeywordPreference.ktnew file mode 100644index 0000000000..d364418805--- /dev/null+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/data/KeywordPreference.kt@@ -0,0 +1,37 @@+package com.vayunmathur.youpipe.data++import androidx.room.Dao+import androidx.room.Entity+import androidx.room.PrimaryKey+import androidx.room.Query+import androidx.room.Upsert+import kotlinx.coroutines.flow.Flow++/**+ * A per-keyword recommendation preference keyed by the lowercased token+ * ([keyword]). When [muted], candidates whose title tokens include this keyword+ * are hard-filtered from the feed.+ */+@Entity+data class KeywordPreference(+ @PrimaryKey val keyword: String,+ val muted: Boolean = true,+)++@Dao+interface KeywordPreferenceDao {+ @Query("SELECT * FROM KeywordPreference")+ suspend fun getAll(): List<KeywordPreference>++ @Query("SELECT * FROM KeywordPreference")+ fun getAllFlow(): Flow<List<KeywordPreference>>++ @Upsert+ suspend fun upsert(value: KeywordPreference)++ @Query("DELETE FROM KeywordPreference WHERE keyword = :keyword")+ suspend fun delete(keyword: String)++ @Query("DELETE FROM KeywordPreference")+ suspend fun clearAll()+}
youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationImpression.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationImpression.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationImpression.ktnew file mode 100644index 0000000000..5f5a93ecde--- /dev/null+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationImpression.kt@@ -0,0 +1,60 @@+package com.vayunmathur.youpipe.data++import androidx.room.Dao+import androidx.room.Entity+import androidx.room.PrimaryKey+import androidx.room.Query+import kotlinx.coroutines.flow.Flow+import kotlin.time.Instant++/**+ * A record that a video was shown in the recommendation feed. Used to drive the+ * feedback loop: rarely-shown channels earn an exploration bonus, while channels+ * shown many times but never watched are demoted, and recently-shown videos are+ * suppressed on refresh. [source] is stored as the [com.vayunmathur.youpipe.util.RecSource]+ * name.+ */+@Entity+data class RecommendationImpression(+ @PrimaryKey val videoID: Long,+ val channelKey: String,+ val source: String,+ val shownCount: Int,+ val firstShownAt: Instant,+ val lastShownAt: Instant,+)++@Dao+interface RecommendationImpressionDao {+ @Query("SELECT * FROM RecommendationImpression")+ suspend fun getAll(): List<RecommendationImpression>++ @Query("SELECT * FROM RecommendationImpression")+ fun getAllFlow(): Flow<List<RecommendationImpression>>++ @Query("SELECT * FROM RecommendationImpression WHERE videoID = :videoID")+ suspend fun getById(videoID: Long): RecommendationImpression?++ /**+ * Records an impression for [videoID], incrementing [shownCount] and updating+ * [lastShownAt] when a row already exists, otherwise inserting a new row.+ */+ @Query(+ """+ INSERT INTO RecommendationImpression (videoID, channelKey, source, shownCount, firstShownAt, lastShownAt)+ VALUES (:videoID, :channelKey, :source, 1, :now, :now)+ ON CONFLICT(videoID) DO UPDATE SET+ shownCount = shownCount + 1,+ lastShownAt = :now,+ channelKey = :channelKey,+ source = :source+ """+ )+ suspend fun recordImpression(videoID: Long, channelKey: String, source: String, now: Instant)++ @Query("DELETE FROM RecommendationImpression WHERE lastShownAt < :cutoff")+ suspend fun deleteOlderThan(cutoff: Instant)++ @Query("DELETE FROM RecommendationImpression")+ suspend fun clearAll()+}
youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationPreferences.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationPreferences.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationPreferences.ktnew file mode 100644index 0000000000..6f4f7dfcfc--- /dev/null+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/data/RecommendationPreferences.kt@@ -0,0 +1,51 @@+package com.vayunmathur.youpipe.data++import androidx.room.Dao+import androidx.room.Entity+import androidx.room.PrimaryKey+import androidx.room.Query+import androidx.room.Upsert+import kotlinx.coroutines.flow.Flow++/**+ * User-facing recommendation controls. A single row (`id = 0`) holds all+ * feed-mix dials, source toggles, and content filters. Defaults reproduce+ * today's behavior so the feature is opt-in.+ *+ * The three dials are floats in [0, 1]:+ * - [discoveryFamiliar]: 0 = mostly familiar/subscriptions, 1 = maximum discovery.+ * - [freshEvergreen]: 0 = evergreen-friendly, 1 = strongly favor fresh uploads.+ * - [focusedDiverse]: 0 = focused (fewer creators), 1 = maximally diverse.+ */+@Entity+data class RecommendationPreferences(+ @PrimaryKey val id: Int = 0,+ val preset: String = "BALANCED",+ val discoveryFamiliar: Float = 0.5f,+ val freshEvergreen: Float = 0.5f,+ val focusedDiverse: Float = 0.5f,+ val sourceRelated: Boolean = true,+ val sourceTrending: Boolean = true,+ val sourceSubscription: Boolean = true,+ val sourceTopChannel: Boolean = true,+ val sourceSearch: Boolean = true,+ val hideShorts: Boolean = false,+ val hideLive: Boolean = false,+ val minDurationSec: Long = 0,+ val maxDurationSec: Long = 0,+)++@Dao+interface RecommendationPreferencesDao {+ @Query("SELECT * FROM RecommendationPreferences WHERE id = 0")+ fun getFlow(): Flow<RecommendationPreferences?>++ @Query("SELECT * FROM RecommendationPreferences WHERE id = 0")+ suspend fun get(): RecommendationPreferences?++ @Upsert+ suspend fun upsert(value: RecommendationPreferences)++ @Query("DELETE FROM RecommendationPreferences")+ suspend fun clearAll()+}
youpipe/src/main/java/com/vayunmathur/youpipe/data/SubscriptionDatabase.kt
diff --git a/youpipe/src/main/java/com/vayunmathur/youpipe/data/SubscriptionDatabase.kt b/youpipe/src/main/java/com/vayunmathur/youpipe/data/SubscriptionDatabase.ktindex 9f194428cb..4706bf2217 100644--- a/youpipe/src/main/java/com/vayunmathur/youpipe/data/SubscriptionDatabase.kt+++ b/youpipe/src/main/java/com/vayunmathur/youpipe/data/SubscriptionDatabase.kt@@ -47,6 +47,55 @@ val MIGRATION_2_3 = Migration(2, 3) { """.trimIndent()) } +val MIGRATION_3_4 = Migration(3, 4) {+ it.execSQL("""+ CREATE TABLE IF NOT EXISTS `RecommendationImpression` (+ `videoID` INTEGER NOT NULL,+ `channelKey` TEXT NOT NULL,+ `source` TEXT NOT NULL,+ `shownCount` INTEGER NOT NULL,+ `firstShownAt` INTEGER NOT NULL,+ `lastShownAt` INTEGER NOT NULL,+ PRIMARY KEY(`videoID`)+ )+ """.trimIndent())+ it.execSQL("""+ CREATE TABLE IF NOT EXISTS `RecommendationPreferences` (+ `id` INTEGER NOT NULL,+ `preset` TEXT NOT NULL,+ `discoveryFamiliar` REAL NOT NULL,+ `freshEvergreen` REAL NOT NULL,+ `focusedDiverse` REAL NOT NULL,+ `sourceRelated` INTEGER NOT NULL,+ `sourceTrending` INTEGER NOT NULL,+ `sourceSubscription` INTEGER NOT NULL,+ `sourceTopChannel` INTEGER NOT NULL,+ `sourceSearch` INTEGER NOT NULL,+ `hideShorts` INTEGER NOT NULL,+ `hideLive` INTEGER NOT NULL,+ `minDurationSec` INTEGER NOT NULL,+ `maxDurationSec` INTEGER NOT NULL,+ PRIMARY KEY(`id`)+ )+ """.trimIndent())+ it.execSQL("""+ CREATE TABLE IF NOT EXISTS `ChannelPreference` (+ `channelKey` TEXT NOT NULL,+ `multiplier` REAL NOT NULL,+ `blocked` INTEGER NOT NULL,+ `pinned` INTEGER NOT NULL,+ PRIMARY KEY(`channelKey`)+ )+ """.trimIndent())+ it.execSQL("""+ CREATE TABLE IF NOT EXISTS `KeywordPreference` (+ `keyword` TEXT NOT NULL,+ `muted` INTEGER NOT NULL,+ PRIMARY KEY(`keyword`)+ )+ """.trimIndent())+}+ @Dao interface HistoryVideoDao { @Query("SELECT * FROM HistoryVideo")@@ -55,6 +104,9 @@ interface HistoryVideoDao { @Query("SELECT * FROM HistoryVideo WHERE id = :id") fun getByIdFlow(id: Long): Flow<HistoryVideo?> + @Query("SELECT * FROM HistoryVideo")+ suspend fun getAll(): List<HistoryVideo>+ @Upsert suspend fun upsert(value: HistoryVideo): Long @@ -103,12 +155,15 @@ interface SubscriptionVideoDao { @Query("SELECT * FROM SubscriptionVideo WHERE id = :id") fun getByIdFlow(id: Long): Flow<SubscriptionVideo?> + @Query("SELECT * FROM SubscriptionVideo")+ suspend fun getAll(): List<SubscriptionVideo>+ @Upsert suspend fun upsertAll(values: List<SubscriptionVideo>) } @TypeConverters(DefaultConverters::class)-@Database(entities = [Subscription::class, SubscriptionVideo::class, HistoryVideo::class, SubscriptionCategory::class, DownloadedVideo::class, CachedRelatedVideo::class], version = 3)+@Database(entities = [Subscription::class, SubscriptionVideo::class, HistoryVideo::class, SubscriptionCategory::class, DownloadedVideo::class, CachedRelatedVideo::class, RecommendationImpression::class, RecommendationPreferences::class, ChannelPreference::class, KeywordPreference::class], version = 4) abstract class SubscriptionDatabase : RoomDatabase() { abstract fun subscriptionDao(): SubscriptionDao abstract fun subscriptionVideoDao(): SubscriptionVideoDao@@ -116,8 +171,12 @@ abstract class SubscriptionDatabase : RoomDatabase() { abstract fun subscriptionCategoryDao(): SubscriptionCategoryDao abstract fun downloadedVideoDao(): DownloadedVideoDao abstract fun cachedRelatedVideoDao(): CachedRelatedVideoDao+ abstract fun recommendationImpressionDao(): RecommendationImpressionDao+ abstract fun recommendationPreferencesDao(): RecommendationPreferencesDao+ abstract fun channelPreferenceDao(): ChannelPreferenceDao+ abstract fun keywordPreferenceDao(): KeywordPreferenceDao companion object : com.vayunmathur.library.util.DatabaseMigrations {- override val migrations: List<Migration> = listOf(MIGRATION_1_2, MIGRATION_2_3)+ override val migrations: List<Migration> = listOf(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4) } }