2023-07-05 01:14:44 +02:00
|
|
|
const uuidv4 = require("uuid").v4
|
2021-11-22 03:00:40 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
const User = require('../objects/user/User')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
const { toNumber } = require('../utils/index')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
class UserController {
|
|
|
|
constructor() { }
|
|
|
|
|
2023-06-29 00:57:46 +02:00
|
|
|
async findAll(req, res) {
|
2022-05-04 02:16:16 +02:00
|
|
|
if (!req.user.isAdminOrUp) return res.sendStatus(403)
|
|
|
|
const hideRootToken = !req.user.isRoot
|
2023-06-29 00:57:46 +02:00
|
|
|
|
|
|
|
const includes = (req.query.include || '').split(',').map(i => i.trim())
|
|
|
|
|
|
|
|
// Minimal toJSONForBrowser does not include mediaProgress and bookmarks
|
2023-07-22 22:32:20 +02:00
|
|
|
const allUsers = await Database.models.user.getOldUsers()
|
|
|
|
const users = allUsers.map(u => u.toJSONForBrowser(hideRootToken, true))
|
2023-06-29 00:57:46 +02:00
|
|
|
|
|
|
|
if (includes.includes('latestSession')) {
|
|
|
|
for (const user of users) {
|
2023-07-05 01:14:44 +02:00
|
|
|
const userSessions = await Database.getPlaybackSessions({ userId: user.id })
|
2023-06-29 00:57:46 +02:00
|
|
|
user.latestSession = userSessions.sort((a, b) => b.updatedAt - a.updatedAt).shift() || null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 18:43:39 +01:00
|
|
|
res.json({
|
2023-06-29 00:57:46 +02:00
|
|
|
users
|
2022-11-29 18:43:39 +01:00
|
|
|
})
|
2022-03-18 01:10:47 +01:00
|
|
|
}
|
|
|
|
|
2023-07-22 22:32:20 +02:00
|
|
|
async 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)
|
|
|
|
}
|
|
|
|
|
2023-07-22 22:32:20 +02:00
|
|
|
res.json(this.userJsonWithItemProgressDetails(req.reqUser, !req.user.isRoot))
|
2022-03-18 01:10:47 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
async create(req, res) {
|
2023-07-22 22:32:20 +02:00
|
|
|
const account = req.body
|
|
|
|
const username = account.username
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2023-07-22 22:32:20 +02:00
|
|
|
const usernameExists = await Database.models.user.getUserByUsername(username)
|
2021-11-22 03:00:40 +01:00
|
|
|
if (usernameExists) {
|
|
|
|
return res.status(500).send('Username already taken')
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
account.id = uuidv4()
|
2021-11-22 03:00:40 +01:00
|
|
|
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()
|
2023-07-05 01:14:44 +02:00
|
|
|
const newUser = new User(account)
|
|
|
|
|
|
|
|
const success = await Database.createUser(newUser)
|
2021-11-22 03:00:40 +01:00
|
|
|
if (success) {
|
2022-12-22 23:26:11 +01:00
|
|
|
SocketAuthority.adminEmitter('user_added', newUser.toJSONForBrowser())
|
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) {
|
2023-07-22 22:32:20 +02:00
|
|
|
const 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) {
|
2023-07-22 22:32:20 +02:00
|
|
|
const usernameExists = await Database.models.user.getUserByUsername(account.username)
|
2021-11-22 03:00:40 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
if (user.update(account)) {
|
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`)
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateUser(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-11-27 21:54:17 +01:00
|
|
|
const user = req.reqUser
|
2021-11-22 03:00:40 +01:00
|
|
|
|
|
|
|
// Todo: check if user is logged in and cancel streams
|
|
|
|
|
2022-11-27 21:54:17 +01:00
|
|
|
// Remove user playlists
|
2023-08-13 18:22:38 +02:00
|
|
|
const userPlaylists = await Database.models.playlist.findAll({
|
|
|
|
where: {
|
|
|
|
userId: user.id
|
|
|
|
}
|
|
|
|
})
|
2022-11-27 21:54:17 +01:00
|
|
|
for (const playlist of userPlaylists) {
|
2023-08-13 18:22:38 +02:00
|
|
|
await playlist.destroy()
|
2022-11-27 21:54:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const userJson = user.toJSONForBrowser()
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.removeUser(user.id)
|
2022-11-27 21:54:17 +01:00
|
|
|
SocketAuthority.adminEmitter('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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-22 22:32:20 +02:00
|
|
|
async 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) {
|
2023-07-22 22:32:20 +02:00
|
|
|
req.reqUser = await Database.models.user.getUserById(req.params.id)
|
2022-09-26 00:11:39 +02:00
|
|
|
if (!req.reqUser) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new UserController()
|