From cc4d92f0efb0c26e24ee60ade7df8bffa94d97a3 Mon Sep 17 00:00:00 2001 From: adb-sh Date: Wed, 2 Nov 2022 03:25:47 +0100 Subject: [PATCH] Add sync, resources --- backend/api/auth.mjs | 7 ------- backend/api/session.mjs | 18 ++++-------------- backend/api/user.mjs | 19 +++++-------------- backend/index.mjs | 7 ++++++- backend/resources/session.mjs | 15 +++++++++++++++ backend/resources/user.mjs | 9 +++++++++ backend/syncSessions.js | 26 -------------------------- backend/syncSessions.mjs | 25 +++++++++++++++++++++++++ frontend | 2 +- 9 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 backend/resources/session.mjs create mode 100644 backend/resources/user.mjs delete mode 100644 backend/syncSessions.js create mode 100644 backend/syncSessions.mjs diff --git a/backend/api/auth.mjs b/backend/api/auth.mjs index ab054a0..04153ae 100644 --- a/backend/api/auth.mjs +++ b/backend/api/auth.mjs @@ -38,13 +38,6 @@ export const applyAuthRoutes = (router) => { { upsert: true }, ); - if (!await SessionStore.findOne().bySpotifyId(newUser.client.user.id)) - await new SessionStore({ - host: userStore, - clients: [], - queue: [], - }).save(); - res.status(200); res.send({ message: 'authorized', accessToken }); } catch (e) { diff --git a/backend/api/session.mjs b/backend/api/session.mjs index bf86f5f..c3b1c2f 100644 --- a/backend/api/session.mjs +++ b/backend/api/session.mjs @@ -1,4 +1,5 @@ import { SessionStore, UserStore } from "../db/schemas.mjs"; +import { getSessionResource } from "../resources/session.mjs"; export const applySessionRoutes = (router) => { @@ -29,18 +30,7 @@ export const applySessionRoutes = (router) => { return; } - const session = { - ...sessionStore, - clients: await Promise.all([...sessionStore.clients].map(async client => { - const userStore = await UserStore.findById(client._id); - const user = await userStore.spotify.local; - return { - displayName: user.client.user.displayName, - totalFollowers: user.client.user.totalFollowers, - images: user.client.user.images, - }; - })), - } + const session = await getSessionResource(sessionStore); res.status(200); res.send({ session }); @@ -98,10 +88,10 @@ export const applySessionRoutes = (router) => { return; } - sessionStore.clients = sessionStore.clients.filter(client => client.id !== user.id); + sessionStore.clients = sessionStore.clients.filter(client => client.spotify.userId !== user.spotify.userId); await sessionStore.save(); - res.status(200); + res.status(201); res.send({ message: 'you left' }); }); diff --git a/backend/api/user.mjs b/backend/api/user.mjs index c1f7149..473cbe2 100644 --- a/backend/api/user.mjs +++ b/backend/api/user.mjs @@ -1,5 +1,5 @@ -import { store } from "../store.mjs"; import { SessionStore, UserStore } from "../db/schemas.mjs"; +import { getUserResource } from "../resources/user.mjs"; export const applyUserRoutes = (router) => { @@ -26,28 +26,19 @@ export const applyUserRoutesPublic = (router) => { res.send({ message: 'userId is missing' }); } const { userId } = req.params; - const hostDB = await UserStore.findOne().bySpotifyId(userId); - if (!hostDB) { + const userStore = await UserStore.findOne().bySpotifyId(userId); + if (!userStore) { res.status(400); res.send({ message: 'user is not registered' }); return; } - const host = await hostDB?.spotify?.local; - if (!host.player) { - res.status(500); - res.send({ message: 'user is outdated' }); - return; - } + const host = await userStore?.spotify?.local; try { const currentlyPlaying = await host.player.getCurrentlyPlaying('track'); res.status(200); res.send({ currentlyPlaying, - user: { - displayName: host.client.user.displayName, - totalFollowers: host.client.user.totalFollowers, - images: host.client.user.images, - }, + user: await getUserResource(userStore), }); } catch (e) { console.log(e); diff --git a/backend/index.mjs b/backend/index.mjs index 28ba569..05fae5f 100644 --- a/backend/index.mjs +++ b/backend/index.mjs @@ -1,10 +1,10 @@ import express from "express"; import { auth } from "./middlewares/auth.mjs"; - import { applyAuthRoutes } from "./api/auth.mjs"; import { applyApiRoutes, applyPublicRoutes } from "./api/index.mjs"; import mongoose from "mongoose"; import { store } from "./store.mjs"; +import { syncAllSessions } from "./syncSessions.mjs"; // express server const port = 3000; @@ -35,3 +35,8 @@ console.log('mongodb connected'); app.listen(port); console.log('express started'); + +setInterval(async () => { + await syncAllSessions(); +}, 5000); +console.log('sync started'); diff --git a/backend/resources/session.mjs b/backend/resources/session.mjs new file mode 100644 index 0000000..c1ab7cc --- /dev/null +++ b/backend/resources/session.mjs @@ -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) + ))), + }; +}; \ No newline at end of file diff --git a/backend/resources/user.mjs b/backend/resources/user.mjs new file mode 100644 index 0000000..734f220 --- /dev/null +++ b/backend/resources/user.mjs @@ -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, + }; +}; diff --git a/backend/syncSessions.js b/backend/syncSessions.js deleted file mode 100644 index e1c9708..0000000 --- a/backend/syncSessions.js +++ /dev/null @@ -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); - })); -}; diff --git a/backend/syncSessions.mjs b/backend/syncSessions.mjs new file mode 100644 index 0000000..6aa3040 --- /dev/null +++ b/backend/syncSessions.mjs @@ -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); + } + })); +}; diff --git a/frontend b/frontend index f674f28..d17ac89 160000 --- a/frontend +++ b/frontend @@ -1 +1 @@ -Subproject commit f674f28921e93eaac1b52e080d482c047f7d4170 +Subproject commit d17ac890f2b9d22986e2741e263a5239c0365214