From dec311cb73d26ddad18e287d6777cd9e6b9929ad Mon Sep 17 00:00:00 2001 From: janis Date: Mon, 20 Nov 2023 10:09:03 +0100 Subject: [PATCH] add backend for feeding information to main ui --- frontend/src/app.js | 26 +++++++++++++++---------- frontend/src/client/appleMusic/index.js | 14 +++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/frontend/src/app.js b/frontend/src/app.js index 957251f..42bd14e 100644 --- a/frontend/src/app.js +++ b/frontend/src/app.js @@ -122,7 +122,7 @@ app.get( '/mainNotifier', ( req, res ) => { } ); res.status( 200 ); res.flushHeaders(); - let det = { 'type': 'basics', 'data': currentDetails }; + let det = { 'type': 'basics', 'data': connectedClients }; res.write( `data: ${ JSON.stringify( det ) }\n\n` ); connectedMain = res; } else { @@ -197,19 +197,25 @@ app.post( '/statusUpdate', ( req, res ) => { } } ); - +// STATUS UPDATE from the client display to send to main ui +const allowedMainUpdates = [ 'disconnect', 'fullScreenStatus', 'visibility' ]; app.get( '/clientStatusUpdate/:status', ( req, res ) => { - if ( req.params.status === 'disconnect' ) { - - } else if ( req.params.status === 'fullScreenExit' ) { - - } else if ( req.params.status === 'inactive' ) { - - } else if ( req.params.status === 'reactivated' ) { - + if ( allowedTypes.includes( req.body.type ) ) { + const ipRetrieved = req.headers[ 'x-forwarded-for' ]; + const ip = ipRetrieved ? ipRetrieved.split( /, / )[ 0 ] : req.connection.remoteAddress; + sendClientUpdate( req.body.type, req.body.data, ip ); + res.send( 'ok' ); + } else { + res.status( 400 ).send( 'ERR_UNKNOWN_TYPE' ); } } ); +const sendClientUpdate = ( update, data, ip ) => { + for ( let main in connectedMain ) { + connectedMain[ main ].write( 'data: ' + JSON.stringify( { 'type': update, 'ip': ip, 'data': data } ) + '\n\n' ); + } +} + app.get( '/indexDirs', ( req, res ) => { if ( req.query.dir ) { indexer.index( req ).then( dirIndex => { diff --git a/frontend/src/client/appleMusic/index.js b/frontend/src/client/appleMusic/index.js index 549901f..b226424 100644 --- a/frontend/src/client/appleMusic/index.js +++ b/frontend/src/client/appleMusic/index.js @@ -88,6 +88,17 @@ const app = Vue.createApp( { setTimeout( () => { this.sendUpdate( 'pos' ); }, 500 ); + const minuteCounts = Math.floor( ( this.playingSong.duration ) / 60 ); + this.durationBeautified = String( minuteCounts ) + ':'; + if ( ( '' + minuteCounts ).length === 1 ) { + this.durationBeautified = '0' + minuteCounts + ':'; + } + const secondCounts = Math.floor( ( this.playingSong.duration ) - minuteCounts * 60 ); + if ( ( '' + secondCounts ).length === 1 ) { + this.durationBeautified += '0' + secondCounts; + } else { + this.durationBeautified += secondCounts; + } } } ); this.apiGetRequest( 'https://api.music.apple.com/v1/me/library/playlists', this.playlistHandler ); @@ -405,6 +416,9 @@ const app = Vue.createApp( { console.log( err ); } ); }, + toggleShowMode() { + this.isShowingRemainingTime = !this.isShowingRemainingTime; + } }, watch: { pos() {