]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/friends.js
Client: add ability to report a video
[github/Chocobozzz/PeerTube.git] / server / lib / friends.js
index ad9e4fdaef8d3da067b74b61cae55ed2999808f8..2ea837c9997b5463b5eda6807d0f1ca86b2194bb 100644 (file)
@@ -3,27 +3,65 @@
 const each = require('async/each')
 const eachLimit = require('async/eachLimit')
 const eachSeries = require('async/eachSeries')
-const fs = require('fs')
 const request = require('request')
 const waterfall = require('async/waterfall')
 
 const constants = require('../initializers/constants')
 const db = require('../initializers/database')
 const logger = require('../helpers/logger')
+const peertubeCrypto = require('../helpers/peertube-crypto')
 const requests = require('../helpers/requests')
 
+const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
+
 const friends = {
   addVideoToFriends,
+  updateVideoToFriends,
+  reportAbuseVideoToFriend,
   hasFriends,
-  getMyCertificate,
   makeFriends,
   quitFriends,
   removeVideoToFriends,
   sendOwnedVideosToPod
 }
 
-function addVideoToFriends (video) {
-  createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, video)
+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, transaction, callback) {
+  const options = {
+    type: ENDPOINT_ACTIONS.UPDATE,
+    endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+    data: videoData,
+    transaction
+  }
+  createRequest(options, callback)
+}
+
+function removeVideoToFriends (videoParams) {
+  const options = {
+    type: ENDPOINT_ACTIONS.REMOVE,
+    endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+    data: videoParams
+  }
+  createRequest(options)
+}
+
+function reportAbuseVideoToFriend (reportData, video) {
+  const options = {
+    type: ENDPOINT_ACTIONS.REPORT_ABUSE,
+    endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+    data: reportData,
+    toIds: [ video.Author.podId ]
+  }
+  createRequest(options)
 }
 
 function hasFriends (callback) {
@@ -35,15 +73,11 @@ function hasFriends (callback) {
   })
 }
 
-function getMyCertificate (callback) {
-  fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
-}
-
 function makeFriends (hosts, callback) {
   const podsScore = {}
 
   logger.info('Make friends!')
-  getMyCertificate(function (err, cert) {
+  peertubeCrypto.getMyPublicCert(function (err, cert) {
     if (err) {
       logger.error('Cannot read public cert.')
       return callback(err)
@@ -115,10 +149,6 @@ function quitFriends (callback) {
   })
 }
 
-function removeVideoToFriends (videoParams) {
-  createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams)
-}
-
 function sendOwnedVideosToPod (podId) {
   db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) {
     if (err) {
@@ -127,14 +157,20 @@ function sendOwnedVideosToPod (podId) {
     }
 
     videosList.forEach(function (video) {
-      video.toRemoteJSON(function (err, remoteVideo) {
+      video.toAddRemoteJSON(function (err, remoteVideo) {
         if (err) {
           logger.error('Cannot convert video to remote.', { error: err })
           // Don't break the process
           return
         }
 
-        createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, remoteVideo, [ podId ])
+        const options = {
+          type: 'add',
+          endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+          data: remoteVideo,
+          toIds: [ podId ]
+        }
+        createRequest(options)
       })
     })
   })
@@ -147,10 +183,10 @@ module.exports = friends
 // ---------------------------------------------------------------------------
 
 function computeForeignPodsList (host, podsScore, callback) {
-  getForeignPodsList(host, function (err, foreignPodsList) {
+  getForeignPodsList(host, function (err, res) {
     if (err) return callback(err)
 
-    if (!foreignPodsList) foreignPodsList = []
+    const foreignPodsList = res.data
 
     // Let's give 1 point to the pod we ask the friends list
     foreignPodsList.push({ host })
@@ -162,7 +198,7 @@ function computeForeignPodsList (host, podsScore, callback) {
       else podsScore[foreignPodHost] = 1
     })
 
-    callback()
+    return callback()
   })
 }
 
@@ -171,6 +207,7 @@ function computeWinningPods (hosts, podsScore) {
   // Only add a pod if it exists in more than a half base pods
   const podsList = []
   const baseScore = hosts.length / 2
+
   Object.keys(podsScore).forEach(function (podHost) {
     // If the pod is not me and with a good score we add it
     if (isMe(podHost) === false && podsScore[podHost] > baseScore) {
@@ -247,28 +284,38 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
   })
 }
 
-// Wrapper that populate "to" argument with all our friends if it is not specified
-function createRequest (type, endpoint, data, to) {
-  if (to) return _createRequest(type, endpoint, data, to)
+// Wrapper that populate "toIds" argument with all our friends if it is not specified
+// { type, endpoint, data, toIds, transaction }
+function createRequest (options, callback) {
+  if (!callback) callback = function () {}
+  if (options.toIds) return _createRequest(options, callback)
 
-  // If the "to" pods is not specified, we send the request to all our friends
-  db.Pod.listAllIds(function (err, podIds) {
+  // If the "toIds" pods is not specified, we send the request to all our friends
+  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, to) {
+// { 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 (to.length === 0) return
+  if (toIds.length === 0) return callback(null)
 
-  to.forEach(function (toPod) {
+  toIds.forEach(function (toPod) {
     pods.push(db.Pod.build({ id: toPod }))
   })
 
@@ -280,17 +327,14 @@ function _createRequest (type, endpoint, data, to) {
     }
   }
 
-  // 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)
   })
 }