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
21 } from '../../../middlewares'
22 import { UserVideoRateUpdate, VideoRateType } from '../../../../shared'
24 const rateVideoRouter = express.Router()
26 rateVideoRouter.put('/:id/rate',
29 asyncMiddleware(rateVideoRetryWrapper)
32 // ---------------------------------------------------------------------------
38 // ---------------------------------------------------------------------------
40 async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
42 arguments: [ req, res ],
43 errorMessage: 'Cannot update the user video rate.'
46 await retryTransactionWrapper(rateVideo, options)
48 return res.type('json').status(204).end()
51 async function rateVideo (req: express.Request, res: express.Response) {
52 const body: UserVideoRateUpdate = req.body
53 const rateType = body.rating
54 const videoInstance = res.locals.video
55 const userInstance = res.locals.oauth.token.User
57 await db.sequelize.transaction(async t => {
58 const sequelizeOptions = { transaction: t }
59 const previousRate = await db.UserVideoRate.load(userInstance.id, videoInstance.id, t)
61 let likesToIncrement = 0
62 let dislikesToIncrement = 0
64 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
65 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
67 // There was a previous rate, update it
69 // We will remove the previous rate, so we will need to update the video count attribute
70 if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
71 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
73 if (rateType === 'none') { // Destroy previous rate
74 await previousRate.destroy()
75 } else { // Update previous rate
76 previousRate.type = rateType as VideoRateType
78 await previousRate.save()
80 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
82 userId: userInstance.id,
83 videoId: videoInstance.id,
87 await db.UserVideoRate.create(query, sequelizeOptions)
90 const incrementQuery = {
91 likes: likesToIncrement,
92 dislikes: dislikesToIncrement
95 // Even if we do not own the video we increment the attributes
96 // It is useful for the user to have a feedback
97 await videoInstance.increment(incrementQuery, sequelizeOptions)
99 // Send a event to original pod
100 if (videoInstance.isOwned() === false) {
102 const eventsParams = []
104 if (likesToIncrement !== 0) {
106 videoId: videoInstance.id,
107 type: REQUEST_VIDEO_EVENT_TYPES.LIKES,
108 count: likesToIncrement
112 if (dislikesToIncrement !== 0) {
114 videoId: videoInstance.id,
115 type: REQUEST_VIDEO_EVENT_TYPES.DISLIKES,
116 count: dislikesToIncrement
120 await addEventsToRemoteVideo(eventsParams, t)
121 } else { // We own the video, we need to send a quick and dirty update to friends to notify the counts changed
122 const qadusParams = []
124 if (likesToIncrement !== 0) {
126 videoId: videoInstance.id,
127 type: REQUEST_VIDEO_QADU_TYPES.LIKES
131 if (dislikesToIncrement !== 0) {
133 videoId: videoInstance.id,
134 type: REQUEST_VIDEO_QADU_TYPES.DISLIKES
138 await quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
142 logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username)