feat!: replace OpenSubsonic with a native mood-first API
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:
1 parent
28bfdd9d00
commit
b33eb076c1
46 files changed
+3472
-5823
No files matched your search
@@ -0,0 +1,209 @@
|
||||
package win.taptap.musicmanager.api
|
||||
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.routing.Route
|
||||
import io.ktor.server.routing.delete
|
||||
import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.patch
|
||||
import io.ktor.server.routing.post
|
||||
import kotlinx.serialization.Serializable
|
||||
import win.taptap.musicmanager.api.AddSeedRequest
|
||||
import win.taptap.musicmanager.api.AddToMoodResponse
|
||||
import win.taptap.musicmanager.api.CreateMoodRequest
|
||||
import win.taptap.musicmanager.api.MoodDetailDto
|
||||
import win.taptap.musicmanager.api.MoodDto
|
||||
import win.taptap.musicmanager.api.RecommendationDto
|
||||
import win.taptap.musicmanager.api.SearchResponse
|
||||
import win.taptap.musicmanager.api.UpdateMoodRequest
|
||||
import win.taptap.musicmanager.api.seedDto
|
||||
import win.taptap.musicmanager.api.toDto
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UserAccount
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.LibraryRepository
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.radio.RadioService
|
||||
|
||||
@Serializable
|
||||
data class MoodsResponse(val moods: List<MoodDto>)
|
||||
|
||||
fun Route.moodRoutes(
|
||||
users: UsersConfig,
|
||||
library: LibraryConfig,
|
||||
radioService: RadioService,
|
||||
) {
|
||||
get("/moods") {
|
||||
val user = call.requireApiUser(users)
|
||||
call.respond(MoodsResponse(MoodRepository.list(user.name).map { it.toDto() }))
|
||||
}
|
||||
|
||||
post("/moods") {
|
||||
val user = call.requireApiUser(users)
|
||||
val request = call.receiveJson<CreateMoodRequest>()
|
||||
val name = request.name.trim()
|
||||
if (name.isEmpty()) badRequest("Mood name is required")
|
||||
|
||||
val folders = user.musicFolderIds(library)
|
||||
val seedIds = request.seedSongIds
|
||||
.distinct()
|
||||
.filter { LibraryRepository.song(it, folders, user.name) != null }
|
||||
|
||||
val mood = MoodRepository.create(
|
||||
owner = user.name,
|
||||
name = name,
|
||||
description = request.description?.trim()?.takeIf { it.isNotEmpty() },
|
||||
query = request.query?.trim()?.takeIf { it.isNotEmpty() },
|
||||
genres = request.genres,
|
||||
targetSize = request.targetSize,
|
||||
refreshHours = request.refreshHours,
|
||||
ytEnabled = request.ytEnabled,
|
||||
seedSongIds = seedIds,
|
||||
)
|
||||
call.respond(HttpStatusCode.Created, detail(mood, user, library))
|
||||
}
|
||||
|
||||
get("/moods/{id}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
call.respond(detail(mood, user, library))
|
||||
}
|
||||
|
||||
patch("/moods/{id}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val request = call.receiveJson<UpdateMoodRequest>()
|
||||
|
||||
val description = when {
|
||||
request.clearDescription -> null
|
||||
request.description != null -> request.description.trim().takeIf { it.isNotEmpty() }
|
||||
else -> mood.description
|
||||
}
|
||||
val query = when {
|
||||
request.clearQuery -> null
|
||||
request.query != null -> request.query.trim().takeIf { it.isNotEmpty() }
|
||||
else -> mood.query
|
||||
}
|
||||
|
||||
val updated = MoodRepository.update(
|
||||
id = mood.id,
|
||||
owner = user.name,
|
||||
name = request.name?.trim()?.takeIf { it.isNotEmpty() } ?: mood.name,
|
||||
description = description,
|
||||
query = query,
|
||||
genres = request.genres ?: mood.genres,
|
||||
targetSize = request.targetSize ?: mood.targetSize,
|
||||
refreshHours = request.refreshHours ?: mood.refreshHours,
|
||||
ytEnabled = request.ytEnabled ?: mood.ytEnabled,
|
||||
) ?: notFound("Mood")
|
||||
|
||||
call.respond(detail(updated, user, library))
|
||||
}
|
||||
|
||||
delete("/moods/{id}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
MoodRepository.delete(mood.id, user.name)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
post("/moods/{id}/seeds") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val request = call.receiveJson<AddSeedRequest>()
|
||||
val song = requireMusicSong(request.songId, user, library)
|
||||
val added = MoodRepository.addSeed(
|
||||
id = mood.id,
|
||||
owner = user.name,
|
||||
songId = song.id,
|
||||
origin = request.origin,
|
||||
)
|
||||
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
|
||||
call.respond(
|
||||
HttpStatusCode.OK,
|
||||
AddToMoodResponse(mood = refreshed.toDto(), added = added),
|
||||
)
|
||||
}
|
||||
|
||||
delete("/moods/{id}/seeds/{songId}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val songId = call.parameters["songId"] ?: badRequest("Song id is required")
|
||||
MoodRepository.removeSeed(mood.id, user.name, songId)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
/** A short, unpersisted radio preview for the mood detail screen. */
|
||||
get("/moods/{id}/preview") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
val limit = (call.request.queryParameters["limit"]?.toIntOrNull() ?: 20).coerceIn(1, 50)
|
||||
call.respond(SearchResponse(radioService.preview(mood, user, library, limit).map { it.toDto() }))
|
||||
}
|
||||
|
||||
get("/moods/{id}/recommendations") {
|
||||
val user = call.requireApiUser(users)
|
||||
val mood = ownedMood(call.parameters["id"], user)
|
||||
call.respond(recommendations(mood, user, library))
|
||||
}
|
||||
}
|
||||
|
||||
private fun ownedMood(id: String?, user: UserAccount): MoodRecord {
|
||||
if (id.isNullOrBlank()) badRequest("Mood id is required")
|
||||
return MoodRepository.get(id, user.name) ?: notFound("Mood")
|
||||
}
|
||||
|
||||
private fun requireMusicSong(songId: String, user: UserAccount, library: LibraryConfig) =
|
||||
LibraryRepository.song(songId, user.musicFolderIds(library), user.name)
|
||||
?: notFound("Song")
|
||||
|
||||
private fun detail(mood: MoodRecord, user: UserAccount, library: LibraryConfig): MoodDetailDto {
|
||||
val folders = user.musicFolderIds(library)
|
||||
val seedRecords = MoodRepository.seeds(mood.id)
|
||||
val seedSongs = LibraryRepository
|
||||
.songsByIds(seedRecords.map { it.songId }, folders, user.name)
|
||||
.associateBy { it.id }
|
||||
val seeds = seedRecords.mapNotNull { seed ->
|
||||
seedSongs[seed.songId]?.let { seedDto(seed, it) }
|
||||
}
|
||||
|
||||
return MoodDetailDto(
|
||||
mood = mood.toDto(),
|
||||
seeds = seeds,
|
||||
recommendations = recommendations(mood, user, library).recommendations,
|
||||
)
|
||||
}
|
||||
|
||||
private fun recommendations(
|
||||
mood: MoodRecord,
|
||||
user: UserAccount,
|
||||
library: LibraryConfig,
|
||||
): RecommendationsResponse {
|
||||
val records = MoodRepository.recommendations(mood.id)
|
||||
val matchedIds = records.mapNotNull { it.matchedSongId }
|
||||
val matchedSongs = LibraryRepository
|
||||
.songsByIds(matchedIds, user.visibleFolderIds(library), user.name)
|
||||
.associateBy { it.id }
|
||||
|
||||
return RecommendationsResponse(
|
||||
recommendations = records.map { record ->
|
||||
RecommendationDto(
|
||||
videoId = record.videoId,
|
||||
title = record.title,
|
||||
artist = record.artist,
|
||||
album = record.album,
|
||||
duration = record.duration,
|
||||
thumbnail = record.thumbnail,
|
||||
state = record.state,
|
||||
matchedSong = record.matchedSongId?.let { matchedSongs[it] }?.toDto(),
|
||||
timesAdded = record.timesAdded,
|
||||
firstSeen = record.firstSeen,
|
||||
lastSeen = record.lastSeen,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class RecommendationsResponse(val recommendations: List<RecommendationDto>)
|
||||
Reference in new issue
Block a user