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?`)) { if (confirm(`Are you sure you want to reset your progress?`)) {
this.resettingProgress = true this.resettingProgress = true
this.$axios this.$axios
.$delete(`/api/user/audiobook/${this.audiobookId}`) .$patch(`/api/user/audiobook/${this.audiobookId}/reset-progress`)
.then(() => { .then(() => {
console.log('Progress reset complete') console.log('Progress reset complete')
this.$toast.success(`Your progress was reset`) this.$toast.success(`Your progress was reset`)

View File

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

View File

@ -462,14 +462,9 @@ class Server {
Logger.error('[Server] audiobookProgressUpdate invalid socket client') Logger.error('[Server] audiobookProgressUpdate invalid socket client')
return return
} }
var audiobookProgress = client.user.updateAudiobookProgress(progressPayload.audiobookId, progressPayload) var audiobookProgress = client.user.updateAudiobookData(progressPayload.audiobookId, progressPayload)
if (audiobookProgress) { if (audiobookProgress) {
await this.db.updateEntity('user', client.user) 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', { this.clientEmitter(client.user.id, 'current_user_audiobook_update', {
id: progressPayload.audiobookId, id: progressPayload.audiobookId,
data: audiobookProgress || null data: audiobookProgress || null

View File

@ -1,5 +1,5 @@
const Logger = require('../Logger') const Logger = require('../Logger')
const AudiobookProgress = require('./AudiobookProgress') const UserAudiobookData = require('./UserAudiobookData')
class User { class User {
constructor(user) { constructor(user) {
@ -141,7 +141,7 @@ class User {
this.audiobooks = {} this.audiobooks = {}
for (const key in user.audiobooks) { for (const key in user.audiobooks) {
if (user.audiobooks[key]) { 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) { updateAudiobookProgressFromStream(stream) {
if (!this.audiobooks) this.audiobooks = {} if (!this.audiobooks) this.audiobooks = {}
if (!this.audiobooks[stream.audiobookId]) { 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] return this.audiobooks[stream.audiobookId]
} }
updateAudiobookProgress(audiobook, updatePayload) { updateAudiobookData(audiobook, updatePayload) {
if (!this.audiobooks) this.audiobooks = {} if (!this.audiobooks) this.audiobooks = {}
if (!this.audiobooks[audiobook.id]) { if (!this.audiobooks[audiobook.id]) {
this.audiobooks[audiobook.id] = new AudiobookProgress() this.audiobooks[audiobook.id] = new UserAudiobookData()
this.audiobooks[audiobook.id].audiobookId = audiobook.id this.audiobooks[audiobook.id].audiobookId = audiobook.id
} }
var wasUpdated = this.audiobooks[audiobook.id].update(updatePayload) var wasUpdated = this.audiobooks[audiobook.id].update(updatePayload)
if (wasUpdated) { 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 this.audiobooks[audiobook.id]
} }
return false return false
@ -251,7 +251,7 @@ class User {
if (!this.audiobooks || !this.audiobooks[audiobook.id]) { if (!this.audiobooks || !this.audiobooks[audiobook.id]) {
return false return false
} }
return this.updateAudiobookProgress(audiobook, { return this.updateAudiobookData(audiobook, {
progress: 0, progress: 0,
currentTime: 0, currentTime: 0,
isRead: false, isRead: false,
@ -261,7 +261,7 @@ class User {
}) })
} }
deleteAudiobookProgress(audiobookId) { deleteAudiobookData(audiobookId) {
if (!this.audiobooks || !this.audiobooks[audiobookId]) { if (!this.audiobooks || !this.audiobooks[audiobookId]) {
return false return false
} }

View File

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