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,137 @@
|
||||
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.post
|
||||
import io.ktor.server.routing.route
|
||||
import java.security.MessageDigest
|
||||
import win.taptap.musicmanager.api.DeviceDto
|
||||
import win.taptap.musicmanager.api.LoginRequest
|
||||
import win.taptap.musicmanager.api.LoginResponse
|
||||
import win.taptap.musicmanager.api.ScanStatusDto
|
||||
import win.taptap.musicmanager.api.UserDto
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.ApiTokenRepository
|
||||
import win.taptap.musicmanager.radio.RadioService
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
import win.taptap.musicmanager.transcoding.FfmpegTranscoder
|
||||
|
||||
/**
|
||||
* Routing for the native API.
|
||||
*
|
||||
* Every handler resolves the bearer token explicitly. Media URLs may also carry
|
||||
* `?token=` because HTML audio and `<img>` tags cannot set headers.
|
||||
*/
|
||||
fun Route.apiRoutes(
|
||||
users: UsersConfig,
|
||||
library: LibraryConfig,
|
||||
coverArtService: CoverArtService,
|
||||
transcoder: FfmpegTranscoder,
|
||||
radioService: RadioService,
|
||||
startScan: () -> Boolean,
|
||||
scanStatus: () -> ScanStatus,
|
||||
) {
|
||||
route("/api/v1") {
|
||||
post("/auth/login") {
|
||||
val request = call.receiveJson<LoginRequest>()
|
||||
val user = users.byName(request.username.trim())
|
||||
?: throw ApiException(
|
||||
HttpStatusCode.Unauthorized,
|
||||
"invalid_credentials",
|
||||
"Wrong username or password",
|
||||
)
|
||||
if (!constantTimeEquals(request.password, user.password)) {
|
||||
throw ApiException(
|
||||
HttpStatusCode.Unauthorized,
|
||||
"invalid_credentials",
|
||||
"Wrong username or password",
|
||||
)
|
||||
}
|
||||
|
||||
val issued = ApiTokenRepository.create(
|
||||
userName = user.name,
|
||||
deviceName = request.deviceName.ifBlank { "Unknown device" },
|
||||
platform = request.platform.ifBlank { "unknown" },
|
||||
)
|
||||
call.respond(
|
||||
HttpStatusCode.OK,
|
||||
LoginResponse(
|
||||
token = issued.raw,
|
||||
expiresAt = issued.record.expiresAt,
|
||||
user = user.toDto(library),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
get("/me") {
|
||||
val user = call.requireApiUser(users)
|
||||
call.respond(UserDto(
|
||||
name = user.name,
|
||||
admin = user.admin,
|
||||
musicFolders = user.musicFolderIds(library).sorted(),
|
||||
longformFolders = user.longformFolderIds(library).sorted(),
|
||||
))
|
||||
}
|
||||
|
||||
post("/auth/logout") {
|
||||
val user = call.requireApiUser(users)
|
||||
ApiTokenRepository.revokeRaw(call.bearerToken().orEmpty())
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
get("/auth/devices") {
|
||||
val user = call.requireApiUser(users)
|
||||
val current = call.apiToken()
|
||||
val devices = ApiTokenRepository.list(user.name).map { record ->
|
||||
DeviceDto(
|
||||
id = record.id,
|
||||
deviceName = record.deviceName,
|
||||
platform = record.platform,
|
||||
createdAt = record.createdAt,
|
||||
lastUsedAt = record.lastUsedAt,
|
||||
expiresAt = record.expiresAt,
|
||||
current = record.id == current?.id,
|
||||
)
|
||||
}
|
||||
call.respond(devices)
|
||||
}
|
||||
|
||||
delete("/auth/devices/{id}") {
|
||||
val user = call.requireApiUser(users)
|
||||
val id = call.parameters["id"] ?: badRequest("Device id is required")
|
||||
if (!ApiTokenRepository.revoke(id, user.name)) notFound("Device")
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
get("/scan") {
|
||||
call.requireApiUser(users)
|
||||
val status = scanStatus()
|
||||
call.respond(ScanStatusDto(status.scanning, status.count, status.error))
|
||||
}
|
||||
|
||||
post("/scan") {
|
||||
val user = call.requireApiUser(users)
|
||||
if (!user.admin) forbidden("Only admins can start a scan")
|
||||
val started = startScan()
|
||||
call.respond(
|
||||
HttpStatusCode.Accepted,
|
||||
ScanStatusDto(scanning = started, count = scanStatus().count),
|
||||
)
|
||||
}
|
||||
|
||||
libraryRoutes(users, library, coverArtService, transcoder)
|
||||
moodRoutes(users, library, radioService)
|
||||
sessionRoutes(users, library, radioService)
|
||||
}
|
||||
}
|
||||
|
||||
private fun constantTimeEquals(candidate: String, expected: String): Boolean =
|
||||
MessageDigest.isEqual(
|
||||
candidate.toByteArray(Charsets.UTF_8),
|
||||
expected.toByteArray(Charsets.UTF_8),
|
||||
)
|
||||
Reference in new issue
Block a user