From 0023959398bfec020d6fb31bd74e7d30bd3789f2 Mon Sep 17 00:00:00 2001 From: adb-sh Date: Wed, 2 Nov 2022 12:15:16 +0100 Subject: [PATCH] debug --- backend/api/user.mjs | 7 ++++++- backend/lib/helpers.mjs | 2 +- backend/syncSessions.mjs | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/backend/api/user.mjs b/backend/api/user.mjs index 473cbe2..3089d20 100644 --- a/backend/api/user.mjs +++ b/backend/api/user.mjs @@ -6,7 +6,12 @@ export const applyUserRoutes = (router) => { applyUserRoutesPublic(router); router.get('/me/currentlyPlaying', async (req, res) => { - const currentlyPlaying = await (await res.locals.user.spotify.local)?.player?.getCurrentlyPlaying('track'); + const userLocal = await res.locals.user.spotify.local; + if (!userLocal.player) { + res.status(500); + res.send({ message: 'player is unavailable' }); + } + const currentlyPlaying = await userLocal.player.getCurrentlyPlaying('track'); res.status(200); res.send({ currentlyPlaying }); }); diff --git a/backend/lib/helpers.mjs b/backend/lib/helpers.mjs index 4dd7004..be87a7e 100644 --- a/backend/lib/helpers.mjs +++ b/backend/lib/helpers.mjs @@ -41,4 +41,4 @@ export const findUserBySpotifyId = async ( const newUser = await createLocalUser({ refreshToken }); store.users.push(newUser); return newUser; -} +}; diff --git a/backend/syncSessions.mjs b/backend/syncSessions.mjs index de8dea0..6d512b2 100644 --- a/backend/syncSessions.mjs +++ b/backend/syncSessions.mjs @@ -14,14 +14,14 @@ export const syncSession = async sessionStore => { 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) { + if (clientCP?.isPlaying && !hostCP?.item?.uri || !hostCP?.isPlaying) { await clientLocal.player.pause(); - } else if (hostCP.isPlaying && clientCP?.item?.id !== hostCP?.item?.id || !clientCP.isPlaying) { + } 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 (Math.abs(clientCP.progress - hostCP.progress + (new Date - hostCPDate)) > 500) { + } 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)); } }));