[bugfix]: fix jellyfin add to playlist

This commit is contained in:
Kendall Garner 2024-05-02 18:42:49 -07:00
parent 297d6f0d2e
commit d1bcd2b2fb
No known key found for this signature in database
GPG Key ID: 18D2767419676C87
2 changed files with 39 additions and 25 deletions

View File

@ -63,6 +63,7 @@ import { JFSongListSort, JFSortOrder } from '/@/renderer/api/jellyfin.types';
import isElectron from 'is-electron';
import { ServerFeature } from '/@/renderer/api/features-types';
import { VersionInfo, getFeatures } from '/@/renderer/api/utils';
import chunk from 'lodash/chunk';
const formatCommaDelimitedString = (value: string[]) => {
return value.join(',');
@ -473,6 +474,11 @@ const getSongList = async (args: SongListArgs): Promise<SongListResponse> => {
};
};
// Limit the query to 50 at a time to be *extremely* conservative on the
// length of the full URL, since the ids are part of the query string and
// not the POST body
const MAX_ITEMS_PER_PLAYLIST_ADD = 50;
const addToPlaylist = async (args: AddToPlaylistArgs): Promise<AddToPlaylistResponse> => {
const { query, body, apiClientProps } = args;
@ -480,19 +486,23 @@ const addToPlaylist = async (args: AddToPlaylistArgs): Promise<AddToPlaylistResp
throw new Error('No userId found');
}
const res = await jfApiClient(apiClientProps).addToPlaylist({
body: null,
params: {
id: query.id,
},
query: {
Ids: body.songId,
UserId: apiClientProps.server?.userId,
},
});
const chunks = chunk(body.songId, MAX_ITEMS_PER_PLAYLIST_ADD);
if (res.status !== 204) {
throw new Error('Failed to add to playlist');
for (const chunk of chunks) {
const res = await jfApiClient(apiClientProps).addToPlaylist({
body: null,
params: {
id: query.id,
},
query: {
Ids: chunk.join(','),
UserId: apiClientProps.server?.userId,
},
});
if (res.status !== 204) {
throw new Error('Failed to add to playlist');
}
}
return null;
@ -503,18 +513,22 @@ const removeFromPlaylist = async (
): Promise<RemoveFromPlaylistResponse> => {
const { query, apiClientProps } = args;
const res = await jfApiClient(apiClientProps).removeFromPlaylist({
body: null,
params: {
id: query.id,
},
query: {
EntryIds: query.songId,
},
});
const chunks = chunk(query.songId, MAX_ITEMS_PER_PLAYLIST_ADD);
if (res.status !== 204) {
throw new Error('Failed to remove from playlist');
for (const chunk of chunks) {
const res = await jfApiClient(apiClientProps).removeFromPlaylist({
body: null,
params: {
id: query.id,
},
query: {
EntryIds: chunk.join(','),
},
});
if (res.status !== 204) {
throw new Error('Failed to remove from playlist');
}
}
return null;

View File

@ -609,14 +609,14 @@ const addToPlaylist = z.object({
});
const addToPlaylistParameters = z.object({
Ids: z.array(z.string()),
Ids: z.string(),
UserId: z.string(),
});
const removeFromPlaylist = z.null();
const removeFromPlaylistParameters = z.object({
EntryIds: z.array(z.string()),
EntryIds: z.string(),
});
const deletePlaylist = z.null();