]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/friends.js
Server: remote video validators refractoring
[github/Chocobozzz/PeerTube.git] / server / lib / friends.js
index 4afb91b8bf23512d978ce4f3befbfde9d4054796..f634aedbb4c0b80150b17733cf75d631554a124f 100644 (file)
@@ -12,6 +12,8 @@ const db = require('../initializers/database')
 const logger = require('../helpers/logger')
 const requests = require('../helpers/requests')
 
+const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
+
 const friends = {
   addVideoToFriends,
   updateVideoToFriends,
@@ -24,20 +26,43 @@ const friends = {
   sendOwnedVideosToPod
 }
 
-function addVideoToFriends (videoData) {
-  createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, videoData)
+function addVideoToFriends (videoData, transaction, callback) {
+  const options = {
+    type: ENDPOINT_ACTIONS.ADD,
+    endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+    data: videoData,
+    transaction
+  }
+  createRequest(options, callback)
 }
 
-function updateVideoToFriends (videoData) {
-  createRequest('update', constants.REQUEST_ENDPOINTS.VIDEOS, videoData)
+function updateVideoToFriends (videoData, transaction, callback) {
+  const options = {
+    type: ENDPOINT_ACTIONS.UPDATE,
+    endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+    data: videoData,
+    transaction
+  }
+  createRequest(options, callback)
 }
 
 function removeVideoToFriends (videoParams) {
-  createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams)
+  const options = {
+    type: ENDPOINT_ACTIONS.REMOVE,
+    endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+    data: videoParams
+  }
+  createRequest(options)
 }
 
 function reportAbuseVideoToFriend (reportData, video) {
-  createRequest('report-abuse', constants.REQUEST_ENDPOINTS.VIDEOS, reportData, [ video.Author.podId ])
+  const options = {
+    type: ENDPOINT_ACTIONS.REPORT_ABUSE,
+    endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+    data: reportData,
+    toIds: [ video.Author.podId ]
+  }
+  createRequest(options)
 }
 
 function hasFriends (callback) {
@@ -144,7 +169,13 @@ function sendOwnedVideosToPod (podId) {
           return
         }
 
-        createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, remoteVideo, [ podId ])
+        const options = {
+          type: 'add',
+          endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+          data: remoteVideo,
+          toIds: [ podId ]
+        }
+        createRequest(options)
       })
     })
   })
@@ -258,25 +289,35 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
 }
 
 // Wrapper that populate "toIds" argument with all our friends if it is not specified
-function createRequest (type, endpoint, data, toIds) {
-  if (toIds) return _createRequest(type, endpoint, data, toIds)
+// { type, endpoint, data, toIds, transaction }
+function createRequest (options, callback) {
+  if (!callback) callback = function () {}
+  if (options.toIds) return _createRequest(options, callback)
 
   // If the "toIds" pods is not specified, we send the request to all our friends
-  db.Pod.listAllIds(function (err, podIds) {
+  db.Pod.listAllIds(options.transaction, function (err, podIds) {
     if (err) {
       logger.error('Cannot get pod ids', { error: err })
       return
     }
 
-    return _createRequest(type, endpoint, data, podIds)
+    const newOptions = Object.assign(options, { toIds: podIds })
+    return _createRequest(newOptions, callback)
   })
 }
 
-function _createRequest (type, endpoint, data, toIds) {
+// { type, endpoint, data, toIds, transaction }
+function _createRequest (options, callback) {
+  const type = options.type
+  const endpoint = options.endpoint
+  const data = options.data
+  const toIds = options.toIds
+  const transaction = options.transaction
+
   const pods = []
 
   // If there are no destination pods abort
-  if (toIds.length === 0) return
+  if (toIds.length === 0) return callback(null)
 
   toIds.forEach(function (toPod) {
     pods.push(db.Pod.build({ id: toPod }))
@@ -290,17 +331,14 @@ function _createRequest (type, endpoint, data, toIds) {
     }
   }
 
-  // We run in transaction to keep coherency between Request and RequestToPod tables
-  db.sequelize.transaction(function (t) {
-    const dbRequestOptions = {
-      transaction: t
-    }
+  const dbRequestOptions = {
+    transaction
+  }
 
-    return db.Request.create(createQuery, dbRequestOptions).then(function (request) {
-      return request.setPods(pods, dbRequestOptions)
-    })
-  }).asCallback(function (err) {
-    if (err) logger.error('Error in createRequest transaction.', { error: err })
+  return db.Request.create(createQuery, dbRequestOptions).asCallback(function (err, request) {
+    if (err) return callback(err)
+
+    return request.setPods(pods, dbRequestOptions).asCallback(callback)
   })
 }