]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/videos/rate.ts
Remove references to author
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / rate.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
d33242b0 2
e02643f3 3import { database as db } from '../../../initializers/database'
65fcc311
C
4import {
5 logger,
6fcd19ba 6 retryTransactionWrapper
65fcc311
C
7} from '../../../helpers'
8import {
9 VIDEO_RATE_TYPES,
10 REQUEST_VIDEO_EVENT_TYPES,
11 REQUEST_VIDEO_QADU_TYPES
12} from '../../../initializers'
13import {
14 addEventsToRemoteVideo,
15 quickAndDirtyUpdatesVideoToFriends
16} from '../../../lib'
17import {
18 authenticate,
eb080476
C
19 videoRateValidator,
20 asyncMiddleware
65fcc311 21} from '../../../middlewares'
53abc4c2 22import { UserVideoRateUpdate } from '../../../../shared'
65fcc311
C
23
24const rateVideoRouter = express.Router()
25
26rateVideoRouter.put('/:id/rate',
27 authenticate,
28 videoRateValidator,
eb080476 29 asyncMiddleware(rateVideoRetryWrapper)
d33242b0
C
30)
31
32// ---------------------------------------------------------------------------
33
65fcc311
C
34export {
35 rateVideoRouter
36}
d33242b0
C
37
38// ---------------------------------------------------------------------------
39
eb080476 40async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
d33242b0
C
41 const options = {
42 arguments: [ req, res ],
43 errorMessage: 'Cannot update the user video rate.'
44 }
45
eb080476
C
46 await retryTransactionWrapper(rateVideo, options)
47
48 return res.type('json').status(204).end()
d33242b0
C
49}
50
eb080476 51async function rateVideo (req: express.Request, res: express.Response) {
4771e000
C
52 const body: UserVideoRateUpdate = req.body
53 const rateType = body.rating
d33242b0
C
54 const videoInstance = res.locals.video
55 const userInstance = res.locals.oauth.token.User
56
eb080476
C
57 await db.sequelize.transaction(async t => {
58 const sequelizeOptions = { transaction: t }
59 const previousRate = await db.UserVideoRate.load(userInstance.id, videoInstance.id, t)
60
61 let likesToIncrement = 0
62 let dislikesToIncrement = 0
63
64 if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
65 else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
66
67 // There was a previous rate, update it
68 if (previousRate) {
69 // We will remove the previous rate, so we will need to update the video count attribute
70 if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
71 else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
72
73 if (rateType === 'none') { // Destroy previous rate
74 await previousRate.destroy()
75 } else { // Update previous rate
53abc4c2 76 previousRate.type = rateType
eb080476
C
77
78 await previousRate.save()
79 }
80 } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
81 const query = {
82 userId: userInstance.id,
83 videoId: videoInstance.id,
84 type: rateType
85 }
86
87 await db.UserVideoRate.create(query, sequelizeOptions)
88 }
89
90 const incrementQuery = {
91 likes: likesToIncrement,
92 dislikes: dislikesToIncrement
93 }
94
95 // Even if we do not own the video we increment the attributes
96 // It is useful for the user to have a feedback
97 await videoInstance.increment(incrementQuery, sequelizeOptions)
98
99 // Send a event to original pod
100 if (videoInstance.isOwned() === false) {
101
102 const eventsParams = []
103
104 if (likesToIncrement !== 0) {
105 eventsParams.push({
106 videoId: videoInstance.id,
107 type: REQUEST_VIDEO_EVENT_TYPES.LIKES,
108 count: likesToIncrement
109 })
110 }
111
112 if (dislikesToIncrement !== 0) {
113 eventsParams.push({
114 videoId: videoInstance.id,
115 type: REQUEST_VIDEO_EVENT_TYPES.DISLIKES,
116 count: dislikesToIncrement
117 })
118 }
119
120 await addEventsToRemoteVideo(eventsParams, t)
121 } else { // We own the video, we need to send a quick and dirty update to friends to notify the counts changed
122 const qadusParams = []
123
124 if (likesToIncrement !== 0) {
125 qadusParams.push({
126 videoId: videoInstance.id,
127 type: REQUEST_VIDEO_QADU_TYPES.LIKES
128 })
129 }
130
131 if (dislikesToIncrement !== 0) {
132 qadusParams.push({
133 videoId: videoInstance.id,
134 type: REQUEST_VIDEO_QADU_TYPES.DISLIKES
135 })
136 }
137
138 await quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
139 }
d33242b0 140 })
eb080476
C
141
142 logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username)
d33242b0 143}