feat: add per-user moods as generated playlists
Add private server-managed playlist moods with curated seeds and related library fill. Moods are created or promoted from existing playlists in the web UI, refresh lazily after their TTL, and are marked stale after a scan. Symfonium stars pin the active mood: opening a mood restores its context, and a starred song becomes a permanent seed. Without an active session the song is pinned to every mood that already contains it. Includes the V4 migration, custom mood endpoints, management UI, tests and smoke coverage. Bump the chart to 0.1.4.
This commit is contained in:
1 parent
e08d993e35
commit
28bfdd9d00
25 files changed
+2039
-14
No files matched your search
@@ -0,0 +1,413 @@
|
||||
package win.taptap.musicmanager.db
|
||||
|
||||
import java.time.Instant
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.jetbrains.exposed.sql.JoinType
|
||||
import org.jetbrains.exposed.sql.Op
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
|
||||
import org.jetbrains.exposed.sql.ResultRow
|
||||
import org.jetbrains.exposed.sql.SortOrder
|
||||
import org.jetbrains.exposed.sql.and
|
||||
import org.jetbrains.exposed.sql.deleteWhere
|
||||
import org.jetbrains.exposed.sql.insert
|
||||
import org.jetbrains.exposed.sql.selectAll
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import org.jetbrains.exposed.sql.update
|
||||
|
||||
data class MoodRecord(
|
||||
val playlistId: String,
|
||||
val owner: String,
|
||||
val name: String,
|
||||
val comment: String?,
|
||||
val query: String?,
|
||||
val genres: List<String>,
|
||||
val targetSize: Int,
|
||||
val ytSlots: Int,
|
||||
val expandRelated: Boolean,
|
||||
val refreshHours: Int,
|
||||
val generatedAt: String?,
|
||||
val generation: Int,
|
||||
val lastError: String?,
|
||||
val created: String,
|
||||
val changed: String,
|
||||
val songCount: Int,
|
||||
val duration: Int,
|
||||
val seedCount: Int,
|
||||
)
|
||||
|
||||
data class MoodSeedRecord(
|
||||
val songId: String,
|
||||
val origin: String,
|
||||
val addedAt: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* Moods are private playlists with generation settings. The playlist row remains
|
||||
* the client-visible object; this repository owns the settings, seeds and the
|
||||
* materialized mix in `playlist_songs`.
|
||||
*/
|
||||
object MoodRepository {
|
||||
|
||||
private val genreSerializer = ListSerializer(String.serializer())
|
||||
|
||||
fun list(userName: String): List<MoodRecord> = transaction {
|
||||
PlaylistsTable
|
||||
.join(MoodsTable, JoinType.INNER, PlaylistsTable.id, MoodsTable.playlistId)
|
||||
.selectAll()
|
||||
.where {
|
||||
(PlaylistsTable.owner eq userName) and
|
||||
(PlaylistsTable.kind eq "mood")
|
||||
}
|
||||
.orderBy(PlaylistsTable.changedAt to SortOrder.DESC)
|
||||
.map { moodRecord(it) }
|
||||
}
|
||||
|
||||
fun get(playlistId: String, userName: String): MoodRecord? = transaction {
|
||||
val row = PlaylistsTable
|
||||
.join(MoodsTable, JoinType.INNER, PlaylistsTable.id, MoodsTable.playlistId)
|
||||
.selectAll()
|
||||
.where {
|
||||
(PlaylistsTable.id eq playlistId) and
|
||||
(PlaylistsTable.owner eq userName) and
|
||||
(PlaylistsTable.kind eq "mood")
|
||||
}
|
||||
.singleOrNull()
|
||||
|
||||
row?.let { moodRecord(it) }
|
||||
}
|
||||
|
||||
fun create(
|
||||
userName: String,
|
||||
name: String,
|
||||
comment: String?,
|
||||
query: String?,
|
||||
genres: List<String>,
|
||||
targetSize: Int,
|
||||
ytSlots: Int,
|
||||
expandRelated: Boolean,
|
||||
refreshHours: Int,
|
||||
seedSongIds: List<String>,
|
||||
): MoodRecord {
|
||||
val playlist = PlaylistRepository.create(
|
||||
userName = userName,
|
||||
name = name,
|
||||
songIds = emptyList(),
|
||||
isPublic = false,
|
||||
kind = "mood",
|
||||
)
|
||||
|
||||
try {
|
||||
transaction {
|
||||
insertSettings(
|
||||
playlistId = playlist.id,
|
||||
query = query,
|
||||
genres = genres,
|
||||
targetSize = targetSize,
|
||||
ytSlots = ytSlots,
|
||||
expandRelated = expandRelated,
|
||||
refreshHours = refreshHours,
|
||||
)
|
||||
insertSeeds(playlist.id, seedSongIds, "manual")
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
PlaylistRepository.delete(playlist.id, userName)
|
||||
throw t
|
||||
}
|
||||
|
||||
return get(playlist.id, userName)
|
||||
?: error("Mood ${playlist.id} was not persisted")
|
||||
}
|
||||
|
||||
/** Converts an existing owned playlist into a mood while keeping its ID. */
|
||||
fun promote(
|
||||
userName: String,
|
||||
playlistId: String,
|
||||
query: String?,
|
||||
genres: List<String>,
|
||||
targetSize: Int,
|
||||
ytSlots: Int,
|
||||
expandRelated: Boolean,
|
||||
refreshHours: Int,
|
||||
musicFolderIds: Set<Int>,
|
||||
): MoodRecord? {
|
||||
val playlist = PlaylistRepository.get(playlistId, userName) ?: return null
|
||||
if (playlist.owner != userName || playlist.kind != "user") return null
|
||||
|
||||
val seedIds = transaction {
|
||||
PlaylistRepository.songIds(playlistId).filter { songId ->
|
||||
SongsTable
|
||||
.select(SongsTable.folderId)
|
||||
.where { SongsTable.id eq songId }
|
||||
.singleOrNull()
|
||||
?.get(SongsTable.folderId) in musicFolderIds
|
||||
}
|
||||
}
|
||||
PlaylistRepository.setKind(playlistId, "mood", false)
|
||||
|
||||
try {
|
||||
transaction {
|
||||
insertSettings(
|
||||
playlistId = playlistId,
|
||||
query = query,
|
||||
genres = genres,
|
||||
targetSize = targetSize,
|
||||
ytSlots = ytSlots,
|
||||
expandRelated = expandRelated,
|
||||
refreshHours = refreshHours,
|
||||
)
|
||||
insertSeeds(playlistId, seedIds, "promoted")
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
PlaylistRepository.setKind(playlistId, "user", playlist.isPublic)
|
||||
throw t
|
||||
}
|
||||
|
||||
return get(playlistId, userName)
|
||||
}
|
||||
|
||||
fun update(
|
||||
playlistId: String,
|
||||
userName: String,
|
||||
name: String,
|
||||
comment: String?,
|
||||
query: String?,
|
||||
genres: List<String>,
|
||||
targetSize: Int,
|
||||
ytSlots: Int,
|
||||
expandRelated: Boolean,
|
||||
refreshHours: Int,
|
||||
): Boolean = transaction {
|
||||
val exists = PlaylistsTable.selectAll()
|
||||
.where {
|
||||
(PlaylistsTable.id eq playlistId) and
|
||||
(PlaylistsTable.owner eq userName) and
|
||||
(PlaylistsTable.kind eq "mood")
|
||||
}
|
||||
.any()
|
||||
if (!exists) return@transaction false
|
||||
|
||||
PlaylistsTable.update({ PlaylistsTable.id eq playlistId }) {
|
||||
it[PlaylistsTable.name] = name
|
||||
it[PlaylistsTable.comment] = comment
|
||||
it[changedAt] = Instant.now().toString()
|
||||
}
|
||||
MoodsTable.update({ MoodsTable.playlistId eq playlistId }) {
|
||||
it[MoodsTable.query] = query
|
||||
it[MoodsTable.genres] = encodeGenres(genres)
|
||||
it[MoodsTable.targetSize] = targetSize.coerceIn(1, 500)
|
||||
it[MoodsTable.ytSlots] = ytSlots.coerceIn(0, 500)
|
||||
it[MoodsTable.expandRelated] = expandRelated
|
||||
it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720)
|
||||
it[generatedAt] = null
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fun delete(playlistId: String, userName: String): Boolean =
|
||||
PlaylistRepository.delete(playlistId, userName)
|
||||
|
||||
fun seedSongIds(playlistId: String, origin: String? = null): List<String> = transaction {
|
||||
val condition = if (origin == null) {
|
||||
MoodSeedsTable.playlistId eq playlistId
|
||||
} else {
|
||||
(MoodSeedsTable.playlistId eq playlistId) and (MoodSeedsTable.origin eq origin)
|
||||
}
|
||||
|
||||
MoodSeedsTable.selectAll()
|
||||
.where(condition)
|
||||
.orderBy(
|
||||
MoodSeedsTable.addedAt to SortOrder.ASC,
|
||||
MoodSeedsTable.songId to SortOrder.ASC,
|
||||
)
|
||||
.map { it[MoodSeedsTable.songId] }
|
||||
}
|
||||
|
||||
fun seeds(playlistId: String): List<MoodSeedRecord> = transaction {
|
||||
MoodSeedsTable.selectAll()
|
||||
.where { MoodSeedsTable.playlistId eq playlistId }
|
||||
.orderBy(
|
||||
MoodSeedsTable.addedAt to SortOrder.ASC,
|
||||
MoodSeedsTable.songId to SortOrder.ASC,
|
||||
)
|
||||
.map {
|
||||
MoodSeedRecord(
|
||||
songId = it[MoodSeedsTable.songId],
|
||||
origin = it[MoodSeedsTable.origin],
|
||||
addedAt = it[MoodSeedsTable.addedAt],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true when the seed was newly inserted. */
|
||||
fun addSeed(
|
||||
playlistId: String,
|
||||
userName: String,
|
||||
songId: String,
|
||||
origin: String,
|
||||
): Boolean = transaction {
|
||||
if (get(playlistId, userName) == null) return@transaction false
|
||||
|
||||
val exists = MoodSeedsTable.selectAll()
|
||||
.where {
|
||||
(MoodSeedsTable.playlistId eq playlistId) and
|
||||
(MoodSeedsTable.songId eq songId)
|
||||
}
|
||||
.any()
|
||||
if (exists) return@transaction false
|
||||
|
||||
MoodSeedsTable.insert {
|
||||
it[MoodSeedsTable.playlistId] = playlistId
|
||||
it[MoodSeedsTable.songId] = songId
|
||||
it[MoodSeedsTable.origin] = origin
|
||||
it[addedAt] = Instant.now().toString()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fun removeSeed(
|
||||
playlistId: String,
|
||||
userName: String,
|
||||
songId: String,
|
||||
origin: String? = null,
|
||||
): Boolean = transaction {
|
||||
if (get(playlistId, userName) == null) return@transaction false
|
||||
|
||||
val condition = if (origin == null) {
|
||||
(MoodSeedsTable.playlistId eq playlistId) and (MoodSeedsTable.songId eq songId)
|
||||
} else {
|
||||
(MoodSeedsTable.playlistId eq playlistId) and
|
||||
(MoodSeedsTable.songId eq songId) and
|
||||
(MoodSeedsTable.origin eq origin)
|
||||
}
|
||||
MoodSeedsTable.deleteWhere { condition } > 0
|
||||
}
|
||||
|
||||
/** Mood playlists that currently contain [songId], for context-free star pinning. */
|
||||
fun moodsContainingSong(userName: String, songId: String): List<String> = transaction {
|
||||
PlaylistsTable
|
||||
.join(MoodsTable, JoinType.INNER, PlaylistsTable.id, MoodsTable.playlistId)
|
||||
.join(PlaylistSongsTable, JoinType.INNER, PlaylistsTable.id, PlaylistSongsTable.playlistId)
|
||||
.select(PlaylistsTable.id)
|
||||
.where {
|
||||
(PlaylistsTable.owner eq userName) and
|
||||
(PlaylistsTable.kind eq "mood") and
|
||||
(PlaylistSongsTable.songId eq songId)
|
||||
}
|
||||
.map { it[PlaylistsTable.id] }
|
||||
.distinct()
|
||||
}
|
||||
|
||||
fun markGenerated(playlistId: String, generation: Int, error: String?) {
|
||||
transaction {
|
||||
MoodsTable.update({ MoodsTable.playlistId eq playlistId }) {
|
||||
it[generatedAt] = Instant.now().toString()
|
||||
it[MoodsTable.generation] = generation
|
||||
it[lastError] = error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun markStale(playlistId: String) {
|
||||
transaction {
|
||||
MoodsTable.update({ MoodsTable.playlistId eq playlistId }) {
|
||||
it[generatedAt] = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Records a failed generation without claiming the mood is fresh. */
|
||||
fun markError(playlistId: String, message: String) {
|
||||
transaction {
|
||||
MoodsTable.update({ MoodsTable.playlistId eq playlistId }) {
|
||||
it[lastError] = message.take(1000)
|
||||
it[generatedAt] = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun markAllStale() {
|
||||
transaction {
|
||||
MoodsTable.update({ Op.TRUE }) {
|
||||
it[generatedAt] = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun insertSettings(
|
||||
playlistId: String,
|
||||
query: String?,
|
||||
genres: List<String>,
|
||||
targetSize: Int,
|
||||
ytSlots: Int,
|
||||
expandRelated: Boolean,
|
||||
refreshHours: Int,
|
||||
) {
|
||||
MoodsTable.insert {
|
||||
it[MoodsTable.playlistId] = playlistId
|
||||
it[MoodsTable.query] = query?.trim()?.takeIf(String::isNotEmpty)
|
||||
it[MoodsTable.genres] = encodeGenres(genres)
|
||||
it[MoodsTable.targetSize] = targetSize.coerceIn(1, 500)
|
||||
it[MoodsTable.ytSlots] = ytSlots.coerceIn(0, 500)
|
||||
it[MoodsTable.expandRelated] = expandRelated
|
||||
it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720)
|
||||
}
|
||||
}
|
||||
|
||||
private fun insertSeeds(playlistId: String, songIds: List<String>, origin: String) {
|
||||
val now = Instant.now().toString()
|
||||
songIds.distinct().forEach { songId ->
|
||||
MoodSeedsTable.insert {
|
||||
it[MoodSeedsTable.playlistId] = playlistId
|
||||
it[MoodSeedsTable.songId] = songId
|
||||
it[MoodSeedsTable.origin] = origin
|
||||
it[addedAt] = now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun moodRecord(row: ResultRow): MoodRecord {
|
||||
val id = row[PlaylistsTable.id]
|
||||
val songs = PlaylistSongsTable
|
||||
.join(SongsTable, JoinType.INNER, PlaylistSongsTable.songId, SongsTable.id)
|
||||
.select(SongsTable.duration)
|
||||
.where { PlaylistSongsTable.playlistId eq id }
|
||||
.toList()
|
||||
val seedCount = MoodSeedsTable
|
||||
.selectAll()
|
||||
.where { MoodSeedsTable.playlistId eq id }
|
||||
.count()
|
||||
.toInt()
|
||||
|
||||
return MoodRecord(
|
||||
playlistId = id,
|
||||
owner = row[PlaylistsTable.owner],
|
||||
name = row[PlaylistsTable.name],
|
||||
comment = row[PlaylistsTable.comment],
|
||||
query = row[MoodsTable.query],
|
||||
genres = decodeGenres(row[MoodsTable.genres]),
|
||||
targetSize = row[MoodsTable.targetSize],
|
||||
ytSlots = row[MoodsTable.ytSlots],
|
||||
expandRelated = row[MoodsTable.expandRelated],
|
||||
refreshHours = row[MoodsTable.refreshHours],
|
||||
generatedAt = row[MoodsTable.generatedAt],
|
||||
generation = row[MoodsTable.generation],
|
||||
lastError = row[MoodsTable.lastError],
|
||||
created = row[PlaylistsTable.createdAt],
|
||||
changed = row[PlaylistsTable.changedAt],
|
||||
songCount = songs.size,
|
||||
duration = songs.sumOf { it[SongsTable.duration] },
|
||||
seedCount = seedCount,
|
||||
)
|
||||
}
|
||||
|
||||
private fun encodeGenres(genres: List<String>): String =
|
||||
Json.encodeToString(
|
||||
genreSerializer,
|
||||
genres.map { it.trim() }.filter { it.isNotEmpty() }.distinct().take(20),
|
||||
)
|
||||
|
||||
private fun decodeGenres(value: String): List<String> =
|
||||
runCatching { Json.decodeFromString(genreSerializer, value) }.getOrDefault(emptyList())
|
||||
}
|
||||
Reference in new issue
Block a user