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 updateVideoToFriends (video) {
- createRequest('update', constants.REQUEST_ENDPOINTS.VIDEOS, video)
+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) {
})
}
-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)
})
}
-function removeVideoToFriends (videoParams) {
- createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams)
-}
-
function sendOwnedVideosToPod (podId) {
db.Video.listOwnedAndPopulateAuthorAndTags(function (err, videosList) {
if (err) {
return
}
- createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, remoteVideo, [ podId ])
+ const options = {
+ type: 'add',
+ endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
+ data: remoteVideo,
+ toIds: [ podId ]
+ }
+ createRequest(options)
})
})
})
// ---------------------------------------------------------------------------
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 })
else podsScore[foreignPodHost] = 1
})
- callback()
+ return callback()
})
}
// 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) {
})
}
-// 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 }))
})
}
}
- // 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)
})
}