]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
4d4e5cd4 1import * as express from 'express'
53abc4c2 2import { UserVideoRateUpdate } from '../../../../shared'
571389d4
C
3import { logger, retryTransactionWrapper } from '../../../helpers'
4import { VIDEO_RATE_TYPES } from '../../../initializers'
5import { database as db } from '../../../initializers/database'
6import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
7import { AccountInstance } from '../../../models/account/account-interface'
8import { VideoInstance } from '../../../models/video/video-interface'
65fcc311
C
9
10const rateVideoRouter = express.Router()
11
12rateVideoRouter.put('/:id/rate',
13 authenticate,
14 videoRateValidator,
eb080476 15 asyncMiddleware(rateVideoRetryWrapper)
d33242b0
C
16)
17
18// ---------------------------------------------------------------------------
19
65fcc311
C
20export {
21 rateVideoRouter
22}
d33242b0
C
23
24// ---------------------------------------------------------------------------
25
eb080476 26async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
27 const options = {
28 arguments: [ req, res ],
29 errorMessage: 'Cannot update the user video rate.'
30 }
31
eb080476
C
32 await retryTransactionWrapper(rateVideo, options)
33
34 return res.type('json').status(204).end()
d33242b0
C
35}
36
eb080476 37async function rateVideo (req: express.Request, res: express.Response) {
4771e000
C
38 const body: UserVideoRateUpdate = req.body
39 const rateType = body.rating
571389d4
C
40 const videoInstance: VideoInstance = res.locals.video
41 const accountInstance: AccountInstance = res.locals.oauth.token.User.Account
d33242b0 42
eb080476
C
43 await db.sequelize.transaction(async t => {
44 const sequelizeOptions = { transaction: t }
571389d4 45 const previousRate = await db.AccountVideoRate.load(accountInstance.id, videoInstance.id, t)
eb080476
C
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
53abc4c2 62 previousRate.type = rateType
eb080476
C
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 = {
571389d4 68 accountId: accountInstance.id,
eb080476
C
69 videoId: videoInstance.id,
70 type: rateType
71 }
72
571389d4 73 await db.AccountVideoRate.create(query, sequelizeOptions)
eb080476
C
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
eb080476 85 if (videoInstance.isOwned() === false) {
60862425 86 // TODO: Send a event to original server
571389d4
C
87 } else {
88 // TODO: Send update to followers
eb080476 89 }
d33242b0 90 })
eb080476 91
571389d4 92 logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
d33242b0 93}