diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-12-07 14:32:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-07 14:32:36 +0100 |
commit | 2d53be0267acc49cda46707b885096193a1f4e9c (patch) | |
tree | 887061a34bc67f40acbb96a6278f9544bf83caeb /server/helpers/middlewares | |
parent | adc1f09c0dbd997f34028c1c82d1c118dc8ead80 (diff) | |
download | PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.gz PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.zst PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.zip |
replace numbers with typed http status codes (#3409)
Diffstat (limited to 'server/helpers/middlewares')
-rw-r--r-- | server/helpers/middlewares/abuses.ts | 3 | ||||
-rw-r--r-- | server/helpers/middlewares/accounts.ts | 5 | ||||
-rw-r--r-- | server/helpers/middlewares/video-blacklists.ts | 3 | ||||
-rw-r--r-- | server/helpers/middlewares/video-captions.ts | 3 | ||||
-rw-r--r-- | server/helpers/middlewares/video-channels.ts | 3 | ||||
-rw-r--r-- | server/helpers/middlewares/video-playlists.ts | 3 | ||||
-rw-r--r-- | server/helpers/middlewares/videos.ts | 13 |
7 files changed, 20 insertions, 13 deletions
diff --git a/server/helpers/middlewares/abuses.ts b/server/helpers/middlewares/abuses.ts index 59ba0d3ed..c53bd9efd 100644 --- a/server/helpers/middlewares/abuses.ts +++ b/server/helpers/middlewares/abuses.ts | |||
@@ -1,11 +1,12 @@ | |||
1 | import { Response } from 'express' | 1 | import { Response } from 'express' |
2 | import { AbuseModel } from '../../models/abuse/abuse' | 2 | import { AbuseModel } from '../../models/abuse/abuse' |
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
3 | 4 | ||
4 | async function doesAbuseExist (abuseId: number | string, res: Response) { | 5 | async function doesAbuseExist (abuseId: number | string, res: Response) { |
5 | const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10)) | 6 | const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10)) |
6 | 7 | ||
7 | if (!abuse) { | 8 | if (!abuse) { |
8 | res.status(404) | 9 | res.status(HttpStatusCode.NOT_FOUND_404) |
9 | .json({ error: 'Abuse not found' }) | 10 | .json({ error: 'Abuse not found' }) |
10 | 11 | ||
11 | return false | 12 | return false |
diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts index e9b981262..23470cac6 100644 --- a/server/helpers/middlewares/accounts.ts +++ b/server/helpers/middlewares/accounts.ts | |||
@@ -3,6 +3,7 @@ import { AccountModel } from '../../models/account/account' | |||
3 | import * as Bluebird from 'bluebird' | 3 | import * as Bluebird from 'bluebird' |
4 | import { MAccountDefault } from '../../types/models' | 4 | import { MAccountDefault } from '../../types/models' |
5 | import { UserModel } from '@server/models/account/user' | 5 | import { UserModel } from '@server/models/account/user' |
6 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
6 | 7 | ||
7 | function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { | 8 | function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { |
8 | const promise = AccountModel.load(parseInt(id + '', 10)) | 9 | const promise = AccountModel.load(parseInt(id + '', 10)) |
@@ -27,7 +28,7 @@ async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, se | |||
27 | 28 | ||
28 | if (!account) { | 29 | if (!account) { |
29 | if (sendNotFound === true) { | 30 | if (sendNotFound === true) { |
30 | res.status(404) | 31 | res.status(HttpStatusCode.NOT_FOUND_404) |
31 | .json({ error: 'Account not found' }) | 32 | .json({ error: 'Account not found' }) |
32 | } | 33 | } |
33 | 34 | ||
@@ -43,7 +44,7 @@ async function doesUserFeedTokenCorrespond (id: number, token: string, res: Resp | |||
43 | const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) | 44 | const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) |
44 | 45 | ||
45 | if (token !== user.feedToken) { | 46 | if (token !== user.feedToken) { |
46 | res.status(403) | 47 | res.status(HttpStatusCode.FORBIDDEN_403) |
47 | .json({ error: 'User and token mismatch' }) | 48 | .json({ error: 'User and token mismatch' }) |
48 | 49 | ||
49 | return false | 50 | return false |
diff --git a/server/helpers/middlewares/video-blacklists.ts b/server/helpers/middlewares/video-blacklists.ts index c79420a0c..eda1324d3 100644 --- a/server/helpers/middlewares/video-blacklists.ts +++ b/server/helpers/middlewares/video-blacklists.ts | |||
@@ -1,11 +1,12 @@ | |||
1 | import { Response } from 'express' | 1 | import { Response } from 'express' |
2 | import { VideoBlacklistModel } from '../../models/video/video-blacklist' | 2 | import { VideoBlacklistModel } from '../../models/video/video-blacklist' |
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
3 | 4 | ||
4 | async function doesVideoBlacklistExist (videoId: number, res: Response) { | 5 | async function doesVideoBlacklistExist (videoId: number, res: Response) { |
5 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) | 6 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) |
6 | 7 | ||
7 | if (videoBlacklist === null) { | 8 | if (videoBlacklist === null) { |
8 | res.status(404) | 9 | res.status(HttpStatusCode.NOT_FOUND_404) |
9 | .json({ error: 'Blacklisted video not found' }) | 10 | .json({ error: 'Blacklisted video not found' }) |
10 | .end() | 11 | .end() |
11 | 12 | ||
diff --git a/server/helpers/middlewares/video-captions.ts b/server/helpers/middlewares/video-captions.ts index f5ce29807..10267eda1 100644 --- a/server/helpers/middlewares/video-captions.ts +++ b/server/helpers/middlewares/video-captions.ts | |||
@@ -1,12 +1,13 @@ | |||
1 | import { Response } from 'express' | 1 | import { Response } from 'express' |
2 | import { VideoCaptionModel } from '../../models/video/video-caption' | 2 | import { VideoCaptionModel } from '../../models/video/video-caption' |
3 | import { MVideoId } from '@server/types/models' | 3 | import { MVideoId } from '@server/types/models' |
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | 5 | ||
5 | async function doesVideoCaptionExist (video: MVideoId, language: string, res: Response) { | 6 | async function doesVideoCaptionExist (video: MVideoId, language: string, res: Response) { |
6 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) | 7 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) |
7 | 8 | ||
8 | if (!videoCaption) { | 9 | if (!videoCaption) { |
9 | res.status(404) | 10 | res.status(HttpStatusCode.NOT_FOUND_404) |
10 | .json({ error: 'Video caption not found' }) | 11 | .json({ error: 'Video caption not found' }) |
11 | .end() | 12 | .end() |
12 | 13 | ||
diff --git a/server/helpers/middlewares/video-channels.ts b/server/helpers/middlewares/video-channels.ts index 6eecb8ee5..05499bb74 100644 --- a/server/helpers/middlewares/video-channels.ts +++ b/server/helpers/middlewares/video-channels.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { VideoChannelModel } from '../../models/video/video-channel' | 2 | import { VideoChannelModel } from '../../models/video/video-channel' |
3 | import { MChannelAccountDefault } from '@server/types/models' | 3 | import { MChannelAccountDefault } from '@server/types/models' |
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | 5 | ||
5 | async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { | 6 | async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { |
6 | const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) | 7 | const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) |
@@ -30,7 +31,7 @@ export { | |||
30 | 31 | ||
31 | function processVideoChannelExist (videoChannel: MChannelAccountDefault, res: express.Response) { | 32 | function processVideoChannelExist (videoChannel: MChannelAccountDefault, res: express.Response) { |
32 | if (!videoChannel) { | 33 | if (!videoChannel) { |
33 | res.status(404) | 34 | res.status(HttpStatusCode.NOT_FOUND_404) |
34 | .json({ error: 'Video channel not found' }) | 35 | .json({ error: 'Video channel not found' }) |
35 | .end() | 36 | .end() |
36 | 37 | ||
diff --git a/server/helpers/middlewares/video-playlists.ts b/server/helpers/middlewares/video-playlists.ts index 344104f7c..d2dd80a35 100644 --- a/server/helpers/middlewares/video-playlists.ts +++ b/server/helpers/middlewares/video-playlists.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | 2 | import { VideoPlaylistModel } from '../../models/video/video-playlist' |
3 | import { MVideoPlaylist } from '../../types/models/video/video-playlist' | 3 | import { MVideoPlaylist } from '../../types/models/video/video-playlist' |
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | 5 | ||
5 | export type VideoPlaylistFetchType = 'summary' | 'all' | 6 | export type VideoPlaylistFetchType = 'summary' | 'all' |
6 | async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: VideoPlaylistFetchType = 'summary') { | 7 | async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: VideoPlaylistFetchType = 'summary') { |
@@ -27,7 +28,7 @@ export { | |||
27 | 28 | ||
28 | function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { | 29 | function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { |
29 | if (!videoPlaylist) { | 30 | if (!videoPlaylist) { |
30 | res.status(404) | 31 | res.status(HttpStatusCode.NOT_FOUND_404) |
31 | .json({ error: 'Video playlist not found' }) | 32 | .json({ error: 'Video playlist not found' }) |
32 | .end() | 33 | .end() |
33 | 34 | ||
diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts index 3904f762a..c5eb0607a 100644 --- a/server/helpers/middlewares/videos.ts +++ b/server/helpers/middlewares/videos.ts | |||
@@ -13,6 +13,7 @@ import { | |||
13 | MVideoWithRights | 13 | MVideoWithRights |
14 | } from '@server/types/models' | 14 | } from '@server/types/models' |
15 | import { VideoFileModel } from '@server/models/video/video-file' | 15 | import { VideoFileModel } from '@server/models/video/video-file' |
16 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
16 | 17 | ||
17 | async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { | 18 | async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { |
18 | const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined | 19 | const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined |
@@ -20,7 +21,7 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi | |||
20 | const video = await fetchVideo(id, fetchType, userId) | 21 | const video = await fetchVideo(id, fetchType, userId) |
21 | 22 | ||
22 | if (video === null) { | 23 | if (video === null) { |
23 | res.status(404) | 24 | res.status(HttpStatusCode.NOT_FOUND_404) |
24 | .json({ error: 'Video not found' }) | 25 | .json({ error: 'Video not found' }) |
25 | .end() | 26 | .end() |
26 | 27 | ||
@@ -54,7 +55,7 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi | |||
54 | 55 | ||
55 | async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { | 56 | async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { |
56 | if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) { | 57 | if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) { |
57 | res.status(404) | 58 | res.status(HttpStatusCode.NOT_FOUND_404) |
58 | .json({ error: 'VideoFile matching Video not found' }) | 59 | .json({ error: 'VideoFile matching Video not found' }) |
59 | .end() | 60 | .end() |
60 | 61 | ||
@@ -68,7 +69,7 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc | |||
68 | if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { | 69 | if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { |
69 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) | 70 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) |
70 | if (videoChannel === null) { | 71 | if (videoChannel === null) { |
71 | res.status(400) | 72 | res.status(HttpStatusCode.BAD_REQUEST_400) |
72 | .json({ error: 'Unknown video `video channel` on this instance.' }) | 73 | .json({ error: 'Unknown video `video channel` on this instance.' }) |
73 | .end() | 74 | .end() |
74 | 75 | ||
@@ -81,7 +82,7 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc | |||
81 | 82 | ||
82 | const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) | 83 | const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id) |
83 | if (videoChannel === null) { | 84 | if (videoChannel === null) { |
84 | res.status(400) | 85 | res.status(HttpStatusCode.BAD_REQUEST_400) |
85 | .json({ error: 'Unknown video `video channel` for this account.' }) | 86 | .json({ error: 'Unknown video `video channel` for this account.' }) |
86 | .end() | 87 | .end() |
87 | 88 | ||
@@ -95,7 +96,7 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAcc | |||
95 | function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { | 96 | function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { |
96 | // Retrieve the user who did the request | 97 | // Retrieve the user who did the request |
97 | if (onlyOwned && video.isOwned() === false) { | 98 | if (onlyOwned && video.isOwned() === false) { |
98 | res.status(403) | 99 | res.status(HttpStatusCode.FORBIDDEN_403) |
99 | .json({ error: 'Cannot manage a video of another server.' }) | 100 | .json({ error: 'Cannot manage a video of another server.' }) |
100 | .end() | 101 | .end() |
101 | return false | 102 | return false |
@@ -106,7 +107,7 @@ function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: | |||
106 | // Or if s/he is the video's account | 107 | // Or if s/he is the video's account |
107 | const account = video.VideoChannel.Account | 108 | const account = video.VideoChannel.Account |
108 | if (user.hasRight(right) === false && account.userId !== user.id) { | 109 | if (user.hasRight(right) === false && account.userId !== user.id) { |
109 | res.status(403) | 110 | res.status(HttpStatusCode.FORBIDDEN_403) |
110 | .json({ error: 'Cannot manage a video of another user.' }) | 111 | .json({ error: 'Cannot manage a video of another user.' }) |
111 | .end() | 112 | .end() |
112 | return false | 113 | return false |