]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/rate.ts
Use random names for VOD HLS playlists
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / rate.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
53abc4c2 2import { UserVideoRateUpdate } from '../../../../shared'
4c7e60bc 3import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
da854ddd 4import { logger } from '../../../helpers/logger'
74dc3bca 5import { VIDEO_RATE_TYPES } from '../../../initializers/constants'
4c7e60bc 6import { sequelizeTypescript } from '../../../initializers/database'
de94ac86 7import { getLocalRateUrl, sendVideoRateChange } from '../../../lib/activitypub/video-rates'
5c6d985f 8import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares'
3fd3ab2d
C
9import { AccountModel } from '../../../models/account/account'
10import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
65fcc311
C
11
12const rateVideoRouter = express.Router()
13
14rateVideoRouter.put('/:id/rate',
15 authenticate,
5c6d985f 16 asyncMiddleware(videoUpdateRateValidator),
90d4bb81 17 asyncRetryTransactionMiddleware(rateVideo)
d33242b0
C
18)
19
20// ---------------------------------------------------------------------------
21
65fcc311
C
22export {
23 rateVideoRouter
24}
d33242b0
C
25
26// ---------------------------------------------------------------------------
27
eb080476 28async function rateVideo (req: express.Request, res: express.Response) {
4771e000
C
29 const body: UserVideoRateUpdate = req.body
30 const rateType = body.rating
453e83ea 31 const videoInstance = res.locals.videoAll
dae86118 32 const userAccount = res.locals.oauth.token.User.Account
d33242b0 33
3fd3ab2d 34 await sequelizeTypescript.transaction(async t => {
eb080476 35 const sequelizeOptions = { transaction: t }
91411dba 36
5c6d985f 37 const accountInstance = await AccountModel.load(userAccount.id, t)
3fd3ab2d 38 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
eb080476 39
285981f4
C
40 // Same rate, nothing do to
41 if (rateType === 'none' && !previousRate || previousRate?.type === rateType) return
42
eb080476
C
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
5c6d985f
C
52 if (previousRate.type === 'like') likesToIncrement--
53 else if (previousRate.type === 'dislike') dislikesToIncrement--
eb080476
C
54
55 if (rateType === 'none') { // Destroy previous rate
91411dba 56 await previousRate.destroy(sequelizeOptions)
eb080476 57 } else { // Update previous rate
53abc4c2 58 previousRate.type = rateType
de94ac86 59 previousRate.url = getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
91411dba 60 await previousRate.save(sequelizeOptions)
eb080476
C
61 }
62 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
63 const query = {
571389d4 64 accountId: accountInstance.id,
eb080476 65 videoId: videoInstance.id,
5c6d985f 66 type: rateType,
de94ac86 67 url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
eb080476
C
68 }
69
3fd3ab2d 70 await AccountVideoRateModel.create(query, sequelizeOptions)
eb080476
C
71 }
72
73 const incrementQuery = {
74 likes: likesToIncrement,
75 dislikes: dislikesToIncrement
76 }
77
eb080476
C
78 await videoInstance.increment(incrementQuery, sequelizeOptions)
79
07197db4 80 await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
eb080476 81
91411dba
C
82 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
83 })
90d4bb81 84
2d53be02
RK
85 return res.type('json')
86 .status(HttpStatusCode.NO_CONTENT_204)
87 .end()
d33242b0 88}