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.
29 lines
1.3 KiB
JavaScript
29 lines
1.3 KiB
JavaScript
import { SessionStore, UserStore } from "./db/schemas.mjs";
|
|
|
|
export const syncAllSessions = async () => {
|
|
const sessions = await SessionStore.find();
|
|
await Promise.all([...sessions].map(syncSession));
|
|
};
|
|
|
|
export const syncSession = async sessionStore => {
|
|
const hostStore = await UserStore.findById(sessionStore.host._id);
|
|
const host = await hostStore.spotify.local;
|
|
const hostCP = await host.player.getCurrentlyPlaying("track");
|
|
const hostCPDate = new Date();
|
|
await Promise.all([...sessionStore.clients].map(async client => {
|
|
const clientStore = await UserStore.findById(client._id);
|
|
const clientLocal = await clientStore.spotify.local;
|
|
const clientCP = await clientLocal.player.getCurrentlyPlaying("track");
|
|
if (clientCP?.isPlaying && !hostCP?.item?.uri || !hostCP?.isPlaying) {
|
|
await clientLocal.player.pause();
|
|
} else if (hostCP?.isPlaying && clientCP?.item?.id !== hostCP?.item?.id || !clientCP?.isPlaying) {
|
|
await clientLocal.player.play({
|
|
uris: [hostCP?.item?.uri],
|
|
position: hostCP.progress + (new Date - hostCPDate),
|
|
});
|
|
} else if (clientCP?.isPlaying && hostCP?.isPlaying && Math.abs(clientCP.progress - hostCP.progress + (new Date - hostCPDate)) > 500) {
|
|
await clientLocal.player.seek(hostCP.progress + (new Date - hostCPDate));
|
|
}
|
|
}));
|
|
};
|