1 import * as express from 'express'
2 import { UserVideoRateUpdate } from '../../../../shared'
3 import { logger } from '../../../helpers/logger'
4 import { VIDEO_RATE_TYPES } from '../../../initializers/constants'
5 import { getLocalRateUrl, sendVideoRateChange } from '../../../lib/activitypub/video-rates'
6 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares'
7 import { AccountModel } from '../../../models/account/account'
8 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9 import { sequelizeTypescript } from '../../../initializers/database'
10 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
12 const rateVideoRouter = express.Router()
14 rateVideoRouter.put('/:id/rate',
16 asyncMiddleware(videoUpdateRateValidator),
17 asyncRetryTransactionMiddleware(rateVideo)
20 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
28 async function rateVideo (req: express.Request, res: express.Response) {
29 const body: UserVideoRateUpdate = req.body
30 const rateType = body.rating
31 const videoInstance = res.locals.videoAll
32 const userAccount = res.locals.oauth.token.User.Account
34 await sequelizeTypescript.transaction(async t => {
35 const sequelizeOptions = { transaction: t }
37 const accountInstance = await AccountModel.load(userAccount.id, t)
38 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
40 let likesToIncrement = 0
41 let dislikesToIncrement = 0
43 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
44 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
46 // There was a previous rate, update it
48 // We will remove the previous rate, so we will need to update the video count attribute
49 if (previousRate.type === 'like') likesToIncrement--
50 else if (previousRate.type === 'dislike') dislikesToIncrement--
52 if (rateType === 'none') { // Destroy previous rate
53 await previousRate.destroy(sequelizeOptions)
54 } else { // Update previous rate
55 previousRate.type = rateType
56 previousRate.url = getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
57 await previousRate.save(sequelizeOptions)
59 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
61 accountId: accountInstance.id,
62 videoId: videoInstance.id,
64 url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
67 await AccountVideoRateModel.create(query, sequelizeOptions)
70 const incrementQuery = {
71 likes: likesToIncrement,
72 dislikes: dislikesToIncrement
75 await videoInstance.increment(incrementQuery, sequelizeOptions)
77 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
79 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
82 return res.type('json')
83 .status(HttpStatusCode.NO_CONTENT_204)