diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-07-10 19:43:21 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-07-10 19:43:21 +0200 |
commit | 4771e0008dd26eadbb7eaff64255a6ec914fdadb (patch) | |
tree | 4fd58f8a3f3c2d674b936c99817b4f5fb958c5d8 /server/controllers/api/videos/rate.ts | |
parent | 7a214f746bf420defbf17fa218d90d6233551bf8 (diff) | |
download | PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.tar.gz PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.tar.zst PeerTube-4771e0008dd26eadbb7eaff64255a6ec914fdadb.zip |
Better typescript typing for a better world
Diffstat (limited to 'server/controllers/api/videos/rate.ts')
-rw-r--r-- | server/controllers/api/videos/rate.ts | 23 |
1 files changed, 18 insertions, 5 deletions
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as Promise from 'bluebird' | ||
2 | 3 | ||
3 | import { database as db } from '../../../initializers/database' | 4 | import { database as db } from '../../../initializers/database' |
4 | import { | 5 | import { |
@@ -18,6 +19,7 @@ import { | |||
18 | authenticate, | 19 | authenticate, |
19 | videoRateValidator | 20 | videoRateValidator |
20 | } from '../../../middlewares' | 21 | } from '../../../middlewares' |
22 | import { UserVideoRateUpdate, VideoRateType } from '../../../../shared' | ||
21 | 23 | ||
22 | const rateVideoRouter = express.Router() | 24 | const rateVideoRouter = express.Router() |
23 | 25 | ||
@@ -47,7 +49,8 @@ function rateVideoRetryWrapper (req: express.Request, res: express.Response, nex | |||
47 | } | 49 | } |
48 | 50 | ||
49 | function rateVideo (req: express.Request, res: express.Response) { | 51 | function rateVideo (req: express.Request, res: express.Response) { |
50 | const rateType = req.body.rating | 52 | const body: UserVideoRateUpdate = req.body |
53 | const rateType = body.rating | ||
51 | const videoInstance = res.locals.video | 54 | const videoInstance = res.locals.video |
52 | const userInstance = res.locals.oauth.token.User | 55 | const userInstance = res.locals.oauth.token.User |
53 | 56 | ||
@@ -62,24 +65,34 @@ function rateVideo (req: express.Request, res: express.Response) { | |||
62 | if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++ | 65 | if (rateType === VIDEO_RATE_TYPES.LIKE) likesToIncrement++ |
63 | else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ | 66 | else if (rateType === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement++ |
64 | 67 | ||
68 | let promise: Promise<any> | ||
69 | |||
65 | // There was a previous rate, update it | 70 | // There was a previous rate, update it |
66 | if (previousRate) { | 71 | if (previousRate) { |
67 | // We will remove the previous rate, so we will need to remove it from the video attribute | 72 | // We will remove the previous rate, so we will need to remove it from the video attribute |
68 | if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- | 73 | if (previousRate.type === VIDEO_RATE_TYPES.LIKE) likesToIncrement-- |
69 | else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- | 74 | else if (previousRate.type === VIDEO_RATE_TYPES.DISLIKE) dislikesToIncrement-- |
70 | 75 | ||
71 | previousRate.type = rateType | 76 | if (rateType === 'none') { // Destroy previous rate |
77 | promise = previousRate.destroy() | ||
78 | } else { // Update previous rate | ||
79 | previousRate.type = rateType as VideoRateType | ||
72 | 80 | ||
73 | return previousRate.save(options).then(() => ({ t, likesToIncrement, dislikesToIncrement })) | 81 | promise = previousRate.save() |
74 | } else { // There was not a previous rate, insert a new one | 82 | } |
83 | } else if (rateType !== 'none') { // There was not a previous rate, insert a new one if there is a rate | ||
75 | const query = { | 84 | const query = { |
76 | userId: userInstance.id, | 85 | userId: userInstance.id, |
77 | videoId: videoInstance.id, | 86 | videoId: videoInstance.id, |
78 | type: rateType | 87 | type: rateType |
79 | } | 88 | } |
80 | 89 | ||
81 | return db.UserVideoRate.create(query, options).then(() => ({ likesToIncrement, dislikesToIncrement })) | 90 | promise = db.UserVideoRate.create(query, options) |
91 | } else { | ||
92 | promise = Promise.resolve() | ||
82 | } | 93 | } |
94 | |||
95 | return promise.then(() => ({ likesToIncrement, dislikesToIncrement })) | ||
83 | }) | 96 | }) |
84 | .then(({ likesToIncrement, dislikesToIncrement }) => { | 97 | .then(({ likesToIncrement, dislikesToIncrement }) => { |
85 | const options = { transaction: t } | 98 | const options = { transaction: t } |