Server: remote request process refractoring
authorChocobozzz <florian.bigard@gmail.com>
Tue, 17 Jan 2017 19:50:02 +0000 (20:50 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Tue, 17 Jan 2017 19:50:45 +0000 (20:50 +0100)
server/controllers/api/remote/videos.js
server/initializers/constants.js

index bfe61a35c945132a37ef627d5e35cdc8fbe840f5..83d9b98bf69d40e3238df1d396a569c30e14ec7b 100644 (file)
@@ -5,6 +5,7 @@ const express = require('express')
 const waterfall = require('async/waterfall')
 
 const db = require('../../../initializers/database')
+const constants = require('../../../initializers/constants')
 const middlewares = require('../../../middlewares')
 const secureMiddleware = middlewares.secure
 const videosValidators = middlewares.validators.remote.videos
@@ -12,6 +13,15 @@ const signatureValidators = middlewares.validators.remote.signature
 const logger = require('../../../helpers/logger')
 const databaseUtils = require('../../../helpers/database-utils')
 
+const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
+
+// Functions to call when processing a remote request
+const functionsHash = {}
+functionsHash[ENDPOINT_ACTIONS.ADD] = addRemoteVideoRetryWrapper
+functionsHash[ENDPOINT_ACTIONS.UPDATE] = updateRemoteVideoRetryWrapper
+functionsHash[ENDPOINT_ACTIONS.REMOVE] = removeRemoteVideo
+functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideo
+
 const router = express.Router()
 
 router.post('/',
@@ -36,26 +46,14 @@ function remoteVideos (req, res, next) {
   eachSeries(requests, function (request, callbackEach) {
     const data = request.data
 
-    switch (request.type) {
-      case 'add':
-        addRemoteVideoRetryWrapper(data, fromPod, callbackEach)
-        break
-
-      case 'update':
-        updateRemoteVideoRetryWrapper(data, fromPod, callbackEach)
-        break
-
-      case 'remove':
-        removeRemoteVideo(data, fromPod, callbackEach)
-        break
-
-      case 'report-abuse':
-        reportAbuseRemoteVideo(data, fromPod, callbackEach)
-        break
-
-      default:
-        logger.error('Unkown remote request type %s.', request.type)
+    // Get the function we need to call in order to process the request
+    const fun = functionsHash[request.type]
+    if (fun === undefined) {
+      logger.error('Unkown remote request type %s.', request.type)
+      return callbackEach(null)
     }
+
+    fun.call(this, data, fromPod, callbackEach)
   }, function (err) {
     if (err) logger.error('Error managing remote videos.', { error: err })
   })
@@ -141,7 +139,9 @@ function addRemoteVideo (videoToCreateData, fromPod, finalCallback) {
     },
 
     function associateTagsToVideo (t, tagInstances, video, callback) {
-      const options = { transaction: t }
+      const options = {
+        transaction: t
+      }
 
       video.setTags(tagInstances, options).asCallback(function (err) {
         return callback(err, t)
index 97e3c5296b761a30ddc36f0a7880809ae65611bd..0c080ccd2c6713bdf97b832801f3a75ea978c707 100644 (file)
@@ -119,6 +119,13 @@ const RETRY_REQUESTS = 5
 const REQUEST_ENDPOINTS = {
   VIDEOS: 'videos'
 }
+const REQUEST_ENDPOINT_ACTIONS = {}
+REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS] = {
+  ADD: 'add',
+  UPDATE: 'update',
+  REMOVE: 'remove',
+  REPORT_ABUSE: 'report-abuse'
+}
 
 const REMOTE_SCHEME = {
   HTTP: 'https',
@@ -184,6 +191,7 @@ module.exports = {
   PREVIEWS_SIZE,
   REMOTE_SCHEME,
   REQUEST_ENDPOINTS,
+  REQUEST_ENDPOINT_ACTIONS,
   REQUESTS_IN_PARALLEL,
   REQUESTS_INTERVAL,
   REQUESTS_LIMIT_PODS,