You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
880 B
JavaScript

2 years ago
import { SessionStore } from "./db/schemas.mjs";
setInterval(async () => {
await syncAllSessions();
}, 1000);
const syncAllSessions = async () => {
const sessions = await SessionStore.find();
await Promise.all([...sessions].map(syncSession));
};
const syncSession = async session => {
const host = await session.host.spotify.local;
const hostCP = await host.player.getCurrentlyPlaying("track");
await Promise.all([...session.clients].map(async clientStore => {
const client = await clientStore.spotify.client.local;
const clientCP = client.player.getCurrentlyPlaying();
if (clientCP.item?.id !== hostCP.item?.id)
await client.player.play({
uris: [ hostCP.item?.uri ],
position: hostCP.progress,
});
else if (Math.abs(clientCP.progress - hostCP.progress) > 1000)
await client.player.seek(hostCP.progress);
}));
};