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