aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-06 23:24:47 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-06 23:46:36 +0100
commited04d94f6d7132055f97a2f757b85c03c5f2a0b6 (patch)
tree2f1f8c6f6e46b9ab18ba009403f80b14af61af68 /server/lib
parentbb0b243c92577872a5f4d98f707e078082af4d2a (diff)
downloadPeerTube-ed04d94f6d7132055f97a2f757b85c03c5f2a0b6.tar.gz
PeerTube-ed04d94f6d7132055f97a2f757b85c03c5f2a0b6.tar.zst
PeerTube-ed04d94f6d7132055f97a2f757b85c03c5f2a0b6.zip
Server: try to have a better video integrity
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/friends.js66
1 files changed, 45 insertions, 21 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 4afb91b8b..3d3d0fdee 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -24,16 +24,33 @@ const friends = {
24 sendOwnedVideosToPod 24 sendOwnedVideosToPod
25} 25}
26 26
27function addVideoToFriends (videoData) { 27function addVideoToFriends (videoData, transaction, callback) {
28 createRequest('add', constants.REQUEST_ENDPOINTS.VIDEOS, videoData) 28 const options = {
29 type: 'add',
30 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
31 data: videoData,
32 transaction
33 }
34 createRequest(options, callback)
29} 35}
30 36
31function updateVideoToFriends (videoData) { 37function updateVideoToFriends (videoData, transaction, callback) {
32 createRequest('update', constants.REQUEST_ENDPOINTS.VIDEOS, videoData) 38 const options = {
39 type: 'update',
40 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
41 data: videoData,
42 transaction
43 }
44 createRequest(options, callback)
33} 45}
34 46
35function removeVideoToFriends (videoParams) { 47function removeVideoToFriends (videoParams) {
36 createRequest('remove', constants.REQUEST_ENDPOINTS.VIDEOS, videoParams) 48 const options = {
49 type: 'remove',
50 endpoint: constants.REQUEST_ENDPOINTS.VIDEOS,
51 data: videoParams
52 }
53 createRequest(options)
37} 54}
38 55
39function reportAbuseVideoToFriend (reportData, video) { 56function reportAbuseVideoToFriend (reportData, video) {
@@ -258,25 +275,35 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
258} 275}
259 276
260// Wrapper that populate "toIds" argument with all our friends if it is not specified 277// Wrapper that populate "toIds" argument with all our friends if it is not specified
261function createRequest (type, endpoint, data, toIds) { 278// { type, endpoint, data, toIds, transaction }
262 if (toIds) return _createRequest(type, endpoint, data, toIds) 279function createRequest (options, callback) {
280 if (!callback) callback = function () {}
281 if (options.toIds) return _createRequest(options, callback)
263 282
264 // If the "toIds" pods is not specified, we send the request to all our friends 283 // If the "toIds" pods is not specified, we send the request to all our friends
265 db.Pod.listAllIds(function (err, podIds) { 284 db.Pod.listAllIds(options.transaction, function (err, podIds) {
266 if (err) { 285 if (err) {
267 logger.error('Cannot get pod ids', { error: err }) 286 logger.error('Cannot get pod ids', { error: err })
268 return 287 return
269 } 288 }
270 289
271 return _createRequest(type, endpoint, data, podIds) 290 const newOptions = Object.assign(options, { toIds: podIds })
291 return _createRequest(newOptions, callback)
272 }) 292 })
273} 293}
274 294
275function _createRequest (type, endpoint, data, toIds) { 295// { type, endpoint, data, toIds, transaction }
296function _createRequest (options, callback) {
297 const type = options.type
298 const endpoint = options.endpoint
299 const data = options.data
300 const toIds = options.toIds
301 const transaction = options.transaction
302
276 const pods = [] 303 const pods = []
277 304
278 // If there are no destination pods abort 305 // If there are no destination pods abort
279 if (toIds.length === 0) return 306 if (toIds.length === 0) return callback(null)
280 307
281 toIds.forEach(function (toPod) { 308 toIds.forEach(function (toPod) {
282 pods.push(db.Pod.build({ id: toPod })) 309 pods.push(db.Pod.build({ id: toPod }))
@@ -290,17 +317,14 @@ function _createRequest (type, endpoint, data, toIds) {
290 } 317 }
291 } 318 }
292 319
293 // We run in transaction to keep coherency between Request and RequestToPod tables 320 const dbRequestOptions = {
294 db.sequelize.transaction(function (t) { 321 transaction
295 const dbRequestOptions = { 322 }
296 transaction: t
297 }
298 323
299 return db.Request.create(createQuery, dbRequestOptions).then(function (request) { 324 return db.Request.create(createQuery, dbRequestOptions).asCallback(function (err, request) {
300 return request.setPods(pods, dbRequestOptions) 325 if (err) return callback(err)
301 }) 326
302 }).asCallback(function (err) { 327 return request.setPods(pods, dbRequestOptions).asCallback(callback)
303 if (err) logger.error('Error in createRequest transaction.', { error: err })
304 }) 328 })
305} 329}
306 330