Debug sessions
This commit is contained in:
		
							parent
							
								
									780f542418
								
							
						
					
					
						commit
						c1b3712554
					
				| @ -1,10 +1,10 @@ | |||||||
| import { SessionStore } from "../db/schemas.mjs"; | import { SessionStore, UserStore } 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.id)) { |     if (await SessionStore.findOne().bySpotifyId(user.spotify.userId)) { | ||||||
|       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.id); |     const sessionStore = await SessionStore.findOne().bySpotifyId(user.spotify.userId); | ||||||
| 
 | 
 | ||||||
|     if (!sessionStore) { |     if (!sessionStore) { | ||||||
|       res.status(404); |       res.status(404); | ||||||
| @ -29,13 +29,26 @@ 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: sessionStore }); |     res.send({ session }); | ||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   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.id); |     const sessionStore = await SessionStore.findOne().byHostSpotifyId(user.spotify.userId); | ||||||
| 
 | 
 | ||||||
|     if (!sessionStore) { |     if (!sessionStore) { | ||||||
|       res.status(404); |       res.status(404); | ||||||
| @ -55,14 +68,14 @@ export const applySessionRoutes = (router) => { | |||||||
|       res.send({ message: 'hostId is undefined' }); |       res.send({ message: 'hostId is undefined' }); | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
|     const { hostId } = req.body.hostId; |     const { hostId } = req.body; | ||||||
|     const user = await res.locals.user; |     const user = res.locals.user; | ||||||
|     if (await SessionStore.findOne().bySpotifyId(user.id)) { |     if (await SessionStore.findOne().bySpotifyId(user.spotify.userId)) { | ||||||
|       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 = SessionStore.findOne().byHostSpotifyId(hostId); |     const sessionStore = await 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' }); | ||||||
| @ -77,8 +90,8 @@ export const applySessionRoutes = (router) => { | |||||||
|   }); |   }); | ||||||
| 
 | 
 | ||||||
|   router.post('/session/leave', async (req, res) => { |   router.post('/session/leave', async (req, res) => { | ||||||
|     const user = await res.locals.user; |     const user = res.locals.user; | ||||||
|     const sessionStore = SessionStore.findOne().byClientSpotifyId(user.id); |     const sessionStore = await SessionStore.findOne().byClientSpotifyId(user.spotify.userId); | ||||||
|     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,16 +36,19 @@ const sessionSchema = new Schema({ | |||||||
|   queue: Array, |   queue: Array, | ||||||
| }, { | }, { | ||||||
|   query: { |   query: { | ||||||
|     byHostSpotifyId(id) { |     async byHostSpotifyId(id) { | ||||||
|       return this.where({ 'host.spotify.userId': id }); |       const _id = (await UserStore.findOne().bySpotifyId(id))._id; | ||||||
|  |       return this.where({ 'host._id': _id }); | ||||||
|     }, |     }, | ||||||
|     byClientSpotifyId(id) { |     async byClientSpotifyId(id) { | ||||||
|       return this.where({ 'clients.userId': id }); |       const _id = (await UserStore.findOne().bySpotifyId(id))._id; | ||||||
|  |       return this.where({ 'clients._id': _id }); | ||||||
|     }, |     }, | ||||||
|     bySpotifyId(id) { |     async bySpotifyId(id) { | ||||||
|  |       const _id = (await UserStore.findOne().bySpotifyId(id))._id; | ||||||
|       return this.where({ '$or': [ |       return this.where({ '$or': [ | ||||||
|         { 'host.spotify.userId': id }, |         { 'host._id': _id }, | ||||||
|         { 'clients.spotify.userId': id }, |         { 'clients._id': _id }, | ||||||
|       ]}); |       ]}); | ||||||
|     }, |     }, | ||||||
|   }, |   }, | ||||||
|  | |||||||
							
								
								
									
										26
									
								
								backend/syncSessions.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								backend/syncSessions.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,26 @@ | |||||||
|  | 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); | ||||||
|  |   })); | ||||||
|  | }; | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user