2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
const User = require('../objects/user/User')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2022-06-04 17:52:37 +02:00
|
|
|
const { getId, toNumber } = require('../utils/index')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
class UserController {
|
|
|
|
constructor() { }
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
findAll(req, res) {
|
2022-05-04 02:16:16 +02:00
|
|
|
if (!req.user.isAdminOrUp) return res.sendStatus(403)
|
|
|
|
const hideRootToken = !req.user.isRoot
|
|
|
|
var users = this.db.users.map(u => this.userJsonWithItemProgressDetails(u, hideRootToken))
|
2022-03-18 01:10:47 +01:00
|
|
|
res.json(users)
|
|
|
|
}
|
|
|
|
|
|
|
|
findOne(req, res) {
|
2022-05-04 02:16:16 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error('User other than admin attempting to get user', req.user)
|
2022-03-18 01:10:47 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = this.db.users.find(u => u.id === req.params.id)
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2022-05-04 02:16:16 +02:00
|
|
|
res.json(this.userJsonWithItemProgressDetails(user, !req.user.isRoot))
|
2022-03-18 01:10:47 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
async create(req, res) {
|
|
|
|
var account = req.body
|
|
|
|
|
|
|
|
var username = account.username
|
|
|
|
var usernameExists = this.db.users.find(u => u.username.toLowerCase() === username.toLowerCase())
|
|
|
|
if (usernameExists) {
|
|
|
|
return res.status(500).send('Username already taken')
|
|
|
|
}
|
|
|
|
|
|
|
|
account.id = getId('usr')
|
|
|
|
account.pash = await this.auth.hashPass(account.password)
|
|
|
|
delete account.password
|
2022-07-19 00:19:16 +02:00
|
|
|
account.token = await this.auth.generateAccessToken({ userId: account.id, username })
|
2021-11-22 03:00:40 +01:00
|
|
|
account.createdAt = Date.now()
|
|
|
|
var newUser = new User(account)
|
|
|
|
var success = await this.db.insertEntity('user', newUser)
|
|
|
|
if (success) {
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.clientEmitter(req.user.id, 'user_added', newUser)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json({
|
|
|
|
user: newUser.toJSONForBrowser()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
return res.status(500).send('Failed to save new user')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async update(req, res) {
|
2022-09-26 00:11:39 +02:00
|
|
|
var user = req.reqUser
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2022-05-04 02:16:16 +02:00
|
|
|
if (user.type === 'root' && !req.user.isRoot) {
|
|
|
|
Logger.error(`[UserController] Admin user attempted to update root user`, req.user.username)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
var account = req.body
|
2022-07-19 00:19:16 +02:00
|
|
|
var shouldUpdateToken = false
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
if (account.username !== undefined && account.username !== user.username) {
|
|
|
|
var usernameExists = this.db.users.find(u => u.username.toLowerCase() === account.username.toLowerCase())
|
|
|
|
if (usernameExists) {
|
|
|
|
return res.status(500).send('Username already taken')
|
|
|
|
}
|
2022-07-19 00:19:16 +02:00
|
|
|
shouldUpdateToken = true
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updating password
|
|
|
|
if (account.password) {
|
|
|
|
account.pash = await this.auth.hashPass(account.password)
|
|
|
|
delete account.password
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasUpdated = user.update(account)
|
|
|
|
if (hasUpdated) {
|
2022-07-19 00:19:16 +02:00
|
|
|
if (shouldUpdateToken) {
|
|
|
|
user.token = await this.auth.generateAccessToken({ userId: user.id, username: user.username })
|
|
|
|
Logger.info(`[UserController] User ${user.username} was generated a new api token`)
|
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
await this.db.updateEntity('user', user)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.clientEmitter(req.user.id, 'user_updated', user.toJSONForBrowser())
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
user: user.toJSONForBrowser()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete(req, res) {
|
|
|
|
if (req.params.id === 'root') {
|
2022-09-26 00:11:39 +02:00
|
|
|
Logger.error('[UserController] Attempt to delete root user. Root user cannot be deleted')
|
2021-11-22 03:00:40 +01:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
if (req.user.id === req.params.id) {
|
2022-09-26 00:11:39 +02:00
|
|
|
Logger.error(`[UserController] ${req.user.username} is attempting to delete themselves... why? WHY?`)
|
2021-11-22 03:00:40 +01:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
2022-09-26 00:11:39 +02:00
|
|
|
var user = req.reqUser
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
// Todo: check if user is logged in and cancel streams
|
|
|
|
|
|
|
|
var userJson = user.toJSONForBrowser()
|
|
|
|
await this.db.removeEntity('user', user.id)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.clientEmitter(req.user.id, 'user_removed', userJson)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json({
|
|
|
|
success: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET: api/users/:id/listening-sessions
|
|
|
|
async getListeningSessions(req, res) {
|
|
|
|
var listeningSessions = await this.getUserListeningSessionsHelper(req.params.id)
|
2022-06-04 17:52:37 +02:00
|
|
|
|
|
|
|
const itemsPerPage = toNumber(req.query.itemsPerPage, 10) || 10
|
|
|
|
const page = toNumber(req.query.page, 0)
|
|
|
|
|
|
|
|
const start = page * itemsPerPage
|
|
|
|
const sessions = listeningSessions.slice(start, start + itemsPerPage)
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
total: listeningSessions.length,
|
|
|
|
numPages: Math.ceil(listeningSessions.length / itemsPerPage),
|
|
|
|
page,
|
|
|
|
itemsPerPage,
|
|
|
|
sessions
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(payload)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET: api/users/:id/listening-stats
|
|
|
|
async getListeningStats(req, res) {
|
2022-09-26 00:11:39 +02:00
|
|
|
var listeningStats = await this.getUserListeningStatsHelpers(req.params.id)
|
|
|
|
res.json(listeningStats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST: api/users/:id/purge-media-progress
|
|
|
|
async purgeMediaProgress(req, res) {
|
|
|
|
const user = req.reqUser
|
|
|
|
|
|
|
|
if (user.type === 'root' && !req.user.isRoot) {
|
|
|
|
Logger.error(`[UserController] Admin user attempted to purge media progress of root user`, req.user.username)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
var progressPurged = 0
|
|
|
|
user.mediaProgress = user.mediaProgress.filter(mp => {
|
|
|
|
const libraryItem = this.db.libraryItems.find(li => li.id === mp.libraryItemId)
|
|
|
|
if (!libraryItem) {
|
|
|
|
progressPurged++
|
|
|
|
return false
|
|
|
|
} else if (mp.episodeId) {
|
|
|
|
const episode = libraryItem.mediaType === 'podcast' ? libraryItem.media.getEpisode(mp.episodeId) : null
|
|
|
|
if (!episode) { // Episode not found
|
|
|
|
progressPurged++
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if (progressPurged) {
|
|
|
|
Logger.info(`[UserController] Purged ${progressPurged} media progress for user ${user.username}`)
|
|
|
|
await this.db.updateEntity('user', user)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.clientEmitter(req.user.id, 'user_updated', user.toJSONForBrowser())
|
2022-09-26 00:11:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(this.userJsonWithItemProgressDetails(user, !req.user.isRoot))
|
|
|
|
}
|
|
|
|
|
2022-11-11 00:42:20 +01:00
|
|
|
// POST: api/users/online (admin)
|
|
|
|
async getOnlineUsers(req, res) {
|
|
|
|
if (!req.user.isAdminOrUp) {
|
2022-11-13 15:26:32 +01:00
|
|
|
return res.sendStatus(403)
|
2022-11-11 00:42:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
2022-11-24 22:53:58 +01:00
|
|
|
usersOnline: SocketAuthority.getUsersOnline(),
|
2022-11-11 00:42:20 +01:00
|
|
|
openSessions: this.playbackSessionManager.sessions
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-26 00:11:39 +02:00
|
|
|
middleware(req, res, next) {
|
2022-05-04 02:16:16 +02:00
|
|
|
if (!req.user.isAdminOrUp && req.user.id !== req.params.id) {
|
2021-11-22 03:00:40 +01:00
|
|
|
return res.sendStatus(403)
|
2022-09-26 00:11:39 +02:00
|
|
|
} else if ((req.method == 'PATCH' || req.method == 'POST' || req.method == 'DELETE') && !req.user.isAdminOrUp) {
|
|
|
|
return res.sendStatus(403)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2022-09-26 00:11:39 +02:00
|
|
|
|
|
|
|
if (req.params.id) {
|
|
|
|
req.reqUser = this.db.users.find(u => u.id === req.params.id)
|
|
|
|
if (!req.reqUser) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new UserController()
|