Remove playlists for user when removing user

This commit is contained in:
advplyr 2022-11-27 14:54:17 -06:00
parent e307ded192
commit eb2ea9950a

View File

@ -11,7 +11,7 @@ class UserController {
findAll(req, res) {
if (!req.user.isAdminOrUp) return res.sendStatus(403)
const hideRootToken = !req.user.isRoot
var users = this.db.users.map(u => this.userJsonWithItemProgressDetails(u, hideRootToken))
const users = this.db.users.map(u => this.userJsonWithItemProgressDetails(u, hideRootToken))
res.json(users)
}
@ -21,7 +21,7 @@ class UserController {
return res.sendStatus(403)
}
var user = this.db.users.find(u => u.id === req.params.id)
const user = this.db.users.find(u => u.id === req.params.id)
if (!user) {
return res.sendStatus(404)
}
@ -46,7 +46,7 @@ class UserController {
var newUser = new User(account)
var success = await this.db.insertEntity('user', newUser)
if (success) {
SocketAuthority.clientEmitter(req.user.id, 'user_added', newUser)
SocketAuthority.adminEmitter('user_added', newUser)
res.json({
user: newUser.toJSONForBrowser()
})
@ -105,13 +105,19 @@ class UserController {
Logger.error(`[UserController] ${req.user.username} is attempting to delete themselves... why? WHY?`)
return res.sendStatus(500)
}
var user = req.reqUser
const user = req.reqUser
// Todo: check if user is logged in and cancel streams
var userJson = user.toJSONForBrowser()
// Remove user playlists
const userPlaylists = this.db.playlists.filter(p => p.userId === user.id)
for (const playlist of userPlaylists) {
await this.db.removeEntity('playlist', playlist.id)
}
const userJson = user.toJSONForBrowser()
await this.db.removeEntity('user', user.id)
SocketAuthority.clientEmitter(req.user.id, 'user_removed', userJson)
SocketAuthority.adminEmitter('user_removed', userJson)
res.json({
success: true
})
@ -172,7 +178,7 @@ class UserController {
if (progressPurged) {
Logger.info(`[UserController] Purged ${progressPurged} media progress for user ${user.username}`)
await this.db.updateEntity('user', user)
SocketAuthority.clientEmitter(req.user.id, 'user_updated', user.toJSONForBrowser())
SocketAuthority.adminEmitter('user_updated', user.toJSONForBrowser())
}
res.json(this.userJsonWithItemProgressDetails(user, !req.user.isRoot))