aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api')
-rw-r--r--server/controllers/api/v1/clients.js41
-rw-r--r--server/controllers/api/v1/index.js4
-rw-r--r--server/controllers/api/v1/pods.js62
-rw-r--r--server/controllers/api/v1/remote.js1
-rw-r--r--server/controllers/api/v1/requests.js38
-rw-r--r--server/controllers/api/v1/users.js165
-rw-r--r--server/controllers/api/v1/videos.js7
7 files changed, 281 insertions, 37 deletions
diff --git a/server/controllers/api/v1/clients.js b/server/controllers/api/v1/clients.js
new file mode 100644
index 000000000..5b460db2e
--- /dev/null
+++ b/server/controllers/api/v1/clients.js
@@ -0,0 +1,41 @@
1'use strict'
2
3const express = require('express')
4const mongoose = require('mongoose')
5
6const constants = require('../../../initializers/constants')
7
8const Client = mongoose.model('OAuthClient')
9
10const router = express.Router()
11
12router.get('/local', getLocalClient)
13
14// Get the client credentials for the PeerTube front end
15function getLocalClient (req, res, next) {
16 const serverHost = constants.CONFIG.WEBSERVER.HOST
17 const serverPort = constants.CONFIG.WEBSERVER.PORT
18 let headerHostShouldBe = serverHost
19 if (serverPort !== 80 && serverPort !== 443) {
20 headerHostShouldBe += ':' + serverPort
21 }
22
23 // Don't make this check if this is a test instance
24 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) {
25 return res.type('json').status(403).end()
26 }
27
28 Client.loadFirstClient(function (err, client) {
29 if (err) return next(err)
30 if (!client) return next(new Error('No client available.'))
31
32 res.json({
33 client_id: client._id,
34 client_secret: client.clientSecret
35 })
36 })
37}
38
39// ---------------------------------------------------------------------------
40
41module.exports = router
diff --git a/server/controllers/api/v1/index.js b/server/controllers/api/v1/index.js
index e0c29a8a2..2e4fb2dab 100644
--- a/server/controllers/api/v1/index.js
+++ b/server/controllers/api/v1/index.js
@@ -4,13 +4,17 @@ const express = require('express')
4 4
5const router = express.Router() 5const router = express.Router()
6 6
7const clientsController = require('./clients')
7const podsController = require('./pods') 8const podsController = require('./pods')
8const remoteController = require('./remote') 9const remoteController = require('./remote')
10const requestsController = require('./requests')
9const usersController = require('./users') 11const usersController = require('./users')
10const videosController = require('./videos') 12const videosController = require('./videos')
11 13
14router.use('/clients', clientsController)
12router.use('/pods', podsController) 15router.use('/pods', podsController)
13router.use('/remote', remoteController) 16router.use('/remote', remoteController)
17router.use('/requests', requestsController)
14router.use('/users', usersController) 18router.use('/users', usersController)
15router.use('/videos', videosController) 19router.use('/videos', videosController)
16router.use('/*', badRequest) 20router.use('/*', badRequest)
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js
index 2bc761fef..8ffade578 100644
--- a/server/controllers/api/v1/pods.js
+++ b/server/controllers/api/v1/pods.js
@@ -8,7 +8,10 @@ const waterfall = require('async/waterfall')
8const logger = require('../../../helpers/logger') 8const logger = require('../../../helpers/logger')
9const friends = require('../../../lib/friends') 9const friends = require('../../../lib/friends')
10const middlewares = require('../../../middlewares') 10const middlewares = require('../../../middlewares')
11const admin = middlewares.admin
11const oAuth = middlewares.oauth 12const oAuth = middlewares.oauth
13const podsMiddleware = middlewares.pods
14const checkSignature = middlewares.secure.checkSignature
12const validators = middlewares.validators.pods 15const validators = middlewares.validators.pods
13const signatureValidator = middlewares.validators.remote.signature 16const signatureValidator = middlewares.validators.remote.signature
14 17
@@ -16,12 +19,30 @@ const router = express.Router()
16const Pod = mongoose.model('Pod') 19const Pod = mongoose.model('Pod')
17const Video = mongoose.model('Video') 20const Video = mongoose.model('Video')
18 21
19router.get('/', listPodsUrl) 22router.get('/', listPods)
20router.post('/', validators.podsAdd, addPods) 23router.post('/',
21router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends) 24 validators.podsAdd,
22router.get('/quitfriends', oAuth.authenticate, quitFriends) 25 podsMiddleware.setBodyUrlPort,
26 addPods
27)
28router.post('/makefriends',
29 oAuth.authenticate,
30 admin.ensureIsAdmin,
31 validators.makeFriends,
32 podsMiddleware.setBodyUrlsPort,
33 makeFriends
34)
35router.get('/quitfriends',
36 oAuth.authenticate,
37 admin.ensureIsAdmin,
38 quitFriends
39)
23// Post because this is a secured request 40// Post because this is a secured request
24router.post('/remove', signatureValidator, removePods) 41router.post('/remove',
42 signatureValidator,
43 checkSignature,
44 removePods
45)
25 46
26// --------------------------------------------------------------------------- 47// ---------------------------------------------------------------------------
27 48
@@ -64,20 +85,27 @@ function addPods (req, res, next) {
64 }) 85 })
65} 86}
66 87
67function listPodsUrl (req, res, next) { 88function listPods (req, res, next) {
68 Pod.listOnlyUrls(function (err, podsUrlList) { 89 Pod.list(function (err, podsUrlList) {
69 if (err) return next(err) 90 if (err) return next(err)
70 91
71 res.json(podsUrlList) 92 res.json(getFormatedPods(podsUrlList))
72 }) 93 })
73} 94}
74 95
75function makeFriends (req, res, next) { 96function makeFriends (req, res, next) {
76 friends.makeFriends(function (err) { 97 const urls = req.body.urls
77 if (err) return next(err)
78 98
79 res.type('json').status(204).end() 99 friends.makeFriends(urls, function (err) {
100 if (err) {
101 logger.error('Could not make friends.', { error: err })
102 return
103 }
104
105 logger.info('Made friends!')
80 }) 106 })
107
108 res.type('json').status(204).end()
81} 109}
82 110
83function removePods (req, res, next) { 111function removePods (req, res, next) {
@@ -125,3 +153,15 @@ function quitFriends (req, res, next) {
125 res.type('json').status(204).end() 153 res.type('json').status(204).end()
126 }) 154 })
127} 155}
156
157// ---------------------------------------------------------------------------
158
159function getFormatedPods (pods) {
160 const formatedPods = []
161
162 pods.forEach(function (pod) {
163 formatedPods.push(pod.toFormatedJSON())
164 })
165
166 return formatedPods
167}
diff --git a/server/controllers/api/v1/remote.js b/server/controllers/api/v1/remote.js
index f452986b8..a22c5d151 100644
--- a/server/controllers/api/v1/remote.js
+++ b/server/controllers/api/v1/remote.js
@@ -16,6 +16,7 @@ const Video = mongoose.model('Video')
16router.post('/videos', 16router.post('/videos',
17 validators.signature, 17 validators.signature,
18 validators.dataToDecrypt, 18 validators.dataToDecrypt,
19 secureMiddleware.checkSignature,
19 secureMiddleware.decryptBody, 20 secureMiddleware.decryptBody,
20 validators.remoteVideos, 21 validators.remoteVideos,
21 remoteVideos 22 remoteVideos
diff --git a/server/controllers/api/v1/requests.js b/server/controllers/api/v1/requests.js
new file mode 100644
index 000000000..97616424d
--- /dev/null
+++ b/server/controllers/api/v1/requests.js
@@ -0,0 +1,38 @@
1'use strict'
2
3const express = require('express')
4const mongoose = require('mongoose')
5
6const constants = require('../../../initializers/constants')
7const middlewares = require('../../../middlewares')
8const admin = middlewares.admin
9const oAuth = middlewares.oauth
10
11const Request = mongoose.model('Request')
12
13const router = express.Router()
14
15router.get('/stats',
16 oAuth.authenticate,
17 admin.ensureIsAdmin,
18 getStatsRequests
19)
20
21// ---------------------------------------------------------------------------
22
23module.exports = router
24
25// ---------------------------------------------------------------------------
26
27function getStatsRequests (req, res, next) {
28 Request.list(function (err, requests) {
29 if (err) return next(err)
30
31 return res.json({
32 requests: requests,
33 maxRequestsInParallel: constants.REQUESTS_IN_PARALLEL,
34 remainingMilliSeconds: Request.remainingMilliSeconds(),
35 milliSecondsInterval: constants.REQUESTS_INTERVAL
36 })
37 })
38}
diff --git a/server/controllers/api/v1/users.js b/server/controllers/api/v1/users.js
index fbbe6e472..975e25e68 100644
--- a/server/controllers/api/v1/users.js
+++ b/server/controllers/api/v1/users.js
@@ -1,18 +1,59 @@
1'use strict' 1'use strict'
2 2
3const config = require('config') 3const each = require('async/each')
4const mongoose = require('mongoose')
5const express = require('express') 4const express = require('express')
5const mongoose = require('mongoose')
6const waterfall = require('async/waterfall')
6 7
7const oAuth = require('../../../middlewares').oauth 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
8 19
9const Client = mongoose.model('OAuthClient') 20const User = mongoose.model('User')
21const Video = mongoose.model('Video')
10 22
11const router = express.Router() 23const router = express.Router()
12 24
13router.get('/client', getAngularClient) 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
14router.post('/token', oAuth.token, success) 55router.post('/token', oAuth.token, success)
15// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged,, implement revoke token route 56// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
16 57
17// --------------------------------------------------------------------------- 58// ---------------------------------------------------------------------------
18 59
@@ -20,26 +61,91 @@ module.exports = router
20 61
21// --------------------------------------------------------------------------- 62// ---------------------------------------------------------------------------
22 63
23function getAngularClient (req, res, next) { 64function createUser (req, res, next) {
24 const serverHost = config.get('webserver.host') 65 const user = new User({
25 const serverPort = config.get('webserver.port') 66 username: req.body.username,
26 let headerHostShouldBe = serverHost 67 password: req.body.password,
27 if (serverPort !== 80 && serverPort !== 443) { 68 role: constants.USER_ROLES.USER
28 headerHostShouldBe += ':' + serverPort 69 })
29 }
30 70
31 // Don't make this check if this is a test instance 71 user.save(function (err, createdUser) {
32 if (process.env.NODE_ENV !== 'test' && req.get('host') !== headerHostShouldBe) { 72 if (err) return next(err)
33 return res.type('json').status(403).end()
34 }
35 73
36 Client.loadFirstClient(function (err, client) { 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) {
37 if (err) return next(err) 80 if (err) return next(err)
38 if (!client) return next(new Error('No client available.'))
39 81
40 res.json({ 82 return res.json(user.toFormatedJSON())
41 client_id: client._id, 83 })
42 client_secret: client.clientSecret 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)
43 }) 149 })
44 }) 150 })
45} 151}
@@ -47,3 +153,18 @@ function getAngularClient (req, res, next) {
47function success (req, res, next) { 153function success (req, res, next) {
48 res.end() 154 res.end()
49} 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}
diff --git a/server/controllers/api/v1/videos.js b/server/controllers/api/v1/videos.js
index 1f939b077..70d22f139 100644
--- a/server/controllers/api/v1/videos.js
+++ b/server/controllers/api/v1/videos.js
@@ -1,11 +1,11 @@
1'use strict' 1'use strict'
2 2
3const config = require('config')
4const express = require('express') 3const express = require('express')
5const mongoose = require('mongoose') 4const mongoose = require('mongoose')
6const multer = require('multer') 5const multer = require('multer')
7const waterfall = require('async/waterfall') 6const waterfall = require('async/waterfall')
8 7
8const constants = require('../../../initializers/constants')
9const logger = require('../../../helpers/logger') 9const logger = require('../../../helpers/logger')
10const friends = require('../../../lib/friends') 10const friends = require('../../../lib/friends')
11const middlewares = require('../../../middlewares') 11const middlewares = require('../../../middlewares')
@@ -20,13 +20,12 @@ const sort = middlewares.sort
20const utils = require('../../../helpers/utils') 20const utils = require('../../../helpers/utils')
21 21
22const router = express.Router() 22const router = express.Router()
23const uploads = config.get('storage.uploads')
24const Video = mongoose.model('Video') 23const Video = mongoose.model('Video')
25 24
26// multer configuration 25// multer configuration
27const storage = multer.diskStorage({ 26const storage = multer.diskStorage({
28 destination: function (req, file, cb) { 27 destination: function (req, file, cb) {
29 cb(null, uploads) 28 cb(null, constants.CONFIG.STORAGE.UPLOAD_DIR)
30 }, 29 },
31 30
32 filename: function (req, file, cb) { 31 filename: function (req, file, cb) {
@@ -142,7 +141,7 @@ function getVideo (req, res, next) {
142} 141}
143 142
144function listVideos (req, res, next) { 143function listVideos (req, res, next) {
145 Video.list(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { 144 Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
146 if (err) return next(err) 145 if (err) return next(err)
147 146
148 res.json(getFormatedVideos(videosList, videosTotal)) 147 res.json(getFormatedVideos(videosList, videosTotal))