Compare commits
No commits in common. "e3522e1f62248bf4a4f56496260815b6655ddd5a" and "780f542418fadfd6a3a9af78311e3ab8639814e4" have entirely different histories.
e3522e1f62
...
780f542418
@ -1,10 +1,10 @@
|
|||||||
import { SessionStore, UserStore } from "../db/schemas.mjs";
|
import { SessionStore } from "../db/schemas.mjs";
|
||||||
|
|
||||||
export const applySessionRoutes = (router) => {
|
export const applySessionRoutes = (router) => {
|
||||||
|
|
||||||
router.post('/session', async (req, res) => {
|
router.post('/session', async (req, res) => {
|
||||||
const user = res.locals.user;
|
const user = res.locals.user;
|
||||||
if (await SessionStore.findOne().bySpotifyId(user.spotify.userId)) {
|
if (await SessionStore.findOne().bySpotifyId(user.id)) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
res.send({ message: 'you are already in a session' });
|
res.send({ message: 'you are already in a session' });
|
||||||
return;
|
return;
|
||||||
@ -21,7 +21,7 @@ export const applySessionRoutes = (router) => {
|
|||||||
|
|
||||||
router.get('/session', async (req, res) => {
|
router.get('/session', async (req, res) => {
|
||||||
const user = res.locals.user;
|
const user = res.locals.user;
|
||||||
const sessionStore = await SessionStore.findOne().bySpotifyId(user.spotify.userId);
|
const sessionStore = await SessionStore.findOne().bySpotifyId(user.id);
|
||||||
|
|
||||||
if (!sessionStore) {
|
if (!sessionStore) {
|
||||||
res.status(404);
|
res.status(404);
|
||||||
@ -29,26 +29,13 @@ export const applySessionRoutes = (router) => {
|
|||||||
return;
|
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,
|
|
||||||
};
|
|
||||||
})),
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(200);
|
res.status(200);
|
||||||
res.send({ session });
|
res.send({ session: sessionStore });
|
||||||
});
|
});
|
||||||
|
|
||||||
router.delete('/session', async (req, res) => {
|
router.delete('/session', async (req, res) => {
|
||||||
const user = res.locals.user;
|
const user = res.locals.user;
|
||||||
const sessionStore = await SessionStore.findOne().byHostSpotifyId(user.spotify.userId);
|
const sessionStore = await SessionStore.findOne().byHostSpotifyId(user.id);
|
||||||
|
|
||||||
if (!sessionStore) {
|
if (!sessionStore) {
|
||||||
res.status(404);
|
res.status(404);
|
||||||
@ -68,14 +55,14 @@ export const applySessionRoutes = (router) => {
|
|||||||
res.send({ message: 'hostId is undefined' });
|
res.send({ message: 'hostId is undefined' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { hostId } = req.body;
|
const { hostId } = req.body.hostId;
|
||||||
const user = res.locals.user;
|
const user = await res.locals.user;
|
||||||
if (await SessionStore.findOne().bySpotifyId(user.spotify.userId)) {
|
if (await SessionStore.findOne().bySpotifyId(user.id)) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
res.send({ message: 'you are already in a session' });
|
res.send({ message: 'you are already in a session' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const sessionStore = await SessionStore.findOne().byHostSpotifyId(hostId);
|
const sessionStore = SessionStore.findOne().byHostSpotifyId(hostId);
|
||||||
if (!sessionStore) {
|
if (!sessionStore) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
res.send({ message: 'session does not exist' });
|
res.send({ message: 'session does not exist' });
|
||||||
@ -90,8 +77,8 @@ export const applySessionRoutes = (router) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post('/session/leave', async (req, res) => {
|
router.post('/session/leave', async (req, res) => {
|
||||||
const user = res.locals.user;
|
const user = await res.locals.user;
|
||||||
const sessionStore = await SessionStore.findOne().byClientSpotifyId(user.spotify.userId);
|
const sessionStore = SessionStore.findOne().byClientSpotifyId(user.id);
|
||||||
if (!sessionStore) {
|
if (!sessionStore) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
res.send({ message: 'you are not a client of any session' });
|
res.send({ message: 'you are not a client of any session' });
|
||||||
|
@ -36,19 +36,16 @@ const sessionSchema = new Schema({
|
|||||||
queue: Array,
|
queue: Array,
|
||||||
}, {
|
}, {
|
||||||
query: {
|
query: {
|
||||||
async byHostSpotifyId(id) {
|
byHostSpotifyId(id) {
|
||||||
const _id = (await UserStore.findOne().bySpotifyId(id))._id;
|
return this.where({ 'host.spotify.userId': id });
|
||||||
return this.where({ 'host._id': _id });
|
|
||||||
},
|
},
|
||||||
async byClientSpotifyId(id) {
|
byClientSpotifyId(id) {
|
||||||
const _id = (await UserStore.findOne().bySpotifyId(id))._id;
|
return this.where({ 'clients.userId': id });
|
||||||
return this.where({ 'clients._id': _id });
|
|
||||||
},
|
},
|
||||||
async bySpotifyId(id) {
|
bySpotifyId(id) {
|
||||||
const _id = (await UserStore.findOne().bySpotifyId(id))._id;
|
|
||||||
return this.where({ '$or': [
|
return this.where({ '$or': [
|
||||||
{ 'host._id': _id },
|
{ 'host.spotify.userId': id },
|
||||||
{ 'clients._id': _id },
|
{ 'clients.spotify.userId': id },
|
||||||
]});
|
]});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -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);
|
|
||||||
}));
|
|
||||||
};
|
|
2
frontend
2
frontend
@ -1 +1 @@
|
|||||||
Subproject commit f674f28921e93eaac1b52e080d482c047f7d4170
|
Subproject commit 2ae7ad4cef44048a4dcd68f82bb17d4f3f87c729
|
Loading…
Reference in New Issue
Block a user