Add sync, resources
This commit is contained in:
parent
e3522e1f62
commit
cc4d92f0ef
@ -38,13 +38,6 @@ export const applyAuthRoutes = (router) => {
|
|||||||
{ upsert: true },
|
{ upsert: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!await SessionStore.findOne().bySpotifyId(newUser.client.user.id))
|
|
||||||
await new SessionStore({
|
|
||||||
host: userStore,
|
|
||||||
clients: [],
|
|
||||||
queue: [],
|
|
||||||
}).save();
|
|
||||||
|
|
||||||
res.status(200);
|
res.status(200);
|
||||||
res.send({ message: 'authorized', accessToken });
|
res.send({ message: 'authorized', accessToken });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { SessionStore, UserStore } from "../db/schemas.mjs";
|
import { SessionStore, UserStore } from "../db/schemas.mjs";
|
||||||
|
import { getSessionResource } from "../resources/session.mjs";
|
||||||
|
|
||||||
export const applySessionRoutes = (router) => {
|
export const applySessionRoutes = (router) => {
|
||||||
|
|
||||||
@ -29,18 +30,7 @@ export const applySessionRoutes = (router) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const session = {
|
const session = await getSessionResource(sessionStore);
|
||||||
...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,
|
|
||||||
};
|
|
||||||
})),
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200);
|
res.status(200);
|
||||||
res.send({ session });
|
res.send({ session });
|
||||||
@ -98,10 +88,10 @@ export const applySessionRoutes = (router) => {
|
|||||||
return;
|
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();
|
await sessionStore.save();
|
||||||
|
|
||||||
res.status(200);
|
res.status(201);
|
||||||
res.send({ message: 'you left' });
|
res.send({ message: 'you left' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { store } from "../store.mjs";
|
|
||||||
import { SessionStore, UserStore } from "../db/schemas.mjs";
|
import { SessionStore, UserStore } from "../db/schemas.mjs";
|
||||||
|
import { getUserResource } from "../resources/user.mjs";
|
||||||
|
|
||||||
export const applyUserRoutes = (router) => {
|
export const applyUserRoutes = (router) => {
|
||||||
|
|
||||||
@ -26,28 +26,19 @@ export const applyUserRoutesPublic = (router) => {
|
|||||||
res.send({ message: 'userId is missing' });
|
res.send({ message: 'userId is missing' });
|
||||||
}
|
}
|
||||||
const { userId } = req.params;
|
const { userId } = req.params;
|
||||||
const hostDB = await UserStore.findOne().bySpotifyId(userId);
|
const userStore = await UserStore.findOne().bySpotifyId(userId);
|
||||||
if (!hostDB) {
|
if (!userStore) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
res.send({ message: 'user is not registered' });
|
res.send({ message: 'user is not registered' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const host = await hostDB?.spotify?.local;
|
const host = await userStore?.spotify?.local;
|
||||||
if (!host.player) {
|
|
||||||
res.status(500);
|
|
||||||
res.send({ message: 'user is outdated' });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const currentlyPlaying = await host.player.getCurrentlyPlaying('track');
|
const currentlyPlaying = await host.player.getCurrentlyPlaying('track');
|
||||||
res.status(200);
|
res.status(200);
|
||||||
res.send({
|
res.send({
|
||||||
currentlyPlaying,
|
currentlyPlaying,
|
||||||
user: {
|
user: await getUserResource(userStore),
|
||||||
displayName: host.client.user.displayName,
|
|
||||||
totalFollowers: host.client.user.totalFollowers,
|
|
||||||
images: host.client.user.images,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { auth } from "./middlewares/auth.mjs";
|
import { auth } from "./middlewares/auth.mjs";
|
||||||
|
|
||||||
import { applyAuthRoutes } from "./api/auth.mjs";
|
import { applyAuthRoutes } from "./api/auth.mjs";
|
||||||
import { applyApiRoutes, applyPublicRoutes } from "./api/index.mjs";
|
import { applyApiRoutes, applyPublicRoutes } from "./api/index.mjs";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { store } from "./store.mjs";
|
import { store } from "./store.mjs";
|
||||||
|
import { syncAllSessions } from "./syncSessions.mjs";
|
||||||
|
|
||||||
// express server
|
// express server
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
@ -35,3 +35,8 @@ console.log('mongodb connected');
|
|||||||
|
|
||||||
app.listen(port);
|
app.listen(port);
|
||||||
console.log('express started');
|
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