2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const { isObject } = require('../utils/index')
|
|
|
|
|
|
|
|
class MeController {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
// GET: api/me/listening-sessions
|
|
|
|
async getListeningSessions(req, res) {
|
|
|
|
var listeningSessions = await this.getUserListeningSessionsHelper(req.user.id)
|
|
|
|
res.json(listeningSessions.slice(0, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET: api/me/listening-stats
|
|
|
|
async getListeningStats(req, res) {
|
|
|
|
var listeningStats = await this.getUserListeningStatsHelpers(req.user.id)
|
|
|
|
res.json(listeningStats)
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:33:22 +01:00
|
|
|
// DELETE: api/me/progress/:id
|
|
|
|
async removeLibraryItemProgress(req, res) {
|
|
|
|
var wasRemoved = req.user.removeLibraryItemProgress(req.params.id)
|
|
|
|
if (!wasRemoved) {
|
|
|
|
return res.sendStatus(200)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
await this.db.updateEntity('user', req.user)
|
2022-03-17 19:33:22 +01:00
|
|
|
this.clientEmitter(req.user.id, 'user_item_progress_updated', { id: libraryItem.id, data: null })
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
async createUpdateLibraryItemProgress(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-03-17 19:33:22 +01:00
|
|
|
var wasUpdated = req.user.createUpdateLibraryItemProgress(libraryItem.id, 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-17 19:33:22 +01:00
|
|
|
// PATCH: api/me/progress/batch/update
|
|
|
|
async batchUpdateLibraryItemProgress(req, res) {
|
|
|
|
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-17 19:33:22 +01:00
|
|
|
var wasUpdated = req.user.createUpdateLibraryItemProgress(libraryItem.id, itemProgress)
|
2021-11-22 03:00:40 +01:00
|
|
|
if (wasUpdated) shouldUpdate = true
|
2022-03-17 19:33:22 +01:00
|
|
|
} else {
|
|
|
|
Logger.error(`[MeController] batchUpdateLibraryItemProgress: 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PATCH: api/me/password
|
|
|
|
updatePassword(req, res) {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new MeController()
|