Moving settings to be user specific, adding playbackRate setting, update playbackRate picker to go up to 3x

This commit is contained in:
advplyr 2021-08-23 18:31:04 -05:00
parent 40502aab1e
commit 7ef977b783
22 changed files with 247 additions and 103 deletions

View file

@ -1,5 +1,6 @@
const express = require('express')
const Logger = require('./Logger')
const { isObject } = require('./utils/index')
class ApiController {
constructor(db, scanner, auth, streamManager, rssFeeds, emitter) {
@ -32,6 +33,7 @@ class ApiController {
this.router.get('/users', this.getUsers.bind(this))
this.router.delete('/user/audiobook/:id', this.resetUserAudiobookProgress.bind(this))
this.router.patch('/user/password', this.userChangePassword.bind(this))
this.router.patch('/user/settings', this.userUpdateSettings.bind(this))
this.router.post('/authorize', this.authorize.bind(this))
@ -185,6 +187,21 @@ class ApiController {
res.json(feed)
}
async userUpdateSettings(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
})
}
getGenres(req, res) {
res.json({
genres: this.db.getGenres()

View file

@ -8,12 +8,22 @@ class User {
this.token = null
this.createdAt = null
this.audiobooks = null
this.settings = {}
if (user) {
this.construct(user)
}
}
getDefaultUserSettings() {
return {
orderBy: 'book.title',
orderDesc: false,
filterBy: 'all',
playbackRate: 1
}
}
toJSON() {
return {
id: this.id,
@ -23,7 +33,8 @@ class User {
stream: this.stream,
token: this.token,
audiobooks: this.audiobooks,
createdAt: this.createdAt
createdAt: this.createdAt,
settings: this.settings
}
}
@ -35,7 +46,8 @@ class User {
stream: this.stream,
token: this.token,
audiobooks: this.audiobooks,
createdAt: this.createdAt
createdAt: this.createdAt,
settings: this.settings
}
}
@ -48,6 +60,7 @@ class User {
this.token = user.token
this.audiobooks = user.audiobooks || null
this.createdAt = user.createdAt
this.settings = user.settings || this.getDefaultUserSettings()
}
updateAudiobookProgress(stream) {
@ -64,6 +77,32 @@ class User {
this.audiobooks[stream.audiobookId].currentTime = stream.clientCurrentTime
}
// Returns Boolean If update was made
updateSettings(settings) {
if (!this.settings) {
this.settings = { ...settings }
return true
}
var madeUpdates = false
for (const key in this.settings) {
if (settings[key] !== undefined && this.settings[key] !== settings[key]) {
this.settings[key] = settings[key]
madeUpdates = true
}
}
// Check if new settings update has keys not currently in user settings
for (const key in settings) {
if (settings[key] !== undefined && this.settings[key] === undefined) {
this.settings[key] = settings[key]
madeUpdates = true
}
}
return madeUpdates
}
resetAudiobookProgress(audiobookId) {
if (!this.audiobooks || !this.audiobooks[audiobookId]) {
return false

View file

@ -40,4 +40,8 @@ const cleanString = (str) => {
}
return cleaned
}
module.exports.cleanString = cleanString
module.exports.cleanString = cleanString
module.exports.isObject = (val) => {
return val !== null && typeof val === 'object'
}