From 6763a83b03421c838db6796b9a5767e5fdc48e77 Mon Sep 17 00:00:00 2001 From: TapTap Date: Tue, 17 Nov 2026 16:43:20 +0000 Subject: [PATCH] feat: report download status in the mood detail The client can show a per-track Downloading/Added/Failed state in search instead of relying on a snackbar under the keyboard. --- README.md | 2 +- .../win/taptap/musicmanager/api/ApiModels.kt | 12 ++++++++++++ .../kotlin/win/taptap/musicmanager/api/MoodApi.kt | 15 +++++++++++++++ .../win/taptap/musicmanager/api/ApiRoutesTest.kt | 10 +++++++--- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9652fc5..ce35d07 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ Media URLs may use `?token=` because HTML audio and images cannot send headers. |---|---|---| | `GET` | `/moods` | List the user's moods | | `POST` | `/moods` | Create `{name,description,query,genres,source,collectionPercent,recommendationCount,refreshHours}` | -| `GET` | `/moods/{id}` | Mood detail: songs and active recommendations | +| `GET` | `/moods/{id}` | Mood detail: songs, active recommendations and download status | | `PATCH` | `/moods/{id}` | Update options | | `DELETE` | `/moods/{id}` | Delete mood, detach songs and run GC | | `GET` | `/moods/{id}/preview` | Unpersisted radio preview | diff --git a/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt b/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt index c4923b0..fcddacf 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt @@ -131,6 +131,18 @@ data class MoodDetailDto( val mood: MoodDto, val songs: List = emptyList(), val recommendations: List = emptyList(), + /** In-flight and recent downloads, so the client can show a per-track status. */ + val downloads: List = emptyList(), +) + +@Serializable +data class DownloadStatusDto( + val source: String, + val externalId: String, + val state: String, + val attempts: Int = 0, + val error: String? = null, + val songId: String? = null, ) @Serializable diff --git a/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt b/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt index c427ae4..738a86c 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt @@ -342,6 +342,18 @@ private fun detail(mood: MoodRecord): MoodDetailDto { mood = mood.toDto(), songs = songs, recommendations = recommendations(mood), + downloads = DownloadRepository.forMood(mood.id) + .take(MAX_REPORTED_DOWNLOADS) + .map { job -> + DownloadStatusDto( + source = job.source, + externalId = job.externalId, + state = job.state, + attempts = job.attempts, + error = job.error, + songId = job.songId, + ) + }, ) } @@ -369,3 +381,6 @@ private fun recommendations(mood: MoodRecord): List { private fun recommendationSong(moodId: String, externalId: String): SongRecord? = MoodRepository.recommendation(moodId, externalId)?.songId?.let(SongRepository::byId) + +/** Newest first; older finished jobs do not need to reach the client. */ +private const val MAX_REPORTED_DOWNLOADS = 100 diff --git a/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt b/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt index 4103408..425bf46 100644 --- a/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt +++ b/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt @@ -398,9 +398,13 @@ class ApiRoutesTest { setBody("""{"source":"youtube","externalId":"vid-new"}""") } assertEquals(HttpStatusCode.Accepted, queued.status) - assertTrue( - parse(queued.bodyAsText())["downloading"]!!.jsonPrimitive.content.toBoolean(), - ) + val queuedBody = parse(queued.bodyAsText()) + assertTrue(queuedBody["downloading"]!!.jsonPrimitive.content.toBoolean()) + val queuedDownload = queuedBody["detail"]!!.jsonObject["downloads"]!!.jsonArray + .map { it.jsonObject } + .first { it.string("externalId") == "vid-new" } + assertEquals("queued", queuedDownload.string("state")) + assertEquals("youtube", queuedDownload.string("source")) val youtubeJob = DownloadRepository.find( moodId, "vid-new",