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 // Same rate, nothing do to
41 if (rateType === 'none' && !previousRate || previousRate?.type === rateType) return
43 let likesToIncrement = 0
44 let dislikesToIncrement = 0
46 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
47 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
49 // There was a previous rate, update it
51 // We will remove the previous rate, so we will need to update the video count attribute
52 if (previousRate.type === 'like') likesToIncrement--
53 else if (previousRate.type === 'dislike') dislikesToIncrement--
55 if (rateType === 'none') { // Destroy previous rate
56 await previousRate.destroy(sequelizeOptions)
57 } else { // Update previous rate
58 previousRate.type = rateType
59 previousRate.url = getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
60 await previousRate.save(sequelizeOptions)
62 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
64 accountId: accountInstance.id,
65 videoId: videoInstance.id,
67 url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
70 await AccountVideoRateModel.create(query, sequelizeOptions)
73 const incrementQuery = {
74 likes: likesToIncrement,
75 dislikes: dislikesToIncrement
78 await videoInstance.increment(incrementQuery, sequelizeOptions)
80 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
82 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
85 return res.type('json')
86 .status(HttpStatusCode.NO_CONTENT_204)