diff --git a/src/renderer/api/subsonic/subsonic-controller.ts b/src/renderer/api/subsonic/subsonic-controller.ts index 81033ad8..73db90ca 100644 --- a/src/renderer/api/subsonic/subsonic-controller.ts +++ b/src/renderer/api/subsonic/subsonic-controller.ts @@ -1,12 +1,19 @@ +import type { ServerInferResponses } from '@ts-rest/core'; + import dayjs from 'dayjs'; import filter from 'lodash/filter'; import orderBy from 'lodash/orderBy'; import md5 from 'md5'; +import { z } from 'zod'; -import { ssApiClient } from '/@/renderer/api/subsonic/subsonic-api'; +import { contract, ssApiClient } from '/@/renderer/api/subsonic/subsonic-api'; import { randomString } from '/@/renderer/utils'; import { ssNormalize } from '/@/shared/api/subsonic/subsonic-normalize'; -import { AlbumListSortType, SubsonicExtensions } from '/@/shared/api/subsonic/subsonic-types'; +import { + AlbumListSortType, + ssType, + SubsonicExtensions, +} from '/@/shared/api/subsonic/subsonic-types'; import { AlbumListSort, ControllerEndpoint, @@ -287,7 +294,7 @@ export const SubsonicController: ControllerEndpoint = { let type = ALBUM_LIST_SORT_MAPPING[query.sortBy] ?? AlbumListSortType.ALPHABETICAL_BY_NAME; if (query.artistIds) { - const promises: any[] = []; + const promises: Promise>[] = []; for (const artistId of query.artistIds) { promises.push( @@ -435,6 +442,33 @@ export const SubsonicController: ControllerEndpoint = { return totalRecordCount; } + if (query.artistIds) { + const promises: Promise>[] = []; + + for (const artistId of query.artistIds) { + promises.push( + ssApiClient(apiClientProps).getArtist({ + query: { + id: artistId, + }, + }), + ); + } + + const artistResult = await Promise.all(promises); + + const albums = artistResult.reduce((total: number, artist) => { + if (artist.status !== 200) { + return 0; + } + + const length = artist.body.artist.album?.length ?? 0; + return length + total; + }, 0); + + return albums; + } + if (query.favorite) { const res = await ssApiClient(apiClientProps).getStarred({ query: { @@ -858,9 +892,8 @@ export const SubsonicController: ControllerEndpoint = { return ssNormalize.song(res.body.song, apiClientProps.server); }, getSongList: async ({ apiClientProps, query }) => { - const fromAlbumPromises: any[] = []; - const artistDetailPromises: any[] = []; - let results: any[] = []; + const fromAlbumPromises: Promise>[] = []; + const artistDetailPromises: Promise>[] = []; if (query.searchTerm) { const res = await ssApiClient(apiClientProps).search3({ @@ -984,6 +1017,8 @@ export const SubsonicController: ControllerEndpoint = { } } + let results: z.infer[] = []; + if (fromAlbumPromises) { const albumsResult = await Promise.all(fromAlbumPromises);