Modern Apps git

← Modern-Apps.git

musicbrainz: key downloads by release now track MBIDs are gone

Commit
32870db8954e4c5c3cc2fabffe8f6c7b07d5ca5d
Author
vayun-mathur <[email protected]>
When
2026-08-20 21:17 UTC (3 months ago)
Parents
3070b21

Diff

musicbrainz/src/main/java/com/vayunmathur/musicbrainz/data/download/OggOpusTagger.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/data/download/OggOpusTagger.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/data/download/OggOpusTagger.ktindex 980c1c8a89..6c47eef647 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/data/download/OggOpusTagger.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/data/download/OggOpusTagger.kt@@ -4,7 +4,13 @@ import java.io.ByteArrayOutputStream import java.nio.ByteBuffer import java.nio.ByteOrder -/** Everything the Ogg/Opus tagger will write. Null/blank fields are simply omitted. */+/**+ * Everything the Ogg/Opus tagger will write. Null/blank fields are simply omitted.+ *+ * No release-track id: the catalogue carries no per-track MBID, so there was never a value+ * to write. [com.vayunmathur.musicbrainz.data.library.TagReader] still reads that tag, because+ * files downloaded before the switch carry it and have to keep matching.+ */ data class VorbisTags(     val title: String? = null,     val artist: String? = null,@@ -17,7 +23,6 @@ data class VorbisTags(     val lyrics: String? = null,     val recordingId: String? = null,     val releaseId: String? = null,-    val releaseTrackId: String? = null,     val coverArt: ByteArray? = null,     val coverIsPng: Boolean = false, )
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.ktindex 72e39fdd37..0cfac89c14 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/network/api/MusicBrainzApi.kt@@ -8,7 +8,8 @@ import java.net.URLEncoder  /**  * The server has a catalogue to serve, but not yet: either the data pack was never- * imported or an import is still running.+ * imported or an import is still running. The server signals both as HTTP 503 with an+ * `{"error":"not_ready"}` body, and reserves other statuses for genuine faults.  *  * Kept apart from an ordinary failure because it resolves on its own, so the UI can tell  * the user to come back shortly instead of reporting a fault they cannot act on.@@ -36,10 +37,16 @@ object MusicBrainzApi {     )      /**-     * `coerceInputValues` is the load-bearing setting: every field in the wire models is-     * declared with a default, but a default only applies to a key that is absent. An-     * explicit `null` on one of the non-nullable fields would otherwise abort the whole-     * decode, turning one unset field into a blank screen.+     * `coerceInputValues` makes an explicit `null` fall back to the declared default instead+     * of aborting the decode. Every field in the wire models has a default, but a default only+     * applies to a key that is ABSENT - a `null` on one of the non-nullable fields would+     * otherwise turn one unset value into a blank screen.+     *+     * The server guarantees it never emits `null` anywhere, enforced on its side by+     * `skip_serializing_if` plus a test, so this is defence in depth rather than something the+     * current contract relies on. It is here because the failure it prevents is invisible+     * until it happens against the live server, and `MbTrack.id` - the field the catalogue+     * stopped carrying - is the one most likely to arrive that way if the guarantee ever slips.      *      * Internal rather than private so `MusicBrainzModelsTest` decodes through the real      * configuration instead of a copy of it.
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.ktindex 44f28f1f2d..9abece5f0c 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzUiContract.kt@@ -77,8 +77,10 @@ data class TrackRow(      * [com.vayunmathur.musicbrainz.platform.download.DownloadRequest.key] or a row cannot      * find the download it started. Covered by `DownloadKeyTest`.      */-    fun downloadKey(album: String?): String =-        releaseTrackId ?: recordingId ?: "$artist\u0000$album\u0000$title"+    fun downloadKey(releaseId: String?, album: String?): String =+        releaseTrackId+            ?: recordingId?.let { recording -> releaseId?.let { "$it\u0000$recording" } ?: recording }+            ?: "$artist\u0000$album\u0000$title" }  data class SearchUiState(
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.ktindex 96666dda6a..69716644a1 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/MusicBrainzViewModel.kt@@ -91,7 +91,7 @@ class MusicBrainzViewModel(application: Application) : AndroidViewModel(applicat                             album = state.title,                             title = track.title,                         ),-                        download = downloads[track.downloadKey(state.title)],+                        download = downloads[track.downloadKey(state.id, state.title)],                     )                 },             )
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadQueue.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadQueue.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadQueue.ktindex 13b9bbedb5..785021aadc 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadQueue.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadQueue.kt@@ -43,11 +43,19 @@ data class DownloadRequest(     /**      * Stable identity for the queue and for WorkManager's unique work name.      *-     * Prefers the release track id so the same recording appearing on two albums is two-     * separate downloads rather than one silently replacing the other.+     * The same recording on two albums has to be two separate downloads rather than one+     * silently replacing the other. The release-track id used to carry that on its own, but+     * the catalogue no longer has one, so the release is folded in alongside the recording -+     * without it a track queued from a second edition collides with the first, and the file+     * that lands is tagged with the wrong album.+     *+     * Mirrored by [com.vayunmathur.musicbrainz.platform.TrackRow.downloadKey], which is how a+     * row finds the download it started. `DownloadKeyTest` pins the two together.      */     val key: String-        get() = releaseTrackId ?: recordingId ?: "$artist\u0000$album\u0000$title"+        get() = releaseTrackId+            ?: recordingId?.let { recording -> releaseId?.let { "$it\u0000$recording" } ?: recording }+            ?: "$artist\u0000$album\u0000$title" }  /**
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.ktindex 45b4ad51b6..a3f217a939 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/platform/download/DownloadWorker.kt@@ -265,7 +265,6 @@ private fun DownloadRequest.toVorbisTags(cover: ByteArray?, lyrics: String?) = V     lyrics = lyrics,     recordingId = recordingId,     releaseId = releaseId,-    releaseTrackId = releaseTrackId,     coverArt = cover,     coverIsPng = cover.isPng(), )
musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt
diff --git a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.ktindex 853dd77df4..fe2d431bcf 100644--- a/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt+++ b/musicbrainz/src/main/java/com/vayunmathur/musicbrainz/ui/ReleaseScreen.kt@@ -124,7 +124,7 @@ fun ReleaseScreen(                                 onDevice = track.onDevice,                                 download = track.download,                                 onDownload = { actions.downloadTrack(track) },-                                onCancel = { actions.cancelDownload(track.downloadKey(state.title)) },+                                onCancel = { actions.cancelDownload(track.downloadKey(state.id, state.title)) },                             )                         },                     )
musicbrainz/src/test/java/com/vayunmathur/musicbrainz/data/download/OggOpusTaggerTest.kt
diff --git a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/data/download/OggOpusTaggerTest.kt b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/data/download/OggOpusTaggerTest.ktindex e3df0db8a9..a0fd4e9e01 100644--- a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/data/download/OggOpusTaggerTest.kt+++ b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/data/download/OggOpusTaggerTest.kt@@ -128,7 +128,6 @@ class OggOpusTaggerTest {                     trackTotal = 8,                     recordingId = "11111111-2222-3333-4444-555555555555",                     releaseId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",-                    releaseTrackId = "99999999-8888-7777-6666-555555555555",                 ),             ),         )@@ -140,9 +139,9 @@ class OggOpusTaggerTest {         assertEquals("The Neon Owls", tags.albumArtist)         assertEquals("11111111-2222-3333-4444-555555555555", tags.recordingId)         assertEquals("aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", tags.releaseId)-        // Not written any more: the catalogue has no per-track MBID, so the tagger drops the-        // key even when handed a value. TagReader still parses it for files written earlier,-        // which `TagReaderTest` covers.+        // VorbisTags has no release-track id to give it: the catalogue carries no per-track+        // MBID. TagReader still parses the tag for files written earlier, which+        // `TagReaderTest` covers.         assertNull(tags.releaseTrackId)     } 
musicbrainz/src/test/java/com/vayunmathur/musicbrainz/platform/DownloadKeyTest.kt
diff --git a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/platform/DownloadKeyTest.kt b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/platform/DownloadKeyTest.ktindex 621d8476a0..55d9f55b7f 100644--- a/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/platform/DownloadKeyTest.kt+++ b/musicbrainz/src/test/java/com/vayunmathur/musicbrainz/platform/DownloadKeyTest.kt@@ -28,10 +28,14 @@ class DownloadKeyTest {         artist = ARTIST,     ) -    private fun request(releaseTrackId: String?, recordingId: String?) = DownloadRequest(+    private fun request(+        releaseTrackId: String?,+        recordingId: String?,+        releaseId: String? = RELEASE_ID,+    ) = DownloadRequest(         recordingId = recordingId,         releaseTrackId = releaseTrackId,-        releaseId = null,+        releaseId = releaseId,         releaseGroupId = null,         title = TITLE,         artist = ARTIST,@@ -46,25 +50,54 @@ class DownloadKeyTest {      @Test     fun `agrees with the download request for every combination of ids`() {-        val combinations = listOf(-            "trk-1" to "rec-1",-            null to "rec-1",-            "trk-1" to null,-            null to null,-        )-        for ((releaseTrackId, recordingId) in combinations) {-            assertEquals(-                request(releaseTrackId, recordingId).key,-                trackRow(releaseTrackId, recordingId).downloadKey(ALBUM),-                "key disagreed for releaseTrackId=$releaseTrackId recordingId=$recordingId",-            )+        val ids = listOf("trk-1" to "rec-1", null to "rec-1", "trk-1" to null, null to null)+        for (releaseId in listOf(RELEASE_ID, null)) {+            for ((releaseTrackId, recordingId) in ids) {+                assertEquals(+                    request(releaseTrackId, recordingId, releaseId).key,+                    trackRow(releaseTrackId, recordingId).downloadKey(releaseId, ALBUM),+                    "disagreed for releaseTrackId=$releaseTrackId " ++                        "recordingId=$recordingId releaseId=$releaseId",+                )+            }         }     } -    /** The case the catalogue now produces: no track MBID, so the recording id carries it. */+    /**+     * The case the catalogue now produces: no track MBID, so the release and the recording+     * together carry the identity.+     */     @Test-    fun `falls back to the recording id when there is no release track id`() {-        assertEquals("rec-1", trackRow(null, "rec-1").downloadKey(ALBUM))+    fun `combines the release and recording when there is no release track id`() {+        assertEquals(+            "$RELEASE_ID\u0000rec-1",+            trackRow(null, "rec-1").downloadKey(RELEASE_ID, ALBUM),+        )+    }++    /**+     * The bug this formula exists to prevent. Before the release was folded in, the same+     * recording queued from two editions collapsed to one queue entry and one WorkManager+     * unique name, so the second download was dropped and the file that landed carried the+     * first release's album and track numbers.+     */+    @Test+    fun `keeps the same recording on two releases apart`() {+        val row = trackRow(null, "rec-1")+        assertNotEquals(+            row.downloadKey("release-first", ALBUM),+            row.downloadKey("release-second", ALBUM),+        )+        assertNotEquals(+            request(null, "rec-1", "release-first").key,+            request(null, "rec-1", "release-second").key,+        )+    }++    /** With no release either, the recording id alone still identifies it. */+    @Test+    fun `falls back to the recording id alone without a release`() {+        assertEquals("rec-1", trackRow(null, "rec-1").downloadKey(null, ALBUM))     }      /**@@ -75,13 +108,17 @@ class DownloadKeyTest {     @Test     fun `distinguishes the same untagged song on two albums`() {         val row = trackRow(null, null)-        assertEquals("$ARTIST\u0000$ALBUM\u0000$TITLE", row.downloadKey(ALBUM))-        assertNotEquals(row.downloadKey(ALBUM), row.downloadKey("A Different Album"))+        assertEquals("$ARTIST\u0000$ALBUM\u0000$TITLE", row.downloadKey(RELEASE_ID, ALBUM))+        assertNotEquals(+            row.downloadKey(RELEASE_ID, ALBUM),+            row.downloadKey(RELEASE_ID, "A Different Album"),+        )     }      private companion object {         const val TITLE = "Weird Fishes"         const val ARTIST = "Radiohead"         const val ALBUM = "In Rainbows"+        const val RELEASE_ID = "rel-1"     } }