]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
53abc4c2 | 2 | import { UserVideoRateUpdate } from '../../../../shared' |
da854ddd | 3 | import { logger } from '../../../helpers/logger' |
3fd3ab2d | 4 | import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers' |
07197db4 | 5 | import { sendVideoRateChange } from '../../../lib/activitypub' |
90d4bb81 | 6 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoRateValidator } from '../../../middlewares' |
3fd3ab2d C |
7 | import { AccountModel } from '../../../models/account/account' |
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | |
9 | import { VideoModel } from '../../../models/video/video' | |
65fcc311 C |
10 | |
11 | const rateVideoRouter = express.Router() | |
12 | ||
13 | rateVideoRouter.put('/:id/rate', | |
14 | authenticate, | |
a2431b7d | 15 | asyncMiddleware(videoRateValidator), |
90d4bb81 | 16 | asyncRetryTransactionMiddleware(rateVideo) |
d33242b0 C |
17 | ) |
18 | ||
19 | // --------------------------------------------------------------------------- | |
20 | ||
65fcc311 C |
21 | export { |
22 | rateVideoRouter | |
23 | } | |
d33242b0 C |
24 | |
25 | // --------------------------------------------------------------------------- | |
26 | ||
eb080476 | 27 | async function rateVideo (req: express.Request, res: express.Response) { |
4771e000 C |
28 | const body: UserVideoRateUpdate = req.body |
29 | const rateType = body.rating | |
3fd3ab2d C |
30 | const videoInstance: VideoModel = res.locals.video |
31 | const accountInstance: AccountModel = res.locals.oauth.token.User.Account | |
d33242b0 | 32 | |
3fd3ab2d | 33 | await sequelizeTypescript.transaction(async t => { |
eb080476 | 34 | const sequelizeOptions = { transaction: t } |
3fd3ab2d | 35 | const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t) |
eb080476 C |
36 | |
37 | let likesToIncrement = 0 | |
38 | let dislikesToIncrement = 0 | |
39 | ||
40 | if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++ | |
41 | else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ | |
42 | ||
43 | // There was a previous rate, update it | |
44 | if (previousRate) { | |
45 | // We will remove the previous rate, so we will need to update the video count attribute | |
46 | if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- | |
47 | else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- | |
48 | ||
49 | if (rateType === 'none') { // Destroy previous rate | |
25ed141c | 50 | await previousRate.destroy({ transaction: t }) |
eb080476 | 51 | } else { // Update previous rate |
53abc4c2 | 52 | previousRate.type = rateType |
25ed141c | 53 | await previousRate.save({ transaction: t }) |
eb080476 C |
54 | } |
55 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate | |
56 | const query = { | |
571389d4 | 57 | accountId: accountInstance.id, |
eb080476 C |
58 | videoId: videoInstance.id, |
59 | type: rateType | |
60 | } | |
61 | ||
3fd3ab2d | 62 | await AccountVideoRateModel.create(query, sequelizeOptions) |
eb080476 C |
63 | } |
64 | ||
65 | const incrementQuery = { | |
66 | likes: likesToIncrement, | |
67 | dislikes: dislikesToIncrement | |
68 | } | |
69 | ||
eb080476 C |
70 | await videoInstance.increment(incrementQuery, sequelizeOptions) |
71 | ||
07197db4 | 72 | await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t) |
d33242b0 | 73 | }) |
eb080476 | 74 | |
571389d4 | 75 | logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name) |
90d4bb81 C |
76 | |
77 | return res.type('json').status(204).end() | |
d33242b0 | 78 | } |