Skip to content

Native Android client for Spotify, built with Jetpack Compose

Leave a collaborative playlist: Playlists.leave (PlaylistPermissionsAPI.removeMember), menu entry gated by canCancelMembership, confirmation, docs

kota-zo138407c826e1
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt
index bc42a5dc..7bdb3370 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/Mappers.kt
@@ -105,6 +105,7 @@ internal fun playlist(d: JsonElement?): Playlist? {
ownerImages = images(d.list("ownerV2.data.avatar.sources")),
followers = d.long("followers"),
isBlend = d.str("format") == "blend",
+ canLeave = d.bool("currentUserCapabilities.canCancelMembership") ?: false,
)
}
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt
index 3bafc1d3..9b860ff8 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyLibrary.kt
@@ -312,6 +312,18 @@ class SpotifyPlaylists(private val gql: Gql, private val bridge: Bridge, private
m.list("user.images").firstNotNullOfOrNull { it.str("url") }, m.bool("isOwner") == true)
}.sortedByDescending { it.isOwner }
+ /**
+ * The page's "Leave playlist" (menu entry gated by `canCancelMembership`, then its dialog):
+ * `PlaylistPermissionsAPI.removeMember(uri, ownUsername)`, which is spclient
+ * `DELETE /playlist/{id}/permission/member/user/{username}`. As in the dialog, a private
+ * playlist is taken out of the rootlist first; a public one stays followed (read-only).
+ */
+ override suspend fun leave(uri: Uri) {
+ val me = bridge.service("UserAPI", "getUser").str("username")?.takeIf { it.isNotBlank() } ?: error("no signed-in user")
+ if (access(uri) == PlaylistAccess.PRIVATE) unfollow(uri)
+ bridge.service("PlaylistPermissionsAPI", "removeMember", JsonPrimitive(uri), JsonPrimitive(me))
+ }
+
/**
* The page's `PlaylistAPI.getContents(uri, {offset, limit})`: each row carries `uid` and
* `addedBy {username}`. Read a page at a time to the end (a few hundred rows a request).
diff --git a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyWritesTest.kt b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyWritesTest.kt
index 6a71c389..68910505 100644
--- a/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyWritesTest.kt
+++ b/base/api-spotify/src/test/kotlin/app/myspoti/spotify/SpotifyWritesTest.kt
@@ -189,6 +189,38 @@ class SpotifyWritesTest {
assertEquals("""["spotify:playlist:p","tok123"]""", host.calls[0].args())
}
+ @Test fun `leaving a collaborative playlist removes this account from its members, by username`() = runBridge({ call ->
+ when (call.method()) {
+ "getUser" -> Bridge.json.parseToJsonElement("""{"username":"user0002"}""")
+ "getBasePermission" -> kotlinx.serialization.json.JsonPrimitive("CONTRIBUTOR")
+ else -> JsonNull
+ }
+ }) { host, bridge ->
+ val playlists = SpotifyPlaylists(FakeGql { _, _ -> JsonNull }, bridge, SpotifyHttp(OkHttpClient(), bridge))
+ playlists.leave("spotify:playlist:p")
+ assertEquals(listOf("getUser", "getBasePermission", "removeMember"), host.calls.map { it.method() })
+ assertEquals("""["spotify:playlist:p","user0002"]""", host.calls.last().args())
+ }
+
+ @Test fun `leaving a private playlist takes it out of the library first, as the page's dialog does`() = runBridge({ call ->
+ when (call.method()) {
+ "getUser" -> Bridge.json.parseToJsonElement("""{"username":"user0002"}""")
+ "getBasePermission" -> kotlinx.serialization.json.JsonPrimitive("BLOCKED")
+ else -> JsonNull
+ }
+ }) { host, bridge ->
+ val playlists = SpotifyPlaylists(FakeGql { _, _ -> JsonNull }, bridge, SpotifyHttp(OkHttpClient(), bridge))
+ playlists.leave("spotify:playlist:p")
+ assertEquals(listOf("getUser", "getBasePermission", "remove", "removeMember"), host.calls.map { it.method() })
+ }
+
+ @Test fun `a playlist can be left only when its capabilities say canCancelMembership`() {
+ fun parse(flag: String) = playlist(Bridge.json.parseToJsonElement("""{"uri":"spotify:playlist:p","name":"n","currentUserCapabilities":{"canEditItems":true,"canCancelMembership":$flag}}"""))!!
+ assertEquals(true, parse("true").canLeave)
+ assertEquals(false, parse("false").canLeave)
+ assertEquals(false, playlist(Bridge.json.parseToJsonElement("""{"uri":"spotify:playlist:p","name":"n"}"""))!!.canLeave)
+ }
+
@Test fun `a Blend invitation id is read from the link the page generates, or given bare`() {
val blends = SpotifyBlends(SpotifyHttp(OkHttpClient(), Bridge(RecordingHost { JsonNull }, scope)))
assertEquals("f6c3d87d8beb9a3f", blends.idOf("https://open.spotify.com/blend/taste-match/f6c3d87d8beb9a3f?si=DJpS4omqRFOjF4es_Fnm8A&fallback=getapp&blendDecoration=5f9c38d2"))
diff --git a/base/api/src/main/kotlin/app/myspoti/api/Api.kt b/base/api/src/main/kotlin/app/myspoti/api/Api.kt
index aa1abff0..95e41b15 100644
--- a/base/api/src/main/kotlin/app/myspoti/api/Api.kt
+++ b/base/api/src/main/kotlin/app/myspoti/api/Api.kt
@@ -171,6 +171,13 @@ interface Playlists {
/** The owner and the collaborators of a playlist (just the owner when it is not shared). */
suspend fun members(uri: Uri): List<PlaylistMember>
+ /**
+ * Leaves a playlist this account joined as a collaborator ([Playlist.canLeave]): the page's
+ * `PlaylistPermissionsAPI.removeMember(uri, ownUsername)`. A private one (owner-only base
+ * permission) also leaves the library, as in the Web Player's dialog.
+ */
+ suspend fun leave(uri: Uri)
+
/** Who added each row of a playlist (shown on collaborative ones). */
suspend fun addedBy(uri: Uri): List<RowAdder>
diff --git a/base/api/src/main/kotlin/app/myspoti/api/Models.kt b/base/api/src/main/kotlin/app/myspoti/api/Models.kt
index 83facba5..528a2a44 100644
--- a/base/api/src/main/kotlin/app/myspoti/api/Models.kt
+++ b/base/api/src/main/kotlin/app/myspoti/api/Models.kt
@@ -62,6 +62,8 @@ data class Playlist(
val followers: Long? = null,
/** A Spotify Blend (a playlist shared by taste between people). */
val isBlend: Boolean = false,
+ /** This account is a member (not the owner) and may leave: `currentUserCapabilities.canCancelMembership`. */
+ val canLeave: Boolean = false,
)
/**
diff --git a/base/app/src/main/java/com/music/spotui/data/api/Api.kt b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
index c02e9356..0a7f57d4 100644
--- a/base/app/src/main/java/com/music/spotui/data/api/Api.kt
+++ b/base/app/src/main/java/com/music/spotui/data/api/Api.kt
@@ -825,6 +825,7 @@ class Api constructor(
ownerUri = pl.ownerUri.orEmpty(),
metaLine = saves,
isBlend = pl.isBlend,
+ canLeave = pl.canLeave,
)
// The first page gives the total: the rest is asked for PLAYLIST_PARALLEL pages at a time
// and shown in order as it lands, at most every PLAYLIST_EMIT_MS. One page after the
diff --git a/base/app/src/main/java/com/music/spotui/data/entity/ContextPageModel.kt b/base/app/src/main/java/com/music/spotui/data/entity/ContextPageModel.kt
index fb2ef34c..650456cc 100644
--- a/base/app/src/main/java/com/music/spotui/data/entity/ContextPageModel.kt
+++ b/base/app/src/main/java/com/music/spotui/data/entity/ContextPageModel.kt
@@ -17,4 +17,6 @@ data class ContextPageModel(
val complete: Boolean = true,
/** A Spotify Blend: its page shows the members and the taste match. */
val isBlend: Boolean = false,
+ /** A member who is not the owner and may leave (`canCancelMembership`). */
+ val canLeave: Boolean = false,
)
diff --git a/base/app/src/main/java/com/music/spotui/ui/components/PlaylistOptionsSheet.kt b/base/app/src/main/java/com/music/spotui/ui/components/PlaylistOptionsSheet.kt
index c1a8d228..65be0a4c 100644
--- a/base/app/src/main/java/com/music/spotui/ui/components/PlaylistOptionsSheet.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/components/PlaylistOptionsSheet.kt
@@ -112,6 +112,7 @@ fun PlaylistOptionsSheet(
var deleting by remember { mutableStateOf(false) }
var renamingBlend by remember { mutableStateOf(false) }
var leavingBlend by remember { mutableStateOf(false) }
+ var leavingPlaylist by remember { mutableStateOf(false) }
LaunchedEffect(page.uri, mine) {
if (mine) {
access = runCatching { MyspotiRuntime.api.playlists.access(page.uri) }.getOrNull()
@@ -210,6 +211,8 @@ fun PlaylistOptionsSheet(
OptionRow(Icons.Outlined.Delete, stringResource(R.string.local_delete_playlist)) { deleting = true }
} else {
OptionRow(Icons.Outlined.QrCode2, stringResource(R.string.context_menu_share), onClick = share)
+ // The Web Player offers it only where canCancelMembership is true (a collaborator, not the owner).
+ if (page.canLeave) OptionRow(Icons.AutoMirrored.Outlined.Logout, stringResource(R.string.playlist_leave)) { leavingPlaylist = true }
}
if (!mine && !page.isBlend && page.ownerUri.startsWith("spotify:user:")) {
OptionRow(Icons.Outlined.Person, stringResource(R.string.go_to_profile)) {
@@ -269,6 +272,24 @@ fun PlaylistOptionsSheet(
dismissButton = { TextButton(onClick = { leavingBlend = false }) { Text(stringResource(R.string.cancel), color = Color.White) } },
)
+ if (leavingPlaylist) AlertDialog(
+ onDismissRequest = { leavingPlaylist = false },
+ containerColor = Color(0xFF282828),
+ title = { Text(stringResource(R.string.playlist_leave), color = Color.White) },
+ text = { Text(stringResource(R.string.playlist_leave_confirm, page.name), color = Color(0xFFB3B3B3)) },
+ confirmButton = {
+ TextButton(onClick = {
+ leavingPlaylist = false
+ // The page closes now (as in the Web Player); a refusal says so, and the library is read again.
+ com.music.spotui.data.api.LibraryEdits.run(context, null,
+ write = { MyspotiRuntime.api.playlists.leave(page.uri) },
+ onDone = { ok -> if (ok) com.music.spotui.data.api.LibraryEdits.reloadSoon() })
+ onDismiss(); onDeleted()
+ }) { Text(stringResource(R.string.playlist_leave_button), color = Color(0xFFF15E6C)) }
+ },
+ dismissButton = { TextButton(onClick = { leavingPlaylist = false }) { Text(stringResource(R.string.cancel), color = Color.White) } },
+ )
+
if (deleting) AlertDialog(
onDismissRequest = { deleting = false },
containerColor = Color(0xFF282828),
diff --git a/base/app/src/main/res/values-fr/myspoti_strings.xml b/base/app/src/main/res/values-fr/myspoti_strings.xml
index 0091b16c..57bedd48 100644
--- a/base/app/src/main/res/values-fr/myspoti_strings.xml
+++ b/base/app/src/main/res/values-fr/myspoti_strings.xml
@@ -492,6 +492,9 @@
<string name="playlist_invite_message">Rejoins ma playlist « %1$s » sur Spotify, tu pourras y ajouter des titres : %2$s</string>
<string name="playlist_invite_failed">Impossible de créer le lien d’invitation</string>
<string name="playlist_by">De %1$s</string>
+ <string name="playlist_leave">Quitter la playlist</string>
+ <string name="playlist_leave_confirm">Quitter %1$s ? Tu ne seras plus collaborateur et ne pourras plus ajouter ni retirer de titres. Pour revenir, il faudra un nouveau lien d’invitation.</string>
+ <string name="playlist_leave_button">Quitter</string>
<string name="playlist_make_public">Rendre publique</string>
<string name="playlist_make_private">Rendre privée</string>
<string name="playlist_stop_collab">Arrêter la collaboration</string>
diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xml
index da789139..71a3db26 100644
--- a/base/app/src/main/res/values/myspoti_strings.xml
+++ b/base/app/src/main/res/values/myspoti_strings.xml
@@ -493,6 +493,9 @@
<string name="playlist_invite_message">Join my playlist “%1$s” on Spotify, you can add songs to it: %2$s</string>
<string name="playlist_invite_failed">Couldn’t make the invitation link</string>
<string name="playlist_by">By %1$s</string>
+ <string name="playlist_leave">Leave playlist</string>
+ <string name="playlist_leave_confirm">Leave %1$s? You will no longer be a collaborator and won’t be able to add or remove songs. To join again, you will need a new invitation link.</string>
+ <string name="playlist_leave_button">Leave</string>
<string name="playlist_make_public">Make public</string>
<string name="playlist_make_private">Make private</string>
<string name="playlist_stop_collab">Stop collaborating</string>
diff --git a/docs/api/LIBRARY.md b/docs/api/LIBRARY.md
index bfa8facc..53ad246d 100644
--- a/docs/api/LIBRARY.md
+++ b/docs/api/LIBRARY.md
@@ -466,6 +466,29 @@ Rootlist and liked-songs-artist pushes carried only fields 1, 2 and 9. Simplest
rule: on any playlist push, re-fetch that playlist (or the rootlist / `libraryV3`) unless
the revision equals the one already held.
+## Leaving a collaborative playlist
+
+The playlist menu of the Web Player shows "Leave playlist" only when the playlist's
+`currentUserCapabilities.canCancelMembership` is true (a member who is not the owner; same flag
+in `fetchPlaylist` and in the rootlist item `permissions`). The entry opens a confirmation
+dialog (`leave-playlist.dialog.*`: title, a description that depends on private / public
+contributor / public listener, Cancel, Leave). Read from the bundle (`xpui-root-dialogs`, module
+`PlaylistPermissionsAPI`; not sent on a live account yet):
+
+| Step | Call |
+|---|---|
+| private playlist only (base permission `BLOCKED`) and in the library | `RootlistAPI.remove([{uri}])` (the playlist leaves the library) |
+| the leave itself | page service `PlaylistPermissionsAPI.removeMember(playlistUri, ownUsername)` |
+| then | the dialog closes and the page navigates to `/` |
+
+`removeMember` sends spclient `DELETE /playlist/{id}/permission/member/user/{username}` (JSON content
+type, no body, no market; endpoint id `/playlist/{id}/permission/member/user/{username}`). The
+username is the account's own (`UserAPI.getUser().username`); the same call, with someone else's
+username, is the owner's "Remove from playlist" in the members list. A public playlist stays in the
+library after leaving (followed, read-only); to join again a new invitation link is needed
+(`claimPermissions`). In the app: `Playlists.leave(uri)`, offered in the playlist's menu when
+`Playlist.canLeave`.
+
## Blend ("Mix à plusieurs")
The Web Player has Blend routes (`/blend/invitation`, `/blend/taste-match/{id}`), all on one