aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/custom-validators/video-comments.ts13
-rw-r--r--server/helpers/custom-validators/video-imports.ts3
-rw-r--r--server/helpers/custom-validators/video-ownership.ts5
-rw-r--r--server/helpers/express-utils.ts5
-rw-r--r--server/helpers/middlewares/abuses.ts3
-rw-r--r--server/helpers/middlewares/accounts.ts5
-rw-r--r--server/helpers/middlewares/video-blacklists.ts3
-rw-r--r--server/helpers/middlewares/video-captions.ts3
-rw-r--r--server/helpers/middlewares/video-channels.ts3
-rw-r--r--server/helpers/middlewares/video-playlists.ts3
-rw-r--r--server/helpers/middlewares/videos.ts13
11 files changed, 36 insertions, 23 deletions
diff --git a/server/helpers/custom-validators/video-comments.ts b/server/helpers/custom-validators/video-comments.ts
index 455ff4241..8d3ce580e 100644
--- a/server/helpers/custom-validators/video-comments.ts
+++ b/server/helpers/custom-validators/video-comments.ts
@@ -3,6 +3,7 @@ import validator from 'validator'
3import { VideoCommentModel } from '@server/models/video/video-comment' 3import { VideoCommentModel } from '@server/models/video/video-comment'
4import { CONSTRAINTS_FIELDS } from '../../initializers/constants' 4import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
5import { MVideoId } from '@server/types/models' 5import { MVideoId } from '@server/types/models'
6import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
6 7
7const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS 8const VIDEO_COMMENTS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_COMMENTS
8 9
@@ -15,7 +16,7 @@ async function doesVideoCommentThreadExist (idArg: number | string, video: MVide
15 const videoComment = await VideoCommentModel.loadById(id) 16 const videoComment = await VideoCommentModel.loadById(id)
16 17
17 if (!videoComment) { 18 if (!videoComment) {
18 res.status(404) 19 res.status(HttpStatusCode.NOT_FOUND_404)
19 .json({ error: 'Video comment thread not found' }) 20 .json({ error: 'Video comment thread not found' })
20 .end() 21 .end()
21 22
@@ -23,7 +24,7 @@ async function doesVideoCommentThreadExist (idArg: number | string, video: MVide
23 } 24 }
24 25
25 if (videoComment.videoId !== video.id) { 26 if (videoComment.videoId !== video.id) {
26 res.status(400) 27 res.status(HttpStatusCode.BAD_REQUEST_400)
27 .json({ error: 'Video comment is not associated to this video.' }) 28 .json({ error: 'Video comment is not associated to this video.' })
28 .end() 29 .end()
29 30
@@ -31,7 +32,7 @@ async function doesVideoCommentThreadExist (idArg: number | string, video: MVide
31 } 32 }
32 33
33 if (videoComment.inReplyToCommentId !== null) { 34 if (videoComment.inReplyToCommentId !== null) {
34 res.status(400) 35 res.status(HttpStatusCode.BAD_REQUEST_400)
35 .json({ error: 'Video comment is not a thread.' }) 36 .json({ error: 'Video comment is not a thread.' })
36 .end() 37 .end()
37 38
@@ -47,7 +48,7 @@ async function doesVideoCommentExist (idArg: number | string, video: MVideoId, r
47 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) 48 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
48 49
49 if (!videoComment) { 50 if (!videoComment) {
50 res.status(404) 51 res.status(HttpStatusCode.NOT_FOUND_404)
51 .json({ error: 'Video comment thread not found' }) 52 .json({ error: 'Video comment thread not found' })
52 .end() 53 .end()
53 54
@@ -55,7 +56,7 @@ async function doesVideoCommentExist (idArg: number | string, video: MVideoId, r
55 } 56 }
56 57
57 if (videoComment.videoId !== video.id) { 58 if (videoComment.videoId !== video.id) {
58 res.status(400) 59 res.status(HttpStatusCode.BAD_REQUEST_400)
59 .json({ error: 'Video comment is not associated to this video.' }) 60 .json({ error: 'Video comment is not associated to this video.' })
60 .end() 61 .end()
61 62
@@ -71,7 +72,7 @@ async function doesCommentIdExist (idArg: number | string, res: express.Response
71 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) 72 const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id)
72 73
73 if (!videoComment) { 74 if (!videoComment) {
74 res.status(404) 75 res.status(HttpStatusCode.NOT_FOUND_404)
75 .json({ error: 'Video comment thread not found' }) 76 .json({ error: 'Video comment thread not found' })
76 77
77 return false 78 return false
diff --git a/server/helpers/custom-validators/video-imports.ts b/server/helpers/custom-validators/video-imports.ts
index 33a1fa8ab..0063d3337 100644
--- a/server/helpers/custom-validators/video-imports.ts
+++ b/server/helpers/custom-validators/video-imports.ts
@@ -4,6 +4,7 @@ import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initia
4import { exists, isFileValid } from './misc' 4import { exists, isFileValid } from './misc'
5import * as express from 'express' 5import * as express from 'express'
6import { VideoImportModel } from '../../models/video/video-import' 6import { VideoImportModel } from '../../models/video/video-import'
7import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
7 8
8function isVideoImportTargetUrlValid (url: string) { 9function isVideoImportTargetUrlValid (url: string) {
9 const isURLOptions = { 10 const isURLOptions = {
@@ -35,7 +36,7 @@ async function doesVideoImportExist (id: number, res: express.Response) {
35 const videoImport = await VideoImportModel.loadAndPopulateVideo(id) 36 const videoImport = await VideoImportModel.loadAndPopulateVideo(id)
36 37
37 if (!videoImport) { 38 if (!videoImport) {
38 res.status(404) 39 res.status(HttpStatusCode.NOT_FOUND_404)
39 .json({ error: 'Video import not found' }) 40 .json({ error: 'Video import not found' })
40 .end() 41 .end()
41 42
diff --git a/server/helpers/custom-validators/video-ownership.ts b/server/helpers/custom-validators/video-ownership.ts
index ed5f8cc2f..ee3cebe10 100644
--- a/server/helpers/custom-validators/video-ownership.ts
+++ b/server/helpers/custom-validators/video-ownership.ts
@@ -2,13 +2,14 @@ import { Response } from 'express'
2import { VideoChangeOwnershipModel } from '../../models/video/video-change-ownership' 2import { VideoChangeOwnershipModel } from '../../models/video/video-change-ownership'
3import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership' 3import { MVideoChangeOwnershipFull } from '@server/types/models/video/video-change-ownership'
4import { MUserId } from '@server/types/models' 4import { MUserId } from '@server/types/models'
5import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5 6
6export async function doesChangeVideoOwnershipExist (idArg: number | string, res: Response) { 7export async function doesChangeVideoOwnershipExist (idArg: number | string, res: Response) {
7 const id = parseInt(idArg + '', 10) 8 const id = parseInt(idArg + '', 10)
8 const videoChangeOwnership = await VideoChangeOwnershipModel.load(id) 9 const videoChangeOwnership = await VideoChangeOwnershipModel.load(id)
9 10
10 if (!videoChangeOwnership) { 11 if (!videoChangeOwnership) {
11 res.status(404) 12 res.status(HttpStatusCode.NOT_FOUND_404)
12 .json({ error: 'Video change ownership not found' }) 13 .json({ error: 'Video change ownership not found' })
13 .end() 14 .end()
14 15
@@ -24,7 +25,7 @@ export function checkUserCanTerminateOwnershipChange (user: MUserId, videoChange
24 return true 25 return true
25 } 26 }
26 27
27 res.status(403) 28 res.status(HttpStatusCode.FORBIDDEN_403)
28 .json({ error: 'Cannot terminate an ownership change of another user' }) 29 .json({ error: 'Cannot terminate an ownership change of another user' })
29 .end() 30 .end()
30 return false 31 return false
diff --git a/server/helpers/express-utils.ts b/server/helpers/express-utils.ts
index ba23557ba..c0d3f8f32 100644
--- a/server/helpers/express-utils.ts
+++ b/server/helpers/express-utils.ts
@@ -7,6 +7,7 @@ import { extname } from 'path'
7import { isArray } from './custom-validators/misc' 7import { isArray } from './custom-validators/misc'
8import { CONFIG } from '../initializers/config' 8import { CONFIG } from '../initializers/config'
9import { getExtFromMimetype } from './video' 9import { getExtFromMimetype } from './video'
10import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
10 11
11function buildNSFWFilter (res?: express.Response, paramNSFW?: string) { 12function buildNSFWFilter (res?: express.Response, paramNSFW?: string) {
12 if (paramNSFW === 'true') return true 13 if (paramNSFW === 'true') return true
@@ -61,7 +62,9 @@ function getHostWithPort (host: string) {
61} 62}
62 63
63function badRequest (req: express.Request, res: express.Response) { 64function badRequest (req: express.Request, res: express.Response) {
64 return res.type('json').status(400).end() 65 return res.type('json')
66 .status(HttpStatusCode.BAD_REQUEST_400)
67 .end()
65} 68}
66 69
67function createReqFiles ( 70function createReqFiles (
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 @@
1import { Response } from 'express' 1import { Response } from 'express'
2import { AbuseModel } from '../../models/abuse/abuse' 2import { AbuseModel } from '../../models/abuse/abuse'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
3 4
4async function doesAbuseExist (abuseId: number | string, res: Response) { 5async 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'
3import * as Bluebird from 'bluebird' 3import * as Bluebird from 'bluebird'
4import { MAccountDefault } from '../../types/models' 4import { MAccountDefault } from '../../types/models'
5import { UserModel } from '@server/models/account/user' 5import { UserModel } from '@server/models/account/user'
6import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
6 7
7function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { 8function 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 @@
1import { Response } from 'express' 1import { Response } from 'express'
2import { VideoBlacklistModel } from '../../models/video/video-blacklist' 2import { VideoBlacklistModel } from '../../models/video/video-blacklist'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
3 4
4async function doesVideoBlacklistExist (videoId: number, res: Response) { 5async 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 @@
1import { Response } from 'express' 1import { Response } from 'express'
2import { VideoCaptionModel } from '../../models/video/video-caption' 2import { VideoCaptionModel } from '../../models/video/video-caption'
3import { MVideoId } from '@server/types/models' 3import { MVideoId } from '@server/types/models'
4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 5
5async function doesVideoCaptionExist (video: MVideoId, language: string, res: Response) { 6async 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 @@
1import * as express from 'express' 1import * as express from 'express'
2import { VideoChannelModel } from '../../models/video/video-channel' 2import { VideoChannelModel } from '../../models/video/video-channel'
3import { MChannelAccountDefault } from '@server/types/models' 3import { MChannelAccountDefault } from '@server/types/models'
4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 5
5async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { 6async 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
31function processVideoChannelExist (videoChannel: MChannelAccountDefault, res: express.Response) { 32function 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 @@
1import * as express from 'express' 1import * as express from 'express'
2import { VideoPlaylistModel } from '../../models/video/video-playlist' 2import { VideoPlaylistModel } from '../../models/video/video-playlist'
3import { MVideoPlaylist } from '../../types/models/video/video-playlist' 3import { MVideoPlaylist } from '../../types/models/video/video-playlist'
4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 5
5export type VideoPlaylistFetchType = 'summary' | 'all' 6export type VideoPlaylistFetchType = 'summary' | 'all'
6async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: VideoPlaylistFetchType = 'summary') { 7async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: VideoPlaylistFetchType = 'summary') {
@@ -27,7 +28,7 @@ export {
27 28
28function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { 29function 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'
15import { VideoFileModel } from '@server/models/video/video-file' 15import { VideoFileModel } from '@server/models/video/video-file'
16import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
16 17
17async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { 18async 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
55async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { 56async 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
95function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { 96function 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