Add sync, resources
This commit is contained in:
parent
e3522e1f62
commit
cc4d92f0ef
@ -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) {
|
||||
|
@ -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' });
|
||||
});
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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');
|
||||
|
15
backend/resources/session.mjs
Normal file
15
backend/resources/session.mjs
Normal file
@ -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)
|
||||
))),
|
||||
};
|
||||
};
|
9
backend/resources/user.mjs
Normal file
9
backend/resources/user.mjs
Normal file
@ -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);
|
||||
}));
|
||||
};
|
25
backend/syncSessions.mjs
Normal file
25
backend/syncSessions.mjs
Normal file
@ -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);
|
||||
}
|
||||
}));
|
||||
};
|
2
frontend
2
frontend
@ -1 +1 @@
|
||||
Subproject commit f674f28921e93eaac1b52e080d482c047f7d4170
|
||||
Subproject commit d17ac890f2b9d22986e2741e263a5239c0365214
|
Loading…
Reference in New Issue
Block a user