Split Album into AlbumSummary and Album as per Artist

This commit is contained in:
simojenki
2021-03-06 08:51:31 +11:00
parent 5c75a3d50b
commit 5f9c240cdf
5 changed files with 48 additions and 26 deletions

View File

@@ -38,11 +38,14 @@ export type Artist = ArtistSummary & {
albums: Album[]
};
export type Album = {
export type AlbumSummary = {
id: string;
name: string;
year: string | undefined;
genre: string | undefined;
}
export type Album = AlbumSummary & {
};
export type Paging = {
@@ -80,5 +83,6 @@ export interface MusicService {
export interface MusicLibrary {
artists(q: ArtistQuery): Promise<Result<ArtistSummary>>;
artist(id: string): Promise<Artist>;
albums(q: AlbumQuery): Promise<Result<Album>>;
albums(q: AlbumQuery): Promise<Result<AlbumSummary>>;
// album(id: string): Promise<Album>;
}

View File

@@ -7,11 +7,11 @@ import {
ArtistSummary,
Result,
slice2,
asResult,
AlbumQuery,
ArtistQuery,
MusicLibrary,
Images,
AlbumSummary,
} from "./music_service";
import X2JS from "x2js";
@@ -203,14 +203,13 @@ export class Navidrome implements MusicService {
navidrome
.getArtists(credentials)
.then(slice2(q))
.then(asResult)
.then((result) =>
.then(([page, total]) =>
Promise.all(
result.results.map((idName: IdName) =>
page.map((idName: IdName) =>
navidrome
.getArtistInfo(credentials, idName.id)
.then((artistInfo) => ({
total: result.total,
total,
result: {
id: idName.id,
name: idName.name,
@@ -236,7 +235,7 @@ export class Navidrome implements MusicService {
image: artistInfo.image,
albums: artist.albums,
})),
albums: (_: AlbumQuery): Promise<Result<Album>> => {
albums: (_: AlbumQuery): Promise<Result<AlbumSummary>> => {
return Promise.resolve({ results: [], total: 0 });
},
};

View File

@@ -113,21 +113,21 @@ function server(
res.send("");
});
// app.get("/artist/:artistId/image", (req, res) => {
// console.log(`Trying to load image for ${req.params["artistId"]}, token ${JSON.stringify(req.cookies)}`)
// app.get("/album/:albumId/art", (req, res) => {
// console.log(`Trying to load image for ${req.params["albumId"]}, token ${JSON.stringify(req.cookies)}`)
// const authToken = req.headers["X-AuthToken"]! as string;
// const artistId = req.params["artistId"]!;
// const albumId = req.params["albumId"]!;
// musicService
// .login(authToken)
// .then((it) => it.artist(artistId))
// .then(artist => artist.image.small)
// // .then((it) => it.artist(artistId))
// // .then(artist => artist.image.small)
// .then((url) => {
// if (url) {
// console.log(`${artistId} sending 307 -> ${url}`)
// res.setHeader("Location", url);
// console.log(`${albumId} sending 307 -> ${url}`)
// // res.setHeader("Location", url);
// res.status(307).send();
// } else {
// console.log(`${artistId} sending 404`)
// console.log(`${albumId} sending 404`)
// res.status(404).send();
// }
// });

View File

@@ -6,7 +6,7 @@ import path from "path";
import logger from "./logger";
import { LinkCodes } from "./link_codes";
import { Album, MusicLibrary, MusicService, slice2 } from "./music_service";
import { AlbumSummary, MusicLibrary, MusicService, slice2 } from "./music_service";
export const LOGIN_ROUTE = "/login";
export const SOAP_PATH = "/ws/sonos";
@@ -159,10 +159,16 @@ const container = ({
title,
});
const album = (album: Album) => ({
const album = (album: AlbumSummary) => ({
itemType: "album",
id: `album:${album.id}`,
title: album.name,
// albumArtURI: {
// attributes: {
// requiresAuthentication: "true"
// },
// $value: `${webAddress}/album/${album.id}/art`
// }
});
type SoapyHeaders = {
@@ -244,6 +250,14 @@ function bindSmapiSoapServiceToExpress(
total: result.total,
})
);
case "albums":
return await musicLibrary.albums(paging).then((result) =>
getMetadataResult({
mediaCollection: result.results.map(album),
index: paging._index,
total: result.total,
})
);
case "artist":
return await musicLibrary
.artist(typeId!)
@@ -256,14 +270,6 @@ function bindSmapiSoapServiceToExpress(
total,
})
);
case "albums":
return await musicLibrary.albums(paging).then((result) =>
getMetadataResult({
mediaCollection: result.results.map(album),
index: paging._index,
total: result.total,
})
);
default:
throw `Unsupported id:${id}`;
}

View File

@@ -14,6 +14,8 @@ import {
slice2,
asResult,
ArtistSummary,
Album,
AlbumSummary
} from "../src/music_service";
export const artistToArtistSummary = (
@@ -24,6 +26,15 @@ export const artistToArtistSummary = (
image: it.image,
});
export const albumToAlbumSummary = (
it: Album
): AlbumSummary => ({
id: it.id,
name: it.name,
year: it.year,
genre: it.genre,
});
type P<T> = (t: T) => boolean;
const all: P<any> = (_: any) => true;
const artistWithId = (id: string): P<Artist> => (artist: Artist) =>
@@ -79,8 +90,10 @@ export class InMemoryMusicService implements MusicService {
)
)
.then((artists) => artists.flatMap((it) => it.albums))
.then(it => it.map(albumToAlbumSummary))
.then(slice2(q))
.then(asResult),
// album: (id: albumId) => Promise.resolve(this.artists.flatMap(it => it.albums).find(it => it.id === id))
});
}