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

95 lines
3.4 KiB
TypeScript
Raw Normal View History

2017-06-05 21:53:49 +02:00
import * as express from 'express'
2017-10-31 16:31:24 +01:00
import { UserVideoRateUpdate } from '../../../../shared'
2017-11-10 17:27:49 +01:00
import { logger, retryTransactionWrapper } from '../../../helpers'
import { VIDEO_RATE_TYPES } from '../../../initializers'
import { database as db } from '../../../initializers/database'
2017-11-23 14:19:55 +01:00
import { sendVideoRateChangeToFollowers, sendVideoRateChangeToOrigin } from '../../../lib/activitypub/videos'
2017-11-10 17:27:49 +01:00
import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
import { AccountInstance } from '../../../models/account/account-interface'
import { VideoInstance } from '../../../models/video/video-interface'
2017-05-15 22:22:03 +02:00
const rateVideoRouter = express.Router()
rateVideoRouter.put('/:id/rate',
authenticate,
videoRateValidator,
2017-10-25 11:55:06 +02:00
asyncMiddleware(rateVideoRetryWrapper)
2017-05-05 16:53:35 +02:00
)
// ---------------------------------------------------------------------------
2017-05-15 22:22:03 +02:00
export {
rateVideoRouter
}
2017-05-05 16:53:35 +02:00
// ---------------------------------------------------------------------------
2017-10-25 11:55:06 +02:00
async function rateVideoRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-05-05 16:53:35 +02:00
const options = {
arguments: [ req, res ],
errorMessage: 'Cannot update the user video rate.'
}
2017-10-25 11:55:06 +02:00
await retryTransactionWrapper(rateVideo, options)
return res.type('json').status(204).end()
2017-05-05 16:53:35 +02:00
}
2017-10-25 11:55:06 +02:00
async function rateVideo (req: express.Request, res: express.Response) {
const body: UserVideoRateUpdate = req.body
const rateType = body.rating
2017-11-10 17:27:49 +01:00
const videoInstance: VideoInstance = res.locals.video
const accountInstance: AccountInstance = res.locals.oauth.token.User.Account
2017-05-05 16:53:35 +02:00
2017-10-25 11:55:06 +02:00
await db.sequelize.transaction(async t => {
const sequelizeOptions = { transaction: t }
2017-11-10 17:27:49 +01:00
const previousRate = await db.AccountVideoRate.load(accountInstance.id, videoInstance.id, t)
2017-10-25 11:55:06 +02: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
if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement--
else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement--
if (rateType === 'none') { // Destroy previous rate
await previousRate.destroy()
} else { // Update previous rate
2017-10-31 16:31:24 +01:00
previousRate.type = rateType
2017-10-25 11:55:06 +02:00
await previousRate.save()
}
} else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate
const query = {
2017-11-10 17:27:49 +01:00
accountId: accountInstance.id,
2017-10-25 11:55:06 +02:00
videoId: videoInstance.id,
type: rateType
}
2017-11-10 17:27:49 +01:00
await db.AccountVideoRate.create(query, sequelizeOptions)
2017-10-25 11:55:06 +02:00
}
const incrementQuery = {
likes: likesToIncrement,
dislikes: dislikesToIncrement
}
// Even if we do not own the video we increment the attributes
// It is useful for the user to have a feedback
await videoInstance.increment(incrementQuery, sequelizeOptions)
2017-11-23 14:19:55 +01:00
if (videoInstance.isOwned()) {
await sendVideoRateChangeToFollowers(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
2017-11-10 17:27:49 +01:00
} else {
2017-11-23 14:19:55 +01:00
await sendVideoRateChangeToOrigin(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
2017-10-25 11:55:06 +02:00
}
2017-05-05 16:53:35 +02:00
})
2017-10-25 11:55:06 +02:00
2017-11-10 17:27:49 +01:00
logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)
2017-05-05 16:53:35 +02:00
}