PeerTube/server/core/controllers/api/videos/rate.ts

88 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import express from 'express'
import { HttpStatusCode, UserVideoRateUpdate } from '@peertube/peertube-models'
import { logger } from '../../../helpers/logger.js'
import { VIDEO_RATE_TYPES } from '../../../initializers/constants.js'
import { sequelizeTypescript } from '../../../initializers/database.js'
import { getLocalRateUrl, sendVideoRateChange } from '../../../lib/activitypub/video-rates.js'
import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoUpdateRateValidator } from '../../../middlewares/index.js'
import { AccountModel } from '../../../models/account/account.js'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate.js'
2017-05-15 15:22:03 -05:00
const rateVideoRouter = express.Router()
rateVideoRouter.put('/:id/rate',
authenticate,
2018-11-14 08:01:28 -06:00
asyncMiddleware(videoUpdateRateValidator),
2018-06-13 07:27:40 -05:00
asyncRetryTransactionMiddleware(rateVideo)
2017-05-05 09:53:35 -05:00
)
// ---------------------------------------------------------------------------
2017-05-15 15:22:03 -05:00
export {
rateVideoRouter
}
2017-05-05 09:53:35 -05:00
// ---------------------------------------------------------------------------
2017-10-25 04:55:06 -05:00
async function rateVideo (req: express.Request, res: express.Response) {
const body: UserVideoRateUpdate = req.body
const rateType = body.rating
2019-08-15 04:53:26 -05:00
const videoInstance = res.locals.videoAll
2019-03-19 04:35:15 -05:00
const userAccount = res.locals.oauth.token.User.Account
2017-05-05 09:53:35 -05:00
2017-12-12 10:53:50 -06:00
await sequelizeTypescript.transaction(async t => {
2017-10-25 04:55:06 -05:00
const sequelizeOptions = { transaction: t }
2018-11-14 08:01:28 -06:00
const accountInstance = await AccountModel.load(userAccount.id, t)
2017-12-12 10:53:50 -06:00
const previousRate = await AccountVideoRateModel.load(accountInstance.id, videoInstance.id, t)
2017-10-25 04:55:06 -05:00
2021-02-19 03:26:58 -06:00
// Same rate, nothing do to
if (rateType === 'none' && !previousRate || previousRate?.type === rateType) return
2017-10-25 04:55:06 -05:00
let likesToIncrement = 0
let dislikesToIncrement = 0
if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++
else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++
// There was a previous rate, update it
if (previousRate) {
// We will remove the previous rate, so we will need to update the video count attribute
2018-11-14 08:01:28 -06:00
if (previousRate.type === 'like') likesToIncrement--
else if (previousRate.type === 'dislike') dislikesToIncrement--
2017-10-25 04:55:06 -05:00
if (rateType === 'none') { // Destroy previous rate
await previousRate.destroy(sequelizeOptions)
2017-10-25 04:55:06 -05:00
} else { // Update previous rate
2017-10-31 10:31:24 -05:00
previousRate.type = rateType
2020-11-20 04:21:08 -06:00
previousRate.url = getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
await previousRate.save(sequelizeOptions)
2017-10-25 04:55:06 -05:00
}
} else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
const query = {
2017-11-10 10:27:49 -06:00
accountId: accountInstance.id,
2017-10-25 04:55:06 -05:00
videoId: videoInstance.id,
2018-11-14 08:01:28 -06:00
type: rateType,
2020-11-20 04:21:08 -06:00
url: getLocalRateUrl(rateType, userAccount.Actor, videoInstance)
2017-10-25 04:55:06 -05:00
}
2017-12-12 10:53:50 -06:00
await AccountVideoRateModel.create(query, sequelizeOptions)
2017-10-25 04:55:06 -05:00
}
const incrementQuery = {
likes: likesToIncrement,
dislikes: dislikesToIncrement
}
await videoInstance.increment(incrementQuery, sequelizeOptions)
await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
2017-10-25 04:55:06 -05:00
logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
})
2018-06-13 07:27:40 -05:00
return res.type('json')
.status(HttpStatusCode.NO_CONTENT_204)
.end()
2017-05-05 09:53:35 -05:00
}