aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/v1/users.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-10-21 12:16:28 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-10-21 12:16:28 +0200
commitf253b1c1f19d9cb056ab95b2cb6208952e073894 (patch)
treece75f6a96d9ceebd09d33cedfa008c4efb32477b /server/controllers/api/v1/users.js
parent844e39c2f843c6e2db81699176f13972c29c42fa (diff)
downloadPeerTube-f253b1c1f19d9cb056ab95b2cb6208952e073894.tar.gz
PeerTube-f253b1c1f19d9cb056ab95b2cb6208952e073894.tar.zst
PeerTube-f253b1c1f19d9cb056ab95b2cb6208952e073894.zip
Server: remove v1 directory, we don't really need it
Diffstat (limited to 'server/controllers/api/v1/users.js')
-rw-r--r--server/controllers/api/v1/users.js170
1 files changed, 0 insertions, 170 deletions
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js
deleted file mode 100644
index 975e25e68..000000000
--- a/server/controllers/api/v1/users.js
+++ /dev/null
@@ -1,170 +0,0 @@
1'use strict'
2
3const each = require('async/each')
4const express = require('express')
5const mongoose = require('mongoose')
6const waterfall = require('async/waterfall')
7
8const constants = require('../../../initializers/constants')
9const friends = require('../../../lib/friends')
10const logger = require('../../../helpers/logger')
11const middlewares = require('../../../middlewares')
12const admin = middlewares.admin
13const oAuth = middlewares.oauth
14const pagination = middlewares.pagination
15const sort = middlewares.sort
16const validatorsPagination = middlewares.validators.pagination
17const validatorsSort = middlewares.validators.sort
18const validatorsUsers = middlewares.validators.users
19
20const User = mongoose.model('User')
21const Video = mongoose.model('Video')
22
23const router = express.Router()
24
25router.get('/me', oAuth.authenticate, getUserInformation)
26
27router.get('/',
28 validatorsPagination.pagination,
29 validatorsSort.usersSort,
30 sort.setUsersSort,
31 pagination.setPagination,
32 listUsers
33)
34
35router.post('/',
36 oAuth.authenticate,
37 admin.ensureIsAdmin,
38 validatorsUsers.usersAdd,
39 createUser
40)
41
42router.put('/:id',
43 oAuth.authenticate,
44 validatorsUsers.usersUpdate,
45 updateUser
46)
47
48router.delete('/:id',
49 oAuth.authenticate,
50 admin.ensureIsAdmin,
51 validatorsUsers.usersRemove,
52 removeUser
53)
54
55router.post('/token', oAuth.token, success)
56// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
57
58// ---------------------------------------------------------------------------
59
60module.exports = router
61
62// ---------------------------------------------------------------------------
63
64function createUser (req, res, next) {
65 const user = new User({
66 username: req.body.username,
67 password: req.body.password,
68 role: constants.USER_ROLES.USER
69 })
70
71 user.save(function (err, createdUser) {
72 if (err) return next(err)
73
74 return res.type('json').status(204).end()
75 })
76}
77
78function getUserInformation (req, res, next) {
79 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
80 if (err) return next(err)
81
82 return res.json(user.toFormatedJSON())
83 })
84}
85
86function listUsers (req, res, next) {
87 User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
88 if (err) return next(err)
89
90 res.json(getFormatedUsers(usersList, usersTotal))
91 })
92}
93
94function removeUser (req, res, next) {
95 waterfall([
96 function getUser (callback) {
97 User.loadById(req.params.id, callback)
98 },
99
100 function getVideos (user, callback) {
101 Video.listOwnedByAuthor(user.username, function (err, videos) {
102 return callback(err, user, videos)
103 })
104 },
105
106 function removeVideosFromDB (user, videos, callback) {
107 each(videos, function (video, callbackEach) {
108 video.remove(callbackEach)
109 }, function (err) {
110 return callback(err, user, videos)
111 })
112 },
113
114 function sendInformationToFriends (user, videos, callback) {
115 videos.forEach(function (video) {
116 const params = {
117 name: video.name,
118 magnetUri: video.magnetUri
119 }
120
121 friends.removeVideoToFriends(params)
122 })
123
124 return callback(null, user)
125 },
126
127 function removeUserFromDB (user, callback) {
128 user.remove(callback)
129 }
130 ], function andFinally (err) {
131 if (err) {
132 logger.error('Errors when removed the user.', { error: err })
133 return next(err)
134 }
135
136 return res.sendStatus(204)
137 })
138}
139
140function updateUser (req, res, next) {
141 User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
142 if (err) return next(err)
143
144 user.password = req.body.password
145 user.save(function (err) {
146 if (err) return next(err)
147
148 return res.sendStatus(204)
149 })
150 })
151}
152
153function success (req, res, next) {
154 res.end()
155}
156
157// ---------------------------------------------------------------------------
158
159function getFormatedUsers (users, usersTotal) {
160 const formatedUsers = []
161
162 users.forEach(function (user) {
163 formatedUsers.push(user.toFormatedJSON())
164 })
165
166 return {
167 total: usersTotal,
168 data: formatedUsers
169 }
170}