mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
move some code
This commit is contained in:
@@ -724,10 +724,22 @@ export class Subsonic {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// getStarred2 = (credentials: Credentials): Promise<{ albums: Album[] }> =>
|
private st4r = (credentials: Credentials, action: string, { id } : { id: string }) =>
|
||||||
// this.getJSON<GetStarredResponse>(credentials, "/rest/getStarred2")
|
this.getJSON<SubsonicResponse>(credentials, `/rest/${action}`, { id }).then(it =>
|
||||||
// .then((it) => it.starred2)
|
it.status == "ok"
|
||||||
// .then((it) => ({
|
);
|
||||||
// albums: it.album.map(asAlbum),
|
|
||||||
// }));
|
star = (credentials: Credentials, ids : { id: string }) =>
|
||||||
|
this.st4r(credentials, "star", ids)
|
||||||
|
|
||||||
|
unstar = (credentials: Credentials, ids : { id: string }) =>
|
||||||
|
this.st4r(credentials, "unstar", ids)
|
||||||
|
|
||||||
|
setRating = (credentials: Credentials, id: string, rating: number) =>
|
||||||
|
this.getJSON<SubsonicResponse>(credentials, `/rest/setRating`, {
|
||||||
|
id,
|
||||||
|
rating,
|
||||||
|
}).then(it =>
|
||||||
|
it.status == "ok"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,21 +191,12 @@ export class SubsonicMusicLibrary implements MusicLibrary {
|
|||||||
const thingsToUpdate = [];
|
const thingsToUpdate = [];
|
||||||
if (track.rating.love != rating.love) {
|
if (track.rating.love != rating.love) {
|
||||||
thingsToUpdate.push(
|
thingsToUpdate.push(
|
||||||
this.subsonic.getJSON(
|
(rating.love ? this.subsonic.star : this.subsonic.unstar)(this.credentials,{ id: trackId })
|
||||||
this.credentials,
|
|
||||||
`/rest/${rating.love ? "star" : "unstar"}`,
|
|
||||||
{
|
|
||||||
id: trackId,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (track.rating.stars != rating.stars) {
|
if (track.rating.stars != rating.stars) {
|
||||||
thingsToUpdate.push(
|
thingsToUpdate.push(
|
||||||
this.subsonic.getJSON(this.credentials, `/rest/setRating`, {
|
this.subsonic.setRating(this.credentials, trackId, rating.stars)
|
||||||
id: trackId,
|
|
||||||
rating: rating.stars,
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return Promise.all(thingsToUpdate);
|
return Promise.all(thingsToUpdate);
|
||||||
|
|||||||
@@ -532,15 +532,21 @@ describe("asTrack", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const subsonicOK = (body: any = {}) => ({
|
const subsonicResponse = (response : Partial<{ status: string, body: any }> = { }) => {
|
||||||
"subsonic-response": {
|
const status = response.status || "ok"
|
||||||
status: "ok",
|
const body = response.body || {}
|
||||||
version: "1.16.1",
|
return {
|
||||||
type: "subsonic",
|
"subsonic-response": {
|
||||||
serverVersion: "0.45.1 (c55e6590)",
|
status,
|
||||||
...body,
|
version: "1.16.1",
|
||||||
},
|
type: "subsonic",
|
||||||
});
|
serverVersion: "0.45.1 (c55e6590)",
|
||||||
|
...body,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const subsonicOK = (body: any = {}) => subsonicResponse({ status: "ok", body });
|
||||||
|
|
||||||
const asGenreJson = (genre: { name: string; albumCount: number }) => ({
|
const asGenreJson = (genre: { name: string; albumCount: number }) => ({
|
||||||
songCount: 1475,
|
songCount: 1475,
|
||||||
@@ -1008,4 +1014,93 @@ describe("subsonic", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("stars and unstars", () => {
|
||||||
|
const id = uuid();
|
||||||
|
|
||||||
|
describe("staring a track", () => {
|
||||||
|
describe("when ok", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockGET.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve(ok(subsonicResponse({ status: "ok" })))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return true", async () => {
|
||||||
|
const result = await subsonic.star(credentials, { id });
|
||||||
|
|
||||||
|
expect(result).toEqual(true);
|
||||||
|
expect(axios.get).toHaveBeenCalledWith(
|
||||||
|
url.append({ pathname: "/rest/star" }).href(),
|
||||||
|
{
|
||||||
|
params: asURLSearchParams({
|
||||||
|
...authParamsPlusJson,
|
||||||
|
id
|
||||||
|
}),
|
||||||
|
headers,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when not ok", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockGET.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve(ok(subsonicResponse({ status: "not-ok" })))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return false", async () => {
|
||||||
|
const result = await subsonic.star(credentials, { id });
|
||||||
|
|
||||||
|
expect(result).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("setting ratings", () => {
|
||||||
|
const id = uuid();
|
||||||
|
|
||||||
|
describe("when the rating is valid", () => {
|
||||||
|
describe("when response is ok", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockGET.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve(ok(subsonicResponse({ status: "ok" })))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return true", async () => {
|
||||||
|
const result = await subsonic.setRating(credentials, id, 4);
|
||||||
|
|
||||||
|
expect(result).toEqual(true);
|
||||||
|
expect(axios.get).toHaveBeenCalledWith(
|
||||||
|
url.append({ pathname: "/rest/setRating" }).href(),
|
||||||
|
{
|
||||||
|
params: asURLSearchParams({
|
||||||
|
...authParamsPlusJson,
|
||||||
|
id,
|
||||||
|
rating: 4
|
||||||
|
}),
|
||||||
|
headers,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when response is not ok", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mockGET.mockImplementationOnce(() =>
|
||||||
|
Promise.resolve(ok(subsonicResponse({ status: "not-ok" })))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should return false", async () => {
|
||||||
|
const result = await subsonic.setRating(credentials, id, 2);
|
||||||
|
|
||||||
|
expect(result).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -833,6 +833,7 @@ describe("SubsonicMusicLibrary", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const subsonic = new SubsonicMusicLibrary(
|
const subsonic = new SubsonicMusicLibrary(
|
||||||
|
// todo: this should be a mock...
|
||||||
new Subsonic(url, customPlayers),
|
new Subsonic(url, customPlayers),
|
||||||
{ username, password },
|
{ username, password },
|
||||||
customPlayers as unknown as CustomPlayers
|
customPlayers as unknown as CustomPlayers
|
||||||
|
|||||||
Reference in New Issue
Block a user