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