feat!: replace OpenSubsonic with a native mood-first API
CI / chart (push) Successful in 3s
CI / chart-push (push) Successful in 3s
CI / test (push) Successful in 1m40s
CI / image (push) Successful in 3m12s

Remove the /rest compatibility layer and the playlist model. Moods are now
first-class private collections, and playback is driven by short-lived radio
sessions whose queues are generated by the server.

- Bearer device tokens (hashed) with revocable sessions.
- Library snapshot/delta sync with tombstones and per-folder scoping.
- Mood CRUD, seeds, preview and recommendations.
- Radio batches weighted by seeds, genres, artists, text and playback affinity.
- Playback events (started/completed/skipped) and explicit add-to-mood.
- Stream, download and cover endpoints with query-token support for browsers.
- V5 migration drops playlists and migrates existing moods; scanner writes
  tombstones and song update timestamps.
- Built-in UI rebuilt on the native API with a radio player.
- Native API integration tests and a rewritten smoke test (49 checks).
- Chart bumped to 0.2.0.

BREAKING CHANGE: the OpenSubsonic API and static playlists are gone.
This commit is contained in:
TapTap committed 2026-11-14 17:54:44 +00:00
1 parent 28bfdd9d00
commit b33eb076c1
46 files changed
+3472 -5823

No files matched your search

