diff options
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r-- | server/lib/friends.js | 66 |
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 | ||
27 | function addVideoToFriends (videoData) { | 27 | function 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 | ||
31 | function updateVideoToFriends (videoData) { | 37 | function 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 | ||
35 | function removeVideoToFriends (videoParams) { | 47 | function 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 | ||
39 | function reportAbuseVideoToFriend (reportData, video) { | 56 | function 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 |
261 | function createRequest (type, endpoint, data, toIds) { | 278 | // { type, endpoint, data, toIds, transaction } |
262 | if (toIds) return _createRequest(type, endpoint, data, toIds) | 279 | function 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 | ||
275 | function _createRequest (type, endpoint, data, toIds) { | 295 | // { type, endpoint, data, toIds, transaction } |
296 | function _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 | ||