1 import * as express from 'express'
3 import { database as db } from '../../../initializers/database'
6 retryTransactionWrapper
7 } from '../../../helpers'
10 REQUEST_VIDEO_EVENT_TYPES,
11 REQUEST_VIDEO_QADU_TYPES
12 } from '../../../initializers'
14 addEventsToRemoteVideo,
15 quickAndDirtyUpdatesVideoToFriends
20 } from '../../../middlewares'
22 const rateVideoRouter = express.Router()
24 rateVideoRouter.put('/:id/rate',
30 // ---------------------------------------------------------------------------
36 // ---------------------------------------------------------------------------
38 function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
40 arguments: [ req, res ],
41 errorMessage: 'Cannot update the user video rate.'
44 retryTransactionWrapper(rateVideo, options)
45 .then(() => res.type('json').status(204).end())
46 .catch(err => next(err))
49 function rateVideo (req: express.Request, res: express.Response) {
50 const rateType = req.body.rating
51 const videoInstance = res.locals.video
52 const userInstance = res.locals.oauth.token.User
54 return db.sequelize.transaction(t => {
55 return db.UserVideoRate.load(userInstance.id, videoInstance.id, t)
56 .then(previousRate => {
57 const options = { transaction: t }
59 let likesToIncrement = 0
60 let dislikesToIncrement = 0
62 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
63 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
65 // There was a previous rate, update it
67 // We will remove the previous rate, so we will need to remove it from the video attribute
68 if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
69 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
71 previousRate.type = rateType
73 return previousRate.save(options).then(() => ({ t, likesToIncrement, dislikesToIncrement }))
74 } else { // There was not a previous rate, insert a new one
76 userId: userInstance.id,
77 videoId: videoInstance.id,
81 return db.UserVideoRate.create(query, options).then(() => ({ likesToIncrement, dislikesToIncrement }))
84 .then(({ likesToIncrement, dislikesToIncrement }) => {
85 const options = { transaction: t }
86 const incrementQuery = {
87 likes: likesToIncrement,
88 dislikes: dislikesToIncrement
91 // Even if we do not own the video we increment the attributes
92 // It is usefull for the user to have a feedback
93 return videoInstance.increment(incrementQuery, options).then(() => ({ likesToIncrement, dislikesToIncrement }))
95 .then(({ likesToIncrement, dislikesToIncrement }) => {
96 // No need for an event type, we own the video
97 if (videoInstance.isOwned()) return { likesToIncrement, dislikesToIncrement }
99 const eventsParams = []
101 if (likesToIncrement !== 0) {
103 videoId: videoInstance.id,
104 type: REQUEST_VIDEO_EVENT_TYPES.LIKES,
105 count: likesToIncrement
109 if (dislikesToIncrement !== 0) {
111 videoId: videoInstance.id,
112 type: REQUEST_VIDEO_EVENT_TYPES.DISLIKES,
113 count: dislikesToIncrement
117 return addEventsToRemoteVideo(eventsParams, t).then(() => ({ likesToIncrement, dislikesToIncrement }))
119 .then(({ likesToIncrement, dislikesToIncrement }) => {
120 // We do not own the video, there is no need to send a quick and dirty update to friends
121 // Our rate was already sent by the addEvent function
122 if (videoInstance.isOwned() === false) return undefined
124 const qadusParams = []
126 if (likesToIncrement !== 0) {
128 videoId: videoInstance.id,
129 type: REQUEST_VIDEO_QADU_TYPES.LIKES
133 if (dislikesToIncrement !== 0) {
135 videoId: videoInstance.id,
136 type: REQUEST_VIDEO_QADU_TYPES.DISLIKES
140 return quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
143 .then(() => logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username))
145 // This is just a debug because we will retry the insert
146 logger.debug('Cannot add the user video rate.', { error: err })