]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/videos/rate.ts
354c3d8f93d43c3288e82c6a2457b919fd4f4d6e
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / rate.ts
1 import * as express from 'express'
2
3 import { database as db } from '../../../initializers/database'
4 import {
5 logger,
6 retryTransactionWrapper
7 } from '../../../helpers'
8 import {
9 VIDEO_RATE_TYPES,
10 REQUEST_VIDEO_EVENT_TYPES,
11 REQUEST_VIDEO_QADU_TYPES
12 } from '../../../initializers'
13 import {
14 addEventsToRemoteVideo,
15 quickAndDirtyUpdatesVideoToFriends
16 } from '../../../lib'
17 import {
18 authenticate,
19 videoRateValidator,
20 asyncMiddleware
21 } from '../../../middlewares'
22 import { UserVideoRateUpdate, VideoRateType } from '../../../../shared'
23
24 const rateVideoRouter = express.Router()
25
26 rateVideoRouter.put('/:id/rate',
27 authenticate,
28 videoRateValidator,
29 asyncMiddleware(rateVideoRetryWrapper)
30 )
31
32 // ---------------------------------------------------------------------------
33
34 export {
35 rateVideoRouter
36 }
37
38 // ---------------------------------------------------------------------------
39
40 async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
41 const options = {
42 arguments: [ req, res ],
43 errorMessage: 'Cannot update the user video rate.'
44 }
45
46 await retryTransactionWrapper(rateVideo, options)
47
48 return res.type('json').status(204).end()
49 }
50
51 async function rateVideo (req: express.Request, res: express.Response) {
52 const body: UserVideoRateUpdate = req.body
53 const rateType = body.rating
54 const videoInstance = res.locals.video
55 const userInstance = res.locals.oauth.token.User
56
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
76 previousRate.type = rateType as VideoRateType
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 }
140 })
141
142 logger.info('User video rate for video %s of user %s updated.', videoInstance.name, userInstance.username)
143 }