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