]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/rate.ts
Use random names for VOD HLS playlists
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / rate.ts
1 import * as express from 'express'
2 import { UserVideoRateUpdate } from '../../../../shared'
3 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
4 import { logger } from '../../../helpers/logger'
5 import { VIDEO_RATE_TYPES } from '../../../initializers/constants'
6 import { sequelizeTypescript } from '../../../initializers/database'
7 import { getLocalRateUrl, sendVideoRateChange } from '../../../lib/activitypub/video-rates'
8 import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares'
9 import { AccountModel } from '../../../models/account/account'
10 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
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 // Same rate, nothing do to
41 if (rateType === 'none' && !previousRate || previousRate?.type === rateType) return
42
43 let likesToIncrement = 0
44 let dislikesToIncrement = 0
45
46 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
47 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
48
49 // There was a previous rate, update it
50 if (previousRate) {
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--
54
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)
61 }
62 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
63 const query = {
64 accountId: accountInstance.id,
65 videoId: videoInstance.id,
66 type: rateType,
67 url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
68 }
69
70 await AccountVideoRateModel.create(query, sequelizeOptions)
71 }
72
73 const incrementQuery = {
74 likes: likesToIncrement,
75 dislikes: dislikesToIncrement
76 }
77
78 await videoInstance.increment(incrementQuery, sequelizeOptions)
79
80 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
81
82 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
83 })
84
85 return res.type('json')
86 .status(HttpStatusCode.NO_CONTENT_204)
87 .end()
88 }