1 import * as express from 'express'
2 import { UserVideoRateUpdate } from '../../../../shared'
3 import { retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers'
6 import { sendVideoRateChangeToFollowers, sendVideoRateChangeToOrigin } from '../../../lib/activitypub'
7 import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
8 import { AccountModel } from '../../../models/account/account'
9 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
10 import { VideoModel } from '../../../models/video/video'
12 const rateVideoRouter = express.Router()
14 rateVideoRouter.put('/:id/rate',
16 asyncMiddleware(videoRateValidator),
17 asyncMiddleware(rateVideoRetryWrapper)
20 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
28 async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
30 arguments: [ req, res ],
31 errorMessage: 'Cannot update the user video rate.'
34 await retryTransactionWrapper(rateVideo, options)
36 return res.type('json').status(204).end()
39 async function rateVideo (req: express.Request, res: express.Response) {
40 const body: UserVideoRateUpdate = req.body
41 const rateType = body.rating
42 const videoInstance: VideoModel = res.locals.video
43 const accountInstance: AccountModel = res.locals.oauth.token.User.Account
45 await sequelizeTypescript.transaction(async t => {
46 const sequelizeOptions = { transaction: t }
47 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
49 let likesToIncrement = 0
50 let dislikesToIncrement = 0
52 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
53 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
55 // There was a previous rate, update it
57 // We will remove the previous rate, so we will need to update the video count attribute
58 if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
59 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
61 if (rateType === 'none') { // Destroy previous rate
62 await previousRate.destroy({ transaction: t })
63 } else { // Update previous rate
64 previousRate.type = rateType
65 await previousRate.save({ transaction: t })
67 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
69 accountId: accountInstance.id,
70 videoId: videoInstance.id,
74 await AccountVideoRateModel.create(query, sequelizeOptions)
77 const incrementQuery = {
78 likes: likesToIncrement,
79 dislikes: dislikesToIncrement
82 // Even if we do not own the video we increment the attributes
83 // It is useful for the user to have a feedback
84 await videoInstance.increment(incrementQuery, sequelizeOptions)
86 if (videoInstance.isOwned()) {
87 await sendVideoRateChangeToFollowers(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
89 await sendVideoRateChangeToOrigin(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
93 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)