]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users.js
Server: rename Pods -> Pod
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.js
CommitLineData
9457bf88
C
1'use strict'
2
9bd26629 3const each = require('async/each')
0c1cbbfe 4const express = require('express')
9bd26629 5const waterfall = require('async/waterfall')
fc51fde0 6
f253b1c1 7const constants = require('../../initializers/constants')
feb4bdfd 8const db = require('../../initializers/database')
f253b1c1
C
9const friends = require('../../lib/friends')
10const logger = require('../../helpers/logger')
11const middlewares = require('../../middlewares')
9bd26629
C
12const admin = middlewares.admin
13const oAuth = middlewares.oauth
5c39adb7
C
14const pagination = middlewares.pagination
15const sort = middlewares.sort
16const validatorsPagination = middlewares.validators.pagination
17const validatorsSort = middlewares.validators.sort
9bd26629 18const validatorsUsers = middlewares.validators.users
9457bf88 19
9457bf88
C
20const router = express.Router()
21
99a64bfe 22router.get('/me', oAuth.authenticate, getUserInformation)
9bd26629 23
5c39adb7
C
24router.get('/',
25 validatorsPagination.pagination,
26 validatorsSort.usersSort,
27 sort.setUsersSort,
28 pagination.setPagination,
29 listUsers
30)
31
9bd26629
C
32router.post('/',
33 oAuth.authenticate,
34 admin.ensureIsAdmin,
35 validatorsUsers.usersAdd,
36 createUser
37)
38
39router.put('/:id',
40 oAuth.authenticate,
41 validatorsUsers.usersUpdate,
42 updateUser
43)
44
68a3b9f2 45router.delete('/:id',
9bd26629
C
46 oAuth.authenticate,
47 admin.ensureIsAdmin,
48 validatorsUsers.usersRemove,
49 removeUser
50)
6606150c 51
69b0a27c 52router.post('/token', oAuth.token, success)
9bd26629 53// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
9457bf88
C
54
55// ---------------------------------------------------------------------------
56
57module.exports = router
58
59// ---------------------------------------------------------------------------
60
9bd26629 61function createUser (req, res, next) {
feb4bdfd 62 const user = db.User.build({
9bd26629
C
63 username: req.body.username,
64 password: req.body.password,
65 role: constants.USER_ROLES.USER
66 })
67
feb4bdfd 68 user.save().asCallback(function (err, createdUser) {
9bd26629
C
69 if (err) return next(err)
70
71 return res.type('json').status(204).end()
72 })
73}
74
99a64bfe 75function getUserInformation (req, res, next) {
feb4bdfd 76 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
99a64bfe
C
77 if (err) return next(err)
78
79 return res.json(user.toFormatedJSON())
80 })
81}
82
9bd26629 83function listUsers (req, res, next) {
feb4bdfd 84 db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
9bd26629
C
85 if (err) return next(err)
86
5c39adb7 87 res.json(getFormatedUsers(usersList, usersTotal))
9bd26629
C
88 })
89}
90
91function removeUser (req, res, next) {
92 waterfall([
93 function getUser (callback) {
feb4bdfd 94 db.User.loadById(req.params.id, callback)
9bd26629
C
95 },
96
feb4bdfd 97 // TODO: use foreignkey?
9bd26629 98 function getVideos (user, callback) {
feb4bdfd 99 db.Video.listOwnedByAuthor(user.username, function (err, videos) {
9bd26629
C
100 return callback(err, user, videos)
101 })
102 },
103
104 function removeVideosFromDB (user, videos, callback) {
105 each(videos, function (video, callbackEach) {
feb4bdfd 106 video.destroy().asCallback(callbackEach)
9bd26629
C
107 }, function (err) {
108 return callback(err, user, videos)
109 })
110 },
111
112 function sendInformationToFriends (user, videos, callback) {
113 videos.forEach(function (video) {
114 const params = {
115 name: video.name,
feb4bdfd 116 remoteId: video.id
9bd26629
C
117 }
118
119 friends.removeVideoToFriends(params)
120 })
121
122 return callback(null, user)
123 },
124
125 function removeUserFromDB (user, callback) {
feb4bdfd 126 user.destroy().asCallback(callback)
9bd26629
C
127 }
128 ], function andFinally (err) {
129 if (err) {
130 logger.error('Errors when removed the user.', { error: err })
131 return next(err)
132 }
133
10431358 134 return res.sendStatus(204)
9bd26629
C
135 })
136}
137
138function updateUser (req, res, next) {
feb4bdfd 139 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
9bd26629
C
140 if (err) return next(err)
141
142 user.password = req.body.password
feb4bdfd 143 user.save().asCallback(function (err) {
9bd26629
C
144 if (err) return next(err)
145
10431358 146 return res.sendStatus(204)
9bd26629
C
147 })
148 })
149}
150
9457bf88
C
151function success (req, res, next) {
152 res.end()
153}
9bd26629
C
154
155// ---------------------------------------------------------------------------
156
5c39adb7 157function getFormatedUsers (users, usersTotal) {
9bd26629
C
158 const formatedUsers = []
159
160 users.forEach(function (user) {
161 formatedUsers.push(user.toFormatedJSON())
162 })
163
164 return {
5c39adb7 165 total: usersTotal,
9bd26629
C
166 data: formatedUsers
167 }
168}