]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/rate.ts
Move models to typescript-sequelize
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / rate.ts
1 import * as express from 'express'
2 import { UserVideoRateUpdate } from '../../../../shared'
3 import { logger, retryTransactionWrapper } from '../../../helpers'
4 import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers'
5 import { sendVideoRateChangeToFollowers, sendVideoRateChangeToOrigin } from '../../../lib/activitypub'
6 import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
7 import { AccountModel } from '../../../models/account/account'
8 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
9 import { VideoModel } from '../../../models/video/video'
10
11 const rateVideoRouter = express.Router()
12
13 rateVideoRouter.put('/:id/rate',
14 authenticate,
15 asyncMiddleware(videoRateValidator),
16 asyncMiddleware(rateVideoRetryWrapper)
17 )
18
19 // ---------------------------------------------------------------------------
20
21 export {
22 rateVideoRouter
23 }
24
25 // ---------------------------------------------------------------------------
26
27 async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
28 const options = {
29 arguments: [ req, res ],
30 errorMessage: 'Cannot update the user video rate.'
31 }
32
33 await retryTransactionWrapper(rateVideo, options)
34
35 return res.type('json').status(204).end()
36 }
37
38 async function rateVideo (req: express.Request, res: express.Response) {
39 const body: UserVideoRateUpdate = req.body
40 const rateType = body.rating
41 const videoInstance: VideoModel = res.locals.video
42 const accountInstance: AccountModel = res.locals.oauth.token.User.Account
43
44 await sequelizeTypescript.transaction(async t => {
45 const sequelizeOptions = { transaction: t }
46 const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
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
61 await previousRate.destroy({ transaction: t })
62 } else { // Update previous rate
63 previousRate.type = rateType
64
65 await previousRate.save({ transaction: t })
66 }
67 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
68 const query = {
69 accountId: accountInstance.id,
70 videoId: videoInstance.id,
71 type: rateType
72 }
73
74 await AccountVideoRateModel.create(query, sequelizeOptions)
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
86 if (videoInstance.isOwned()) {
87 await sendVideoRateChangeToFollowers(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
88 } else {
89 await sendVideoRateChangeToOrigin(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
90 }
91 })
92
93 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
94 }