mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
Marking nowPlaying in smapi setPlayedSeconds handler so does not mark when sonos pre-caches a track (#57)
This commit is contained in:
@@ -311,7 +311,7 @@ function server(
|
|||||||
})
|
})
|
||||||
.then((stream) => ({ musicLibrary: it, stream }))
|
.then((stream) => ({ musicLibrary: it, stream }))
|
||||||
)
|
)
|
||||||
.then(({ musicLibrary, stream }) => {
|
.then(({ stream }) => {
|
||||||
logger.info(
|
logger.info(
|
||||||
`stream response from music service for ${id}, status=${
|
`stream response from music service for ${id}, status=${
|
||||||
stream.status
|
stream.status
|
||||||
@@ -330,32 +330,25 @@ function server(
|
|||||||
filter,
|
filter,
|
||||||
headers,
|
headers,
|
||||||
sendStream,
|
sendStream,
|
||||||
nowPlaying,
|
|
||||||
}: {
|
}: {
|
||||||
status: number;
|
status: number;
|
||||||
filter: Transform;
|
filter: Transform;
|
||||||
headers: Record<string, string>;
|
headers: Record<string, string>;
|
||||||
sendStream: boolean;
|
sendStream: boolean;
|
||||||
nowPlaying: boolean;
|
|
||||||
}) => {
|
}) => {
|
||||||
logger.info(
|
logger.info(
|
||||||
`<- /stream/track/${id}, status=${status}, headers=${JSON.stringify(
|
`<- /stream/track/${id}, status=${status}, headers=${JSON.stringify(
|
||||||
headers
|
headers
|
||||||
)}`
|
)}`
|
||||||
);
|
);
|
||||||
(nowPlaying
|
res.status(status);
|
||||||
? musicLibrary.nowPlaying(id)
|
Object.entries(headers)
|
||||||
: Promise.resolve(true)
|
.filter(([_, v]) => v !== undefined)
|
||||||
).then((_) => {
|
.forEach(([header, value]) => {
|
||||||
res.status(status);
|
res.setHeader(header, value!);
|
||||||
Object.entries(headers)
|
});
|
||||||
.filter(([_, v]) => v !== undefined)
|
if (sendStream) stream.stream.pipe(filter).pipe(res);
|
||||||
.forEach(([header, value]) => {
|
else res.send();
|
||||||
res.setHeader(header, value!);
|
|
||||||
});
|
|
||||||
if (sendStream) stream.stream.pipe(filter).pipe(res);
|
|
||||||
else res.send();
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (stream.status == 200) {
|
if (stream.status == 200) {
|
||||||
@@ -370,7 +363,6 @@ function server(
|
|||||||
"accept-ranges": stream.headers["accept-ranges"],
|
"accept-ranges": stream.headers["accept-ranges"],
|
||||||
},
|
},
|
||||||
sendStream: req.method == "GET",
|
sendStream: req.method == "GET",
|
||||||
nowPlaying: req.method == "GET",
|
|
||||||
});
|
});
|
||||||
} else if (stream.status == 206) {
|
} else if (stream.status == 206) {
|
||||||
respondWith({
|
respondWith({
|
||||||
@@ -385,7 +377,6 @@ function server(
|
|||||||
"accept-ranges": stream.headers["accept-ranges"],
|
"accept-ranges": stream.headers["accept-ranges"],
|
||||||
},
|
},
|
||||||
sendStream: req.method == "GET",
|
sendStream: req.method == "GET",
|
||||||
nowPlaying: req.method == "GET",
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
respondWith({
|
respondWith({
|
||||||
@@ -393,7 +384,6 @@ function server(
|
|||||||
filter: new PassThrough(),
|
filter: new PassThrough(),
|
||||||
headers: {},
|
headers: {},
|
||||||
sendStream: req.method == "GET",
|
sendStream: req.method == "GET",
|
||||||
nowPlaying: false,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -888,6 +888,9 @@ function bindSmapiSoapServiceToExpress(
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case "track":
|
case "track":
|
||||||
musicLibrary.track(typeId).then(({ duration }) => {
|
musicLibrary.track(typeId).then(({ duration }) => {
|
||||||
|
if(+seconds > 0) {
|
||||||
|
musicLibrary.nowPlaying(typeId);
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
(duration < 30 && +seconds >= 10) ||
|
(duration < 30 && +seconds >= 10) ||
|
||||||
(duration >= 30 && +seconds >= 30)
|
(duration >= 30 && +seconds >= 30)
|
||||||
|
|||||||
@@ -707,7 +707,6 @@ describe("server", () => {
|
|||||||
const musicLibrary = {
|
const musicLibrary = {
|
||||||
stream: jest.fn(),
|
stream: jest.fn(),
|
||||||
scrobble: jest.fn(),
|
scrobble: jest.fn(),
|
||||||
nowPlaying: jest.fn(),
|
|
||||||
};
|
};
|
||||||
let now = dayjs();
|
let now = dayjs();
|
||||||
const accessTokens = new ExpiringAccessTokens({ now: () => now });
|
const accessTokens = new ExpiringAccessTokens({ now: () => now });
|
||||||
@@ -871,7 +870,6 @@ describe("server", () => {
|
|||||||
|
|
||||||
expect(res.status).toEqual(404);
|
expect(res.status).toEqual(404);
|
||||||
|
|
||||||
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
|
|
||||||
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -892,7 +890,6 @@ describe("server", () => {
|
|||||||
|
|
||||||
musicService.login.mockResolvedValue(musicLibrary);
|
musicService.login.mockResolvedValue(musicLibrary);
|
||||||
musicLibrary.stream.mockResolvedValue(stream);
|
musicLibrary.stream.mockResolvedValue(stream);
|
||||||
musicLibrary.nowPlaying.mockResolvedValue(true);
|
|
||||||
|
|
||||||
const res = await request(server)
|
const res = await request(server)
|
||||||
.get(
|
.get(
|
||||||
@@ -913,7 +910,6 @@ describe("server", () => {
|
|||||||
expect(Object.keys(res.headers)).not.toContain("content-range");
|
expect(Object.keys(res.headers)).not.toContain("content-range");
|
||||||
|
|
||||||
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
|
||||||
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -933,7 +929,6 @@ describe("server", () => {
|
|||||||
|
|
||||||
musicService.login.mockResolvedValue(musicLibrary);
|
musicService.login.mockResolvedValue(musicLibrary);
|
||||||
musicLibrary.stream.mockResolvedValue(stream);
|
musicLibrary.stream.mockResolvedValue(stream);
|
||||||
musicLibrary.nowPlaying.mockResolvedValue(true);
|
|
||||||
|
|
||||||
const res = await request(server)
|
const res = await request(server)
|
||||||
.get(
|
.get(
|
||||||
@@ -953,7 +948,6 @@ describe("server", () => {
|
|||||||
expect(Object.keys(res.headers)).not.toContain("content-range");
|
expect(Object.keys(res.headers)).not.toContain("content-range");
|
||||||
|
|
||||||
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
|
||||||
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -972,7 +966,6 @@ describe("server", () => {
|
|||||||
|
|
||||||
musicService.login.mockResolvedValue(musicLibrary);
|
musicService.login.mockResolvedValue(musicLibrary);
|
||||||
musicLibrary.stream.mockResolvedValue(stream);
|
musicLibrary.stream.mockResolvedValue(stream);
|
||||||
musicLibrary.nowPlaying.mockResolvedValue(true);
|
|
||||||
|
|
||||||
const res = await request(server)
|
const res = await request(server)
|
||||||
.get(
|
.get(
|
||||||
@@ -992,7 +985,6 @@ describe("server", () => {
|
|||||||
expect(res.header["content-range"]).toBeUndefined();
|
expect(res.header["content-range"]).toBeUndefined();
|
||||||
|
|
||||||
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
|
||||||
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1012,7 +1004,6 @@ describe("server", () => {
|
|||||||
|
|
||||||
musicService.login.mockResolvedValue(musicLibrary);
|
musicService.login.mockResolvedValue(musicLibrary);
|
||||||
musicLibrary.stream.mockResolvedValue(stream);
|
musicLibrary.stream.mockResolvedValue(stream);
|
||||||
musicLibrary.nowPlaying.mockResolvedValue(true);
|
|
||||||
|
|
||||||
const res = await request(server)
|
const res = await request(server)
|
||||||
.get(
|
.get(
|
||||||
@@ -1034,7 +1025,6 @@ describe("server", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
|
||||||
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
expect(musicLibrary.stream).toHaveBeenCalledWith({ trackId });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1055,7 +1045,6 @@ describe("server", () => {
|
|||||||
|
|
||||||
musicService.login.mockResolvedValue(musicLibrary);
|
musicService.login.mockResolvedValue(musicLibrary);
|
||||||
musicLibrary.stream.mockResolvedValue(stream);
|
musicLibrary.stream.mockResolvedValue(stream);
|
||||||
musicLibrary.nowPlaying.mockResolvedValue(true);
|
|
||||||
|
|
||||||
const requestedRange = "40-";
|
const requestedRange = "40-";
|
||||||
|
|
||||||
@@ -1078,7 +1067,6 @@ describe("server", () => {
|
|||||||
expect(res.header["content-range"]).toBeUndefined();
|
expect(res.header["content-range"]).toBeUndefined();
|
||||||
|
|
||||||
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
|
||||||
expect(musicLibrary.stream).toHaveBeenCalledWith({
|
expect(musicLibrary.stream).toHaveBeenCalledWith({
|
||||||
trackId,
|
trackId,
|
||||||
range: requestedRange,
|
range: requestedRange,
|
||||||
@@ -1101,7 +1089,6 @@ describe("server", () => {
|
|||||||
|
|
||||||
musicService.login.mockResolvedValue(musicLibrary);
|
musicService.login.mockResolvedValue(musicLibrary);
|
||||||
musicLibrary.stream.mockResolvedValue(stream);
|
musicLibrary.stream.mockResolvedValue(stream);
|
||||||
musicLibrary.nowPlaying.mockResolvedValue(true);
|
|
||||||
|
|
||||||
const res = await request(server)
|
const res = await request(server)
|
||||||
.get(
|
.get(
|
||||||
@@ -1124,7 +1111,6 @@ describe("server", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
|
||||||
expect(musicLibrary.stream).toHaveBeenCalledWith({
|
expect(musicLibrary.stream).toHaveBeenCalledWith({
|
||||||
trackId,
|
trackId,
|
||||||
range: "4000-5000",
|
range: "4000-5000",
|
||||||
|
|||||||
@@ -472,6 +472,7 @@ describe("api", () => {
|
|||||||
deletePlaylist: jest.fn(),
|
deletePlaylist: jest.fn(),
|
||||||
removeFromPlaylist: jest.fn(),
|
removeFromPlaylist: jest.fn(),
|
||||||
scrobble: jest.fn(),
|
scrobble: jest.fn(),
|
||||||
|
nowPlaying: jest.fn(),
|
||||||
};
|
};
|
||||||
const accessTokens = {
|
const accessTokens = {
|
||||||
mint: jest.fn(),
|
mint: jest.fn(),
|
||||||
@@ -2810,9 +2811,11 @@ describe("api", () => {
|
|||||||
function itShouldScroble({
|
function itShouldScroble({
|
||||||
trackId,
|
trackId,
|
||||||
secondsPlayed,
|
secondsPlayed,
|
||||||
|
shouldMarkNowPlaying,
|
||||||
}: {
|
}: {
|
||||||
trackId: string;
|
trackId: string;
|
||||||
secondsPlayed: number;
|
secondsPlayed: number;
|
||||||
|
shouldMarkNowPlaying: boolean,
|
||||||
}) {
|
}) {
|
||||||
it("should scrobble", async () => {
|
it("should scrobble", async () => {
|
||||||
musicLibrary.scrobble.mockResolvedValue(true);
|
musicLibrary.scrobble.mockResolvedValue(true);
|
||||||
@@ -2827,15 +2830,22 @@ describe("api", () => {
|
|||||||
expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
|
expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.track).toHaveBeenCalledWith(trackId);
|
expect(musicLibrary.track).toHaveBeenCalledWith(trackId);
|
||||||
expect(musicLibrary.scrobble).toHaveBeenCalledWith(trackId);
|
expect(musicLibrary.scrobble).toHaveBeenCalledWith(trackId);
|
||||||
|
if(shouldMarkNowPlaying) {
|
||||||
|
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
||||||
|
} else {
|
||||||
|
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function itShouldNotScroble({
|
function itShouldNotScroble({
|
||||||
trackId,
|
trackId,
|
||||||
secondsPlayed,
|
secondsPlayed,
|
||||||
|
shouldMarkNowPlaying,
|
||||||
}: {
|
}: {
|
||||||
trackId: string;
|
trackId: string;
|
||||||
secondsPlayed: number;
|
secondsPlayed: number;
|
||||||
|
shouldMarkNowPlaying: boolean,
|
||||||
}) {
|
}) {
|
||||||
it("should scrobble", async () => {
|
it("should scrobble", async () => {
|
||||||
const result = await ws.setPlayedSecondsAsync({
|
const result = await ws.setPlayedSecondsAsync({
|
||||||
@@ -2848,6 +2858,11 @@ describe("api", () => {
|
|||||||
expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
|
expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
|
||||||
expect(musicLibrary.track).toHaveBeenCalledWith(trackId);
|
expect(musicLibrary.track).toHaveBeenCalledWith(trackId);
|
||||||
expect(musicLibrary.scrobble).not.toHaveBeenCalled();
|
expect(musicLibrary.scrobble).not.toHaveBeenCalled();
|
||||||
|
if(shouldMarkNowPlaying) {
|
||||||
|
expect(musicLibrary.nowPlaying).toHaveBeenCalledWith(trackId);
|
||||||
|
} else {
|
||||||
|
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2858,16 +2873,24 @@ describe("api", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is 30 seconds", () => {
|
describe("when the seconds played is 30 seconds", () => {
|
||||||
itShouldScroble({ trackId, secondsPlayed: 30 });
|
itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is > 30 seconds", () => {
|
describe("when the seconds played is > 30 seconds", () => {
|
||||||
itShouldScroble({ trackId, secondsPlayed: 90 });
|
itShouldScroble({ trackId, secondsPlayed: 90, shouldMarkNowPlaying: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is < 30 seconds", () => {
|
describe("when the seconds played is < 30 seconds", () => {
|
||||||
itShouldNotScroble({ trackId, secondsPlayed: 29 });
|
itShouldNotScroble({ trackId, secondsPlayed: 29, shouldMarkNowPlaying: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when the seconds played is 1 seconds", () => {
|
||||||
|
itShouldNotScroble({ trackId, secondsPlayed: 1, shouldMarkNowPlaying: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when the seconds played is 0 seconds", () => {
|
||||||
|
itShouldNotScroble({ trackId, secondsPlayed: 0, shouldMarkNowPlaying: false });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -2878,16 +2901,24 @@ describe("api", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is 30 seconds", () => {
|
describe("when the seconds played is 30 seconds", () => {
|
||||||
itShouldScroble({ trackId, secondsPlayed: 30 });
|
itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is > 30 seconds", () => {
|
describe("when the seconds played is > 30 seconds", () => {
|
||||||
itShouldScroble({ trackId, secondsPlayed: 90 });
|
itShouldScroble({ trackId, secondsPlayed: 90, shouldMarkNowPlaying: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is < 30 seconds", () => {
|
describe("when the seconds played is < 30 seconds", () => {
|
||||||
itShouldNotScroble({ trackId, secondsPlayed: 29 });
|
itShouldNotScroble({ trackId, secondsPlayed: 29, shouldMarkNowPlaying: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when the seconds played is 1 seconds", () => {
|
||||||
|
itShouldNotScroble({ trackId, secondsPlayed: 1, shouldMarkNowPlaying: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when the seconds played is 0 seconds", () => {
|
||||||
|
itShouldNotScroble({ trackId, secondsPlayed: 0, shouldMarkNowPlaying: false });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -2898,20 +2929,28 @@ describe("api", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is 29 seconds", () => {
|
describe("when the seconds played is 29 seconds", () => {
|
||||||
itShouldScroble({ trackId, secondsPlayed: 30 });
|
itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is > 29 seconds", () => {
|
describe("when the seconds played is > 29 seconds", () => {
|
||||||
itShouldScroble({ trackId, secondsPlayed: 30 });
|
itShouldScroble({ trackId, secondsPlayed: 30, shouldMarkNowPlaying: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is 10 seconds", () => {
|
describe("when the seconds played is 10 seconds", () => {
|
||||||
itShouldScroble({ trackId, secondsPlayed: 10 });
|
itShouldScroble({ trackId, secondsPlayed: 10, shouldMarkNowPlaying: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when the played length is < 10 seconds", () => {
|
describe("when the seconds played is < 10 seconds", () => {
|
||||||
itShouldNotScroble({ trackId, secondsPlayed: 9 });
|
itShouldNotScroble({ trackId, secondsPlayed: 9, shouldMarkNowPlaying: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when the seconds played is 1 seconds", () => {
|
||||||
|
itShouldNotScroble({ trackId, secondsPlayed: 1, shouldMarkNowPlaying: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("when the seconds played is 0 seconds", () => {
|
||||||
|
itShouldNotScroble({ trackId, secondsPlayed: 0, shouldMarkNowPlaying: false });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -2926,6 +2965,7 @@ describe("api", () => {
|
|||||||
expect(result[0]).toEqual({ setPlayedSecondsResult: null });
|
expect(result[0]).toEqual({ setPlayedSecondsResult: null });
|
||||||
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
expect(musicService.login).toHaveBeenCalledWith(authToken);
|
||||||
expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
|
expect(accessTokens.mint).toHaveBeenCalledWith(authToken);
|
||||||
|
expect(musicLibrary.nowPlaying).not.toHaveBeenCalled();
|
||||||
expect(musicLibrary.scrobble).not.toHaveBeenCalled();
|
expect(musicLibrary.scrobble).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user