diff --git a/backend/syncSessions.mjs b/backend/syncSessions.mjs index 6aa3040..de8dea0 100644 --- a/backend/syncSessions.mjs +++ b/backend/syncSessions.mjs @@ -9,17 +9,20 @@ 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?.item?.id !== hostCP?.item?.id) { + 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, + uris: [hostCP?.item?.uri], + position: hostCP.progress + (new Date - hostCPDate), }); - } else if (Math.abs(clientCP.progress - hostCP.progress) > 1000) { - await clientLocal.player.seek(hostCP.progress); + } else if (Math.abs(clientCP.progress - hostCP.progress + (new Date - hostCPDate)) > 500) { + await clientLocal.player.seek(hostCP.progress + (new Date - hostCPDate)); } })); };