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