@@ -4,37 +4,34 @@ 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.SqlExpressionBuilder.eq
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.util.IdGenerator
data class MoodRecord(
val playlistId: String,
val id: String,
val owner: String,
val name: String,
val comment: String?,
val description: 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 ytEnabled: Boolean,
val lastDiscoveryAt: String?,
val createdAt: String,
val changedAt: String,
val lastError: String?,
val created: String,
val changed: String,
val songCount: Int,
val duration: Int,
val seedCount: Int,
val newRecommendations: Int,
)
data class MoodSeedRecord(
@@ -43,190 +40,117 @@ data class MoodSeedRecord(
val addedAt: String,
)
data class RecommendationRecord(
val videoId: String,
val title: String,
val artist: String,
val album: String?,
val duration: Int?,
val thumbnail: String?,
val state: String,
val matchedSongId: String?,
val timesAdded: Int,
val firstSeen: String,
val lastSeen: 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`.
* Moods are the product's only collection. Seeds are permanent anchors; the
* play queue is generated by [win.taptap.musicmanager.radio.RadioService] for
* every playback session instead of being stored as a playlist.
*/
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)
fun list(owner: String): List<MoodRecord> = transaction {
MoodsTable.selectAll()
.where { MoodsTable.owner eq owner }
.orderBy(MoodsTable.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")
}
fun get(id: String, owner: String): MoodRecord? = transaction {
MoodsTable.selectAll()
.where { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) }
.singleOrNull()
row?.let { moodRecord(it) }
?.let { moodRecord(it) }
}
fun create(
userName: String,
owner: String,
name: String,
comment: String?,
description: String?,
query: String?,
genres: List<String>,
targetSize: Int,
ytSlots: Int,
expandRelated: Boolean,
refreshHours: Int,
ytEnabled: Boolean,
seedSongIds: List<String>,
): MoodRecord {
val playlist = PlaylistRepository.create(
userName = userName,
name = name,
songIds = emptyList(),
isPublic = false,
kind = "mood",
): 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.targetSize] = targetSize.coerceIn(5, 500)
it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720)
it[MoodsTable.ytEnabled] = ytEnabled
it[createdAt] = now
it[changedAt] = now
}
insertSeeds(id, seedSongIds, "manual")
moodRecord(
MoodsTable.selectAll().where { MoodsTable.id eq id }.single(),
)
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,
id: String,
owner: String,
name: String,
comment: String?,
description: 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")
}
ytEnabled: Boolean,
): MoodRecord? = transaction {
val exists = MoodsTable.selectAll()
.where { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) }
.any()
if (!exists) return@transaction false
if (!exists) return@transaction null
PlaylistsTable.update({ PlaylistsTable.id eq playlistId }) {
it[PlaylistsTable.name] = name
it[PlaylistsTable.comment] = comment
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.targetSize] = targetSize.coerceIn(5, 500)
it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720)
it[MoodsTable.ytEnabled] = ytEnabled
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
moodRecord(MoodsTable.selectAll().where { MoodsTable.id eq id }.single())
}
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 delete(id: String, owner: String): Boolean = transaction {
MoodsTable.deleteWhere {
(MoodsTable.id eq id) and (MoodsTable.owner eq owner)
} > 0
}
fun seeds(playlistId: String): List<MoodSeedRecord> = transaction {
fun seeds(id: String): List<MoodSeedRecord> = transaction {
MoodSeedsTable.selectAll()
.where { MoodSeedsTable.playlistId eq playlistId }
.where { MoodSeedsTable.moodId eq id }
.orderBy(
MoodSeedsTable.addedAt to SortOrder.ASC,
MoodSeedsTable.songId to SortOrder.ASC,
@@ -240,165 +164,130 @@ object MoodRepository {
}
}
/** 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
fun seedSongIds(id: String): List<String> = seeds(id).map { it.songId }
/** Returns false when the song is already a seed or the mood is not owned. */
fun addSeed(id: String, owner: String, songId: String, origin: String): Boolean = transaction {
val owned = MoodsTable.selectAll()
.where { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) }
.any()
if (!owned) return@transaction false
val exists = MoodSeedsTable.selectAll()
.where {
(MoodSeedsTable.playlistId eq playlistId) and
(MoodSeedsTable.songId eq songId)
}
.where { (MoodSeedsTable.moodId eq id) and (MoodSeedsTable.songId eq songId) }
.any()
if (exists) return@transaction false
MoodSeedsTable.insert {
it[MoodSeedsTable.playlistId] = playlistId
val now = Instant.now().toString()
MoodSeedsTable.insertIgnore {
it[MoodSeedsTable.moodId] = id
it[MoodSeedsTable.songId] = songId
it[MoodSeedsTable.origin] = origin
it[addedAt] = Instant.now().toString()
it[MoodSeedsTable.origin] = origin.takeIf { value -> value in ORIGINS } ?: "manual"
it[addedAt] = now
}
MoodsTable.update({ MoodsTable.id eq id }) {
it[changedAt] = now
}
true
}
fun removeSeed(
playlistId: String,
userName: String,
songId: String,
origin: String? = null,
): Boolean = transaction {
if (get(playlistId, userName) == null) return@transaction false
fun removeSeed(id: String, owner: String, songId: String): Boolean = transaction {
val owned = MoodsTable.selectAll()
.where { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) }
.any()
if (!owned) 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
val removed = MoodSeedsTable.deleteWhere {
(MoodSeedsTable.moodId eq id) and (MoodSeedsTable.songId eq songId)
} > 0
if (removed) {
MoodsTable.update({ MoodsTable.id eq id }) {
it[changedAt] = Instant.now().toString()
}
}
removed
}
fun markStale(playlistId: String) {
transaction {
MoodsTable.update({ MoodsTable.playlistId eq playlistId }) {
it[generatedAt] = null
fun recommendations(id: String): List<RecommendationRecord> = transaction {
MoodRecommendationsTable.selectAll()
.where { MoodRecommendationsTable.moodId eq id }
.orderBy(MoodRecommendationsTable.lastSeen to SortOrder.DESC)
.map {
RecommendationRecord(
videoId = it[MoodRecommendationsTable.videoId],
title = it[MoodRecommendationsTable.title],
artist = it[MoodRecommendationsTable.artist],
album = it[MoodRecommendationsTable.album],
duration = it[MoodRecommendationsTable.duration],
thumbnail = it[MoodRecommendationsTable.thumbnail],
state = it[MoodRecommendationsTable.state],
matchedSongId = it[MoodRecommendationsTable.matchedSongId],
timesAdded = it[MoodRecommendationsTable.timesAdded],
firstSeen = it[MoodRecommendationsTable.firstSeen],
lastSeen = it[MoodRecommendationsTable.lastSeen],
)
}
}
}
/** Records a failed generation without claiming the mood is fresh. */
fun markError(playlistId: String, message: String) {
fun markError(id: String, message: String) {
transaction {
MoodsTable.update({ MoodsTable.playlistId eq playlistId }) {
MoodsTable.update({ MoodsTable.id eq id }) {
it[lastError] = message.take(1000)
it[generatedAt] = null
}
}
}
fun markAllStale() {
fun markDiscovered(id: String) {
transaction {
MoodsTable.update({ Op.TRUE }) {
it[generatedAt] = null
MoodsTable.update({ MoodsTable.id eq id }) {
it[lastDiscoveryAt] = Instant.now().toString()
it[lastError] = 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) {
private fun insertSeeds(id: 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
MoodSeedsTable.insertIgnore {
it[moodId] = id
it[this.songId] = songId
it[this.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 }
val id = row[MoodsTable.id]
val seedCount = MoodSeedsTable.selectAll()
.where { MoodSeedsTable.moodId eq id }
.count()
.toInt()
val newRecommendations = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq id) and
(MoodRecommendationsTable.state eq "new")
}
.count()
.toInt()
return MoodRecord(
playlistId = id,
owner = row[PlaylistsTable.owner],
name = row[PlaylistsTable.name],
comment = row[PlaylistsTable.comment],
id = id,
owner = row[MoodsTable.owner],
name = row[MoodsTable.name],
description = row[MoodsTable.description],
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],
ytEnabled = row[MoodsTable.ytEnabled],
lastDiscoveryAt = row[MoodsTable.lastDiscoveryAt],
createdAt = row[MoodsTable.createdAt],
changedAt = row[MoodsTable.changedAt],
lastError = row[MoodsTable.lastError],
created = row[PlaylistsTable.createdAt],
changed = row[PlaylistsTable.changedAt],
songCount = songs.size,
duration = songs.sumOf { it[SongsTable.duration] },
seedCount = seedCount,
newRecommendations = newRecommendations,
)
}
@@ -410,4 +299,6 @@ object MoodRepository {
private fun decodeGenres(value: String): List<String> =
runCatching { Json.decodeFromString(genreSerializer, value) }.getOrDefault(emptyList())
private val ORIGINS = setOf("manual", "recommendation", "yt")
}