]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/rate.ts
Guess if we need to generate the thumbnail for imports
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / rate.ts
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'
11
12 const rateVideoRouter = express.Router()
13
14 rateVideoRouter.put('/:id/rate',
15 authenticate,
16 asyncMiddleware(videoUpdateRateValidator),
17 asyncRetryTransactionMiddleware(rateVideo)
18 )
19
20 // ---------------------------------------------------------------------------
21
22 export {
23 rateVideoRouter
24 }
25
26 // ---------------------------------------------------------------------------
27
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
33
34 await sequelizeTypescript.transaction(async t => {
35 const sequelizeOptions = { transaction: t }
36
37 const accountInstance = await AccountModel.load(userAccount.id, t)
38 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
39
40 let likesToIncrement = 0
41 let dislikesToIncrement = 0
42
43 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
44 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
45
46 // There was a previous rate, update it
47 if (previousRate) {
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--
51
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)
58 }
59 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
60 const query = {
61 accountId: accountInstance.id,
62 videoId: videoInstance.id,
63 type: rateType,
64 url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
65 }
66
67 await AccountVideoRateModel.create(query, sequelizeOptions)
68 }
69
70 const incrementQuery = {
71 likes: likesToIncrement,
72 dislikes: dislikesToIncrement
73 }
74
75 await videoInstance.increment(incrementQuery, sequelizeOptions)
76
77 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
78
79 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
80 })
81
82 return res.type('json')
83 .status(HttpStatusCode.NO_CONTENT_204)
84 .end()
85 }