Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate getInstantMixItems to jellyfin-sdk-typescript #694

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/maincontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ export async function instantMix(
options: PlayRequest,
item: BaseItemDto
): Promise<void> {
const result = await getInstantMixItems(data.userId, item);
const result = await getInstantMixItems(item);

options.items = result.Items ?? [];
PlaybackManager.playFromOptions(data.options);
Expand Down
59 changes: 32 additions & 27 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@
BaseItemDto,
BaseItemPerson,
TvShowsApiGetEpisodesRequest,
UserDto
UserDto,
InstantMixApiGetInstantMixFromAlbumRequest,
InstantMixApiGetInstantMixFromPlaylistRequest,
InstantMixApiGetInstantMixFromArtistsRequest,
InstantMixApiGetInstantMixFromSongRequest
} from '@jellyfin/sdk/lib/generated-client';
import { getTvShowsApi } from '@jellyfin/sdk/lib/utils/api';
import { getInstantMixApi, getTvShowsApi } from '@jellyfin/sdk/lib/utils/api';
import { JellyfinApi } from './components/jellyfinApi';
import { PlaybackManager, PlaybackState } from './components/playbackManager';
import { BusMessage, ItemQuery } from './types/global';

type InstantMixApiRequest =
| InstantMixApiGetInstantMixFromAlbumRequest
| InstantMixApiGetInstantMixFromArtistsRequest
| InstantMixApiGetInstantMixFromSongRequest
| InstantMixApiGetInstantMixFromPlaylistRequest;

export const TicksPerSecond = 10000000;

/**
Expand Down Expand Up @@ -497,49 +507,44 @@

/**
* Get an "Instant Mix" given an item, which can be a
* music artist, genre, album, playlist

Check warning on line 510 in src/helpers.ts

View workflow job for this annotation

GitHub Actions / Lint TS and CSS

Expected only 0 line after block description
*
* TODO: JellyfinApi.userId should be fine for this.
* @param userId - User ID to look up items with
* @param item - Parent item of the search
* @returns items for the queue
*/
export async function getInstantMixItems(
userId: string,
item: BaseItemDto
): Promise<BaseItemDtoQueryResult> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const query: any = {
Fields: requiredItemFields,
Limit: 50,
UserId: userId
if (item.Id === undefined) {
throw new Error('Item ID not provided');
}

const query: InstantMixApiRequest = {
fields: ['MediaSources', 'Chapters'],
itemId: item.Id,
limit: 50
};

let url: string | null = null;
const instantMixApi = getInstantMixApi(JellyfinApi.jellyfinApi);

if (item.Type == 'MusicArtist') {
url = 'Artists/InstantMix';
query.Id = item.Id;
return (await instantMixApi.getInstantMixFromArtists(query)).data;
} else if (item.Type == 'MusicGenre') {
url = 'MusicGenres/InstantMix';
query.Id = item.Id;
return (
await instantMixApi.getInstantMixFromMusicGenreById({
...query,
id: item.Id
})
).data;
} else if (item.Type == 'MusicAlbum') {
url = `Albums/${item.Id}/InstantMix`;
return (await instantMixApi.getInstantMixFromAlbum(query)).data;
} else if (item.Type == 'Audio') {
url = `Songs/${item.Id}/InstantMix`;
return (await instantMixApi.getInstantMixFromSong(query)).data;
} else if (item.Type == 'Playlist') {
url = `Playlists/${item.Id}/InstantMix`;
return (await instantMixApi.getInstantMixFromPlaylist(query)).data;
}

if (url) {
return JellyfinApi.authAjax(url, {
dataType: 'json',
query: query,
type: 'GET'
});
} else {
throw new Error(`InstantMix: Unknown item type: ${item.Type}`);
}
throw new Error(`InstantMix: Unknown item type: ${item.Type}`);
}

/**
Expand Down
Loading