fix(playlists): a replayed append whose reply was lost adds nothing more (#8122)

An append that reached the server but whose reply never reached the
phone stayed in the offline queue and was sent again, and since an
append is an action, the song landed twice. Android now names each
append with a request id, carried by the live send and its queued row
alike; POST /api/playlists/{id}/tracks takes it as request_id, and the
server records it per playlist in the append's own transaction
(playlist_append_requests, pruned after 30 days). A send whose id is
already recorded changes nothing; a failed append rolls its id back
with it. Without an id an append behaves as before, so queued rows
from older builds and Subsonic clients are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-28 11:52:12 -05:00
co-authored by Claude Opus 5.5
parent 6996561cb1
commit ad17849205
15 changed files with 207 additions and 20 deletions
@@ -93,12 +93,14 @@ interface PlaylistsApi {
}
/**
* POST /api/playlists/{id}/tracks body. The server expects snake_case
* `track_ids`.
* POST /api/playlists/{id}/tracks body. [requestId] names the append: the
* server applies an id once per playlist, so sending it again after a lost
* reply adds nothing more.
*/
@Serializable
data class AppendTracksRequest(
@SerialName("track_ids") val trackIds: List<String>,
@SerialName("request_id") val requestId: String? = null,
)
/**
@@ -131,15 +131,9 @@ class MutationQueue @Inject constructor(
),
)
suspend fun enqueuePlaylistAppend(
playlistId: String,
trackIds: List<String>,
): Long = insertUserDriven(
suspend fun enqueuePlaylistAppend(payload: PlaylistAppendPayload): Long = insertUserDriven(
MutationKind.PLAYLIST_APPEND,
json.encodeToString(
PlaylistAppendPayload.serializer(),
PlaylistAppendPayload(playlistId, trackIds),
),
json.encodeToString(PlaylistAppendPayload.serializer(), payload),
)
suspend fun enqueuePlaylistCreate(payload: PlaylistCreatePayload): Long = insertUserDriven(
@@ -294,11 +288,18 @@ data class QuarantineUnflagPayload(val trackId: String)
/** The key [MutationQueue.pendingLikeStates] files a like toggle under. */
fun likeToggleKey(entityType: String, entityId: String): String = "$entityType:$entityId"
/** Persisted payload for `MutationKind.PLAYLIST_APPEND`: the track IDs to append to one playlist. */
/**
* Persisted payload for `MutationKind.PLAYLIST_APPEND`: the track IDs to
* append to one playlist. [requestId] is the id the live send carried, so
* the server can tell a replay of an append it already applied (its reply
* was lost) from a new one. Rows queued before it existed have none and
* append on every send, as they always did.
*/
@Serializable
data class PlaylistAppendPayload(
val playlistId: String,
val trackIds: List<String>,
val requestId: String? = null,
)
/** Persisted payload for `MutationKind.PLAYLIST_CREATE`; [id] is the one the app chose. */
@@ -252,7 +252,10 @@ class MutationReplayer @Inject constructor(
when (row.kind) {
MutationKind.PLAYLIST_APPEND -> {
val p = json.decodeFromString(PlaylistAppendPayload.serializer(), payload)
playlistsApi.appendTracks(p.playlistId, AppendTracksRequest(trackIds = p.trackIds))
playlistsApi.appendTracks(
p.playlistId,
AppendTracksRequest(trackIds = p.trackIds, requestId = p.requestId),
)
}
MutationKind.PLAYLIST_CREATE -> {
val p = json.decodeFromString(PlaylistCreatePayload.serializer(), payload)
@@ -16,6 +16,7 @@ import com.fabledsword.minstrel.cache.db.dao.PlaylistCachedCount
import com.fabledsword.minstrel.cache.db.entities.CachedPlaylistEntity
import com.fabledsword.minstrel.cache.db.entities.CachedPlaylistTrackEntity
import com.fabledsword.minstrel.cache.mutations.MutationQueue
import com.fabledsword.minstrel.cache.mutations.PlaylistAppendPayload
import com.fabledsword.minstrel.cache.mutations.PlaylistCreatePayload
import com.fabledsword.minstrel.cache.mutations.PlaylistRemoveTrackPayload
import com.fabledsword.minstrel.cache.mutations.PlaylistReorderPayload
@@ -195,16 +196,19 @@ class PlaylistsRepository @Inject constructor(
/**
* Append a single track to a user-owned playlist. The (playlistId,
* trackId) key means a song already in the list keeps its place in
* Room.
* Room. The send and its queued replay carry one request id, so a
* replay of a send that reached the server, but whose reply didn't
* reach the phone, doesn't add the song a second time.
*/
suspend fun appendTrack(playlistId: String, trackId: String): WriteOutcome {
val nextPos = (playlistTrackDao.maxPosition(playlistId) ?: -1) + 1
playlistTrackDao.insertOrIgnore(
CachedPlaylistTrackEntity(playlistId = playlistId, trackId = trackId, position = nextPos),
)
val append = PlaylistAppendPayload(playlistId, listOf(trackId), requestId = UUID.randomUUID().toString())
return sendOrQueue(
send = { api.appendTracks(playlistId, AppendTracksRequest(trackIds = listOf(trackId))) },
queue = { mutationQueue.enqueuePlaylistAppend(playlistId, listOf(trackId)) },
send = { api.appendTracks(playlistId, AppendTracksRequest(append.trackIds, append.requestId)) },
queue = { mutationQueue.enqueuePlaylistAppend(append) },
)
}