aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/videos
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/videos')
-rw-r--r--server/controllers/api/videos/abuse.ts (renamed from server/controllers/api/videos/abuse.js)77
-rw-r--r--server/controllers/api/videos/blacklist.ts (renamed from server/controllers/api/videos/blacklist.js)34
-rw-r--r--server/controllers/api/videos/index.ts (renamed from server/controllers/api/videos/index.js)182
-rw-r--r--server/controllers/api/videos/rate.ts (renamed from server/controllers/api/videos/rate.js)78
4 files changed, 205 insertions, 166 deletions
diff --git a/server/controllers/api/videos/abuse.js b/server/controllers/api/videos/abuse.ts
index 0fb44bb14..88204120f 100644
--- a/server/controllers/api/videos/abuse.js
+++ b/server/controllers/api/videos/abuse.ts
@@ -1,43 +1,48 @@
1'use strict' 1import express = require('express')
2 2import { waterfall } from 'async'
3const express = require('express')
4const waterfall = require('async/waterfall')
5 3
6const db = require('../../../initializers/database') 4const db = require('../../../initializers/database')
7const logger = require('../../../helpers/logger') 5import friends = require('../../../lib/friends')
8const friends = require('../../../lib/friends') 6import {
9const middlewares = require('../../../middlewares') 7 logger,
10const admin = middlewares.admin 8 getFormatedObjects,
11const oAuth = middlewares.oauth 9 retryTransactionWrapper,
12const pagination = middlewares.pagination 10 startSerializableTransaction,
13const validators = middlewares.validators 11 commitTransaction,
14const validatorsPagination = validators.pagination 12 rollbackTransaction
15const validatorsSort = validators.sort 13} from '../../../helpers'
16const validatorsVideos = validators.videos 14import {
17const sort = middlewares.sort 15 authenticate,
18const databaseUtils = require('../../../helpers/database-utils') 16 ensureIsAdmin,
19const utils = require('../../../helpers/utils') 17 paginationValidator,
20 18 videoAbuseReportValidator,
21const router = express.Router() 19 videoAbusesSortValidator,
22 20 setVideoAbusesSort,
23router.get('/abuse', 21 setPagination
24 oAuth.authenticate, 22} from '../../../middlewares'
25 admin.ensureIsAdmin, 23
26 validatorsPagination.pagination, 24const abuseVideoRouter = express.Router()
27 validatorsSort.videoAbusesSort, 25
28 sort.setVideoAbusesSort, 26abuseVideoRouter.get('/abuse',
29 pagination.setPagination, 27 authenticate,
28 ensureIsAdmin,
29 paginationValidator,
30 videoAbusesSortValidator,
31 setVideoAbusesSort,
32 setPagination,
30 listVideoAbuses 33 listVideoAbuses
31) 34)
32router.post('/:id/abuse', 35abuseVideoRouter.post('/:id/abuse',
33 oAuth.authenticate, 36 authenticate,
34 validatorsVideos.videoAbuseReport, 37 videoAbuseReportValidator,
35 reportVideoAbuseRetryWrapper 38 reportVideoAbuseRetryWrapper
36) 39)
37 40
38// --------------------------------------------------------------------------- 41// ---------------------------------------------------------------------------
39 42
40module.exports = router 43export {
44 abuseVideoRouter
45}
41 46
42// --------------------------------------------------------------------------- 47// ---------------------------------------------------------------------------
43 48
@@ -45,7 +50,7 @@ function listVideoAbuses (req, res, next) {
45 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) { 50 db.VideoAbuse.listForApi(req.query.start, req.query.count, req.query.sort, function (err, abusesList, abusesTotal) {
46 if (err) return next(err) 51 if (err) return next(err)
47 52
48 res.json(utils.getFormatedObjects(abusesList, abusesTotal)) 53 res.json(getFormatedObjects(abusesList, abusesTotal))
49 }) 54 })
50} 55}
51 56
@@ -55,7 +60,7 @@ function reportVideoAbuseRetryWrapper (req, res, next) {
55 errorMessage: 'Cannot report abuse to the video with many retries.' 60 errorMessage: 'Cannot report abuse to the video with many retries.'
56 } 61 }
57 62
58 databaseUtils.retryTransactionWrapper(reportVideoAbuse, options, function (err) { 63 retryTransactionWrapper(reportVideoAbuse, options, function (err) {
59 if (err) return next(err) 64 if (err) return next(err)
60 65
61 return res.type('json').status(204).end() 66 return res.type('json').status(204).end()
@@ -75,7 +80,7 @@ function reportVideoAbuse (req, res, finalCallback) {
75 80
76 waterfall([ 81 waterfall([
77 82
78 databaseUtils.startSerializableTransaction, 83 startSerializableTransaction,
79 84
80 function createAbuse (t, callback) { 85 function createAbuse (t, callback) {
81 db.VideoAbuse.create(abuse).asCallback(function (err, abuse) { 86 db.VideoAbuse.create(abuse).asCallback(function (err, abuse) {
@@ -98,12 +103,12 @@ function reportVideoAbuse (req, res, finalCallback) {
98 return callback(null, t) 103 return callback(null, t)
99 }, 104 },
100 105
101 databaseUtils.commitTransaction 106 commitTransaction
102 107
103 ], function andFinally (err, t) { 108 ], function andFinally (err, t) {
104 if (err) { 109 if (err) {
105 logger.debug('Cannot update the video.', { error: err }) 110 logger.debug('Cannot update the video.', { error: err })
106 return databaseUtils.rollbackTransaction(err, t, finalCallback) 111 return rollbackTransaction(err, t, finalCallback)
107 } 112 }
108 113
109 logger.info('Abuse report for video %s created.', videoInstance.name) 114 logger.info('Abuse report for video %s created.', videoInstance.name)
diff --git a/server/controllers/api/videos/blacklist.js b/server/controllers/api/videos/blacklist.ts
index 8c3e2a69d..db6d95e73 100644
--- a/server/controllers/api/videos/blacklist.js
+++ b/server/controllers/api/videos/blacklist.ts
@@ -1,27 +1,27 @@
1'use strict' 1import express = require('express')
2
3const express = require('express')
4 2
5const db = require('../../../initializers/database') 3const db = require('../../../initializers/database')
6const logger = require('../../../helpers/logger') 4import { logger } from '../../../helpers'
7const middlewares = require('../../../middlewares') 5import {
8const admin = middlewares.admin 6 authenticate,
9const oAuth = middlewares.oauth 7 ensureIsAdmin,
10const validators = middlewares.validators 8 videosBlacklistValidator
11const validatorsVideos = validators.videos 9} from '../../../middlewares'
12 10
13const router = express.Router() 11const blacklistRouter = express.Router()
14 12
15router.post('/:id/blacklist', 13blacklistRouter.post('/:id/blacklist',
16 oAuth.authenticate, 14 authenticate,
17 admin.ensureIsAdmin, 15 ensureIsAdmin,
18 validatorsVideos.videosBlacklist, 16 videosBlacklistValidator,
19 addVideoToBlacklist 17 addVideoToBlacklist
20) 18)
21 19
22// --------------------------------------------------------------------------- 20// ---------------------------------------------------------------------------
23 21
24module.exports = router 22export {
23 blacklistRouter
24}
25 25
26// --------------------------------------------------------------------------- 26// ---------------------------------------------------------------------------
27 27
diff --git a/server/controllers/api/videos/index.js b/server/controllers/api/videos/index.ts
index 8de44d5ac..5fbf03676 100644
--- a/server/controllers/api/videos/index.js
+++ b/server/controllers/api/videos/index.ts
@@ -1,37 +1,57 @@
1'use strict' 1import express = require('express')
2import fs = require('fs')
3import multer = require('multer')
4import path = require('path')
5import { waterfall } from 'async'
2 6
3const express = require('express')
4const fs = require('fs')
5const multer = require('multer')
6const path = require('path')
7const waterfall = require('async/waterfall')
8
9const constants = require('../../../initializers/constants')
10const db = require('../../../initializers/database') 7const db = require('../../../initializers/database')
11const logger = require('../../../helpers/logger') 8import {
12const friends = require('../../../lib/friends') 9 CONFIG,
13const middlewares = require('../../../middlewares') 10 REQUEST_VIDEO_QADU_TYPES,
14const oAuth = middlewares.oauth 11 REQUEST_VIDEO_EVENT_TYPES,
15const pagination = middlewares.pagination 12 VIDEO_CATEGORIES,
16const validators = middlewares.validators 13 VIDEO_LICENCES,
17const validatorsPagination = validators.pagination 14 VIDEO_LANGUAGES
18const validatorsSort = validators.sort 15} from '../../../initializers'
19const validatorsVideos = validators.videos 16import {
20const search = middlewares.search 17 addEventToRemoteVideo,
21const sort = middlewares.sort 18 quickAndDirtyUpdateVideoToFriends,
22const databaseUtils = require('../../../helpers/database-utils') 19 addVideoToFriends,
23const utils = require('../../../helpers/utils') 20 updateVideoToFriends
24 21} from '../../../lib'
25const abuseController = require('./abuse') 22import {
26const blacklistController = require('./blacklist') 23 authenticate,
27const rateController = require('./rate') 24 paginationValidator,
28 25 videosSortValidator,
29const router = express.Router() 26 setVideosSort,
27 setPagination,
28 setVideosSearch,
29 videosUpdateValidator,
30 videosSearchValidator,
31 videosAddValidator,
32 videosGetValidator,
33 videosRemoveValidator
34} from '../../../middlewares'
35import {
36 logger,
37 commitTransaction,
38 retryTransactionWrapper,
39 rollbackTransaction,
40 startSerializableTransaction,
41 generateRandomString,
42 getFormatedObjects
43} from '../../../helpers'
44
45import { abuseVideoRouter } from './abuse'
46import { blacklistRouter } from './blacklist'
47import { rateVideoRouter } from './rate'
48
49const videosRouter = express.Router()
30 50
31// multer configuration 51// multer configuration
32const storage = multer.diskStorage({ 52const storage = multer.diskStorage({
33 destination: function (req, file, cb) { 53 destination: function (req, file, cb) {
34 cb(null, constants.CONFIG.STORAGE.VIDEOS_DIR) 54 cb(null, CONFIG.STORAGE.VIDEOS_DIR)
35 }, 55 },
36 56
37 filename: function (req, file, cb) { 57 filename: function (req, file, cb) {
@@ -39,7 +59,7 @@ const storage = multer.diskStorage({
39 if (file.mimetype === 'video/webm') extension = 'webm' 59 if (file.mimetype === 'video/webm') extension = 'webm'
40 else if (file.mimetype === 'video/mp4') extension = 'mp4' 60 else if (file.mimetype === 'video/mp4') extension = 'mp4'
41 else if (file.mimetype === 'video/ogg') extension = 'ogv' 61 else if (file.mimetype === 'video/ogg') extension = 'ogv'
42 utils.generateRandomString(16, function (err, randomString) { 62 generateRandomString(16, function (err, randomString) {
43 const fieldname = err ? undefined : randomString 63 const fieldname = err ? undefined : randomString
44 cb(null, fieldname + '.' + extension) 64 cb(null, fieldname + '.' + extension)
45 }) 65 })
@@ -48,70 +68,72 @@ const storage = multer.diskStorage({
48 68
49const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }]) 69const reqFiles = multer({ storage: storage }).fields([{ name: 'videofile', maxCount: 1 }])
50 70
51router.use('/', abuseController) 71videosRouter.use('/', abuseVideoRouter)
52router.use('/', blacklistController) 72videosRouter.use('/', blacklistRouter)
53router.use('/', rateController) 73videosRouter.use('/', rateVideoRouter)
54 74
55router.get('/categories', listVideoCategories) 75videosRouter.get('/categories', listVideoCategories)
56router.get('/licences', listVideoLicences) 76videosRouter.get('/licences', listVideoLicences)
57router.get('/languages', listVideoLanguages) 77videosRouter.get('/languages', listVideoLanguages)
58 78
59router.get('/', 79videosRouter.get('/',
60 validatorsPagination.pagination, 80 paginationValidator,
61 validatorsSort.videosSort, 81 videosSortValidator,
62 sort.setVideosSort, 82 setVideosSort,
63 pagination.setPagination, 83 setPagination,
64 listVideos 84 listVideos
65) 85)
66router.put('/:id', 86videosRouter.put('/:id',
67 oAuth.authenticate, 87 authenticate,
68 reqFiles, 88 reqFiles,
69 validatorsVideos.videosUpdate, 89 videosUpdateValidator,
70 updateVideoRetryWrapper 90 updateVideoRetryWrapper
71) 91)
72router.post('/', 92videosRouter.post('/',
73 oAuth.authenticate, 93 authenticate,
74 reqFiles, 94 reqFiles,
75 validatorsVideos.videosAdd, 95 videosAddValidator,
76 addVideoRetryWrapper 96 addVideoRetryWrapper
77) 97)
78router.get('/:id', 98videosRouter.get('/:id',
79 validatorsVideos.videosGet, 99 videosGetValidator,
80 getVideo 100 getVideo
81) 101)
82 102
83router.delete('/:id', 103videosRouter.delete('/:id',
84 oAuth.authenticate, 104 authenticate,
85 validatorsVideos.videosRemove, 105 videosRemoveValidator,
86 removeVideo 106 removeVideo
87) 107)
88 108
89router.get('/search/:value', 109videosRouter.get('/search/:value',
90 validatorsVideos.videosSearch, 110 videosSearchValidator,
91 validatorsPagination.pagination, 111 paginationValidator,
92 validatorsSort.videosSort, 112 videosSortValidator,
93 sort.setVideosSort, 113 setVideosSort,
94 pagination.setPagination, 114 setPagination,
95 search.setVideosSearch, 115 setVideosSearch,
96 searchVideos 116 searchVideos
97) 117)
98 118
99// --------------------------------------------------------------------------- 119// ---------------------------------------------------------------------------
100 120
101module.exports = router 121export {
122 videosRouter
123}
102 124
103// --------------------------------------------------------------------------- 125// ---------------------------------------------------------------------------
104 126
105function listVideoCategories (req, res, next) { 127function listVideoCategories (req, res, next) {
106 res.json(constants.VIDEO_CATEGORIES) 128 res.json(VIDEO_CATEGORIES)
107} 129}
108 130
109function listVideoLicences (req, res, next) { 131function listVideoLicences (req, res, next) {
110 res.json(constants.VIDEO_LICENCES) 132 res.json(VIDEO_LICENCES)
111} 133}
112 134
113function listVideoLanguages (req, res, next) { 135function listVideoLanguages (req, res, next) {
114 res.json(constants.VIDEO_LANGUAGES) 136 res.json(VIDEO_LANGUAGES)
115} 137}
116 138
117// Wrapper to video add that retry the function if there is a database error 139// Wrapper to video add that retry the function if there is a database error
@@ -122,7 +144,7 @@ function addVideoRetryWrapper (req, res, next) {
122 errorMessage: 'Cannot insert the video with many retries.' 144 errorMessage: 'Cannot insert the video with many retries.'
123 } 145 }
124 146
125 databaseUtils.retryTransactionWrapper(addVideo, options, function (err) { 147 retryTransactionWrapper(addVideo, options, function (err) {
126 if (err) return next(err) 148 if (err) return next(err)
127 149
128 // TODO : include Location of the new video -> 201 150 // TODO : include Location of the new video -> 201
@@ -135,7 +157,7 @@ function addVideo (req, res, videoFile, finalCallback) {
135 157
136 waterfall([ 158 waterfall([
137 159
138 databaseUtils.startSerializableTransaction, 160 startSerializableTransaction,
139 161
140 function findOrCreateAuthor (t, callback) { 162 function findOrCreateAuthor (t, callback) {
141 const user = res.locals.oauth.token.User 163 const user = res.locals.oauth.token.User
@@ -179,7 +201,7 @@ function addVideo (req, res, videoFile, finalCallback) {
179 201
180 // Set the videoname the same as the id 202 // Set the videoname the same as the id
181 function renameVideoFile (t, author, tagInstances, video, callback) { 203 function renameVideoFile (t, author, tagInstances, video, callback) {
182 const videoDir = constants.CONFIG.STORAGE.VIDEOS_DIR 204 const videoDir = CONFIG.STORAGE.VIDEOS_DIR
183 const source = path.join(videoDir, videoFile.filename) 205 const source = path.join(videoDir, videoFile.filename)
184 const destination = path.join(videoDir, video.getVideoFilename()) 206 const destination = path.join(videoDir, video.getVideoFilename())
185 207
@@ -218,25 +240,25 @@ function addVideo (req, res, videoFile, finalCallback) {
218 240
219 function sendToFriends (t, video, callback) { 241 function sendToFriends (t, video, callback) {
220 // Let transcoding job send the video to friends because the videofile extension might change 242 // Let transcoding job send the video to friends because the videofile extension might change
221 if (constants.CONFIG.TRANSCODING.ENABLED === true) return callback(null, t) 243 if (CONFIG.TRANSCODING.ENABLED === true) return callback(null, t)
222 244
223 video.toAddRemoteJSON(function (err, remoteVideo) { 245 video.toAddRemoteJSON(function (err, remoteVideo) {
224 if (err) return callback(err) 246 if (err) return callback(err)
225 247
226 // Now we'll add the video's meta data to our friends 248 // Now we'll add the video's meta data to our friends
227 friends.addVideoToFriends(remoteVideo, t, function (err) { 249 addVideoToFriends(remoteVideo, t, function (err) {
228 return callback(err, t) 250 return callback(err, t)
229 }) 251 })
230 }) 252 })
231 }, 253 },
232 254
233 databaseUtils.commitTransaction 255 commitTransaction
234 256
235 ], function andFinally (err, t) { 257 ], function andFinally (err, t) {
236 if (err) { 258 if (err) {
237 // This is just a debug because we will retry the insert 259 // This is just a debug because we will retry the insert
238 logger.debug('Cannot insert the video.', { error: err }) 260 logger.debug('Cannot insert the video.', { error: err })
239 return databaseUtils.rollbackTransaction(err, t, finalCallback) 261 return rollbackTransaction(err, t, finalCallback)
240 } 262 }
241 263
242 logger.info('Video with name %s created.', videoInfos.name) 264 logger.info('Video with name %s created.', videoInfos.name)
@@ -250,7 +272,7 @@ function updateVideoRetryWrapper (req, res, next) {
250 errorMessage: 'Cannot update the video with many retries.' 272 errorMessage: 'Cannot update the video with many retries.'
251 } 273 }
252 274
253 databaseUtils.retryTransactionWrapper(updateVideo, options, function (err) { 275 retryTransactionWrapper(updateVideo, options, function (err) {
254 if (err) return next(err) 276 if (err) return next(err)
255 277
256 // TODO : include Location of the new video -> 201 278 // TODO : include Location of the new video -> 201
@@ -265,7 +287,7 @@ function updateVideo (req, res, finalCallback) {
265 287
266 waterfall([ 288 waterfall([
267 289
268 databaseUtils.startSerializableTransaction, 290 startSerializableTransaction,
269 291
270 function findOrCreateTags (t, callback) { 292 function findOrCreateTags (t, callback) {
271 if (videoInfosToUpdate.tags) { 293 if (videoInfosToUpdate.tags) {
@@ -312,12 +334,12 @@ function updateVideo (req, res, finalCallback) {
312 const json = videoInstance.toUpdateRemoteJSON() 334 const json = videoInstance.toUpdateRemoteJSON()
313 335
314 // Now we'll update the video's meta data to our friends 336 // Now we'll update the video's meta data to our friends
315 friends.updateVideoToFriends(json, t, function (err) { 337 updateVideoToFriends(json, t, function (err) {
316 return callback(err, t) 338 return callback(err, t)
317 }) 339 })
318 }, 340 },
319 341
320 databaseUtils.commitTransaction 342 commitTransaction
321 343
322 ], function andFinally (err, t) { 344 ], function andFinally (err, t) {
323 if (err) { 345 if (err) {
@@ -331,7 +353,7 @@ function updateVideo (req, res, finalCallback) {
331 videoInstance.set(key, value) 353 videoInstance.set(key, value)
332 }) 354 })
333 355
334 return databaseUtils.rollbackTransaction(err, t, finalCallback) 356 return rollbackTransaction(err, t, finalCallback)
335 } 357 }
336 358
337 logger.info('Video with name %s updated.', videoInfosToUpdate.name) 359 logger.info('Video with name %s updated.', videoInfosToUpdate.name)
@@ -354,17 +376,17 @@ function getVideo (req, res, next) {
354 // For example, only add a view when a user watch a video during 30s etc 376 // For example, only add a view when a user watch a video during 30s etc
355 const qaduParams = { 377 const qaduParams = {
356 videoId: videoInstance.id, 378 videoId: videoInstance.id,
357 type: constants.REQUEST_VIDEO_QADU_TYPES.VIEWS 379 type: REQUEST_VIDEO_QADU_TYPES.VIEWS
358 } 380 }
359 friends.quickAndDirtyUpdateVideoToFriends(qaduParams) 381 quickAndDirtyUpdateVideoToFriends(qaduParams)
360 }) 382 })
361 } else { 383 } else {
362 // Just send the event to our friends 384 // Just send the event to our friends
363 const eventParams = { 385 const eventParams = {
364 videoId: videoInstance.id, 386 videoId: videoInstance.id,
365 type: constants.REQUEST_VIDEO_EVENT_TYPES.VIEWS 387 type: REQUEST_VIDEO_EVENT_TYPES.VIEWS
366 } 388 }
367 friends.addEventToRemoteVideo(eventParams) 389 addEventToRemoteVideo(eventParams)
368 } 390 }
369 391
370 // Do not wait the view system 392 // Do not wait the view system
@@ -375,7 +397,7 @@ function listVideos (req, res, next) {
375 db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) { 397 db.Video.listForApi(req.query.start, req.query.count, req.query.sort, function (err, videosList, videosTotal) {
376 if (err) return next(err) 398 if (err) return next(err)
377 399
378 res.json(utils.getFormatedObjects(videosList, videosTotal)) 400 res.json(getFormatedObjects(videosList, videosTotal))
379 }) 401 })
380} 402}
381 403
@@ -398,7 +420,7 @@ function searchVideos (req, res, next) {
398 function (err, videosList, videosTotal) { 420 function (err, videosList, videosTotal) {
399 if (err) return next(err) 421 if (err) return next(err)
400 422
401 res.json(utils.getFormatedObjects(videosList, videosTotal)) 423 res.json(getFormatedObjects(videosList, videosTotal))
402 } 424 }
403 ) 425 )
404} 426}
diff --git a/server/controllers/api/videos/rate.js b/server/controllers/api/videos/rate.ts
index df8a69a1d..21053792a 100644
--- a/server/controllers/api/videos/rate.js
+++ b/server/controllers/api/videos/rate.ts
@@ -1,29 +1,41 @@
1'use strict' 1import express = require('express')
2import { waterfall } from 'async'
2 3
3const express = require('express')
4const waterfall = require('async/waterfall')
5
6const constants = require('../../../initializers/constants')
7const db = require('../../../initializers/database') 4const db = require('../../../initializers/database')
8const logger = require('../../../helpers/logger') 5import {
9const friends = require('../../../lib/friends') 6 logger,
10const middlewares = require('../../../middlewares') 7 retryTransactionWrapper,
11const oAuth = middlewares.oauth 8 startSerializableTransaction,
12const validators = middlewares.validators 9 commitTransaction,
13const validatorsVideos = validators.videos 10 rollbackTransaction
14const databaseUtils = require('../../../helpers/database-utils') 11} from '../../../helpers'
15 12import {
16const router = express.Router() 13 VIDEO_RATE_TYPES,
17 14 REQUEST_VIDEO_EVENT_TYPES,
18router.put('/:id/rate', 15 REQUEST_VIDEO_QADU_TYPES
19 oAuth.authenticate, 16} from '../../../initializers'
20 validatorsVideos.videoRate, 17import {
18 addEventsToRemoteVideo,
19 quickAndDirtyUpdatesVideoToFriends
20} from '../../../lib'
21import {
22 authenticate,
23 videoRateValidator
24} from '../../../middlewares'
25
26const rateVideoRouter = express.Router()
27
28rateVideoRouter.put('/:id/rate',
29 authenticate,
30 videoRateValidator,
21 rateVideoRetryWrapper 31 rateVideoRetryWrapper
22) 32)
23 33
24// --------------------------------------------------------------------------- 34// ---------------------------------------------------------------------------
25 35
26module.exports = router 36export {
37 rateVideoRouter
38}
27 39
28// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------
29 41
@@ -33,7 +45,7 @@ function rateVideoRetryWrapper (req, res, next) {
33 errorMessage: 'Cannot update the user video rate.' 45 errorMessage: 'Cannot update the user video rate.'
34 } 46 }
35 47
36 databaseUtils.retryTransactionWrapper(rateVideo, options, function (err) { 48 retryTransactionWrapper(rateVideo, options, function (err) {
37 if (err) return next(err) 49 if (err) return next(err)
38 50
39 return res.type('json').status(204).end() 51 return res.type('json').status(204).end()
@@ -46,7 +58,7 @@ function rateVideo (req, res, finalCallback) {
46 const userInstance = res.locals.oauth.token.User 58 const userInstance = res.locals.oauth.token.User
47 59
48 waterfall([ 60 waterfall([
49 databaseUtils.startSerializableTransaction, 61 startSerializableTransaction,
50 62
51 function findPreviousRate (t, callback) { 63 function findPreviousRate (t, callback) {
52 db.UserVideoRate.load(userInstance.id, videoInstance.id, t, function (err, previousRate) { 64 db.UserVideoRate.load(userInstance.id, videoInstance.id, t, function (err, previousRate) {
@@ -60,14 +72,14 @@ function rateVideo (req, res, finalCallback) {
60 let likesToIncrement = 0 72 let likesToIncrement = 0
61 let dislikesToIncrement = 0 73 let dislikesToIncrement = 0
62 74
63 if (rateType === constants.VIDEO_RATE_TYPES.LIKE) likesToIncrement++ 75 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
64 else if (rateType === constants.VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ 76 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
65 77
66 // There was a previous rate, update it 78 // There was a previous rate, update it
67 if (previousRate) { 79 if (previousRate) {
68 // We will remove the previous rate, so we will need to remove it from the video attribute 80 // We will remove the previous rate, so we will need to remove it from the video attribute
69 if (previousRate.type === constants.VIDEO_RATE_TYPES.LIKE) likesToIncrement-- 81 if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
70 else if (previousRate.type === constants.VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- 82 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
71 83
72 previousRate.type = rateType 84 previousRate.type = rateType
73 85
@@ -110,7 +122,7 @@ function rateVideo (req, res, finalCallback) {
110 if (likesToIncrement !== 0) { 122 if (likesToIncrement !== 0) {
111 eventsParams.push({ 123 eventsParams.push({
112 videoId: videoInstance.id, 124 videoId: videoInstance.id,
113 type: constants.REQUEST_VIDEO_EVENT_TYPES.LIKES, 125 type: REQUEST_VIDEO_EVENT_TYPES.LIKES,
114 count: likesToIncrement 126 count: likesToIncrement
115 }) 127 })
116 } 128 }
@@ -118,12 +130,12 @@ function rateVideo (req, res, finalCallback) {
118 if (dislikesToIncrement !== 0) { 130 if (dislikesToIncrement !== 0) {
119 eventsParams.push({ 131 eventsParams.push({
120 videoId: videoInstance.id, 132 videoId: videoInstance.id,
121 type: constants.REQUEST_VIDEO_EVENT_TYPES.DISLIKES, 133 type: REQUEST_VIDEO_EVENT_TYPES.DISLIKES,
122 count: dislikesToIncrement 134 count: dislikesToIncrement
123 }) 135 })
124 } 136 }
125 137
126 friends.addEventsToRemoteVideo(eventsParams, t, function (err) { 138 addEventsToRemoteVideo(eventsParams, t, function (err) {
127 return callback(err, t, likesToIncrement, dislikesToIncrement) 139 return callback(err, t, likesToIncrement, dislikesToIncrement)
128 }) 140 })
129 }, 141 },
@@ -138,29 +150,29 @@ function rateVideo (req, res, finalCallback) {
138 if (likesToIncrement !== 0) { 150 if (likesToIncrement !== 0) {
139 qadusParams.push({ 151 qadusParams.push({
140 videoId: videoInstance.id, 152 videoId: videoInstance.id,
141 type: constants.REQUEST_VIDEO_QADU_TYPES.LIKES 153 type: REQUEST_VIDEO_QADU_TYPES.LIKES
142 }) 154 })
143 } 155 }
144 156
145 if (dislikesToIncrement !== 0) { 157 if (dislikesToIncrement !== 0) {
146 qadusParams.push({ 158 qadusParams.push({
147 videoId: videoInstance.id, 159 videoId: videoInstance.id,
148 type: constants.REQUEST_VIDEO_QADU_TYPES.DISLIKES 160 type: REQUEST_VIDEO_QADU_TYPES.DISLIKES
149 }) 161 })
150 } 162 }
151 163
152 friends.quickAndDirtyUpdatesVideoToFriends(qadusParams, t, function (err) { 164 quickAndDirtyUpdatesVideoToFriends(qadusParams, t, function (err) {
153 return callback(err, t) 165 return callback(err, t)
154 }) 166 })
155 }, 167 },
156 168
157 databaseUtils.commitTransaction 169 commitTransaction
158 170
159 ], function (err, t) { 171 ], function (err, t) {
160 if (err) { 172 if (err) {
161 // This is just a debug because we will retry the insert 173 // This is just a debug because we will retry the insert
162 logger.debug('Cannot add the user video rate.', { error: err }) 174 logger.debug('Cannot add the user video rate.', { error: err })
163 return databaseUtils.rollbackTransaction(err, t, finalCallback) 175 return rollbackTransaction(err, t, finalCallback)
164 } 176 }
165 177
166 logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username) 178 logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username)