1 import * as express from 'express'
2 import { UserVideoRateUpdate } from '../../../../shared'
3 import { logger, retryTransactionWrapper } from '../../../helpers'
4 import { VIDEO_RATE_TYPES } from '../../../initializers'
5 import { database as db } from '../../../initializers/database'
6 import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
7 import { AccountInstance } from '../../../models/account/account-interface'
8 import { VideoInstance } from '../../../models/video/video-interface'
10 const rateVideoRouter = express.Router()
12 rateVideoRouter.put('/:id/rate',
15 asyncMiddleware(rateVideoRetryWrapper)
18 // ---------------------------------------------------------------------------
24 // ---------------------------------------------------------------------------
26 async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
28 arguments: [ req, res ],
29 errorMessage: 'Cannot update the user video rate.'
32 await retryTransactionWrapper(rateVideo, options)
34 return res.type('json').status(204).end()
37 async function rateVideo (req: express.Request, res: express.Response) {
38 const body: UserVideoRateUpdate = req.body
39 const rateType = body.rating
40 const videoInstance: VideoInstance = res.locals.video
41 const accountInstance: AccountInstance = res.locals.oauth.token.User.Account
43 await db.sequelize.transaction(async t => {
44 const sequelizeOptions = { transaction: t }
45 const previousRate = await db.AccountVideoRate.load(accountInstance.id, videoInstance.id, t)
47 let likesToIncrement = 0
48 let dislikesToIncrement = 0
50 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
51 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
53 // There was a previous rate, update it
55 // We will remove the previous rate, so we will need to update the video count attribute
56 if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
57 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
59 if (rateType === 'none') { // Destroy previous rate
60 await previousRate.destroy()
61 } else { // Update previous rate
62 previousRate.type = rateType
64 await previousRate.save()
66 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
68 accountId: accountInstance.id,
69 videoId: videoInstance.id,
73 await db.AccountVideoRate.create(query, sequelizeOptions)
76 const incrementQuery = {
77 likes: likesToIncrement,
78 dislikes: dislikesToIncrement
81 // Even if we do not own the video we increment the attributes
82 // It is useful for the user to have a feedback
83 await videoInstance.increment(incrementQuery, sequelizeOptions)
85 if (videoInstance.isOwned() === false) {
86 // TODO: Send a event to original server
88 // TODO: Send update to followers
92 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)