2021-11-22 03:00:40 +01:00
const Logger = require ( '../Logger' )
2022-06-04 17:52:37 +02:00
const { isObject , toNumber } = require ( '../utils/index' )
2021-11-22 03:00:40 +01:00
class MeController {
constructor ( ) { }
// GET: api/me/listening-sessions
async getListeningSessions ( req , res ) {
var listeningSessions = await this . getUserListeningSessionsHelper ( req . user . id )
2022-06-04 17:52:37 +02:00
const itemsPerPage = toNumber ( req . query . itemsPerPage , 10 ) || 10
const page = toNumber ( req . query . page , 0 )
const start = page * itemsPerPage
const sessions = listeningSessions . slice ( start , start + itemsPerPage )
const payload = {
total : listeningSessions . length ,
numPages : Math . ceil ( listeningSessions . length / itemsPerPage ) ,
page ,
itemsPerPage ,
sessions
}
res . json ( payload )
2021-11-22 03:00:40 +01:00
}
// GET: api/me/listening-stats
async getListeningStats ( req , res ) {
var listeningStats = await this . getUserListeningStatsHelpers ( req . user . id )
res . json ( listeningStats )
}
2022-06-04 01:59:42 +02:00
// GET: api/me/progress/:id/:episodeId?
async getMediaProgress ( req , res ) {
2022-07-03 19:15:40 +02:00
const mediaProgress = req . user . getMediaProgress ( req . params . id , req . params . episodeId || null )
2022-06-04 01:59:42 +02:00
if ( ! mediaProgress ) {
return res . sendStatus ( 404 )
}
res . json ( mediaProgress )
}
2022-03-17 19:33:22 +01:00
// DELETE: api/me/progress/:id
2022-03-26 17:59:34 +01:00
async removeMediaProgress ( req , res ) {
var wasRemoved = req . user . removeMediaProgress ( req . params . id )
2022-03-17 19:33:22 +01:00
if ( ! wasRemoved ) {
return res . sendStatus ( 200 )
2021-11-22 03:00:40 +01:00
}
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
res . sendStatus ( 200 )
}
2022-03-17 19:33:22 +01:00
// PATCH: api/me/progress/:id
2022-03-26 17:59:34 +01:00
async createUpdateMediaProgress ( req , res ) {
2022-03-14 01:34:31 +01:00
var libraryItem = this . db . libraryItems . find ( ab => ab . id === req . params . id )
if ( ! libraryItem ) {
return res . status ( 404 ) . send ( 'Item not found' )
2021-11-22 03:00:40 +01:00
}
2022-06-25 18:01:01 +02:00
2022-03-26 17:59:34 +01:00
var wasUpdated = req . user . createUpdateMediaProgress ( libraryItem , req . body )
2021-11-22 03:00:40 +01:00
if ( wasUpdated ) {
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
}
res . sendStatus ( 200 )
}
2022-03-26 23:41:26 +01:00
// PATCH: api/me/progress/:id/:episodeId
async createUpdateEpisodeMediaProgress ( req , res ) {
var episodeId = req . params . episodeId
var libraryItem = this . db . libraryItems . find ( ab => ab . id === req . params . id )
if ( ! libraryItem ) {
return res . status ( 404 ) . send ( 'Item not found' )
}
if ( ! libraryItem . media . episodes . find ( ep => ep . id === episodeId ) ) {
Logger . error ( ` [MeController] removeEpisode episode ${ episodeId } not found for item ${ libraryItem . id } ` )
return res . status ( 404 ) . send ( 'Episode not found' )
}
var wasUpdated = req . user . createUpdateMediaProgress ( libraryItem , req . body , episodeId )
if ( wasUpdated ) {
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
}
res . sendStatus ( 200 )
}
2022-03-17 19:33:22 +01:00
// PATCH: api/me/progress/batch/update
2022-03-26 17:59:34 +01:00
async batchUpdateMediaProgress ( req , res ) {
2022-03-17 19:33:22 +01:00
var itemProgressPayloads = req . body
if ( ! itemProgressPayloads || ! itemProgressPayloads . length ) {
2021-11-22 03:00:40 +01:00
return res . sendStatus ( 500 )
}
var shouldUpdate = false
2022-03-17 19:33:22 +01:00
itemProgressPayloads . forEach ( ( itemProgress ) => {
var libraryItem = this . db . libraryItems . find ( li => li . id === itemProgress . id ) // Make sure this library item exists
2022-03-14 01:34:31 +01:00
if ( libraryItem ) {
2022-03-26 17:59:34 +01:00
var wasUpdated = req . user . createUpdateMediaProgress ( libraryItem , itemProgress )
2021-11-22 03:00:40 +01:00
if ( wasUpdated ) shouldUpdate = true
2022-03-17 19:33:22 +01:00
} else {
2022-03-26 17:59:34 +01:00
Logger . error ( ` [MeController] batchUpdateMediaProgress: Library Item does not exist ${ itemProgress . id } ` )
2021-11-22 03:00:40 +01:00
}
} )
if ( shouldUpdate ) {
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
}
res . sendStatus ( 200 )
}
2022-03-18 02:28:04 +01:00
// POST: api/me/item/:id/bookmark
async createBookmark ( req , res ) {
var libraryItem = this . db . libraryItems . find ( li => li . id === req . params . id )
if ( ! libraryItem ) return res . sendStatus ( 404 )
const { time , title } = req . body
var bookmark = req . user . createBookmark ( libraryItem . id , time , title )
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
res . json ( bookmark )
}
// PATCH: api/me/item/:id/bookmark
async updateBookmark ( req , res ) {
var libraryItem = this . db . libraryItems . find ( li => li . id === req . params . id )
if ( ! libraryItem ) return res . sendStatus ( 404 )
const { time , title } = req . body
if ( ! req . user . findBookmark ( libraryItem . id , time ) ) {
Logger . error ( ` [MeController] updateBookmark not found ` )
return res . sendStatus ( 404 )
}
var bookmark = req . user . updateBookmark ( libraryItem . id , time , title )
if ( ! bookmark ) return res . sendStatus ( 500 )
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
res . json ( bookmark )
}
// DELETE: api/me/item/:id/bookmark/:time
async removeBookmark ( req , res ) {
var libraryItem = this . db . libraryItems . find ( li => li . id === req . params . id )
if ( ! libraryItem ) return res . sendStatus ( 404 )
var time = Number ( req . params . time )
if ( isNaN ( time ) ) return res . sendStatus ( 500 )
if ( ! req . user . findBookmark ( libraryItem . id , time ) ) {
Logger . error ( ` [MeController] removeBookmark not found ` )
return res . sendStatus ( 404 )
}
req . user . removeBookmark ( libraryItem . id , time )
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
res . sendStatus ( 200 )
}
2021-11-22 03:00:40 +01:00
// PATCH: api/me/password
updatePassword ( req , res ) {
2022-04-30 01:38:13 +02:00
if ( req . user . isGuest ) {
Logger . error ( ` [MeController] Guest user attempted to change password ` , req . user . username )
return res . sendStatus ( 500 )
}
2021-11-22 03:00:40 +01:00
this . auth . userChangePassword ( req , res )
}
// PATCH: api/me/settings
async updateSettings ( req , res ) {
var settingsUpdate = req . body
if ( ! settingsUpdate || ! isObject ( settingsUpdate ) ) {
return res . sendStatus ( 500 )
}
var madeUpdates = req . user . updateSettings ( settingsUpdate )
if ( madeUpdates ) {
await this . db . updateEntity ( 'user' , req . user )
}
return res . json ( {
success : true ,
settings : req . user . settings
} )
}
2022-04-10 00:56:51 +02:00
// POST: api/me/sync-local-progress
async syncLocalMediaProgress ( req , res ) {
if ( ! req . body . localMediaProgress ) {
Logger . error ( ` [MeController] syncLocalMediaProgress invalid post body ` )
return res . sendStatus ( 500 )
}
const updatedLocalMediaProgress = [ ]
var numServerProgressUpdates = 0
var localMediaProgress = req . body . localMediaProgress || [ ]
2022-07-14 02:18:49 +02:00
2022-04-10 00:56:51 +02:00
localMediaProgress . forEach ( localProgress => {
if ( ! localProgress . libraryItemId ) {
Logger . error ( ` [MeController] syncLocalMediaProgress invalid local media progress object ` , localProgress )
return
}
var libraryItem = this . db . getLibraryItem ( localProgress . libraryItemId )
if ( ! libraryItem ) {
Logger . error ( ` [MeController] syncLocalMediaProgress invalid local media progress object no library item ` , localProgress )
return
}
var mediaProgress = req . user . getMediaProgress ( localProgress . libraryItemId , localProgress . episodeId )
if ( ! mediaProgress ) {
// New media progress from mobile
Logger . debug ( ` [MeController] syncLocalMediaProgress local progress is new - creating ${ localProgress . id } ` )
req . user . createUpdateMediaProgress ( libraryItem , localProgress , localProgress . episodeId )
numServerProgressUpdates ++
} else if ( mediaProgress . lastUpdate < localProgress . lastUpdate ) {
Logger . debug ( ` [MeController] syncLocalMediaProgress local progress is more recent - updating ${ mediaProgress . id } ` )
req . user . createUpdateMediaProgress ( libraryItem , localProgress , localProgress . episodeId )
numServerProgressUpdates ++
} else if ( mediaProgress . lastUpdate > localProgress . lastUpdate ) {
var updateTimeDifference = mediaProgress . lastUpdate - localProgress . lastUpdate
Logger . debug ( ` [MeController] syncLocalMediaProgress server progress is more recent by ${ updateTimeDifference } ms - ${ mediaProgress . id } ` )
for ( const key in localProgress ) {
2022-07-14 02:18:49 +02:00
// Local media progress ID uses the local library item id and server media progress uses the library item id
if ( key !== 'id' && mediaProgress [ key ] != undefined && localProgress [ key ] !== mediaProgress [ key ] ) {
2022-04-10 00:56:51 +02:00
// Logger.debug(`[MeController] syncLocalMediaProgress key ${key} changed from ${localProgress[key]} to ${mediaProgress[key]} - ${mediaProgress.id}`)
localProgress [ key ] = mediaProgress [ key ]
}
}
updatedLocalMediaProgress . push ( localProgress )
} else {
Logger . debug ( ` [MeController] syncLocalMediaProgress server and local are in sync - ${ mediaProgress . id } ` )
}
} )
Logger . debug ( ` [MeController] syncLocalMediaProgress server updates = ${ numServerProgressUpdates } , local updates = ${ updatedLocalMediaProgress . length } ` )
if ( numServerProgressUpdates > 0 ) {
await this . db . updateEntity ( 'user' , req . user )
this . clientEmitter ( req . user . id , 'user_updated' , req . user . toJSONForBrowser ( ) )
}
res . json ( {
numServerProgressUpdates ,
localProgressUpdates : updatedLocalMediaProgress
} )
}
2021-11-22 03:00:40 +01:00
}
module . exports = new MeController ( )