mirror of
https://github.com/janishutz/MusicPlayer.git
synced 2026-09-10 14:25:24 +02:00
feat: basic connection between frontend and backend
This commit is contained in:
@@ -14,6 +14,7 @@ import room from './routes/room';
|
||||
import user from './routes/user';
|
||||
|
||||
const run = () => {
|
||||
// FIXME: Use DB instead of memory optionally
|
||||
const sdkConfig = JSON.parse( fs.readFileSync( path.join(
|
||||
__dirname,
|
||||
'/../config/sdk.config.testing.json'
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
Client,
|
||||
Room,
|
||||
RoomStore
|
||||
} from './room';
|
||||
import {
|
||||
@@ -16,7 +17,7 @@ const rooms: RoomStore = {};
|
||||
* @param room - The name of the room to get
|
||||
* @returns The room, or undefined if it is not present
|
||||
*/
|
||||
const get = ( room: string ) => {
|
||||
const get = ( room: string ): Room | undefined => {
|
||||
return rooms[room];
|
||||
};
|
||||
|
||||
@@ -114,7 +115,9 @@ const close = ( name: string, uid: string ) => {
|
||||
* @returns true if update succeeded
|
||||
*/
|
||||
const updateState = ( room: string, uid: string, playing: boolean, index: number, start: number ) => {
|
||||
if ( !rooms[room] || rooms[room].owner !== uid ) return false;
|
||||
if ( !rooms[room] || rooms[room].owner !== uid ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
rooms[room].state = {
|
||||
'playing': playing,
|
||||
@@ -149,10 +152,12 @@ const sendUpdate = ( room: string, kind: 'state' | 'playlist' ) => {
|
||||
|
||||
if ( !roomObject ) return false;
|
||||
|
||||
roomObject.clients.forEach( client => client.response.write( `data: ${ {
|
||||
roomObject.clients.forEach( client => client.response.write( `data: ${ JSON.stringify( {
|
||||
'type': kind,
|
||||
'data': JSON.stringify( roomObject[kind] )
|
||||
} }\n\n` ) );
|
||||
} ) }\n\n` ) );
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,7 @@ export const sseMiddleware = ( kind: 'client' | 'trackingClient', config?: Confi
|
||||
|
||||
const room = rooms.get( request.params.id );
|
||||
|
||||
if ( !room ) response.sendStatus( 404 );
|
||||
if ( !room ) return response.sendStatus( 404 );
|
||||
|
||||
response.writeHead( 200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
|
||||
@@ -24,12 +24,12 @@ const routes = ( app: express.Application, foss: boolean, config: Config ) => {
|
||||
sdk.loginCheck(),
|
||||
ownership.middleware(),
|
||||
( request: express.Request, response: express.Response ) => {
|
||||
if ( !request.query.room ) return response.sendStatus( 400 );
|
||||
if ( !request.query.room || !( /^[a-zA-Z0-9-]{3,20}/ ).test( String( request.query.room ) ) ) return response.sendStatus( 400 );
|
||||
|
||||
if ( rooms.create( String( request.query.room ), sdk.getUID( request )! ) )
|
||||
response.sendStatus( 200 );
|
||||
else
|
||||
response.sendStatus( 500 );
|
||||
response.sendStatus( 409 );
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ const routes = ( app: express.Application, foss: boolean ) => {
|
||||
'/room/:id/admin',
|
||||
corsManager.middleware( false ),
|
||||
sdk.loginCheck(),
|
||||
sseMiddleware( 'client' )
|
||||
sseMiddleware( 'trackingClient' )
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ const routes = ( app: express.Application, foss: boolean, config: Config ) => {
|
||||
|
||||
response.send(
|
||||
JSON.stringify( {
|
||||
'state': room.state,
|
||||
'playlist': lastRequest < room.playlist.lastUpdate ? room.playlist : undefined
|
||||
'state': room?.state,
|
||||
'playlist': lastRequest < room!.playlist!.lastUpdate ? room!.playlist : undefined
|
||||
} )
|
||||
);
|
||||
} );
|
||||
@@ -51,7 +51,7 @@ const routes = ( app: express.Application, foss: boolean, config: Config ) => {
|
||||
if ( rooms.updatePlaylist(
|
||||
request.params.id,
|
||||
sdk.getUID( request )!,
|
||||
request.body.playlist ? JSON.parse( request.body.playlist ) : []
|
||||
request.body.playlist ?? []
|
||||
) )
|
||||
response.sendStatus( 200 );
|
||||
else
|
||||
@@ -70,8 +70,9 @@ const routes = ( app: express.Application, foss: boolean, config: Config ) => {
|
||||
sdk.loginCheck(),
|
||||
bodyParser.json(),
|
||||
( request: express.Request, response: express.Response ) => {
|
||||
if ( typeof request.params.id !== 'string' )
|
||||
if ( typeof request.params.id !== 'string' ) {
|
||||
return response.sendStatus( 400 );
|
||||
}
|
||||
|
||||
try {
|
||||
if ( rooms.updateState(
|
||||
|
||||
Reference in New Issue
Block a user