Change: AudiobookProgress becomes UserAudiobookData, Change: Delete audiobook progress route

This commit is contained in:
advplyr 2021-10-28 17:19:09 -05:00
parent ad4dad1c29
commit ff92fbb849
5 changed files with 30 additions and 32 deletions

View File

@ -413,7 +413,7 @@ export default {
if (confirm(`Are you sure you want to reset your progress?`)) {
this.resettingProgress = true
this.$axios
.$delete(`/api/user/audiobook/${this.audiobookId}`)
.$patch(`/api/user/audiobook/${this.audiobookId}/reset-progress`)
.then(() => {
console.log('Progress reset complete')
this.$toast.success(`Your progress was reset`)

View File

@ -60,9 +60,12 @@ class ApiController {
this.router.patch('/match/:id', this.match.bind(this))
// Old Route : Wait until refactor of mobile app to replace with path /reset-progress
this.router.delete('/user/audiobook/:id', this.resetUserAudiobookProgress.bind(this))
this.router.patch('/user/audiobook/:id', this.updateUserAudiobookProgress.bind(this))
this.router.patch('/user/audiobooks', this.batchUpdateUserAudiobooksProgress.bind(this))
this.router.patch('/user/audiobook/:id/reset-progress', this.resetUserAudiobookProgress.bind(this))
this.router.patch('/user/audiobook/:id', this.updateUserAudiobookData.bind(this))
this.router.patch('/user/audiobooks', this.batchUpdateUserAudiobookData.bind(this))
this.router.patch('/user/password', this.userChangePassword.bind(this))
this.router.patch('/user/settings', this.userUpdateSettings.bind(this))
@ -342,7 +345,7 @@ class ApiController {
// Remove audiobook from users
for (let i = 0; i < this.db.users.length; i++) {
var user = this.db.users[i]
var madeUpdates = user.deleteAudiobookProgress(audiobook.id)
var madeUpdates = user.deleteAudiobookData(audiobook.id)
if (madeUpdates) {
await this.db.updateEntity('user', user)
}
@ -564,12 +567,12 @@ class ApiController {
res.sendStatus(200)
}
async updateUserAudiobookProgress(req, res) {
async updateUserAudiobookData(req, res) {
var audiobook = this.db.audiobooks.find(ab => ab.id === req.params.id)
if (!audiobook) {
return res.status(404).send('Audiobook not found')
}
var wasUpdated = req.user.updateAudiobookProgress(audiobook, req.body)
var wasUpdated = req.user.updateAudiobookData(audiobook, req.body)
if (wasUpdated) {
await this.db.updateEntity('user', req.user)
this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
@ -577,17 +580,17 @@ class ApiController {
res.sendStatus(200)
}
async batchUpdateUserAudiobooksProgress(req, res) {
var abProgresses = req.body
if (!abProgresses || !abProgresses.length) {
async batchUpdateUserAudiobookData(req, res) {
var userAbDataPayloads = req.body
if (!userAbDataPayloads || !userAbDataPayloads.length) {
return res.sendStatus(500)
}
var shouldUpdate = false
abProgresses.forEach((progress) => {
var audiobook = this.db.audiobooks.find(ab => ab.id === progress.audiobookId)
userAbDataPayloads.forEach((userAbData) => {
var audiobook = this.db.audiobooks.find(ab => ab.id === userAbData.audiobookId)
if (audiobook) {
var wasUpdated = req.user.updateAudiobookProgress(audiobook, progress)
var wasUpdated = req.user.updateAudiobookData(audiobook, userAbData)
if (wasUpdated) shouldUpdate = true
}
})

View File

@ -462,14 +462,9 @@ class Server {
Logger.error('[Server] audiobookProgressUpdate invalid socket client')
return
}
var audiobookProgress = client.user.updateAudiobookProgress(progressPayload.audiobookId, progressPayload)
var audiobookProgress = client.user.updateAudiobookData(progressPayload.audiobookId, progressPayload)
if (audiobookProgress) {
await this.db.updateEntity('user', client.user)
// This audiobook progress is out of date, why?
// var userAudiobook = client.user.getAudiobookJSON(progressPayload.audiobookId)
// Logger.debug(`[Server] Emitting audiobook progress update to clients ${this.getClientsForUser(client.user.id).length}: ${JSON.stringify(userAudiobook)}`)
this.clientEmitter(client.user.id, 'current_user_audiobook_update', {
id: progressPayload.audiobookId,
data: audiobookProgress || null

View File

@ -1,5 +1,5 @@
const Logger = require('../Logger')
const AudiobookProgress = require('./AudiobookProgress')
const UserAudiobookData = require('./UserAudiobookData')
class User {
constructor(user) {
@ -141,7 +141,7 @@ class User {
this.audiobooks = {}
for (const key in user.audiobooks) {
if (user.audiobooks[key]) {
this.audiobooks[key] = new AudiobookProgress(user.audiobooks[key])
this.audiobooks[key] = new UserAudiobookData(user.audiobooks[key])
}
}
}
@ -201,21 +201,21 @@ class User {
updateAudiobookProgressFromStream(stream) {
if (!this.audiobooks) this.audiobooks = {}
if (!this.audiobooks[stream.audiobookId]) {
this.audiobooks[stream.audiobookId] = new AudiobookProgress()
this.audiobooks[stream.audiobookId] = new UserAudiobookData()
}
this.audiobooks[stream.audiobookId].updateFromStream(stream)
this.audiobooks[stream.audiobookId].updateProgressFromStream(stream)
return this.audiobooks[stream.audiobookId]
}
updateAudiobookProgress(audiobook, updatePayload) {
updateAudiobookData(audiobook, updatePayload) {
if (!this.audiobooks) this.audiobooks = {}
if (!this.audiobooks[audiobook.id]) {
this.audiobooks[audiobook.id] = new AudiobookProgress()
this.audiobooks[audiobook.id] = new UserAudiobookData()
this.audiobooks[audiobook.id].audiobookId = audiobook.id
}
var wasUpdated = this.audiobooks[audiobook.id].update(updatePayload)
if (wasUpdated) {
Logger.debug(`[User] Audiobook progress was updated ${JSON.stringify(this.audiobooks[audiobook.id])}`)
Logger.debug(`[User] UserAudiobookData was updated ${JSON.stringify(this.audiobooks[audiobook.id])}`)
return this.audiobooks[audiobook.id]
}
return false
@ -251,7 +251,7 @@ class User {
if (!this.audiobooks || !this.audiobooks[audiobook.id]) {
return false
}
return this.updateAudiobookProgress(audiobook, {
return this.updateAudiobookData(audiobook, {
progress: 0,
currentTime: 0,
isRead: false,
@ -261,7 +261,7 @@ class User {
})
}
deleteAudiobookProgress(audiobookId) {
deleteAudiobookData(audiobookId) {
if (!this.audiobooks || !this.audiobooks[audiobookId]) {
return false
}

View File

@ -1,7 +1,7 @@
const Logger = require('../Logger')
const AudioBookmark = require('./AudioBookmark')
class AudiobookProgress {
class UserAudiobookData {
constructor(progress) {
this.audiobookId = null
@ -24,7 +24,7 @@ class AudiobookProgress {
if (!this.bookmarks) return []
return this.bookmarks.filter((b) => {
if (!b.toJSON) {
Logger.error(`[AudiobookProgress] Invalid bookmark ${JSON.stringify(b)}`)
Logger.error(`[UserAudiobookData] Invalid bookmark ${JSON.stringify(b)}`)
return false
}
return true
@ -61,7 +61,7 @@ class AudiobookProgress {
}
}
updateFromStream(stream) {
updateProgressFromStream(stream) {
this.audiobookId = stream.audiobookId
this.totalDuration = stream.totalDuration
this.progress = stream.clientProgress
@ -86,7 +86,7 @@ class AudiobookProgress {
update(payload) {
var hasUpdates = false
Logger.debug(`[AudiobookProgress] Update called ${JSON.stringify(payload)}`)
Logger.debug(`[UserAudiobookData] Update called ${JSON.stringify(payload)}`)
for (const key in payload) {
if (payload[key] !== this[key]) {
if (key === 'isRead') {
@ -135,4 +135,4 @@ class AudiobookProgress {
this.bookmarks = this.bookmarks.filter(bm => bm.time !== time)
}
}
module.exports = AudiobookProgress
module.exports = UserAudiobookData