diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-01-17 20:50:02 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-01-17 20:50:45 +0100 |
commit | 62f4ef413c8ef4269e7cdc21dea0e99ab2191201 (patch) | |
tree | 68d20b6597d154c09f05f5cd414cb5e43d180ccf | |
parent | 4145c1c68923c13538a5b60d1b384037d0dded9d (diff) | |
download | PeerTube-62f4ef413c8ef4269e7cdc21dea0e99ab2191201.tar.gz PeerTube-62f4ef413c8ef4269e7cdc21dea0e99ab2191201.tar.zst PeerTube-62f4ef413c8ef4269e7cdc21dea0e99ab2191201.zip |
Server: remote request process refractoring
-rw-r--r-- | server/controllers/api/remote/videos.js | 40 | ||||
-rw-r--r-- | server/initializers/constants.js | 8 |
2 files changed, 28 insertions, 20 deletions
diff --git a/server/controllers/api/remote/videos.js b/server/controllers/api/remote/videos.js index bfe61a35c..83d9b98bf 100644 --- a/server/controllers/api/remote/videos.js +++ b/server/controllers/api/remote/videos.js | |||
@@ -5,6 +5,7 @@ const express = require('express') | |||
5 | const waterfall = require('async/waterfall') | 5 | const waterfall = require('async/waterfall') |
6 | 6 | ||
7 | const db = require('../../../initializers/database') | 7 | const db = require('../../../initializers/database') |
8 | const constants = require('../../../initializers/constants') | ||
8 | const middlewares = require('../../../middlewares') | 9 | const middlewares = require('../../../middlewares') |
9 | const secureMiddleware = middlewares.secure | 10 | const secureMiddleware = middlewares.secure |
10 | const videosValidators = middlewares.validators.remote.videos | 11 | const videosValidators = middlewares.validators.remote.videos |
@@ -12,6 +13,15 @@ const signatureValidators = middlewares.validators.remote.signature | |||
12 | const logger = require('../../../helpers/logger') | 13 | const logger = require('../../../helpers/logger') |
13 | const databaseUtils = require('../../../helpers/database-utils') | 14 | const databaseUtils = require('../../../helpers/database-utils') |
14 | 15 | ||
16 | const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] | ||
17 | |||
18 | // Functions to call when processing a remote request | ||
19 | const functionsHash = {} | ||
20 | functionsHash[ENDPOINT_ACTIONS.ADD] = addRemoteVideoRetryWrapper | ||
21 | functionsHash[ENDPOINT_ACTIONS.UPDATE] = updateRemoteVideoRetryWrapper | ||
22 | functionsHash[ENDPOINT_ACTIONS.REMOVE] = removeRemoteVideo | ||
23 | functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideo | ||
24 | |||
15 | const router = express.Router() | 25 | const router = express.Router() |
16 | 26 | ||
17 | router.post('/', | 27 | router.post('/', |
@@ -36,26 +46,14 @@ function remoteVideos (req, res, next) { | |||
36 | eachSeries(requests, function (request, callbackEach) { | 46 | eachSeries(requests, function (request, callbackEach) { |
37 | const data = request.data | 47 | const data = request.data |
38 | 48 | ||
39 | switch (request.type) { | 49 | // Get the function we need to call in order to process the request |
40 | case 'add': | 50 | const fun = functionsHash[request.type] |
41 | addRemoteVideoRetryWrapper(data, fromPod, callbackEach) | 51 | if (fun === undefined) { |
42 | break | 52 | logger.error('Unkown remote request type %s.', request.type) |
43 | 53 | return callbackEach(null) | |
44 | case 'update': | ||
45 | updateRemoteVideoRetryWrapper(data, fromPod, callbackEach) | ||
46 | break | ||
47 | |||
48 | case 'remove': | ||
49 | removeRemoteVideo(data, fromPod, callbackEach) | ||
50 | break | ||
51 | |||
52 | case 'report-abuse': | ||
53 | reportAbuseRemoteVideo(data, fromPod, callbackEach) | ||
54 | break | ||
55 | |||
56 | default: | ||
57 | logger.error('Unkown remote request type %s.', request.type) | ||
58 | } | 54 | } |
55 | |||
56 | fun.call(this, data, fromPod, callbackEach) | ||
59 | }, function (err) { | 57 | }, function (err) { |
60 | if (err) logger.error('Error managing remote videos.', { error: err }) | 58 | if (err) logger.error('Error managing remote videos.', { error: err }) |
61 | }) | 59 | }) |
@@ -141,7 +139,9 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) { | |||
141 | }, | 139 | }, |
142 | 140 | ||
143 | function associateTagsToVideo (t, tagInstances, video, callback) { | 141 | function associateTagsToVideo (t, tagInstances, video, callback) { |
144 | const options = { transaction: t } | 142 | const options = { |
143 | transaction: t | ||
144 | } | ||
145 | 145 | ||
146 | video.setTags(tagInstances, options).asCallback(function (err) { | 146 | video.setTags(tagInstances, options).asCallback(function (err) { |
147 | return callback(err, t) | 147 | return callback(err, t) |
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index 97e3c5296..0c080ccd2 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -119,6 +119,13 @@ const RETRY_REQUESTS = 5 | |||
119 | const REQUEST_ENDPOINTS = { | 119 | const REQUEST_ENDPOINTS = { |
120 | VIDEOS: 'videos' | 120 | VIDEOS: 'videos' |
121 | } | 121 | } |
122 | const REQUEST_ENDPOINT_ACTIONS = {} | ||
123 | REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = { | ||
124 | ADD: 'add', | ||
125 | UPDATE: 'update', | ||
126 | REMOVE: 'remove', | ||
127 | REPORT_ABUSE: 'report-abuse' | ||
128 | } | ||
122 | 129 | ||
123 | const REMOTE_SCHEME = { | 130 | const REMOTE_SCHEME = { |
124 | HTTP: 'https', | 131 | HTTP: 'https', |
@@ -184,6 +191,7 @@ module.exports = { | |||
184 | PREVIEWS_SIZE, | 191 | PREVIEWS_SIZE, |
185 | REMOTE_SCHEME, | 192 | REMOTE_SCHEME, |
186 | REQUEST_ENDPOINTS, | 193 | REQUEST_ENDPOINTS, |
194 | REQUEST_ENDPOINT_ACTIONS, | ||
187 | REQUESTS_IN_PARALLEL, | 195 | REQUESTS_IN_PARALLEL, |
188 | REQUESTS_INTERVAL, | 196 | REQUESTS_INTERVAL, |
189 | REQUESTS_LIMIT_PODS, | 197 | REQUESTS_LIMIT_PODS, |