From 5f9c240cdfddd2844c2a454429da6f39a8577da5 Mon Sep 17 00:00:00 2001 From: simojenki Date: Sat, 6 Mar 2021 08:51:31 +1100 Subject: [PATCH] Split Album into AlbumSummary and Album as per Artist --- src/music_service.ts | 8 ++++++-- src/navidrome.ts | 11 +++++------ src/server.ts | 16 ++++++++-------- src/smapi.ts | 26 ++++++++++++++++---------- tests/in_memory_music_service.ts | 13 +++++++++++++ 5 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/music_service.ts b/src/music_service.ts index ee8b277..6a838c6 100644 --- a/src/music_service.ts +++ b/src/music_service.ts @@ -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>; artist(id: string): Promise; - albums(q: AlbumQuery): Promise>; + albums(q: AlbumQuery): Promise>; + // album(id: string): Promise; } diff --git a/src/navidrome.ts b/src/navidrome.ts index 143fc1b..e084400 100644 --- a/src/navidrome.ts +++ b/src/navidrome.ts @@ -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> => { + albums: (_: AlbumQuery): Promise> => { return Promise.resolve({ results: [], total: 0 }); }, }; diff --git a/src/server.ts b/src/server.ts index 8a39ded..2e193b6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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(); // } // }); diff --git a/src/smapi.ts b/src/smapi.ts index 4b41cb9..ba4be4d 100644 --- a/src/smapi.ts +++ b/src/smapi.ts @@ -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}`; } diff --git a/tests/in_memory_music_service.ts b/tests/in_memory_music_service.ts index 3121132..668cd5b 100644 --- a/tests/in_memory_music_service.ts +++ b/tests/in_memory_music_service.ts @@ -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) => boolean; const all: P = (_: any) => true; const artistWithId = (id: string): P => (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)) }); }