]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/rate.ts
Move models to typescript-sequelize
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / rate.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
53abc4c2 2import { UserVideoRateUpdate } from '../../../../shared'
571389d4 3import { logger, retryTransactionWrapper } from '../../../helpers'
3fd3ab2d
C
4import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers'
5import { sendVideoRateChangeToFollowers, sendVideoRateChangeToOrigin } from '../../../lib/activitypub'
571389d4 6import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
3fd3ab2d
C
7import { AccountModel } from '../../../models/account/account'
8import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9import { VideoModel } from '../../../models/video/video'
65fcc311
C
10
11const rateVideoRouter = express.Router()
12
13rateVideoRouter.put('/:id/rate',
14 authenticate,
a2431b7d 15 asyncMiddleware(videoRateValidator),
eb080476 16 asyncMiddleware(rateVideoRetryWrapper)
d33242b0
C
17)
18
19// ---------------------------------------------------------------------------
20
65fcc311
C
21export {
22 rateVideoRouter
23}
d33242b0
C
24
25// ---------------------------------------------------------------------------
26
eb080476 27async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
28 const options = {
29 arguments: [ req, res ],
30 errorMessage: 'Cannot update the user video rate.'
31 }
32
eb080476
C
33 await retryTransactionWrapper(rateVideo, options)
34
35 return res.type('json').status(204).end()
d33242b0
C
36}
37
eb080476 38async function rateVideo (req: express.Request, res: express.Response) {
4771e000
C
39 const body: UserVideoRateUpdate = req.body
40 const rateType = body.rating
3fd3ab2d
C
41 const videoInstance: VideoModel = res.locals.video
42 const accountInstance: AccountModel = res.locals.oauth.token.User.Account
d33242b0 43
3fd3ab2d 44 await sequelizeTypescript.transaction(async t => {
eb080476 45 const sequelizeOptions = { transaction: t }
3fd3ab2d 46 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
eb080476
C
47
48 let likesToIncrement = 0
49 let dislikesToIncrement = 0
50
51 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
52 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
53
54 // There was a previous rate, update it
55 if (previousRate) {
56 // We will remove the previous rate, so we will need to update the video count attribute
57 if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
58 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
59
60 if (rateType === 'none') { // Destroy previous rate
25ed141c 61 await previousRate.destroy({ transaction: t })
eb080476 62 } else { // Update previous rate
53abc4c2 63 previousRate.type = rateType
eb080476 64
25ed141c 65 await previousRate.save({ transaction: t })
eb080476
C
66 }
67 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
68 const query = {
571389d4 69 accountId: accountInstance.id,
eb080476
C
70 videoId: videoInstance.id,
71 type: rateType
72 }
73
3fd3ab2d 74 await AccountVideoRateModel.create(query, sequelizeOptions)
eb080476
C
75 }
76
77 const incrementQuery = {
78 likes: likesToIncrement,
79 dislikes: dislikesToIncrement
80 }
81
82 // Even if we do not own the video we increment the attributes
83 // It is useful for the user to have a feedback
84 await videoInstance.increment(incrementQuery, sequelizeOptions)
85
0032ebe9
C
86 if (videoInstance.isOwned()) {
87 await sendVideoRateChangeToFollowers(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
571389d4 88 } else {
0032ebe9 89 await sendVideoRateChangeToOrigin(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
eb080476 90 }
d33242b0 91 })
eb080476 92
571389d4 93 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
d33242b0 94}