Add sync, resources
parent
e3522e1f62
commit
cc4d92f0ef
@ -0,0 +1,15 @@
|
||||
import { getUserResource } from "./user.mjs";
|
||||
import { UserStore } from "../db/schemas.mjs";
|
||||
|
||||
export const getSessionResource = async sessionStore => {
|
||||
return {
|
||||
id: sessionStore._id,
|
||||
host: await getUserResource(
|
||||
await UserStore.findOne().bySpotifyId(sessionStore.host.spotify.userId)
|
||||
),
|
||||
clients: await Promise.all([...sessionStore.clients]
|
||||
.map(async client => await getUserResource(
|
||||
await UserStore.findById(client._id)
|
||||
))),
|
||||
};
|
||||
};
|
@ -0,0 +1,9 @@
|
||||
export const getUserResource = async userStore => {
|
||||
const user = await userStore.spotify.local;
|
||||
return {
|
||||
id: user.client.user.id,
|
||||
displayName: user.client.user.displayName,
|
||||
totalFollowers: user.client.user.totalFollowers,
|
||||
images: user.client.user.images,
|
||||
};
|
||||
};
|
@ -1,26 +0,0 @@
|
||||
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);
|
||||
}));
|
||||
};
|
@ -0,0 +1,25 @@
|
||||
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");
|
||||
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?.item?.id !== hostCP?.item?.id) {
|
||||
await clientLocal.player.play({
|
||||
uris: [hostCP.item?.uri],
|
||||
position: hostCP.progress,
|
||||
});
|
||||
} else if (Math.abs(clientCP.progress - hostCP.progress) > 1000) {
|
||||
await clientLocal.player.seek(hostCP.progress);
|
||||
}
|
||||
}));
|
||||
};
|
@ -1 +1 @@
|
||||
Subproject commit f674f28921e93eaac1b52e080d482c047f7d4170
|
||||
Subproject commit d17ac890f2b9d22986e2741e263a5239c0365214
|
Loading…
Reference in New Issue