package win.taptap.musicmanager.db import java.time.Instant import java.time.temporal.ChronoUnit import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.serializer import kotlinx.serialization.json.Json import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.SortOrder import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList import org.jetbrains.exposed.sql.SqlExpressionBuilder.inSubQuery import org.jetbrains.exposed.sql.SqlExpressionBuilder.notInList import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.insertIgnore import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.update import win.taptap.musicmanager.source.MusicSource import win.taptap.musicmanager.util.IdGenerator data class MoodRecord( val id: String, val owner: String, val name: String, val description: String?, val query: String?, val genres: List, val source: String, val collectionPercent: Int, val recommendationCount: Int, val refreshHours: Int, val lastDiscoveryAt: String?, val createdAt: String, val changedAt: String, val lastError: String?, val songCount: Int, val recommendationReady: Int, val recommendationDownloading: Int, val recommendationFailed: Int, ) data class MoodSongRecord( val songId: String, val origin: String, val addedAt: String, ) data class RecommendationUpsert( val source: String, val externalId: String, val title: String, val artist: String, val album: String?, val duration: Int?, val thumbnail: String?, ) data class RecommendationRecord( val source: String, val externalId: String, val title: String, val artist: String, val album: String?, val duration: Int?, val thumbnail: String?, val state: String, val songId: String?, val attempts: Int, val error: String?, val rank: Int, val firstSeen: String, val lastSeen: String, ) /** Result of applying a finished download to a recommendation. */ enum class RecommendationCompletion { READY, ALREADY_ADDED, REJECTED, MISSING } /** * Moods are the product's only collection. Songs are permanent entries; the * recommendation table also holds hidden `new` candidates and the lifecycle of * the active slots. The play queue is generated by [win.taptap.musicmanager.radio.RadioService] * for every playback session instead of being stored as a playlist. * * A mood has one download source at a time. Switching it clears the active and * hidden recommendations of the old source; permanent songs and the * added/rejected history stay. */ object MoodRepository { private val genreSerializer = ListSerializer(String.serializer()) private val ACTIVE_STATES = listOf("downloading", "ready", "failed") private val VISIBLE_STATES = ACTIVE_STATES private val HIDDEN_DECISIONS = listOf("added", "rejected") private val CLEARED_ON_SOURCE_CHANGE = listOf("new", "downloading", "ready", "failed") fun list(owner: String): List = transaction { MoodsTable.selectAll() .where { MoodsTable.owner eq owner } .orderBy(MoodsTable.changedAt to SortOrder.DESC) .map { moodRecord(it) } } fun all(): List = transaction { MoodsTable.selectAll() .orderBy(MoodsTable.changedAt to SortOrder.DESC) .map { moodRecord(it) } } fun get(id: String, owner: String): MoodRecord? = transaction { MoodsTable.selectAll() .where { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) } .singleOrNull() ?.let { moodRecord(it) } } fun create( owner: String, name: String, description: String?, query: String?, genres: List, source: String, collectionPercent: Int, recommendationCount: Int, refreshHours: Int, ): MoodRecord = transaction { val now = Instant.now().toString() val id = IdGenerator.moodId(owner, name, now) MoodsTable.insert { it[MoodsTable.id] = id it[MoodsTable.owner] = owner it[MoodsTable.name] = name.take(200) it[MoodsTable.description] = description?.take(2000) it[MoodsTable.query] = query?.trim()?.takeIf(String::isNotEmpty) it[MoodsTable.genres] = encodeGenres(genres) it[MoodsTable.musicSource] = source it[MoodsTable.collectionPercent] = collectionPercent.coerceIn(0, 100) it[MoodsTable.recommendationCount] = recommendationCount.coerceIn(0, 30) it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720) it[createdAt] = now it[changedAt] = now } moodRecord(MoodsTable.selectAll().where { MoodsTable.id eq id }.single()) } fun update( id: String, owner: String, name: String, description: String?, query: String?, genres: List, source: String, collectionPercent: Int, recommendationCount: Int, refreshHours: Int, ): MoodRecord? = transaction { val current = MoodsTable.selectAll() .where { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) } .singleOrNull() ?: return@transaction null val sourceChanged = current[MoodsTable.musicSource] != source if (sourceChanged) { // Candidates are tied to the old catalog. Keep permanent songs and // the added/rejected decisions, but drop the active pool so it can // be refilled from the new source. MoodRecommendationsTable.deleteWhere { (MoodRecommendationsTable.moodId eq id) and (MoodRecommendationsTable.state inList CLEARED_ON_SOURCE_CHANGE) } } MoodsTable.update({ MoodsTable.id eq id }) { it[MoodsTable.name] = name.take(200) it[MoodsTable.description] = description?.take(2000) it[MoodsTable.query] = query?.trim()?.takeIf(String::isNotEmpty) it[MoodsTable.genres] = encodeGenres(genres) it[MoodsTable.musicSource] = source it[MoodsTable.collectionPercent] = collectionPercent.coerceIn(0, 100) it[MoodsTable.recommendationCount] = recommendationCount.coerceIn(0, 30) it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720) it[MoodsTable.changedAt] = Instant.now().toString() if (sourceChanged) { it[lastDiscoveryAt] = null it[lastError] = null } } moodRecord(MoodsTable.selectAll().where { MoodsTable.id eq id }.single()) } fun delete(id: String, owner: String): Boolean = transaction { MoodsTable.deleteWhere { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) } > 0 } // ---------------------------------------------------------------- songs fun songs(id: String): List = transaction { MoodSongsTable.selectAll() .where { MoodSongsTable.moodId eq id } .orderBy( MoodSongsTable.addedAt to SortOrder.ASC, MoodSongsTable.songId to SortOrder.ASC, ) .map { MoodSongRecord( songId = it[MoodSongsTable.songId], origin = it[MoodSongsTable.origin], addedAt = it[MoodSongsTable.addedAt], ) } } fun songIds(id: String): List = songs(id).map { it.songId } fun songRecords(id: String): List { val ids = songIds(id) return SongRepository.byIds(ids) } /** Returns false when the song is already in the mood or the mood does not exist. */ fun addSong(id: String, songId: String, origin: String): Boolean = transaction { val exists = MoodSongsTable.selectAll() .where { (MoodSongsTable.moodId eq id) and (MoodSongsTable.songId eq songId) } .any() if (exists) return@transaction false val moodExists = MoodsTable.selectAll().where { MoodsTable.id eq id }.any() if (!moodExists) return@transaction false val now = Instant.now().toString() MoodSongsTable.insertIgnore { it[moodId] = id it[this.songId] = songId it[this.origin] = origin.takeIf { value -> value in ORIGINS } ?: "search" it[addedAt] = now } MoodsTable.update({ MoodsTable.id eq id }) { it[changedAt] = now } // A track that is now permanently in the mood must not also linger as a // hidden candidate or an active recommendation slot. val song = SongRepository.byId(songId) if (song != null) { MoodRecommendationsTable.deleteWhere { (MoodRecommendationsTable.moodId eq id) and (MoodRecommendationsTable.musicSource eq song.source) and (MoodRecommendationsTable.externalId eq song.externalId) and (MoodRecommendationsTable.state eq "new") } MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq id) and (MoodRecommendationsTable.musicSource eq song.source) and (MoodRecommendationsTable.externalId eq song.externalId) and (MoodRecommendationsTable.state inList listOf("downloading", "ready", "failed")) }) { it[state] = "added" it[this.songId] = songId } } true } fun removeSong(id: String, songId: String): Boolean = transaction { val removed = MoodSongsTable.deleteWhere { (MoodSongsTable.moodId eq id) and (MoodSongsTable.songId eq songId) } > 0 if (removed) { MoodsTable.update({ MoodsTable.id eq id }) { it[changedAt] = Instant.now().toString() } // Detaching must not keep a permanent `added` decision pinned to the // file, otherwise the GC could never delete it. MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq id) and (MoodRecommendationsTable.songId eq songId) and (MoodRecommendationsTable.state eq "added") }) { it[MoodRecommendationsTable.songId] = null } } removed } // ------------------------------------------------------ recommendations /** Active recommendations, i.e. the rows the client is allowed to see. */ fun recommendations(id: String): List = transaction { MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq id) and (MoodRecommendationsTable.state inList VISIBLE_STATES) } .orderBy( MoodRecommendationsTable.rank to SortOrder.ASC, MoodRecommendationsTable.firstSeen to SortOrder.ASC, ) .map { it.toRecommendationRecord() } } fun allRecommendations(id: String): List = transaction { MoodRecommendationsTable.selectAll() .where { MoodRecommendationsTable.moodId eq id } .orderBy(MoodRecommendationsTable.rank to SortOrder.ASC) .map { it.toRecommendationRecord() } } fun recommendation(moodId: String, externalId: String): RecommendationRecord? = transaction { MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) } .singleOrNull() ?.toRecommendationRecord() } fun activeCount(moodId: String): Int = transaction { MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.state inList ACTIVE_STATES) } .count() .toInt() } fun externalIdsInMood(moodId: String, source: String): Set = transaction { val songs = MoodSongsTable.select(MoodSongsTable.songId) .where { MoodSongsTable.moodId eq moodId } SongsTable.select(SongsTable.externalId) .where { (SongsTable.id inSubQuery songs) and (SongsTable.musicSource eq source) } .map { it[SongsTable.externalId] } .toSet() } fun decidedExternalIds(moodId: String, source: String): Set = transaction { MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.musicSource eq source) and (MoodRecommendationsTable.state inList HIDDEN_DECISIONS) } .map { it[MoodRecommendationsTable.externalId] } .toSet() } /** * Stores a sorted discovery batch as hidden `new` candidates. Existing rows * keep their state: active slots and decisions must survive a refresh. */ fun storeCandidates( moodId: String, source: String, items: List, seenAt: String, poolLimit: Int = CANDIDATE_POOL, ): Int = transaction { val existing = MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.musicSource eq source) } .associateBy { it[MoodRecommendationsTable.externalId] } var stored = 0 items.take(poolLimit.coerceAtLeast(1)).forEachIndexed { index, item -> val row = existing[item.externalId] if (row == null) { MoodRecommendationsTable.insert { it[MoodRecommendationsTable.moodId] = moodId it[MoodRecommendationsTable.musicSource] = source it[externalId] = item.externalId it[title] = item.title.take(500) it[artist] = item.artist.take(300) it[album] = item.album?.take(300) it[duration] = item.duration it[thumbnail] = item.thumbnail?.take(500) it[state] = "new" it[rank] = index it[firstSeen] = seenAt it[lastSeen] = seenAt } } else if (row[MoodRecommendationsTable.state] == "new") { MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq item.externalId) }) { it[title] = item.title.take(500) it[artist] = item.artist.take(300) it[album] = item.album?.take(300) it[duration] = item.duration it[thumbnail] = item.thumbnail?.take(500) it[rank] = index it[lastSeen] = seenAt } } stored += 1 } // Candidates that were not returned again are no longer useful. val keep = items.take(poolLimit.coerceAtLeast(1)).map { it.externalId }.toSet() if (keep.isNotEmpty()) { MoodRecommendationsTable.deleteWhere { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.musicSource eq source) and (MoodRecommendationsTable.state eq "new") and (MoodRecommendationsTable.externalId notInList keep.toList()) } } stored } /** Moves up to [limit] hidden candidates into the downloading state. */ fun promoteCandidates(moodId: String, source: String, limit: Int): List = transaction { if (limit <= 0) return@transaction emptyList() val rows = MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.musicSource eq source) and (MoodRecommendationsTable.state eq "new") } .orderBy( MoodRecommendationsTable.rank to SortOrder.ASC, MoodRecommendationsTable.firstSeen to SortOrder.ASC, ) .limit(limit) .toList() rows.forEach { row -> MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq row[MoodRecommendationsTable.externalId]) }) { it[state] = "downloading" it[error] = null } } rows.map { it.toRecommendationRecord().copy(state = "downloading", error = null) } } fun setRecommendationState(moodId: String, externalId: String, state: String, error: String? = null) { transaction { MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) }) { it[this.state] = state it[this.error] = error?.take(1000) } } } fun markRecommendationDownloading(moodId: String, externalId: String) { transaction { MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) }) { it[state] = "downloading" it[attempts] = 0 it[error] = null } } } fun failRecommendation(moodId: String, externalId: String, error: String, attempts: Int) { transaction { MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) and (MoodRecommendationsTable.state eq "downloading") }) { it[state] = "failed" it[this.error] = error.take(1000) it[this.attempts] = attempts } } } /** * Applies a finished download. A rejection that happened while the download * was running wins: the file is left for the GC to delete. */ fun completeDownload( moodId: String, source: String, externalId: String, songId: String, ): RecommendationCompletion = transaction { val row = MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.musicSource eq source) and (MoodRecommendationsTable.externalId eq externalId) } .singleOrNull() ?: return@transaction RecommendationCompletion.MISSING when (row[MoodRecommendationsTable.state]) { "rejected" -> RecommendationCompletion.REJECTED "added" -> { insertRecommendationSong(moodId, songId) MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) }) { it[this.songId] = songId it[state] = "added" } RecommendationCompletion.ALREADY_ADDED } else -> { MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) }) { it[this.songId] = songId it[state] = "ready" it[error] = null it[lastSeen] = Instant.now().toString() } RecommendationCompletion.READY } } } /** Called when the user presses Add. Marks the row and attaches a ready song. */ fun markAdded(moodId: String, externalId: String): RecommendationCompletion = transaction { val row = MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) } .singleOrNull() ?: return@transaction RecommendationCompletion.MISSING val songId = row[MoodRecommendationsTable.songId] MoodRecommendationsTable.update({ (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.externalId eq externalId) }) { it[state] = "added" } if (songId != null && row[MoodRecommendationsTable.state] != "added") { insertRecommendationSong(moodId, songId) return@transaction RecommendationCompletion.ALREADY_ADDED } RecommendationCompletion.READY } fun markRejected(moodId: String, externalId: String) { setRecommendationState(moodId, externalId, "rejected") } fun readySongs(moodId: String): List = transaction { val songIds = MoodRecommendationsTable .select(MoodRecommendationsTable.songId) .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.state eq "ready") } .mapNotNull { it[MoodRecommendationsTable.songId] } if (songIds.isEmpty()) emptyList() else SongsTable.selectAll() .where { SongsTable.id inList songIds } .map { it.toSongRecord() } } // ---------------------------------------------------------- discovery /** Moods whose automatic discovery is due, oldest first. */ fun staleDiscovery(now: Instant, limit: Int): List = transaction { MoodsTable.selectAll() .orderBy(MoodsTable.lastDiscoveryAt to SortOrder.ASC) .map { moodRecord(it) } .filter { mood -> mood.recommendationCount > 0 && isStale(mood.lastDiscoveryAt, mood.refreshHours, now) } .take(limit.coerceAtLeast(1)) } fun markError(id: String, message: String) { transaction { MoodsTable.update({ MoodsTable.id eq id }) { it[lastError] = message.take(1000) } } } fun markDiscovered(id: String) { transaction { MoodsTable.update({ MoodsTable.id eq id }) { it[lastDiscoveryAt] = Instant.now().toString() it[lastError] = null } } } fun isDiscoveryDue(mood: MoodRecord, now: Instant = Instant.now()): Boolean = isStale(mood.lastDiscoveryAt, mood.refreshHours, now) // ------------------------------------------------------------ internal private fun insertRecommendationSong(moodId: String, songId: String) { MoodSongsTable.insertIgnore { it[this.moodId] = moodId it[this.songId] = songId it[origin] = "recommendation" it[addedAt] = Instant.now().toString() } MoodsTable.update({ MoodsTable.id eq moodId }) { it[changedAt] = Instant.now().toString() } } private fun isStale(lastDiscoveryAt: String?, refreshHours: Int, now: Instant): Boolean { if (lastDiscoveryAt == null) return true val at = runCatching { Instant.parse(lastDiscoveryAt) }.getOrNull() ?: return true return !at.plus(refreshHours.toLong().coerceAtLeast(1), ChronoUnit.HOURS).isAfter(now) } private fun ResultRow.toRecommendationRecord() = RecommendationRecord( source = this[MoodRecommendationsTable.musicSource], externalId = this[MoodRecommendationsTable.externalId], title = this[MoodRecommendationsTable.title], artist = this[MoodRecommendationsTable.artist], album = this[MoodRecommendationsTable.album], duration = this[MoodRecommendationsTable.duration], thumbnail = this[MoodRecommendationsTable.thumbnail], state = this[MoodRecommendationsTable.state], songId = this[MoodRecommendationsTable.songId], attempts = this[MoodRecommendationsTable.attempts], error = this[MoodRecommendationsTable.error], rank = this[MoodRecommendationsTable.rank], firstSeen = this[MoodRecommendationsTable.firstSeen], lastSeen = this[MoodRecommendationsTable.lastSeen], ) private fun ResultRow.toSongRecord() = SongRecord( id = this[SongsTable.id], source = this[SongsTable.musicSource], externalId = this[SongsTable.externalId], title = this[SongsTable.title], artist = this[SongsTable.artist], album = this[SongsTable.album], duration = this[SongsTable.duration], bitRate = this[SongsTable.bitRate], size = this[SongsTable.size], suffix = this[SongsTable.suffix], contentType = this[SongsTable.contentType], relativePath = this[SongsTable.relativePath], fileMtime = this[SongsTable.fileMtime], thumbnail = this[SongsTable.thumbnail], createdAt = this[SongsTable.createdAt], updatedAt = this[SongsTable.updatedAt], ) private fun moodRecord(row: ResultRow): MoodRecord { val id = row[MoodsTable.id] val songCount = MoodSongsTable.selectAll() .where { MoodSongsTable.moodId eq id } .count() .toInt() val ready = countState(id, "ready") val downloading = countState(id, "downloading") val failed = countState(id, "failed") return MoodRecord( id = id, owner = row[MoodsTable.owner], name = row[MoodsTable.name], description = row[MoodsTable.description], query = row[MoodsTable.query], genres = decodeGenres(row[MoodsTable.genres]), source = row[MoodsTable.musicSource], collectionPercent = row[MoodsTable.collectionPercent], recommendationCount = row[MoodsTable.recommendationCount], refreshHours = row[MoodsTable.refreshHours], lastDiscoveryAt = row[MoodsTable.lastDiscoveryAt], createdAt = row[MoodsTable.createdAt], changedAt = row[MoodsTable.changedAt], lastError = row[MoodsTable.lastError], songCount = songCount, recommendationReady = ready, recommendationDownloading = downloading, recommendationFailed = failed, ) } private fun countState(moodId: String, state: String): Int = MoodRecommendationsTable.selectAll() .where { (MoodRecommendationsTable.moodId eq moodId) and (MoodRecommendationsTable.state eq state) } .count() .toInt() private fun encodeGenres(genres: List): String = Json.encodeToString( genreSerializer, genres.map { it.trim() }.filter { it.isNotEmpty() }.distinct().take(20), ) private fun decodeGenres(value: String): List = runCatching { Json.decodeFromString(genreSerializer, value) }.getOrDefault(emptyList()) private val ORIGINS = setOf("search", "recommendation", "url") private const val CANDIDATE_POOL = 60 }