From 4771e0008dd26eadbb7eaff64255a6ec914fdadb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 10 Jul 2017 19:43:21 +0200 Subject: Better typescript typing for a better world --- server/controllers/api/videos/abuse.ts | 4 +++- server/controllers/api/videos/index.ts | 5 +++-- server/controllers/api/videos/rate.ts | 23 ++++++++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) (limited to 'server/controllers/api/videos') diff --git a/server/controllers/api/videos/abuse.ts b/server/controllers/api/videos/abuse.ts index 3b1b7f58e..7d2e3bcfb 100644 --- a/server/controllers/api/videos/abuse.ts +++ b/server/controllers/api/videos/abuse.ts @@ -17,6 +17,7 @@ import { setPagination } from '../../../middlewares' import { VideoInstance } from '../../../models' +import { VideoAbuseCreate } from '../../../../shared' const abuseVideoRouter = express.Router() @@ -63,10 +64,11 @@ function reportVideoAbuseRetryWrapper (req: express.Request, res: express.Respon function reportVideoAbuse (req: express.Request, res: express.Response) { const videoInstance = res.locals.video const reporterUsername = res.locals.oauth.token.User.username + const body: VideoAbuseCreate = req.body const abuse = { reporterUsername, - reason: req.body.reason, + reason: body.reason, videoId: videoInstance.id, reporterPodId: null // This is our pod that reported this abuse } diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 3532c753e..4ae7ea2ed 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -39,6 +39,7 @@ import { renamePromise } from '../../../helpers' import { TagInstance } from '../../../models' +import { VideoCreate, VideoUpdate } from '../../../../shared' import { abuseVideoRouter } from './abuse' import { blacklistRouter } from './blacklist' @@ -155,7 +156,7 @@ function addVideoRetryWrapper (req: express.Request, res: express.Response, next } function addVideo (req: express.Request, res: express.Response, videoFile: Express.Multer.File) { - const videoInfos = req.body + const videoInfos: VideoCreate = req.body return db.sequelize.transaction(t => { const user = res.locals.oauth.token.User @@ -257,7 +258,7 @@ function updateVideoRetryWrapper (req: express.Request, res: express.Response, n function updateVideo (req: express.Request, res: express.Response) { const videoInstance = res.locals.video const videoFieldsSave = videoInstance.toJSON() - const videoInfosToUpdate = req.body + const videoInfosToUpdate: VideoUpdate = req.body return db.sequelize.transaction(t => { let tagsPromise: Promise diff --git a/server/controllers/api/videos/rate.ts b/server/controllers/api/videos/rate.ts index dfb5a450f..8456cbaf2 100644 --- a/server/controllers/api/videos/rate.ts +++ b/server/controllers/api/videos/rate.ts @@ -1,4 +1,5 @@ import * as express from 'express' +import * as Promise from 'bluebird' import { database as db } from '../../../initializers/database' import { @@ -18,6 +19,7 @@ import { authenticate, videoRateValidator } from '../../../middlewares' +import { UserVideoRateUpdate, VideoRateType } from '../../../../shared' const rateVideoRouter = express.Router() @@ -47,7 +49,8 @@ function rateVideoRetryWrapper (req: express.Request, res: express.Response, nex } function rateVideo (req: express.Request, res: express.Response) { - const rateType = req.body.rating + const body: UserVideoRateUpdate = req.body + const rateType = body.rating const videoInstance = res.locals.video const userInstance = res.locals.oauth.token.User @@ -62,24 +65,34 @@ function rateVideo (req: express.Request, res: express.Response) { if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++ else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ + let promise: Promise + // There was a previous rate, update it if (previousRate) { // We will remove the previous rate, so we will need to remove it from the video attribute if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- - previousRate.type = rateType + if (rateType === 'none') { // Destroy previous rate + promise = previousRate.destroy() + } else { // Update previous rate + previousRate.type = rateType as VideoRateType - return previousRate.save(options).then(() => ({ t, likesToIncrement, dislikesToIncrement })) - } else { // There was not a previous rate, insert a new one + promise = previousRate.save() + } + } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate const query = { userId: userInstance.id, videoId: videoInstance.id, type: rateType } - return db.UserVideoRate.create(query, options).then(() => ({ likesToIncrement, dislikesToIncrement })) + promise = db.UserVideoRate.create(query, options) + } else { + promise = Promise.resolve() } + + return promise.then(() => ({ likesToIncrement, dislikesToIncrement })) }) .then(({ likesToIncrement, dislikesToIncrement }) => { const options = { transaction: t } -- cgit v1.2.3