diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-05-15 22:22:03 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-05-20 09:57:40 +0200 |
commit | 65fcc3119c334b75dd13bcfdebf186afdc580a8f (patch) | |
tree | 4f2158c61a9b7c3f47cfa233d01413b946ee53c0 /server/controllers/api/remote | |
parent | d5f345ed4cfac4e1fa84dcb4fce1cda4d32f9c73 (diff) | |
download | PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.gz PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.tar.zst PeerTube-65fcc3119c334b75dd13bcfdebf186afdc580a8f.zip |
First typescript iteration
Diffstat (limited to 'server/controllers/api/remote')
-rw-r--r-- | server/controllers/api/remote/index.js | 18 | ||||
-rw-r--r-- | server/controllers/api/remote/index.ts | 18 | ||||
-rw-r--r-- | server/controllers/api/remote/pods.ts (renamed from server/controllers/api/remote/pods.js) | 20 | ||||
-rw-r--r-- | server/controllers/api/remote/videos.ts (renamed from server/controllers/api/remote/videos.js) | 122 |
4 files changed, 94 insertions, 84 deletions
diff --git a/server/controllers/api/remote/index.js b/server/controllers/api/remote/index.js deleted file mode 100644 index 6106850ab..000000000 --- a/server/controllers/api/remote/index.js +++ /dev/null | |||
@@ -1,18 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const express = require('express') | ||
4 | |||
5 | const utils = require('../../../helpers/utils') | ||
6 | |||
7 | const router = express.Router() | ||
8 | |||
9 | const podsRemoteController = require('./pods') | ||
10 | const videosRemoteController = require('./videos') | ||
11 | |||
12 | router.use('/pods', podsRemoteController) | ||
13 | router.use('/videos', videosRemoteController) | ||
14 | router.use('/*', utils.badRequest) | ||
15 | |||
16 | // --------------------------------------------------------------------------- | ||
17 | |||
18 | module.exports = router | ||
diff --git a/server/controllers/api/remote/index.ts b/server/controllers/api/remote/index.ts new file mode 100644 index 000000000..b11439204 --- /dev/null +++ b/server/controllers/api/remote/index.ts | |||
@@ -0,0 +1,18 @@ | |||
1 | import express = require('express') | ||
2 | |||
3 | import { badRequest } from '../../../helpers' | ||
4 | |||
5 | import { remotePodsRouter } from './pods' | ||
6 | import { remoteVideosRouter } from './videos' | ||
7 | |||
8 | const remoteRouter = express.Router() | ||
9 | |||
10 | remoteRouter.use('/pods', remotePodsRouter) | ||
11 | remoteRouter.use('/videos', remoteVideosRouter) | ||
12 | remoteRouter.use('/*', badRequest) | ||
13 | |||
14 | // --------------------------------------------------------------------------- | ||
15 | |||
16 | export { | ||
17 | remoteRouter | ||
18 | } | ||
diff --git a/server/controllers/api/remote/pods.js b/server/controllers/api/remote/pods.ts index 0343bc62e..85ef7bb42 100644 --- a/server/controllers/api/remote/pods.js +++ b/server/controllers/api/remote/pods.ts | |||
@@ -1,25 +1,23 @@ | |||
1 | 'use strict' | 1 | import express = require('express') |
2 | 2 | import { waterfall } from 'async/waterfall' | |
3 | const express = require('express') | ||
4 | const waterfall = require('async/waterfall') | ||
5 | 3 | ||
6 | const db = require('../../../initializers/database') | 4 | const db = require('../../../initializers/database') |
7 | const middlewares = require('../../../middlewares') | 5 | import { checkSignature, signatureValidator } from '../../../middlewares' |
8 | const checkSignature = middlewares.secure.checkSignature | ||
9 | const signatureValidator = middlewares.validators.remote.signature | ||
10 | 6 | ||
11 | const router = express.Router() | 7 | const remotePodsRouter = express.Router() |
12 | 8 | ||
13 | // Post because this is a secured request | 9 | // Post because this is a secured request |
14 | router.post('/remove', | 10 | remotePodsRouter.post('/remove', |
15 | signatureValidator.signature, | 11 | signatureValidator, |
16 | checkSignature, | 12 | checkSignature, |
17 | removePods | 13 | removePods |
18 | ) | 14 | ) |
19 | 15 | ||
20 | // --------------------------------------------------------------------------- | 16 | // --------------------------------------------------------------------------- |
21 | 17 | ||
22 | module.exports = router | 18 | export { |
19 | remotePodsRouter | ||
20 | } | ||
23 | 21 | ||
24 | // --------------------------------------------------------------------------- | 22 | // --------------------------------------------------------------------------- |
25 | 23 | ||
diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.ts index e54793628..df4ba8309 100644 --- a/server/controllers/api/remote/videos.js +++ b/server/controllers/api/remote/videos.ts | |||
@@ -1,20 +1,30 @@ | |||
1 | 'use strict' | 1 | import express = require('express') |
2 | 2 | import { eachSeries, waterfall } from 'async' | |
3 | const eachSeries = require('async/eachSeries') | ||
4 | const express = require('express') | ||
5 | const waterfall = require('async/waterfall') | ||
6 | 3 | ||
7 | const db = require('../../../initializers/database') | 4 | const db = require('../../../initializers/database') |
8 | const constants = require('../../../initializers/constants') | 5 | import { |
9 | const middlewares = require('../../../middlewares') | 6 | REQUEST_ENDPOINT_ACTIONS, |
10 | const secureMiddleware = middlewares.secure | 7 | REQUEST_ENDPOINTS, |
11 | const videosValidators = middlewares.validators.remote.videos | 8 | REQUEST_VIDEO_EVENT_TYPES, |
12 | const signatureValidators = middlewares.validators.remote.signature | 9 | REQUEST_VIDEO_QADU_TYPES |
13 | const logger = require('../../../helpers/logger') | 10 | } from '../../../initializers' |
14 | const friends = require('../../../lib/friends') | 11 | import { |
15 | const databaseUtils = require('../../../helpers/database-utils') | 12 | checkSignature, |
16 | 13 | signatureValidator, | |
17 | const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] | 14 | remoteVideosValidator, |
15 | remoteQaduVideosValidator, | ||
16 | remoteEventsVideosValidator | ||
17 | } from '../../../middlewares' | ||
18 | import { | ||
19 | logger, | ||
20 | commitTransaction, | ||
21 | retryTransactionWrapper, | ||
22 | rollbackTransaction, | ||
23 | startSerializableTransaction | ||
24 | } from '../../../helpers' | ||
25 | import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib' | ||
26 | |||
27 | const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] | ||
18 | 28 | ||
19 | // Functions to call when processing a remote request | 29 | // Functions to call when processing a remote request |
20 | const functionsHash = {} | 30 | const functionsHash = {} |
@@ -23,32 +33,34 @@ functionsHash[ENDPOINT_ACTIONS.UPDATE] = updateRemoteVideoRetryWrapper | |||
23 | functionsHash[ENDPOINT_ACTIONS.REMOVE] = removeRemoteVideo | 33 | functionsHash[ENDPOINT_ACTIONS.REMOVE] = removeRemoteVideo |
24 | functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideo | 34 | functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideo |
25 | 35 | ||
26 | const router = express.Router() | 36 | const remoteVideosRouter = express.Router() |
27 | 37 | ||
28 | router.post('/', | 38 | remoteVideosRouter.post('/', |
29 | signatureValidators.signature, | 39 | signatureValidator, |
30 | secureMiddleware.checkSignature, | 40 | checkSignature, |
31 | videosValidators.remoteVideos, | 41 | remoteVideosValidator, |
32 | remoteVideos | 42 | remoteVideos |
33 | ) | 43 | ) |
34 | 44 | ||
35 | router.post('/qadu', | 45 | remoteVideosRouter.post('/qadu', |
36 | signatureValidators.signature, | 46 | signatureValidator, |
37 | secureMiddleware.checkSignature, | 47 | checkSignature, |
38 | videosValidators.remoteQaduVideos, | 48 | remoteQaduVideosValidator, |
39 | remoteVideosQadu | 49 | remoteVideosQadu |
40 | ) | 50 | ) |
41 | 51 | ||
42 | router.post('/events', | 52 | remoteVideosRouter.post('/events', |
43 | signatureValidators.signature, | 53 | signatureValidator, |
44 | secureMiddleware.checkSignature, | 54 | checkSignature, |
45 | videosValidators.remoteEventsVideos, | 55 | remoteEventsVideosValidator, |
46 | remoteVideosEvents | 56 | remoteVideosEvents |
47 | ) | 57 | ) |
48 | 58 | ||
49 | // --------------------------------------------------------------------------- | 59 | // --------------------------------------------------------------------------- |
50 | 60 | ||
51 | module.exports = router | 61 | export { |
62 | remoteVideosRouter | ||
63 | } | ||
52 | 64 | ||
53 | // --------------------------------------------------------------------------- | 65 | // --------------------------------------------------------------------------- |
54 | 66 | ||
@@ -58,7 +70,7 @@ function remoteVideos (req, res, next) { | |||
58 | 70 | ||
59 | // We need to process in the same order to keep consistency | 71 | // We need to process in the same order to keep consistency |
60 | // TODO: optimization | 72 | // TODO: optimization |
61 | eachSeries(requests, function (request, callbackEach) { | 73 | eachSeries(requests, function (request: any, callbackEach) { |
62 | const data = request.data | 74 | const data = request.data |
63 | 75 | ||
64 | // Get the function we need to call in order to process the request | 76 | // Get the function we need to call in order to process the request |
@@ -81,7 +93,7 @@ function remoteVideosQadu (req, res, next) { | |||
81 | const requests = req.body.data | 93 | const requests = req.body.data |
82 | const fromPod = res.locals.secure.pod | 94 | const fromPod = res.locals.secure.pod |
83 | 95 | ||
84 | eachSeries(requests, function (request, callbackEach) { | 96 | eachSeries(requests, function (request: any, callbackEach) { |
85 | const videoData = request.data | 97 | const videoData = request.data |
86 | 98 | ||
87 | quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod, callbackEach) | 99 | quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod, callbackEach) |
@@ -96,7 +108,7 @@ function remoteVideosEvents (req, res, next) { | |||
96 | const requests = req.body.data | 108 | const requests = req.body.data |
97 | const fromPod = res.locals.secure.pod | 109 | const fromPod = res.locals.secure.pod |
98 | 110 | ||
99 | eachSeries(requests, function (request, callbackEach) { | 111 | eachSeries(requests, function (request: any, callbackEach) { |
100 | const eventData = request.data | 112 | const eventData = request.data |
101 | 113 | ||
102 | processVideosEventsRetryWrapper(eventData, fromPod, callbackEach) | 114 | processVideosEventsRetryWrapper(eventData, fromPod, callbackEach) |
@@ -113,12 +125,12 @@ function processVideosEventsRetryWrapper (eventData, fromPod, finalCallback) { | |||
113 | errorMessage: 'Cannot process videos events with many retries.' | 125 | errorMessage: 'Cannot process videos events with many retries.' |
114 | } | 126 | } |
115 | 127 | ||
116 | databaseUtils.retryTransactionWrapper(processVideosEvents, options, finalCallback) | 128 | retryTransactionWrapper(processVideosEvents, options, finalCallback) |
117 | } | 129 | } |
118 | 130 | ||
119 | function processVideosEvents (eventData, fromPod, finalCallback) { | 131 | function processVideosEvents (eventData, fromPod, finalCallback) { |
120 | waterfall([ | 132 | waterfall([ |
121 | databaseUtils.startSerializableTransaction, | 133 | startSerializableTransaction, |
122 | 134 | ||
123 | function findVideo (t, callback) { | 135 | function findVideo (t, callback) { |
124 | fetchOwnedVideo(eventData.remoteId, function (err, videoInstance) { | 136 | fetchOwnedVideo(eventData.remoteId, function (err, videoInstance) { |
@@ -133,19 +145,19 @@ function processVideosEvents (eventData, fromPod, finalCallback) { | |||
133 | let qaduType | 145 | let qaduType |
134 | 146 | ||
135 | switch (eventData.eventType) { | 147 | switch (eventData.eventType) { |
136 | case constants.REQUEST_VIDEO_EVENT_TYPES.VIEWS: | 148 | case REQUEST_VIDEO_EVENT_TYPES.VIEWS: |
137 | columnToUpdate = 'views' | 149 | columnToUpdate = 'views' |
138 | qaduType = constants.REQUEST_VIDEO_QADU_TYPES.VIEWS | 150 | qaduType = REQUEST_VIDEO_QADU_TYPES.VIEWS |
139 | break | 151 | break |
140 | 152 | ||
141 | case constants.REQUEST_VIDEO_EVENT_TYPES.LIKES: | 153 | case REQUEST_VIDEO_EVENT_TYPES.LIKES: |
142 | columnToUpdate = 'likes' | 154 | columnToUpdate = 'likes' |
143 | qaduType = constants.REQUEST_VIDEO_QADU_TYPES.LIKES | 155 | qaduType = REQUEST_VIDEO_QADU_TYPES.LIKES |
144 | break | 156 | break |
145 | 157 | ||
146 | case constants.REQUEST_VIDEO_EVENT_TYPES.DISLIKES: | 158 | case REQUEST_VIDEO_EVENT_TYPES.DISLIKES: |
147 | columnToUpdate = 'dislikes' | 159 | columnToUpdate = 'dislikes' |
148 | qaduType = constants.REQUEST_VIDEO_QADU_TYPES.DISLIKES | 160 | qaduType = REQUEST_VIDEO_QADU_TYPES.DISLIKES |
149 | break | 161 | break |
150 | 162 | ||
151 | default: | 163 | default: |
@@ -168,17 +180,17 @@ function processVideosEvents (eventData, fromPod, finalCallback) { | |||
168 | } | 180 | } |
169 | ] | 181 | ] |
170 | 182 | ||
171 | friends.quickAndDirtyUpdatesVideoToFriends(qadusParams, t, function (err) { | 183 | quickAndDirtyUpdatesVideoToFriends(qadusParams, t, function (err) { |
172 | return callback(err, t) | 184 | return callback(err, t) |
173 | }) | 185 | }) |
174 | }, | 186 | }, |
175 | 187 | ||
176 | databaseUtils.commitTransaction | 188 | commitTransaction |
177 | 189 | ||
178 | ], function (err, t) { | 190 | ], function (err, t) { |
179 | if (err) { | 191 | if (err) { |
180 | logger.debug('Cannot process a video event.', { error: err }) | 192 | logger.debug('Cannot process a video event.', { error: err }) |
181 | return databaseUtils.rollbackTransaction(err, t, finalCallback) | 193 | return rollbackTransaction(err, t, finalCallback) |
182 | } | 194 | } |
183 | 195 | ||
184 | logger.info('Remote video event processed for video %s.', eventData.remoteId) | 196 | logger.info('Remote video event processed for video %s.', eventData.remoteId) |
@@ -192,14 +204,14 @@ function quickAndDirtyUpdateVideoRetryWrapper (videoData, fromPod, finalCallback | |||
192 | errorMessage: 'Cannot update quick and dirty the remote video with many retries.' | 204 | errorMessage: 'Cannot update quick and dirty the remote video with many retries.' |
193 | } | 205 | } |
194 | 206 | ||
195 | databaseUtils.retryTransactionWrapper(quickAndDirtyUpdateVideo, options, finalCallback) | 207 | retryTransactionWrapper(quickAndDirtyUpdateVideo, options, finalCallback) |
196 | } | 208 | } |
197 | 209 | ||
198 | function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) { | 210 | function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) { |
199 | let videoName | 211 | let videoName |
200 | 212 | ||
201 | waterfall([ | 213 | waterfall([ |
202 | databaseUtils.startSerializableTransaction, | 214 | startSerializableTransaction, |
203 | 215 | ||
204 | function findVideo (t, callback) { | 216 | function findVideo (t, callback) { |
205 | fetchRemoteVideo(fromPod.host, videoData.remoteId, function (err, videoInstance) { | 217 | fetchRemoteVideo(fromPod.host, videoData.remoteId, function (err, videoInstance) { |
@@ -229,12 +241,12 @@ function quickAndDirtyUpdateVideo (videoData, fromPod, finalCallback) { | |||
229 | }) | 241 | }) |
230 | }, | 242 | }, |
231 | 243 | ||
232 | databaseUtils.commitTransaction | 244 | commitTransaction |
233 | 245 | ||
234 | ], function (err, t) { | 246 | ], function (err, t) { |
235 | if (err) { | 247 | if (err) { |
236 | logger.debug('Cannot quick and dirty update the remote video.', { error: err }) | 248 | logger.debug('Cannot quick and dirty update the remote video.', { error: err }) |
237 | return databaseUtils.rollbackTransaction(err, t, finalCallback) | 249 | return rollbackTransaction(err, t, finalCallback) |
238 | } | 250 | } |
239 | 251 | ||
240 | logger.info('Remote video %s quick and dirty updated', videoName) | 252 | logger.info('Remote video %s quick and dirty updated', videoName) |
@@ -249,7 +261,7 @@ function addRemoteVideoRetryWrapper (videoToCreateData, fromPod, finalCallback) | |||
249 | errorMessage: 'Cannot insert the remote video with many retries.' | 261 | errorMessage: 'Cannot insert the remote video with many retries.' |
250 | } | 262 | } |
251 | 263 | ||
252 | databaseUtils.retryTransactionWrapper(addRemoteVideo, options, finalCallback) | 264 | retryTransactionWrapper(addRemoteVideo, options, finalCallback) |
253 | } | 265 | } |
254 | 266 | ||
255 | function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | 267 | function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { |
@@ -257,7 +269,7 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
257 | 269 | ||
258 | waterfall([ | 270 | waterfall([ |
259 | 271 | ||
260 | databaseUtils.startSerializableTransaction, | 272 | startSerializableTransaction, |
261 | 273 | ||
262 | function assertRemoteIdAndHostUnique (t, callback) { | 274 | function assertRemoteIdAndHostUnique (t, callback) { |
263 | db.Video.loadByHostAndRemoteId(fromPod.host, videoToCreateData.remoteId, function (err, video) { | 275 | db.Video.loadByHostAndRemoteId(fromPod.host, videoToCreateData.remoteId, function (err, video) { |
@@ -345,13 +357,13 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
345 | }) | 357 | }) |
346 | }, | 358 | }, |
347 | 359 | ||
348 | databaseUtils.commitTransaction | 360 | commitTransaction |
349 | 361 | ||
350 | ], function (err, t) { | 362 | ], function (err, t) { |
351 | if (err) { | 363 | if (err) { |
352 | // This is just a debug because we will retry the insert | 364 | // This is just a debug because we will retry the insert |
353 | logger.debug('Cannot insert the remote video.', { error: err }) | 365 | logger.debug('Cannot insert the remote video.', { error: err }) |
354 | return databaseUtils.rollbackTransaction(err, t, finalCallback) | 366 | return rollbackTransaction(err, t, finalCallback) |
355 | } | 367 | } |
356 | 368 | ||
357 | logger.info('Remote video %s inserted.', videoToCreateData.name) | 369 | logger.info('Remote video %s inserted.', videoToCreateData.name) |
@@ -366,7 +378,7 @@ function updateRemoteVideoRetryWrapper (videoAttributesToUpdate, fromPod, finalC | |||
366 | errorMessage: 'Cannot update the remote video with many retries' | 378 | errorMessage: 'Cannot update the remote video with many retries' |
367 | } | 379 | } |
368 | 380 | ||
369 | databaseUtils.retryTransactionWrapper(updateRemoteVideo, options, finalCallback) | 381 | retryTransactionWrapper(updateRemoteVideo, options, finalCallback) |
370 | } | 382 | } |
371 | 383 | ||
372 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { | 384 | function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { |
@@ -374,7 +386,7 @@ function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { | |||
374 | 386 | ||
375 | waterfall([ | 387 | waterfall([ |
376 | 388 | ||
377 | databaseUtils.startSerializableTransaction, | 389 | startSerializableTransaction, |
378 | 390 | ||
379 | function findVideo (t, callback) { | 391 | function findVideo (t, callback) { |
380 | fetchRemoteVideo(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) { | 392 | fetchRemoteVideo(fromPod.host, videoAttributesToUpdate.remoteId, function (err, videoInstance) { |
@@ -421,13 +433,13 @@ function updateRemoteVideo (videoAttributesToUpdate, fromPod, finalCallback) { | |||
421 | }) | 433 | }) |
422 | }, | 434 | }, |
423 | 435 | ||
424 | databaseUtils.commitTransaction | 436 | commitTransaction |
425 | 437 | ||
426 | ], function (err, t) { | 438 | ], function (err, t) { |
427 | if (err) { | 439 | if (err) { |
428 | // This is just a debug because we will retry the insert | 440 | // This is just a debug because we will retry the insert |
429 | logger.debug('Cannot update the remote video.', { error: err }) | 441 | logger.debug('Cannot update the remote video.', { error: err }) |
430 | return databaseUtils.rollbackTransaction(err, t, finalCallback) | 442 | return rollbackTransaction(err, t, finalCallback) |
431 | } | 443 | } |
432 | 444 | ||
433 | logger.info('Remote video %s updated', videoAttributesToUpdate.name) | 445 | logger.info('Remote video %s updated', videoAttributesToUpdate.name) |