mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-03-05 00:18:30 +01:00
Add:Api route to sync stream data for use in android native
This commit is contained in:
parent
a052366dac
commit
1b03af4041
server
@ -172,6 +172,8 @@ class ApiController {
|
|||||||
this.router.post('/syncUserAudiobookData', this.syncUserAudiobookData.bind(this))
|
this.router.post('/syncUserAudiobookData', this.syncUserAudiobookData.bind(this))
|
||||||
|
|
||||||
this.router.post('/purgecache', this.purgeCache.bind(this))
|
this.router.post('/purgecache', this.purgeCache.bind(this))
|
||||||
|
|
||||||
|
this.router.post('/syncStream', this.syncStream.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
async findBooks(req, res) {
|
async findBooks(req, res) {
|
||||||
@ -405,6 +407,11 @@ class ApiController {
|
|||||||
res.json(allUserAudiobookData)
|
res.json(allUserAudiobookData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async syncStream(req, res) {
|
||||||
|
Logger.debug(`[ApiController] syncStream for ${req.user.username} - ${req.body.streamId}`)
|
||||||
|
this.streamManager.streamSyncFromApi(req, res)
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Helper Methods
|
// Helper Methods
|
||||||
//
|
//
|
||||||
|
@ -229,10 +229,6 @@ class Server {
|
|||||||
// Used in development to set-up streams without authentication
|
// Used in development to set-up streams without authentication
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
app.use('/test-hls', this.hlsController.router)
|
app.use('/test-hls', this.hlsController.router)
|
||||||
app.get('/catalog.json', (req, res) => {
|
|
||||||
Logger.error('Catalog request made', req.headers)
|
|
||||||
res.json()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.server.listen(this.Port, this.Host, () => {
|
this.server.listen(this.Port, this.Host, () => {
|
||||||
|
@ -194,6 +194,46 @@ class StreamManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
streamSyncFromApi(req, res) {
|
||||||
|
var user = req.user
|
||||||
|
var syncData = req.body
|
||||||
|
|
||||||
|
var stream = this.streams.find(s => s.id === syncData.streamId)
|
||||||
|
if (!stream) {
|
||||||
|
Logger.error(`[StreamManager] streamSyncFromApi stream not found ${syncData.streamId}`)
|
||||||
|
return res.status(404).send('Stream not found')
|
||||||
|
}
|
||||||
|
if (stream.userToken !== user.token) {
|
||||||
|
Logger.error(`[StreamManager] streamSyncFromApi Invalid stream not owned by user`)
|
||||||
|
return res.status(500).send('Invalid stream auth')
|
||||||
|
}
|
||||||
|
|
||||||
|
var listeningSession = stream.syncStream(syncData)
|
||||||
|
|
||||||
|
if (listeningSession && listeningSession.timeListening > 0) {
|
||||||
|
// Save listening session
|
||||||
|
var existingListeningSession = this.db.sessions.find(s => s.id === listeningSession.id)
|
||||||
|
if (existingListeningSession) {
|
||||||
|
this.db.updateEntity('session', listeningSession)
|
||||||
|
} else {
|
||||||
|
this.db.sessions.push(listeningSession.toJSON()) // Insert right away to prevent duplicate session
|
||||||
|
this.db.insertEntity('session', listeningSession)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var userAudiobook = user.updateAudiobookProgressFromStream(stream)
|
||||||
|
this.db.updateEntity('user', user)
|
||||||
|
|
||||||
|
if (userAudiobook) {
|
||||||
|
this.clientEmitter(user.id, 'current_user_audiobook_update', {
|
||||||
|
id: userAudiobook.audiobookId,
|
||||||
|
data: userAudiobook.toJSON()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
res.sendStatus(200)
|
||||||
|
}
|
||||||
|
|
||||||
streamUpdate(socket, { currentTime, streamId }) {
|
streamUpdate(socket, { currentTime, streamId }) {
|
||||||
var client = socket.sheepClient
|
var client = socket.sheepClient
|
||||||
if (!client || !client.stream) {
|
if (!client || !client.stream) {
|
||||||
|
@ -179,10 +179,13 @@ class Stream extends EventEmitter {
|
|||||||
|
|
||||||
syncStream({ timeListened, currentTime }) {
|
syncStream({ timeListened, currentTime }) {
|
||||||
var syncLog = ''
|
var syncLog = ''
|
||||||
|
// Set user current time
|
||||||
if (currentTime !== null && !isNaN(currentTime)) {
|
if (currentTime !== null && !isNaN(currentTime)) {
|
||||||
syncLog = `Update client current time ${secondsToTimestamp(currentTime)}`
|
syncLog = `Update client current time ${secondsToTimestamp(currentTime)}`
|
||||||
this.clientCurrentTime = currentTime
|
this.clientCurrentTime = currentTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update user listening session
|
||||||
var saveListeningSession = false
|
var saveListeningSession = false
|
||||||
if (timeListened && !isNaN(timeListened)) {
|
if (timeListened && !isNaN(timeListened)) {
|
||||||
|
|
||||||
@ -202,6 +205,7 @@ class Stream extends EventEmitter {
|
|||||||
syncLog += `Add listening time ${timeListened}s, Total time listened ${this.listeningSession.timeListening}s`
|
syncLog += `Add listening time ${timeListened}s, Total time listened ${this.listeningSession.timeListening}s`
|
||||||
saveListeningSession = true
|
saveListeningSession = true
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.debug('[Stream]', syncLog)
|
Logger.debug('[Stream]', syncLog)
|
||||||
return saveListeningSession ? this.listeningSession : null
|
return saveListeningSession ? this.listeningSession : null
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user