feat: add client compatibility endpoints and a smoke test script
- getMusicDirectory for folder/album browsing - server-side play queues with savePlayQueue/getPlayQueue and index variants - jukebox status and internet radio compatibility responses - scripts/smoke.sh generates a tagged MP3 library, runs the server and verifies all endpoints clients use, including Range, download and covers
This commit is contained in:
1 parent
46266d8071
commit
4d313a2846
9 files changed
+474
No files matched your search
@@ -0,0 +1,33 @@
|
||||
package win.taptap.musicmanager.subsonic
|
||||
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
data class PlayQueueState(
|
||||
val songIds: List<String>,
|
||||
val currentIndex: Int,
|
||||
val positionMs: Long,
|
||||
val changed: String,
|
||||
val changedBy: String,
|
||||
)
|
||||
|
||||
/**
|
||||
* Server-side play queue per user. Kept in memory: losing it on restart only means
|
||||
* clients restore their local queue instead, which is acceptable for the MVP.
|
||||
*/
|
||||
object PlayQueueStore {
|
||||
|
||||
private val queues = ConcurrentHashMap<String, PlayQueueState>()
|
||||
|
||||
fun save(userName: String, songIds: List<String>, currentIndex: Int, positionMs: Long) {
|
||||
queues[userName] = PlayQueueState(
|
||||
songIds = songIds,
|
||||
currentIndex = currentIndex.coerceIn(0, (songIds.size - 1).coerceAtLeast(0)),
|
||||
positionMs = positionMs.coerceAtLeast(0),
|
||||
changed = Instant.now().toString(),
|
||||
changedBy = userName,
|
||||
)
|
||||
}
|
||||
|
||||
fun get(userName: String): PlayQueueState? = queues[userName]
|
||||
}
|
||||
@@ -118,6 +118,81 @@ fun Route.browsingRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
call.respondSubsonic(buildJsonObject { put("song", songJson(song)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getMusicDirectory") { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val id = parameters["id"]
|
||||
if (id.isNullOrBlank()) {
|
||||
call.respondSubsonicError(10, "Required parameter is missing: id")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val folders = user.effectiveFolderIds(library, parameters["musicFolderId"])
|
||||
if (id.startsWith("al-")) {
|
||||
val album = LibraryRepository.album(id, folders, user.name)
|
||||
if (album == null) {
|
||||
call.respondSubsonicError(70, "Album not found")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val songs = LibraryRepository.albumSongs(id, folders, user.name)
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("directory", buildJsonObject {
|
||||
put("id", album.id)
|
||||
put("title", album.name)
|
||||
album.artistName?.let { put("artist", it) }
|
||||
album.coverArt?.let { put("coverArt", it) }
|
||||
put("child", buildJsonArray { songs.forEach { add(songJson(it)) } })
|
||||
})
|
||||
},
|
||||
)
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val folderId = id.toIntOrNull()
|
||||
if (folderId == null || folderId !in folders) {
|
||||
call.respondSubsonicError(70, "Directory not found")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
val folder = library.byId(folderId) ?: run {
|
||||
call.respondSubsonicError(70, "Directory not found")
|
||||
return@subsonicGet
|
||||
}
|
||||
val albums = LibraryRepository.albumList(
|
||||
type = "alphabeticalByName",
|
||||
size = 10_000,
|
||||
offset = 0,
|
||||
fromYear = null,
|
||||
toYear = null,
|
||||
genre = null,
|
||||
folderIds = setOf(folderId),
|
||||
userName = user.name,
|
||||
)
|
||||
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("directory", buildJsonObject {
|
||||
put("id", folderId)
|
||||
put("title", folder.name)
|
||||
put("child", buildJsonArray {
|
||||
albums.forEach { album ->
|
||||
add(buildJsonObject {
|
||||
put("id", album.id)
|
||||
put("parent", folderId)
|
||||
put("title", album.name)
|
||||
put("isDir", true)
|
||||
put("album", album.name)
|
||||
album.artistName?.let { put("artist", it) }
|
||||
album.coverArt?.let { put("coverArt", it) }
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
val albumListTypes = setOf(
|
||||
"newest",
|
||||
"recent",
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package win.taptap.musicmanager.subsonic.routes
|
||||
|
||||
import io.ktor.server.routing.Route
|
||||
import kotlinx.serialization.json.add
|
||||
import kotlinx.serialization.json.buildJsonArray
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.put
|
||||
import win.taptap.musicmanager.config.LibraryConfig
|
||||
import win.taptap.musicmanager.config.UsersConfig
|
||||
import win.taptap.musicmanager.db.LibraryRepository
|
||||
import win.taptap.musicmanager.subsonic.PlayQueueStore
|
||||
import win.taptap.musicmanager.subsonic.models.songJson
|
||||
import win.taptap.musicmanager.subsonic.respondSubsonic
|
||||
|
||||
/** Optional server-side queue used by clients that support queue hand-off. */
|
||||
fun Route.playQueueRoutes(users: UsersConfig, library: LibraryConfig) {
|
||||
|
||||
for (endpoint in listOf("/savePlayQueue", "/savePlayQueueByIndex")) {
|
||||
subsonicGet(users, endpoint) { call, user ->
|
||||
val parameters = call.subsonicParams()
|
||||
val folders = user.visibleFolderIds(library)
|
||||
|
||||
val requestedIds = parameters.getAll("id").orEmpty().filter { it.isNotBlank() }
|
||||
val visible = requestedIds.filter { LibraryRepository.song(it, folders, user.name) != null }
|
||||
|
||||
val currentId = parameters["current"]
|
||||
val explicitIndex = parameters["currentIndex"]?.toIntOrNull()
|
||||
val currentIndex = explicitIndex
|
||||
?: currentId?.let { visible.indexOf(it) }?.takeIf { it >= 0 }
|
||||
?: 0
|
||||
|
||||
val position = parameters["position"]?.toLongOrNull() ?: 0L
|
||||
PlayQueueStore.save(user.name, visible, currentIndex, position)
|
||||
|
||||
call.respondSubsonic()
|
||||
}
|
||||
}
|
||||
|
||||
for (endpoint in listOf("/getPlayQueue", "/getPlayQueueByIndex")) {
|
||||
subsonicGet(users, endpoint) { call, user ->
|
||||
val state = PlayQueueStore.get(user.name)
|
||||
val folders = user.visibleFolderIds(library)
|
||||
val songs = LibraryRepository.songsByIds(state?.songIds.orEmpty(), folders, user.name)
|
||||
val byIndex = endpoint == "/getPlayQueueByIndex"
|
||||
|
||||
val queue = buildJsonObject {
|
||||
put("entry", buildJsonArray { songs.forEach { add(songJson(it)) } })
|
||||
put("position", state?.positionMs ?: 0L)
|
||||
put("username", user.name)
|
||||
put("changed", state?.changed.orEmpty())
|
||||
put("changedBy", state?.changedBy.orEmpty())
|
||||
|
||||
if (byIndex) {
|
||||
put("currentIndex", state?.currentIndex ?: 0)
|
||||
} else {
|
||||
state?.songIds?.getOrNull(state.currentIndex)?.let { put("current", it) }
|
||||
}
|
||||
}
|
||||
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put(if (byIndex) "playQueueByIndex" else "playQueue", queue)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,6 @@ fun Route.subsonicRoutes(
|
||||
mediaRoutes(users, library, coverArtService)
|
||||
interactionRoutes(users, library)
|
||||
playlistRoutes(users, library)
|
||||
playQueueRoutes(users, library)
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,44 @@ fun Route.systemRoutes(
|
||||
call.respondSubsonic(buildJsonObject { put("user", userJson(requested, library)) })
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getInternetRadioStations") { call, _ ->
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("internetRadioStations", buildJsonObject {
|
||||
put("internetRadioStation", buildJsonArray { })
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
for (endpoint in listOf(
|
||||
"/createInternetRadioStation",
|
||||
"/updateInternetRadioStation",
|
||||
"/deleteInternetRadioStation",
|
||||
)) {
|
||||
subsonicGet(users, endpoint) { call, _ ->
|
||||
call.respondSubsonicError(0, "Internet radio is not supported")
|
||||
}
|
||||
}
|
||||
|
||||
subsonicGet(users, "/jukeboxControl") { call, _ ->
|
||||
if (call.subsonicParams()["action"] != "status") {
|
||||
call.respondSubsonicError(0, "Jukebox is not supported")
|
||||
return@subsonicGet
|
||||
}
|
||||
|
||||
call.respondSubsonic(
|
||||
buildJsonObject {
|
||||
put("jukeboxStatus", buildJsonObject {
|
||||
put("currentIndex", 0)
|
||||
put("playing", false)
|
||||
put("gain", 1.0)
|
||||
put("position", 0)
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
subsonicGet(users, "/getUsers") { call, currentUser ->
|
||||
if (!currentUser.admin) {
|
||||
call.respondSubsonicError(50, "Not authorized")
|
||||
|
||||
@@ -335,6 +335,35 @@ class BrowsingRoutesTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `music directory and play queue expose folders albums and songs`() {
|
||||
seededContext().use { seed ->
|
||||
testApplication {
|
||||
setupApp(seed)
|
||||
val auth = credentials("alice", "alice-pw")
|
||||
|
||||
val folder = parseBody(
|
||||
client.get("/rest/getMusicDirectory?$auth&id=1").bodyAsText(),
|
||||
)["directory"]!!.jsonObject
|
||||
val folderChildren = folder["child"]!!.jsonArray
|
||||
assertEquals(1, folderChildren.size)
|
||||
assertEquals(seed.aliceAlbumId, folderChildren[0].jsonObject["id"]!!.jsonPrimitive.content)
|
||||
assertEquals("true", folderChildren[0].jsonObject["isDir"]!!.jsonPrimitive.content)
|
||||
|
||||
val album = parseBody(
|
||||
client.get("/rest/getMusicDirectory?$auth&id=${seed.aliceAlbumId}").bodyAsText(),
|
||||
)["directory"]!!.jsonObject
|
||||
assertEquals(seed.aliceSongId, album["child"]!!.jsonArray[0].jsonObject["id"]!!.jsonPrimitive.content)
|
||||
|
||||
client.get("/rest/savePlayQueue?$auth&id=${seed.aliceSongId}&position=1000")
|
||||
val queue = parseBody(client.get("/rest/getPlayQueue?$auth").bodyAsText())["playQueue"]!!
|
||||
.jsonObject
|
||||
assertEquals(seed.aliceSongId, queue["entry"]!!.jsonArray[0].jsonObject["id"]!!.jsonPrimitive.content)
|
||||
assertEquals(1000, queue["position"]!!.jsonPrimitive.int)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `lyrics endpoint returns an empty structure when the song has no lyrics`() {
|
||||
seededContext().use { seed ->
|
||||
|
||||
@@ -136,6 +136,20 @@ class SystemRoutesTest {
|
||||
assertEquals(10, missing["error"]!!.jsonObject["code"]!!.jsonPrimitive.int)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `jukebox and internet radio endpoints return valid empty responses`() = testApplication {
|
||||
setupApp()
|
||||
|
||||
val jukebox = parse(client.get("/rest/jukeboxControl?${credentials()}&action=status").bodyAsText())
|
||||
assertEquals("false", jukebox["jukeboxStatus"]!!.jsonObject["playing"]!!.jsonPrimitive.content)
|
||||
|
||||
val radio = parse(client.get("/rest/getInternetRadioStations?${credentials()}").bodyAsText())
|
||||
assertEquals(
|
||||
0,
|
||||
radio["internetRadioStations"]!!.jsonObject["internetRadioStation"]!!.jsonArray.size,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getUsers is admin only`() = testApplication {
|
||||
setupApp()
|
||||
|
||||
Reference in new issue
Block a user