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