diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 17:33:44 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 18:03:36 +0200 |
commit | 10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 (patch) | |
tree | 008f8dad8032684f46105a261b27b2d6f05b36eb /server/middlewares | |
parent | 5e08989ede1a340b9edb94465a11b1e04bf24094 (diff) | |
download | PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.gz PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.zst PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.zip |
Move middleware utils in middlewares
helpers modules should not import models
Diffstat (limited to 'server/middlewares')
49 files changed, 580 insertions, 124 deletions
diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index a1fdfafcf..6b43b7764 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts | |||
@@ -1,13 +1,12 @@ | |||
1 | import { NextFunction, Request, Response } from 'express' | 1 | import { NextFunction, Request, Response } from 'express' |
2 | import { getAPId } from '@server/helpers/activitypub' | ||
3 | import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' | ||
2 | import { ActivityDelete, ActivityPubSignature } from '../../shared' | 4 | import { ActivityDelete, ActivityPubSignature } from '../../shared' |
5 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' | ||
3 | import { logger } from '../helpers/logger' | 6 | import { logger } from '../helpers/logger' |
4 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' | 7 | import { isHTTPSignatureVerified, isJsonLDSignatureVerified, parseHTTPSignature } from '../helpers/peertube-crypto' |
5 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' | 8 | import { ACCEPT_HEADERS, ACTIVITY_PUB, HTTP_SIGNATURE } from '../initializers/constants' |
6 | import { getOrCreateAPActor } from '../lib/activitypub/actors' | 9 | import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../lib/activitypub/actors' |
7 | import { loadActorUrlOrGetFromWebfinger } from '../helpers/webfinger' | ||
8 | import { isActorDeleteActivityValid } from '@server/helpers/custom-validators/activitypub/actor' | ||
9 | import { getAPId } from '@server/helpers/activitypub' | ||
10 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' | ||
11 | 10 | ||
12 | async function checkSignature (req: Request, res: Response, next: NextFunction) { | 11 | async function checkSignature (req: Request, res: Response, next: NextFunction) { |
13 | try { | 12 | try { |
diff --git a/server/middlewares/validators/abuse.ts b/server/middlewares/validators/abuse.ts index 7f002e0d5..56c97747c 100644 --- a/server/middlewares/validators/abuse.ts +++ b/server/middlewares/validators/abuse.ts | |||
@@ -13,13 +13,11 @@ import { | |||
13 | isAbuseVideoIsValid | 13 | isAbuseVideoIsValid |
14 | } from '@server/helpers/custom-validators/abuses' | 14 | } from '@server/helpers/custom-validators/abuses' |
15 | import { exists, isIdOrUUIDValid, isIdValid, toIntOrNull } from '@server/helpers/custom-validators/misc' | 15 | import { exists, isIdOrUUIDValid, isIdValid, toIntOrNull } from '@server/helpers/custom-validators/misc' |
16 | import { doesCommentIdExist } from '@server/helpers/custom-validators/video-comments' | ||
17 | import { logger } from '@server/helpers/logger' | 16 | import { logger } from '@server/helpers/logger' |
18 | import { doesAbuseExist, doesAccountIdExist, doesVideoExist } from '@server/helpers/middlewares' | ||
19 | import { AbuseMessageModel } from '@server/models/abuse/abuse-message' | 17 | import { AbuseMessageModel } from '@server/models/abuse/abuse-message' |
20 | import { AbuseCreate, UserRight } from '@shared/models' | 18 | import { AbuseCreate, UserRight } from '@shared/models' |
21 | import { areValidationErrors } from './utils' | ||
22 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 19 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
20 | import { areValidationErrors, doesAbuseExist, doesAccountIdExist, doesCommentIdExist, doesVideoExist } from './shared' | ||
23 | 21 | ||
24 | const abuseReportValidator = [ | 22 | const abuseReportValidator = [ |
25 | body('account.id') | 23 | body('account.id') |
diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts index cbdcef2fd..599eb10bb 100644 --- a/server/middlewares/validators/account.ts +++ b/server/middlewares/validators/account.ts | |||
@@ -2,8 +2,7 @@ import * as express from 'express' | |||
2 | import { param } from 'express-validator' | 2 | import { param } from 'express-validator' |
3 | import { isAccountNameValid } from '../../helpers/custom-validators/accounts' | 3 | import { isAccountNameValid } from '../../helpers/custom-validators/accounts' |
4 | import { logger } from '../../helpers/logger' | 4 | import { logger } from '../../helpers/logger' |
5 | import { areValidationErrors } from './utils' | 5 | import { areValidationErrors, doesAccountNameWithHostExist, doesLocalAccountNameExist } from './shared' |
6 | import { doesAccountNameWithHostExist, doesLocalAccountNameExist } from '../../helpers/middlewares' | ||
7 | 6 | ||
8 | const localAccountValidator = [ | 7 | const localAccountValidator = [ |
9 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), | 8 | param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'), |
diff --git a/server/middlewares/validators/activitypub/pagination.ts b/server/middlewares/validators/activitypub/pagination.ts index fa21f063d..c8ec34eb6 100644 --- a/server/middlewares/validators/activitypub/pagination.ts +++ b/server/middlewares/validators/activitypub/pagination.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { query } from 'express-validator' | 2 | import { query } from 'express-validator' |
3 | import { logger } from '../../../helpers/logger' | ||
4 | import { areValidationErrors } from '../utils' | ||
5 | import { PAGINATION } from '@server/initializers/constants' | 3 | import { PAGINATION } from '@server/initializers/constants' |
4 | import { logger } from '../../../helpers/logger' | ||
5 | import { areValidationErrors } from '../shared' | ||
6 | 6 | ||
7 | const apPaginationValidator = [ | 7 | const apPaginationValidator = [ |
8 | query('page') | 8 | query('page') |
diff --git a/server/middlewares/validators/activitypub/signature.ts b/server/middlewares/validators/activitypub/signature.ts index 7896a6128..f2f7d5848 100644 --- a/server/middlewares/validators/activitypub/signature.ts +++ b/server/middlewares/validators/activitypub/signature.ts | |||
@@ -1,12 +1,13 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body } from 'express-validator' | 2 | import { body } from 'express-validator' |
3 | import { | 3 | import { |
4 | isSignatureCreatorValid, isSignatureTypeValid, | 4 | isSignatureCreatorValid, |
5 | isSignatureTypeValid, | ||
5 | isSignatureValueValid | 6 | isSignatureValueValid |
6 | } from '../../../helpers/custom-validators/activitypub/signature' | 7 | } from '../../../helpers/custom-validators/activitypub/signature' |
7 | import { isDateValid } from '../../../helpers/custom-validators/misc' | 8 | import { isDateValid } from '../../../helpers/custom-validators/misc' |
8 | import { logger } from '../../../helpers/logger' | 9 | import { logger } from '../../../helpers/logger' |
9 | import { areValidationErrors } from '../utils' | 10 | import { areValidationErrors } from '../shared' |
10 | 11 | ||
11 | const signatureValidator = [ | 12 | const signatureValidator = [ |
12 | body('signature.type') | 13 | body('signature.type') |
diff --git a/server/middlewares/validators/actor-image.ts b/server/middlewares/validators/actor-image.ts index 961d7a7e5..49daadd61 100644 --- a/server/middlewares/validators/actor-image.ts +++ b/server/middlewares/validators/actor-image.ts | |||
@@ -4,7 +4,7 @@ import { isActorImageFile } from '@server/helpers/custom-validators/actor-images | |||
4 | import { cleanUpReqFiles } from '../../helpers/express-utils' | 4 | import { cleanUpReqFiles } from '../../helpers/express-utils' |
5 | import { logger } from '../../helpers/logger' | 5 | import { logger } from '../../helpers/logger' |
6 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' | 6 | import { CONSTRAINTS_FIELDS } from '../../initializers/constants' |
7 | import { areValidationErrors } from './utils' | 7 | import { areValidationErrors } from './shared' |
8 | 8 | ||
9 | const updateActorImageValidatorFactory = (fieldname: string) => ([ | 9 | const updateActorImageValidatorFactory = (fieldname: string) => ([ |
10 | body(fieldname).custom((value, { req }) => isActorImageFile(req.files, fieldname)).withMessage( | 10 | body(fieldname).custom((value, { req }) => isActorImageFile(req.files, fieldname)).withMessage( |
diff --git a/server/middlewares/validators/blocklist.ts b/server/middlewares/validators/blocklist.ts index 125ff882c..826b16fc8 100644 --- a/server/middlewares/validators/blocklist.ts +++ b/server/middlewares/validators/blocklist.ts | |||
@@ -1,15 +1,14 @@ | |||
1 | import { body, param } from 'express-validator' | ||
2 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param } from 'express-validator' | ||
3 | import { getServerActor } from '@server/models/application/application' | ||
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
5 | import { isHostValid } from '../../helpers/custom-validators/servers' | ||
3 | import { logger } from '../../helpers/logger' | 6 | import { logger } from '../../helpers/logger' |
4 | import { areValidationErrors } from './utils' | 7 | import { WEBSERVER } from '../../initializers/constants' |
5 | import { AccountBlocklistModel } from '../../models/account/account-blocklist' | 8 | import { AccountBlocklistModel } from '../../models/account/account-blocklist' |
6 | import { isHostValid } from '../../helpers/custom-validators/servers' | ||
7 | import { ServerBlocklistModel } from '../../models/server/server-blocklist' | ||
8 | import { ServerModel } from '../../models/server/server' | 9 | import { ServerModel } from '../../models/server/server' |
9 | import { WEBSERVER } from '../../initializers/constants' | 10 | import { ServerBlocklistModel } from '../../models/server/server-blocklist' |
10 | import { doesAccountNameWithHostExist } from '../../helpers/middlewares' | 11 | import { areValidationErrors, doesAccountNameWithHostExist } from './shared' |
11 | import { getServerActor } from '@server/models/application/application' | ||
12 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
13 | 12 | ||
14 | const blockAccountValidator = [ | 13 | const blockAccountValidator = [ |
15 | body('accountName').exists().withMessage('Should have an account name with host'), | 14 | body('accountName').exists().withMessage('Should have an account name with host'), |
diff --git a/server/middlewares/validators/bulk.ts b/server/middlewares/validators/bulk.ts index 847885101..9bb95f5b7 100644 --- a/server/middlewares/validators/bulk.ts +++ b/server/middlewares/validators/bulk.ts | |||
@@ -1,12 +1,11 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body } from 'express-validator' | 2 | import { body } from 'express-validator' |
3 | import { isBulkRemoveCommentsOfScopeValid } from '@server/helpers/custom-validators/bulk' | 3 | import { isBulkRemoveCommentsOfScopeValid } from '@server/helpers/custom-validators/bulk' |
4 | import { doesAccountNameWithHostExist } from '@server/helpers/middlewares' | 4 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
5 | import { UserRight } from '@shared/models' | 5 | import { UserRight } from '@shared/models' |
6 | import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' | 6 | import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' |
7 | import { logger } from '../../helpers/logger' | 7 | import { logger } from '../../helpers/logger' |
8 | import { areValidationErrors } from './utils' | 8 | import { areValidationErrors, doesAccountNameWithHostExist } from './shared' |
9 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
10 | 9 | ||
11 | const bulkRemoveCommentsOfValidator = [ | 10 | const bulkRemoveCommentsOfValidator = [ |
12 | body('accountName').exists().withMessage('Should have an account name with host'), | 11 | body('accountName').exists().withMessage('Should have an account name with host'), |
diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts index b5d6b4622..1aeadbe65 100644 --- a/server/middlewares/validators/config.ts +++ b/server/middlewares/validators/config.ts | |||
@@ -7,7 +7,7 @@ import { isThemeNameValid } from '../../helpers/custom-validators/plugins' | |||
7 | import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' | 7 | import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' |
8 | import { logger } from '../../helpers/logger' | 8 | import { logger } from '../../helpers/logger' |
9 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' | 9 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' |
10 | import { areValidationErrors } from './utils' | 10 | import { areValidationErrors } from './shared' |
11 | 11 | ||
12 | const customConfigUpdateValidator = [ | 12 | const customConfigUpdateValidator = [ |
13 | body('instance.name').exists().withMessage('Should have a valid instance name'), | 13 | body('instance.name').exists().withMessage('Should have a valid instance name'), |
diff --git a/server/middlewares/validators/feeds.ts b/server/middlewares/validators/feeds.ts index aa16cc993..51e6d6fff 100644 --- a/server/middlewares/validators/feeds.ts +++ b/server/middlewares/validators/feeds.ts | |||
@@ -1,18 +1,18 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param, query } from 'express-validator' | 2 | import { param, query } from 'express-validator' |
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
3 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' | 4 | import { isValidRSSFeed } from '../../helpers/custom-validators/feeds' |
4 | import { exists, isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' | 5 | import { exists, isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' |
5 | import { logger } from '../../helpers/logger' | 6 | import { logger } from '../../helpers/logger' |
6 | import { | 7 | import { |
8 | areValidationErrors, | ||
7 | doesAccountIdExist, | 9 | doesAccountIdExist, |
8 | doesAccountNameWithHostExist, | 10 | doesAccountNameWithHostExist, |
9 | doesUserFeedTokenCorrespond, | 11 | doesUserFeedTokenCorrespond, |
10 | doesVideoChannelIdExist, | 12 | doesVideoChannelIdExist, |
11 | doesVideoChannelNameWithHostExist | 13 | doesVideoChannelNameWithHostExist, |
12 | } from '../../helpers/middlewares' | 14 | doesVideoExist |
13 | import { doesVideoExist } from '../../helpers/middlewares/videos' | 15 | } from './shared' |
14 | import { areValidationErrors } from './utils' | ||
15 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
16 | 16 | ||
17 | const feedsFormatValidator = [ | 17 | const feedsFormatValidator = [ |
18 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), | 18 | param('format').optional().custom(isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'), |
diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts index 733be379b..205baca48 100644 --- a/server/middlewares/validators/follows.ts +++ b/server/middlewares/validators/follows.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param, query } from 'express-validator' | 2 | import { body, param, query } from 'express-validator' |
3 | import { isFollowStateValid } from '@server/helpers/custom-validators/follows' | 3 | import { isFollowStateValid } from '@server/helpers/custom-validators/follows' |
4 | import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors' | ||
4 | import { getServerActor } from '@server/models/application/application' | 5 | import { getServerActor } from '@server/models/application/application' |
5 | import { MActorFollowActorsDefault } from '@server/types/models' | 6 | import { MActorFollowActorsDefault } from '@server/types/models' |
6 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 7 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
@@ -8,11 +9,10 @@ import { isTestInstance } from '../../helpers/core-utils' | |||
8 | import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' | 9 | import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' |
9 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' | 10 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' |
10 | import { logger } from '../../helpers/logger' | 11 | import { logger } from '../../helpers/logger' |
11 | import { loadActorUrlOrGetFromWebfinger } from '../../helpers/webfinger' | ||
12 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' | 12 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' |
13 | import { ActorModel } from '../../models/actor/actor' | 13 | import { ActorModel } from '../../models/actor/actor' |
14 | import { ActorFollowModel } from '../../models/actor/actor-follow' | 14 | import { ActorFollowModel } from '../../models/actor/actor-follow' |
15 | import { areValidationErrors } from './utils' | 15 | import { areValidationErrors } from './shared' |
16 | 16 | ||
17 | const listFollowsValidator = [ | 17 | const listFollowsValidator = [ |
18 | query('state') | 18 | query('state') |
diff --git a/server/middlewares/validators/jobs.ts b/server/middlewares/validators/jobs.ts index d87b28c06..5d89d167f 100644 --- a/server/middlewares/validators/jobs.ts +++ b/server/middlewares/validators/jobs.ts | |||
@@ -2,7 +2,7 @@ import * as express from 'express' | |||
2 | import { param, query } from 'express-validator' | 2 | import { param, query } from 'express-validator' |
3 | import { isValidJobState, isValidJobType } from '../../helpers/custom-validators/jobs' | 3 | import { isValidJobState, isValidJobType } from '../../helpers/custom-validators/jobs' |
4 | import { logger, loggerTagsFactory } from '../../helpers/logger' | 4 | import { logger, loggerTagsFactory } from '../../helpers/logger' |
5 | import { areValidationErrors } from './utils' | 5 | import { areValidationErrors } from './shared' |
6 | 6 | ||
7 | const lTags = loggerTagsFactory('validators', 'jobs') | 7 | const lTags = loggerTagsFactory('validators', 'jobs') |
8 | 8 | ||
diff --git a/server/middlewares/validators/logs.ts b/server/middlewares/validators/logs.ts index ba817d9a9..c55baaee3 100644 --- a/server/middlewares/validators/logs.ts +++ b/server/middlewares/validators/logs.ts | |||
@@ -1,9 +1,9 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { logger } from '../../helpers/logger' | ||
3 | import { areValidationErrors } from './utils' | ||
4 | import { isDateValid } from '../../helpers/custom-validators/misc' | ||
5 | import { query } from 'express-validator' | 2 | import { query } from 'express-validator' |
6 | import { isValidLogLevel } from '../../helpers/custom-validators/logs' | 3 | import { isValidLogLevel } from '../../helpers/custom-validators/logs' |
4 | import { isDateValid } from '../../helpers/custom-validators/misc' | ||
5 | import { logger } from '../../helpers/logger' | ||
6 | import { areValidationErrors } from './shared' | ||
7 | 7 | ||
8 | const getLogsValidator = [ | 8 | const getLogsValidator = [ |
9 | query('startDate') | 9 | query('startDate') |
diff --git a/server/middlewares/validators/oembed.ts b/server/middlewares/validators/oembed.ts index b1d763fbe..ab117635e 100644 --- a/server/middlewares/validators/oembed.ts +++ b/server/middlewares/validators/oembed.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { query } from 'express-validator' | 2 | import { query } from 'express-validator' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { fetchVideo } from '@server/helpers/video' | 4 | import { fetchVideo } from '@server/lib/model-loaders' |
5 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' | 5 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' |
6 | import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' | 6 | import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' |
7 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 7 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
@@ -9,7 +9,7 @@ import { isTestInstance } from '../../helpers/core-utils' | |||
9 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' | 9 | import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' |
10 | import { logger } from '../../helpers/logger' | 10 | import { logger } from '../../helpers/logger' |
11 | import { WEBSERVER } from '../../initializers/constants' | 11 | import { WEBSERVER } from '../../initializers/constants' |
12 | import { areValidationErrors } from './utils' | 12 | import { areValidationErrors } from './shared' |
13 | 13 | ||
14 | const playlistPaths = [ | 14 | const playlistPaths = [ |
15 | join('videos', 'watch', 'playlist'), | 15 | join('videos', 'watch', 'playlist'), |
diff --git a/server/middlewares/validators/pagination.ts b/server/middlewares/validators/pagination.ts index 6b0a83d80..74eae251e 100644 --- a/server/middlewares/validators/pagination.ts +++ b/server/middlewares/validators/pagination.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { query } from 'express-validator' | 2 | import { query } from 'express-validator' |
3 | import { logger } from '../../helpers/logger' | ||
4 | import { areValidationErrors } from './utils' | ||
5 | import { PAGINATION } from '@server/initializers/constants' | 3 | import { PAGINATION } from '@server/initializers/constants' |
4 | import { logger } from '../../helpers/logger' | ||
5 | import { areValidationErrors } from './shared' | ||
6 | 6 | ||
7 | const paginationValidator = paginationValidatorBuilder() | 7 | const paginationValidator = paginationValidatorBuilder() |
8 | 8 | ||
diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts index 5934a28bc..8c76d2e36 100644 --- a/server/middlewares/validators/plugins.ts +++ b/server/middlewares/validators/plugins.ts | |||
@@ -9,7 +9,7 @@ import { logger } from '../../helpers/logger' | |||
9 | import { CONFIG } from '../../initializers/config' | 9 | import { CONFIG } from '../../initializers/config' |
10 | import { PluginManager } from '../../lib/plugins/plugin-manager' | 10 | import { PluginManager } from '../../lib/plugins/plugin-manager' |
11 | import { PluginModel } from '../../models/server/plugin' | 11 | import { PluginModel } from '../../models/server/plugin' |
12 | import { areValidationErrors } from './utils' | 12 | import { areValidationErrors } from './shared' |
13 | 13 | ||
14 | const getPluginValidator = (pluginType: PluginType, withVersion = true) => { | 14 | const getPluginValidator = (pluginType: PluginType, withVersion = true) => { |
15 | const validators: (ValidationChain | express.Handler)[] = [ | 15 | const validators: (ValidationChain | express.Handler)[] = [ |
diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts index 3d557048a..da24f4c9b 100644 --- a/server/middlewares/validators/redundancy.ts +++ b/server/middlewares/validators/redundancy.ts | |||
@@ -1,14 +1,13 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param, query } from 'express-validator' | 2 | import { body, param, query } from 'express-validator' |
3 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' | ||
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
3 | import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' | 5 | import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' |
6 | import { isHostValid } from '../../helpers/custom-validators/servers' | ||
4 | import { logger } from '../../helpers/logger' | 7 | import { logger } from '../../helpers/logger' |
5 | import { areValidationErrors } from './utils' | ||
6 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' | 8 | import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' |
7 | import { isHostValid } from '../../helpers/custom-validators/servers' | ||
8 | import { ServerModel } from '../../models/server/server' | 9 | import { ServerModel } from '../../models/server/server' |
9 | import { doesVideoExist } from '../../helpers/middlewares' | 10 | import { areValidationErrors, doesVideoExist } from './shared' |
10 | import { isVideoRedundancyTarget } from '@server/helpers/custom-validators/video-redundancies' | ||
11 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
12 | 11 | ||
13 | const videoFileRedundancyGetValidator = [ | 12 | const videoFileRedundancyGetValidator = [ |
14 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), | 13 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), |
diff --git a/server/middlewares/validators/search.ts b/server/middlewares/validators/search.ts index d2f527750..e2e1c6aae 100644 --- a/server/middlewares/validators/search.ts +++ b/server/middlewares/validators/search.ts | |||
@@ -1,9 +1,9 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { areValidationErrors } from './utils' | ||
3 | import { logger } from '../../helpers/logger' | ||
4 | import { query } from 'express-validator' | 2 | import { query } from 'express-validator' |
5 | import { isDateValid } from '../../helpers/custom-validators/misc' | ||
6 | import { isSearchTargetValid } from '@server/helpers/custom-validators/search' | 3 | import { isSearchTargetValid } from '@server/helpers/custom-validators/search' |
4 | import { isDateValid } from '../../helpers/custom-validators/misc' | ||
5 | import { logger } from '../../helpers/logger' | ||
6 | import { areValidationErrors } from './shared' | ||
7 | 7 | ||
8 | const videosSearchValidator = [ | 8 | const videosSearchValidator = [ |
9 | query('search').optional().not().isEmpty().withMessage('Should have a valid search'), | 9 | query('search').optional().not().isEmpty().withMessage('Should have a valid search'), |
diff --git a/server/middlewares/validators/server.ts b/server/middlewares/validators/server.ts index 2b34c4a76..fc7239b25 100644 --- a/server/middlewares/validators/server.ts +++ b/server/middlewares/validators/server.ts | |||
@@ -1,13 +1,13 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { logger } from '../../helpers/logger' | ||
3 | import { areValidationErrors } from './utils' | ||
4 | import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' | ||
5 | import { ServerModel } from '../../models/server/server' | ||
6 | import { body } from 'express-validator' | 2 | import { body } from 'express-validator' |
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' | ||
7 | import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' | 5 | import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' |
8 | import { Redis } from '../../lib/redis' | 6 | import { logger } from '../../helpers/logger' |
9 | import { CONFIG, isEmailEnabled } from '../../initializers/config' | 7 | import { CONFIG, isEmailEnabled } from '../../initializers/config' |
10 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 8 | import { Redis } from '../../lib/redis' |
9 | import { ServerModel } from '../../models/server/server' | ||
10 | import { areValidationErrors } from './shared' | ||
11 | 11 | ||
12 | const serverGetValidator = [ | 12 | const serverGetValidator = [ |
13 | body('host').custom(isHostValid).withMessage('Should have a valid host'), | 13 | body('host').custom(isHostValid).withMessage('Should have a valid host'), |
diff --git a/server/middlewares/validators/shared/abuses.ts b/server/middlewares/validators/shared/abuses.ts new file mode 100644 index 000000000..4a20a55fa --- /dev/null +++ b/server/middlewares/validators/shared/abuses.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { AbuseModel } from '@server/models/abuse/abuse' | ||
3 | import { HttpStatusCode } from '@shared/core-utils' | ||
4 | |||
5 | async function doesAbuseExist (abuseId: number | string, res: Response) { | ||
6 | const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10)) | ||
7 | |||
8 | if (!abuse) { | ||
9 | res.fail({ | ||
10 | status: HttpStatusCode.NOT_FOUND_404, | ||
11 | message: 'Abuse not found' | ||
12 | }) | ||
13 | |||
14 | return false | ||
15 | } | ||
16 | |||
17 | res.locals.abuse = abuse | ||
18 | return true | ||
19 | } | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | export { | ||
24 | doesAbuseExist | ||
25 | } | ||
diff --git a/server/middlewares/validators/shared/accounts.ts b/server/middlewares/validators/shared/accounts.ts new file mode 100644 index 000000000..04da15441 --- /dev/null +++ b/server/middlewares/validators/shared/accounts.ts | |||
@@ -0,0 +1,65 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { AccountModel } from '@server/models/account/account' | ||
3 | import { UserModel } from '@server/models/user/user' | ||
4 | import { MAccountDefault } from '@server/types/models' | ||
5 | import { HttpStatusCode } from '@shared/core-utils' | ||
6 | |||
7 | function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { | ||
8 | const promise = AccountModel.load(parseInt(id + '', 10)) | ||
9 | |||
10 | return doesAccountExist(promise, res, sendNotFound) | ||
11 | } | ||
12 | |||
13 | function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { | ||
14 | const promise = AccountModel.loadLocalByName(name) | ||
15 | |||
16 | return doesAccountExist(promise, res, sendNotFound) | ||
17 | } | ||
18 | |||
19 | function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { | ||
20 | const promise = AccountModel.loadByNameWithHost(nameWithDomain) | ||
21 | |||
22 | return doesAccountExist(promise, res, sendNotFound) | ||
23 | } | ||
24 | |||
25 | async function doesAccountExist (p: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) { | ||
26 | const account = await p | ||
27 | |||
28 | if (!account) { | ||
29 | if (sendNotFound === true) { | ||
30 | res.fail({ | ||
31 | status: HttpStatusCode.NOT_FOUND_404, | ||
32 | message: 'Account not found' | ||
33 | }) | ||
34 | } | ||
35 | return false | ||
36 | } | ||
37 | |||
38 | res.locals.account = account | ||
39 | return true | ||
40 | } | ||
41 | |||
42 | async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) { | ||
43 | const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) | ||
44 | |||
45 | if (token !== user.feedToken) { | ||
46 | res.fail({ | ||
47 | status: HttpStatusCode.FORBIDDEN_403, | ||
48 | message: 'User and token mismatch' | ||
49 | }) | ||
50 | return false | ||
51 | } | ||
52 | |||
53 | res.locals.user = user | ||
54 | return true | ||
55 | } | ||
56 | |||
57 | // --------------------------------------------------------------------------- | ||
58 | |||
59 | export { | ||
60 | doesAccountIdExist, | ||
61 | doesLocalAccountNameExist, | ||
62 | doesAccountNameWithHostExist, | ||
63 | doesAccountExist, | ||
64 | doesUserFeedTokenCorrespond | ||
65 | } | ||
diff --git a/server/middlewares/validators/shared/index.ts b/server/middlewares/validators/shared/index.ts new file mode 100644 index 000000000..fa89d05f2 --- /dev/null +++ b/server/middlewares/validators/shared/index.ts | |||
@@ -0,0 +1,11 @@ | |||
1 | export * from './abuses' | ||
2 | export * from './accounts' | ||
3 | export * from './utils' | ||
4 | export * from './video-blacklists' | ||
5 | export * from './video-captions' | ||
6 | export * from './video-channels' | ||
7 | export * from './video-comments' | ||
8 | export * from './video-imports' | ||
9 | export * from './video-ownerships' | ||
10 | export * from './video-playlists' | ||
11 | export * from './videos' | ||
diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/shared/utils.ts index e291f1b17..d3e4870a9 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/shared/utils.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { query, validationResult } from 'express-validator' | 2 | import { query, validationResult } from 'express-validator' |
3 | import { logger } from '../../helpers/logger' | 3 | import { logger } from '../../../helpers/logger' |
4 | 4 | ||
5 | function areValidationErrors (req: express.Request, res: express.Response) { | 5 | function areValidationErrors (req: express.Request, res: express.Response) { |
6 | const errors = validationResult(req) | 6 | const errors = validationResult(req) |
diff --git a/server/middlewares/validators/shared/video-blacklists.ts b/server/middlewares/validators/shared/video-blacklists.ts new file mode 100644 index 000000000..01491c10f --- /dev/null +++ b/server/middlewares/validators/shared/video-blacklists.ts | |||
@@ -0,0 +1,24 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { VideoBlacklistModel } from '@server/models/video/video-blacklist' | ||
3 | import { HttpStatusCode } from '@shared/core-utils' | ||
4 | |||
5 | async function doesVideoBlacklistExist (videoId: number, res: Response) { | ||
6 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId) | ||
7 | |||
8 | if (videoBlacklist === null) { | ||
9 | res.fail({ | ||
10 | status: HttpStatusCode.NOT_FOUND_404, | ||
11 | message: 'Blacklisted video not found' | ||
12 | }) | ||
13 | return false | ||
14 | } | ||
15 | |||
16 | res.locals.videoBlacklist = videoBlacklist | ||
17 | return true | ||
18 | } | ||
19 | |||
20 | // --------------------------------------------------------------------------- | ||
21 | |||
22 | export { | ||
23 | doesVideoBlacklistExist | ||
24 | } | ||
diff --git a/server/middlewares/validators/shared/video-captions.ts b/server/middlewares/validators/shared/video-captions.ts new file mode 100644 index 000000000..80f6c5a52 --- /dev/null +++ b/server/middlewares/validators/shared/video-captions.ts | |||
@@ -0,0 +1,25 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { VideoCaptionModel } from '@server/models/video/video-caption' | ||
3 | import { MVideoId } from '@server/types/models' | ||
4 | import { HttpStatusCode } from '@shared/core-utils' | ||
5 | |||
6 | async function doesVideoCaptionExist (video: MVideoId, language: string, res: Response) { | ||
7 | const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) | ||
8 | |||
9 | if (!videoCaption) { | ||
10 | res.fail({ | ||
11 | status: HttpStatusCode.NOT_FOUND_404, | ||
12 | message: 'Video caption not found' | ||
13 | }) | ||
14 | return false | ||
15 | } | ||
16 | |||
17 | res.locals.videoCaption = videoCaption | ||
18 | return true | ||
19 | } | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | export { | ||
24 | doesVideoCaptionExist | ||
25 | } | ||
diff --git a/server/middlewares/validators/shared/video-channels.ts b/server/middlewares/validators/shared/video-channels.ts new file mode 100644 index 000000000..fe2e663b7 --- /dev/null +++ b/server/middlewares/validators/shared/video-channels.ts | |||
@@ -0,0 +1,43 @@ | |||
1 | import * as express from 'express' | ||
2 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
3 | import { MChannelBannerAccountDefault } from '@server/types/models' | ||
4 | import { HttpStatusCode } from '@shared/core-utils' | ||
5 | |||
6 | async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { | ||
7 | const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) | ||
8 | |||
9 | return processVideoChannelExist(videoChannel, res) | ||
10 | } | ||
11 | |||
12 | async function doesVideoChannelIdExist (id: number, res: express.Response) { | ||
13 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) | ||
14 | |||
15 | return processVideoChannelExist(videoChannel, res) | ||
16 | } | ||
17 | |||
18 | async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { | ||
19 | const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain) | ||
20 | |||
21 | return processVideoChannelExist(videoChannel, res) | ||
22 | } | ||
23 | |||
24 | // --------------------------------------------------------------------------- | ||
25 | |||
26 | export { | ||
27 | doesLocalVideoChannelNameExist, | ||
28 | doesVideoChannelIdExist, | ||
29 | doesVideoChannelNameWithHostExist | ||
30 | } | ||
31 | |||
32 | function processVideoChannelExist (videoChannel: MChannelBannerAccountDefault, res: express.Response) { | ||
33 | if (!videoChannel) { | ||
34 | res.fail({ | ||
35 | status: HttpStatusCode.NOT_FOUND_404, | ||
36 | message: 'Video channel not found' | ||
37 | }) | ||
38 | return false | ||
39 | } | ||
40 | |||
41 | res.locals.videoChannel = videoChannel | ||
42 | return true | ||
43 | } | ||
diff --git a/server/middlewares/validators/shared/video-comments.ts b/server/middlewares/validators/shared/video-comments.ts new file mode 100644 index 000000000..83ea15c98 --- /dev/null +++ b/server/middlewares/validators/shared/video-comments.ts | |||
@@ -0,0 +1,73 @@ | |||
1 | import * as express from 'express' | ||
2 | import { VideoCommentModel } from '@server/models/video/video-comment' | ||
3 | import { MVideoId } from '@server/types/models' | ||
4 | import { HttpStatusCode } from '@shared/core-utils' | ||
5 | |||
6 | async function doesVideoCommentThreadExist (idArg: number | string, video: MVideoId, res: express.Response) { | ||
7 | const id = parseInt(idArg + '', 10) | ||
8 | const videoComment = await VideoCommentModel.loadById(id) | ||
9 | |||
10 | if (!videoComment) { | ||
11 | res.fail({ | ||
12 | status: HttpStatusCode.NOT_FOUND_404, | ||
13 | message: 'Video comment thread not found' | ||
14 | }) | ||
15 | return false | ||
16 | } | ||
17 | |||
18 | if (videoComment.videoId !== video.id) { | ||
19 | res.fail({ message: 'Video comment is not associated to this video.' }) | ||
20 | return false | ||
21 | } | ||
22 | |||
23 | if (videoComment.inReplyToCommentId !== null) { | ||
24 | res.fail({ message: 'Video comment is not a thread.' }) | ||
25 | return false | ||
26 | } | ||
27 | |||
28 | res.locals.videoCommentThread = videoComment | ||
29 | return true | ||
30 | } | ||
31 | |||
32 | async function doesVideoCommentExist (idArg: number | string, video: MVideoId, res: express.Response) { | ||
33 | const id = parseInt(idArg + '', 10) | ||
34 | const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) | ||
35 | |||
36 | if (!videoComment) { | ||
37 | res.fail({ | ||
38 | status: HttpStatusCode.NOT_FOUND_404, | ||
39 | message: 'Video comment thread not found' | ||
40 | }) | ||
41 | return false | ||
42 | } | ||
43 | |||
44 | if (videoComment.videoId !== video.id) { | ||
45 | res.fail({ message: 'Video comment is not associated to this video.' }) | ||
46 | return false | ||
47 | } | ||
48 | |||
49 | res.locals.videoCommentFull = videoComment | ||
50 | return true | ||
51 | } | ||
52 | |||
53 | async function doesCommentIdExist (idArg: number | string, res: express.Response) { | ||
54 | const id = parseInt(idArg + '', 10) | ||
55 | const videoComment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(id) | ||
56 | |||
57 | if (!videoComment) { | ||
58 | res.fail({ | ||
59 | status: HttpStatusCode.NOT_FOUND_404, | ||
60 | message: 'Video comment thread not found' | ||
61 | }) | ||
62 | return false | ||
63 | } | ||
64 | |||
65 | res.locals.videoCommentFull = videoComment | ||
66 | return true | ||
67 | } | ||
68 | |||
69 | export { | ||
70 | doesVideoCommentThreadExist, | ||
71 | doesVideoCommentExist, | ||
72 | doesCommentIdExist | ||
73 | } | ||
diff --git a/server/middlewares/validators/shared/video-imports.ts b/server/middlewares/validators/shared/video-imports.ts new file mode 100644 index 000000000..0f984bc17 --- /dev/null +++ b/server/middlewares/validators/shared/video-imports.ts | |||
@@ -0,0 +1,22 @@ | |||
1 | import * as express from 'express' | ||
2 | import { VideoImportModel } from '@server/models/video/video-import' | ||
3 | import { HttpStatusCode } from '@shared/core-utils' | ||
4 | |||
5 | async function doesVideoImportExist (id: number, res: express.Response) { | ||
6 | const videoImport = await VideoImportModel.loadAndPopulateVideo(id) | ||
7 | |||
8 | if (!videoImport) { | ||
9 | res.fail({ | ||
10 | status: HttpStatusCode.NOT_FOUND_404, | ||
11 | message: 'Video import not found' | ||
12 | }) | ||
13 | return false | ||
14 | } | ||
15 | |||
16 | res.locals.videoImport = videoImport | ||
17 | return true | ||
18 | } | ||
19 | |||
20 | export { | ||
21 | doesVideoImportExist | ||
22 | } | ||
diff --git a/server/middlewares/validators/shared/video-ownerships.ts b/server/middlewares/validators/shared/video-ownerships.ts new file mode 100644 index 000000000..fc27006ce --- /dev/null +++ b/server/middlewares/validators/shared/video-ownerships.ts | |||
@@ -0,0 +1,24 @@ | |||
1 | import * as express from 'express' | ||
2 | import { VideoChangeOwnershipModel } from '@server/models/video/video-change-ownership' | ||
3 | import { HttpStatusCode } from '@shared/core-utils' | ||
4 | |||
5 | async function doesChangeVideoOwnershipExist (idArg: number | string, res: express.Response) { | ||
6 | const id = parseInt(idArg + '', 10) | ||
7 | const videoChangeOwnership = await VideoChangeOwnershipModel.load(id) | ||
8 | |||
9 | if (!videoChangeOwnership) { | ||
10 | res.fail({ | ||
11 | status: HttpStatusCode.NOT_FOUND_404, | ||
12 | message: 'Video change ownership not found' | ||
13 | }) | ||
14 | return false | ||
15 | } | ||
16 | |||
17 | res.locals.videoChangeOwnership = videoChangeOwnership | ||
18 | |||
19 | return true | ||
20 | } | ||
21 | |||
22 | export { | ||
23 | doesChangeVideoOwnershipExist | ||
24 | } | ||
diff --git a/server/middlewares/validators/shared/video-playlists.ts b/server/middlewares/validators/shared/video-playlists.ts new file mode 100644 index 000000000..d762859a8 --- /dev/null +++ b/server/middlewares/validators/shared/video-playlists.ts | |||
@@ -0,0 +1,39 @@ | |||
1 | import * as express from 'express' | ||
2 | import { VideoPlaylistModel } from '@server/models/video/video-playlist' | ||
3 | import { MVideoPlaylist } from '@server/types/models' | ||
4 | import { HttpStatusCode } from '@shared/core-utils' | ||
5 | |||
6 | export type VideoPlaylistFetchType = 'summary' | 'all' | ||
7 | async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: VideoPlaylistFetchType = 'summary') { | ||
8 | if (fetchType === 'summary') { | ||
9 | const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined) | ||
10 | res.locals.videoPlaylistSummary = videoPlaylist | ||
11 | |||
12 | return handleVideoPlaylist(videoPlaylist, res) | ||
13 | } | ||
14 | |||
15 | const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined) | ||
16 | res.locals.videoPlaylistFull = videoPlaylist | ||
17 | |||
18 | return handleVideoPlaylist(videoPlaylist, res) | ||
19 | } | ||
20 | |||
21 | // --------------------------------------------------------------------------- | ||
22 | |||
23 | export { | ||
24 | doesVideoPlaylistExist | ||
25 | } | ||
26 | |||
27 | // --------------------------------------------------------------------------- | ||
28 | |||
29 | function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { | ||
30 | if (!videoPlaylist) { | ||
31 | res.fail({ | ||
32 | status: HttpStatusCode.NOT_FOUND_404, | ||
33 | message: 'Video playlist not found' | ||
34 | }) | ||
35 | return false | ||
36 | } | ||
37 | |||
38 | return true | ||
39 | } | ||
diff --git a/server/middlewares/validators/shared/videos.ts b/server/middlewares/validators/shared/videos.ts new file mode 100644 index 000000000..a6dad4374 --- /dev/null +++ b/server/middlewares/validators/shared/videos.ts | |||
@@ -0,0 +1,125 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { fetchVideo, VideoFetchType } from '@server/lib/model-loaders' | ||
3 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
4 | import { VideoFileModel } from '@server/models/video/video-file' | ||
5 | import { | ||
6 | MUser, | ||
7 | MUserAccountId, | ||
8 | MVideoAccountLight, | ||
9 | MVideoFullLight, | ||
10 | MVideoIdThumbnail, | ||
11 | MVideoImmutable, | ||
12 | MVideoThumbnail, | ||
13 | MVideoWithRights | ||
14 | } from '@server/types/models' | ||
15 | import { HttpStatusCode } from '@shared/core-utils' | ||
16 | import { UserRight } from '@shared/models' | ||
17 | |||
18 | async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { | ||
19 | const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined | ||
20 | |||
21 | const video = await fetchVideo(id, fetchType, userId) | ||
22 | |||
23 | if (video === null) { | ||
24 | res.fail({ | ||
25 | status: HttpStatusCode.NOT_FOUND_404, | ||
26 | message: 'Video not found' | ||
27 | }) | ||
28 | return false | ||
29 | } | ||
30 | |||
31 | switch (fetchType) { | ||
32 | case 'all': | ||
33 | res.locals.videoAll = video as MVideoFullLight | ||
34 | break | ||
35 | |||
36 | case 'only-immutable-attributes': | ||
37 | res.locals.onlyImmutableVideo = video as MVideoImmutable | ||
38 | break | ||
39 | |||
40 | case 'id': | ||
41 | res.locals.videoId = video as MVideoIdThumbnail | ||
42 | break | ||
43 | |||
44 | case 'only-video': | ||
45 | res.locals.onlyVideo = video as MVideoThumbnail | ||
46 | break | ||
47 | |||
48 | case 'only-video-with-rights': | ||
49 | res.locals.onlyVideoWithRights = video as MVideoWithRights | ||
50 | break | ||
51 | } | ||
52 | |||
53 | return true | ||
54 | } | ||
55 | |||
56 | async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) { | ||
57 | if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) { | ||
58 | res.fail({ | ||
59 | status: HttpStatusCode.NOT_FOUND_404, | ||
60 | message: 'VideoFile matching Video not found' | ||
61 | }) | ||
62 | return false | ||
63 | } | ||
64 | |||
65 | return true | ||
66 | } | ||
67 | |||
68 | async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) { | ||
69 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) | ||
70 | |||
71 | if (videoChannel === null) { | ||
72 | res.fail({ message: 'Unknown video "video channel" for this instance.' }) | ||
73 | return false | ||
74 | } | ||
75 | |||
76 | // Don't check account id if the user can update any video | ||
77 | if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { | ||
78 | res.locals.videoChannel = videoChannel | ||
79 | return true | ||
80 | } | ||
81 | |||
82 | if (videoChannel.Account.id !== user.Account.id) { | ||
83 | res.fail({ | ||
84 | message: 'Unknown video "video channel" for this account.' | ||
85 | }) | ||
86 | return false | ||
87 | } | ||
88 | |||
89 | res.locals.videoChannel = videoChannel | ||
90 | return true | ||
91 | } | ||
92 | |||
93 | function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) { | ||
94 | // Retrieve the user who did the request | ||
95 | if (onlyOwned && video.isOwned() === false) { | ||
96 | res.fail({ | ||
97 | status: HttpStatusCode.FORBIDDEN_403, | ||
98 | message: 'Cannot manage a video of another server.' | ||
99 | }) | ||
100 | return false | ||
101 | } | ||
102 | |||
103 | // Check if the user can delete the video | ||
104 | // The user can delete it if he has the right | ||
105 | // Or if s/he is the video's account | ||
106 | const account = video.VideoChannel.Account | ||
107 | if (user.hasRight(right) === false && account.userId !== user.id) { | ||
108 | res.fail({ | ||
109 | status: HttpStatusCode.FORBIDDEN_403, | ||
110 | message: 'Cannot manage a video of another user.' | ||
111 | }) | ||
112 | return false | ||
113 | } | ||
114 | |||
115 | return true | ||
116 | } | ||
117 | |||
118 | // --------------------------------------------------------------------------- | ||
119 | |||
120 | export { | ||
121 | doesVideoChannelOfAccountExist, | ||
122 | doesVideoExist, | ||
123 | doesVideoFileOfVideoExist, | ||
124 | checkUserCanManageVideo | ||
125 | } | ||
diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index beecc155b..d67b6f3ba 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { SORTABLE_COLUMNS } from '../../initializers/constants' | 1 | import { SORTABLE_COLUMNS } from '../../initializers/constants' |
2 | import { checkSort, createSortableColumns } from './utils' | 2 | import { checkSort, createSortableColumns } from './shared' |
3 | 3 | ||
4 | // Initialize constants here for better performances | 4 | // Initialize constants here for better performances |
5 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) | 5 | const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) |
diff --git a/server/middlewares/validators/themes.ts b/server/middlewares/validators/themes.ts index 91ec0d7ac..d4716257f 100644 --- a/server/middlewares/validators/themes.ts +++ b/server/middlewares/validators/themes.ts | |||
@@ -1,11 +1,11 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param } from 'express-validator' | 2 | import { param } from 'express-validator' |
3 | import { logger } from '../../helpers/logger' | 3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
4 | import { areValidationErrors } from './utils' | 4 | import { isSafePath } from '../../helpers/custom-validators/misc' |
5 | import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' | 5 | import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' |
6 | import { logger } from '../../helpers/logger' | ||
6 | import { PluginManager } from '../../lib/plugins/plugin-manager' | 7 | import { PluginManager } from '../../lib/plugins/plugin-manager' |
7 | import { isSafePath } from '../../helpers/custom-validators/misc' | 8 | import { areValidationErrors } from './shared' |
8 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
9 | 9 | ||
10 | const serveThemeCSSValidator = [ | 10 | const serveThemeCSSValidator = [ |
11 | param('themeName').custom(isPluginNameValid).withMessage('Should have a valid theme name'), | 11 | param('themeName').custom(isPluginNameValid).withMessage('Should have a valid theme name'), |
diff --git a/server/middlewares/validators/user-history.ts b/server/middlewares/validators/user-history.ts index 647294cc3..1db0d9b26 100644 --- a/server/middlewares/validators/user-history.ts +++ b/server/middlewares/validators/user-history.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, query } from 'express-validator' | 2 | import { body, query } from 'express-validator' |
3 | import { logger } from '../../helpers/logger' | ||
4 | import { areValidationErrors } from './utils' | ||
5 | import { exists, isDateValid } from '../../helpers/custom-validators/misc' | 3 | import { exists, isDateValid } from '../../helpers/custom-validators/misc' |
4 | import { logger } from '../../helpers/logger' | ||
5 | import { areValidationErrors } from './shared' | ||
6 | 6 | ||
7 | const userHistoryListValidator = [ | 7 | const userHistoryListValidator = [ |
8 | query('search') | 8 | query('search') |
diff --git a/server/middlewares/validators/user-notifications.ts b/server/middlewares/validators/user-notifications.ts index 21a7be08d..2f8e7686e 100644 --- a/server/middlewares/validators/user-notifications.ts +++ b/server/middlewares/validators/user-notifications.ts | |||
@@ -1,9 +1,9 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, query } from 'express-validator' | 2 | import { body, query } from 'express-validator' |
3 | import { logger } from '../../helpers/logger' | ||
4 | import { areValidationErrors } from './utils' | ||
5 | import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications' | ||
6 | import { isNotEmptyIntArray, toBooleanOrNull } from '../../helpers/custom-validators/misc' | 3 | import { isNotEmptyIntArray, toBooleanOrNull } from '../../helpers/custom-validators/misc' |
4 | import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications' | ||
5 | import { logger } from '../../helpers/logger' | ||
6 | import { areValidationErrors } from './shared' | ||
7 | 7 | ||
8 | const listUserNotificationsValidator = [ | 8 | const listUserNotificationsValidator = [ |
9 | query('unread') | 9 | query('unread') |
diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts index 5f928b05b..ab7962923 100644 --- a/server/middlewares/validators/user-subscriptions.ts +++ b/server/middlewares/validators/user-subscriptions.ts | |||
@@ -6,7 +6,7 @@ import { toArray } from '../../helpers/custom-validators/misc' | |||
6 | import { logger } from '../../helpers/logger' | 6 | import { logger } from '../../helpers/logger' |
7 | import { WEBSERVER } from '../../initializers/constants' | 7 | import { WEBSERVER } from '../../initializers/constants' |
8 | import { ActorFollowModel } from '../../models/actor/actor-follow' | 8 | import { ActorFollowModel } from '../../models/actor/actor-follow' |
9 | import { areValidationErrors } from './utils' | 9 | import { areValidationErrors } from './shared' |
10 | 10 | ||
11 | const userSubscriptionListValidator = [ | 11 | const userSubscriptionListValidator = [ |
12 | query('search').optional().not().isEmpty().withMessage('Should have a valid search'), | 12 | query('search').optional().not().isEmpty().withMessage('Should have a valid search'), |
diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 0eb9172c4..218633b8d 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts | |||
@@ -30,13 +30,12 @@ import { | |||
30 | } from '../../helpers/custom-validators/users' | 30 | } from '../../helpers/custom-validators/users' |
31 | import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' | 31 | import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' |
32 | import { logger } from '../../helpers/logger' | 32 | import { logger } from '../../helpers/logger' |
33 | import { doesVideoExist } from '../../helpers/middlewares' | ||
34 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup' | ||
35 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' | 33 | import { isThemeRegistered } from '../../lib/plugins/theme-utils' |
36 | import { Redis } from '../../lib/redis' | 34 | import { Redis } from '../../lib/redis' |
37 | import { UserModel } from '../../models/user/user' | 35 | import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../lib/signup' |
38 | import { ActorModel } from '../../models/actor/actor' | 36 | import { ActorModel } from '../../models/actor/actor' |
39 | import { areValidationErrors } from './utils' | 37 | import { UserModel } from '../../models/user/user' |
38 | import { areValidationErrors, doesVideoExist } from './shared' | ||
40 | 39 | ||
41 | const usersListValidator = [ | 40 | const usersListValidator = [ |
42 | query('blocked') | 41 | query('blocked') |
diff --git a/server/middlewares/validators/videos/video-blacklist.ts b/server/middlewares/validators/videos/video-blacklist.ts index 65132a09f..7374ba774 100644 --- a/server/middlewares/validators/videos/video-blacklist.ts +++ b/server/middlewares/validators/videos/video-blacklist.ts | |||
@@ -1,11 +1,10 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param, query } from 'express-validator' | 2 | import { body, param, query } from 'express-validator' |
3 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
3 | import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' | 4 | import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' |
4 | import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist' | 5 | import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist' |
5 | import { logger } from '../../../helpers/logger' | 6 | import { logger } from '../../../helpers/logger' |
6 | import { doesVideoBlacklistExist, doesVideoExist } from '../../../helpers/middlewares' | 7 | import { areValidationErrors, doesVideoBlacklistExist, doesVideoExist } from '../shared' |
7 | import { areValidationErrors } from '../utils' | ||
8 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
9 | 8 | ||
10 | const videosBlacklistRemoveValidator = [ | 9 | const videosBlacklistRemoveValidator = [ |
11 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | 10 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), |
diff --git a/server/middlewares/validators/videos/video-captions.ts b/server/middlewares/validators/videos/video-captions.ts index 872d9c2ab..2295e049a 100644 --- a/server/middlewares/validators/videos/video-captions.ts +++ b/server/middlewares/validators/videos/video-captions.ts | |||
@@ -1,13 +1,12 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { areValidationErrors } from '../utils' | ||
3 | import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' | ||
4 | import { body, param } from 'express-validator' | 2 | import { body, param } from 'express-validator' |
5 | import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../../initializers/constants' | ||
6 | import { UserRight } from '../../../../shared' | 3 | import { UserRight } from '../../../../shared' |
7 | import { logger } from '../../../helpers/logger' | 4 | import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' |
8 | import { isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions' | 5 | import { isVideoCaptionFile, isVideoCaptionLanguageValid } from '../../../helpers/custom-validators/video-captions' |
9 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | 6 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
10 | import { checkUserCanManageVideo, doesVideoCaptionExist, doesVideoExist } from '../../../helpers/middlewares' | 7 | import { logger } from '../../../helpers/logger' |
8 | import { CONSTRAINTS_FIELDS, MIMETYPES } from '../../../initializers/constants' | ||
9 | import { areValidationErrors, checkUserCanManageVideo, doesVideoCaptionExist, doesVideoExist } from '../shared' | ||
11 | 10 | ||
12 | const addVideoCaptionValidator = [ | 11 | const addVideoCaptionValidator = [ |
13 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), | 12 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'), |
diff --git a/server/middlewares/validators/videos/video-channels.ts b/server/middlewares/validators/videos/video-channels.ts index 331a51007..911a25bfb 100644 --- a/server/middlewares/validators/videos/video-channels.ts +++ b/server/middlewares/validators/videos/video-channels.ts | |||
@@ -12,10 +12,9 @@ import { | |||
12 | isVideoChannelSupportValid | 12 | isVideoChannelSupportValid |
13 | } from '../../../helpers/custom-validators/video-channels' | 13 | } from '../../../helpers/custom-validators/video-channels' |
14 | import { logger } from '../../../helpers/logger' | 14 | import { logger } from '../../../helpers/logger' |
15 | import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares' | ||
16 | import { ActorModel } from '../../../models/actor/actor' | 15 | import { ActorModel } from '../../../models/actor/actor' |
17 | import { VideoChannelModel } from '../../../models/video/video-channel' | 16 | import { VideoChannelModel } from '../../../models/video/video-channel' |
18 | import { areValidationErrors } from '../utils' | 17 | import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared' |
19 | 18 | ||
20 | const videoChannelsAddValidator = [ | 19 | const videoChannelsAddValidator = [ |
21 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), | 20 | body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'), |
diff --git a/server/middlewares/validators/videos/video-comments.ts b/server/middlewares/validators/videos/video-comments.ts index aac25a787..1451ab988 100644 --- a/server/middlewares/validators/videos/video-comments.ts +++ b/server/middlewares/validators/videos/video-comments.ts | |||
@@ -2,19 +2,14 @@ import * as express from 'express' | |||
2 | import { body, param, query } from 'express-validator' | 2 | import { body, param, query } from 'express-validator' |
3 | import { MUserAccountUrl } from '@server/types/models' | 3 | import { MUserAccountUrl } from '@server/types/models' |
4 | import { UserRight } from '../../../../shared' | 4 | import { UserRight } from '../../../../shared' |
5 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
5 | import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' | 6 | import { exists, isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' |
6 | import { | 7 | import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments' |
7 | doesVideoCommentExist, | ||
8 | doesVideoCommentThreadExist, | ||
9 | isValidVideoCommentText | ||
10 | } from '../../../helpers/custom-validators/video-comments' | ||
11 | import { logger } from '../../../helpers/logger' | 8 | import { logger } from '../../../helpers/logger' |
12 | import { doesVideoExist } from '../../../helpers/middlewares' | ||
13 | import { AcceptResult, isLocalVideoCommentReplyAccepted, isLocalVideoThreadAccepted } from '../../../lib/moderation' | 9 | import { AcceptResult, isLocalVideoCommentReplyAccepted, isLocalVideoThreadAccepted } from '../../../lib/moderation' |
14 | import { Hooks } from '../../../lib/plugins/hooks' | 10 | import { Hooks } from '../../../lib/plugins/hooks' |
15 | import { MCommentOwnerVideoReply, MVideo, MVideoFullLight } from '../../../types/models/video' | 11 | import { MCommentOwnerVideoReply, MVideo, MVideoFullLight } from '../../../types/models/video' |
16 | import { areValidationErrors } from '../utils' | 12 | import { areValidationErrors, doesVideoCommentExist, doesVideoCommentThreadExist, doesVideoExist } from '../shared' |
17 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
18 | 13 | ||
19 | const listVideoCommentsValidator = [ | 14 | const listVideoCommentsValidator = [ |
20 | query('isLocal') | 15 | query('isLocal') |
diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts index 55ff09124..85dc647ce 100644 --- a/server/middlewares/validators/videos/video-imports.ts +++ b/server/middlewares/validators/videos/video-imports.ts | |||
@@ -2,18 +2,17 @@ import * as express from 'express' | |||
2 | import { body } from 'express-validator' | 2 | import { body } from 'express-validator' |
3 | import { isPreImportVideoAccepted } from '@server/lib/moderation' | 3 | import { isPreImportVideoAccepted } from '@server/lib/moderation' |
4 | import { Hooks } from '@server/lib/plugins/hooks' | 4 | import { Hooks } from '@server/lib/plugins/hooks' |
5 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
5 | import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' | 6 | import { VideoImportCreate } from '@shared/models/videos/import/video-import-create.model' |
6 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' | 7 | import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' |
7 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' | 8 | import { isVideoImportTargetUrlValid, isVideoImportTorrentFile } from '../../../helpers/custom-validators/video-imports' |
8 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' | 9 | import { isVideoMagnetUriValid, isVideoNameValid } from '../../../helpers/custom-validators/videos' |
9 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | 10 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
10 | import { logger } from '../../../helpers/logger' | 11 | import { logger } from '../../../helpers/logger' |
11 | import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' | ||
12 | import { CONFIG } from '../../../initializers/config' | 12 | import { CONFIG } from '../../../initializers/config' |
13 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | 13 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
14 | import { areValidationErrors } from '../utils' | 14 | import { areValidationErrors, doesVideoChannelOfAccountExist } from '../shared' |
15 | import { getCommonVideoEditAttributes } from './videos' | 15 | import { getCommonVideoEditAttributes } from './videos' |
16 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
17 | 16 | ||
18 | const videoImportAddValidator = getCommonVideoEditAttributes().concat([ | 17 | const videoImportAddValidator = getCommonVideoEditAttributes().concat([ |
19 | body('channelId') | 18 | body('channelId') |
diff --git a/server/middlewares/validators/videos/video-live.ts b/server/middlewares/validators/videos/video-live.ts index 0fb864098..ffc8c47b3 100644 --- a/server/middlewares/validators/videos/video-live.ts +++ b/server/middlewares/validators/videos/video-live.ts | |||
@@ -1,20 +1,19 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param } from 'express-validator' | 2 | import { body, param } from 'express-validator' |
3 | import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos' | 3 | import { CONSTRAINTS_FIELDS } from '@server/initializers/constants' |
4 | import { isLocalLiveVideoAccepted } from '@server/lib/moderation' | ||
5 | import { Hooks } from '@server/lib/plugins/hooks' | ||
6 | import { VideoModel } from '@server/models/video/video' | ||
4 | import { VideoLiveModel } from '@server/models/video/video-live' | 7 | import { VideoLiveModel } from '@server/models/video/video-live' |
8 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
5 | import { ServerErrorCode, UserRight, VideoState } from '@shared/models' | 9 | import { ServerErrorCode, UserRight, VideoState } from '@shared/models' |
6 | import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' | 10 | import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' |
7 | import { isVideoNameValid } from '../../../helpers/custom-validators/videos' | 11 | import { isVideoNameValid } from '../../../helpers/custom-validators/videos' |
8 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | 12 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
9 | import { logger } from '../../../helpers/logger' | 13 | import { logger } from '../../../helpers/logger' |
10 | import { CONFIG } from '../../../initializers/config' | 14 | import { CONFIG } from '../../../initializers/config' |
11 | import { areValidationErrors } from '../utils' | 15 | import { areValidationErrors, checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '../shared' |
12 | import { getCommonVideoEditAttributes } from './videos' | 16 | import { getCommonVideoEditAttributes } from './videos' |
13 | import { VideoModel } from '@server/models/video/video' | ||
14 | import { Hooks } from '@server/lib/plugins/hooks' | ||
15 | import { isLocalLiveVideoAccepted } from '@server/lib/moderation' | ||
16 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | ||
17 | import { CONSTRAINTS_FIELDS } from '@server/initializers/constants' | ||
18 | 17 | ||
19 | const videoLiveGetValidator = [ | 18 | const videoLiveGetValidator = [ |
20 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | 19 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), |
diff --git a/server/middlewares/validators/videos/video-playlists.ts b/server/middlewares/validators/videos/video-playlists.ts index 90815dd3a..0d2e6e90c 100644 --- a/server/middlewares/validators/videos/video-playlists.ts +++ b/server/middlewares/validators/videos/video-playlists.ts | |||
@@ -25,12 +25,11 @@ import { | |||
25 | import { isVideoImage } from '../../../helpers/custom-validators/videos' | 25 | import { isVideoImage } from '../../../helpers/custom-validators/videos' |
26 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | 26 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
27 | import { logger } from '../../../helpers/logger' | 27 | import { logger } from '../../../helpers/logger' |
28 | import { doesVideoChannelIdExist, doesVideoExist, doesVideoPlaylistExist, VideoPlaylistFetchType } from '../../../helpers/middlewares' | ||
29 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | 28 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
30 | import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element' | 29 | import { VideoPlaylistElementModel } from '../../../models/video/video-playlist-element' |
31 | import { MVideoPlaylist } from '../../../types/models/video/video-playlist' | 30 | import { MVideoPlaylist } from '../../../types/models/video/video-playlist' |
32 | import { authenticatePromiseIfNeeded } from '../../auth' | 31 | import { authenticatePromiseIfNeeded } from '../../auth' |
33 | import { areValidationErrors } from '../utils' | 32 | import { areValidationErrors, doesVideoChannelIdExist, doesVideoExist, doesVideoPlaylistExist, VideoPlaylistFetchType } from '../shared' |
34 | 33 | ||
35 | const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([ | 34 | const videoPlaylistsAddValidator = getCommonPlaylistEditAttributes().concat([ |
36 | body('displayName') | 35 | body('displayName') |
diff --git a/server/middlewares/validators/videos/video-rates.ts b/server/middlewares/validators/videos/video-rates.ts index 5c4176f54..4a802e75e 100644 --- a/server/middlewares/validators/videos/video-rates.ts +++ b/server/middlewares/validators/videos/video-rates.ts | |||
@@ -1,15 +1,14 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param, query } from 'express-validator' | 2 | import { body, param, query } from 'express-validator' |
3 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
4 | import { VideoRateType } from '../../../../shared/models/videos' | ||
5 | import { isAccountNameValid } from '../../../helpers/custom-validators/accounts' | ||
3 | import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' | 6 | import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' |
4 | import { isRatingValid } from '../../../helpers/custom-validators/video-rates' | 7 | import { isRatingValid } from '../../../helpers/custom-validators/video-rates' |
5 | import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos' | 8 | import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos' |
6 | import { logger } from '../../../helpers/logger' | 9 | import { logger } from '../../../helpers/logger' |
7 | import { areValidationErrors } from '../utils' | ||
8 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' | 10 | import { AccountVideoRateModel } from '../../../models/account/account-video-rate' |
9 | import { VideoRateType } from '../../../../shared/models/videos' | 11 | import { areValidationErrors, doesVideoExist } from '../shared' |
10 | import { isAccountNameValid } from '../../../helpers/custom-validators/accounts' | ||
11 | import { doesVideoExist } from '../../../helpers/middlewares' | ||
12 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
13 | 12 | ||
14 | const videoUpdateRateValidator = [ | 13 | const videoUpdateRateValidator = [ |
15 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | 14 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
diff --git a/server/middlewares/validators/videos/video-shares.ts b/server/middlewares/validators/videos/video-shares.ts index f0d8e0c36..cc2f66e94 100644 --- a/server/middlewares/validators/videos/video-shares.ts +++ b/server/middlewares/validators/videos/video-shares.ts | |||
@@ -1,11 +1,10 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param } from 'express-validator' | 2 | import { param } from 'express-validator' |
3 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
3 | import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' | 4 | import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' |
4 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
5 | import { VideoShareModel } from '../../../models/video/video-share' | 6 | import { VideoShareModel } from '../../../models/video/video-share' |
6 | import { areValidationErrors } from '../utils' | 7 | import { areValidationErrors, doesVideoExist } from '../shared' |
7 | import { doesVideoExist } from '../../../helpers/middlewares' | ||
8 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
9 | 8 | ||
10 | const videosShareValidator = [ | 9 | const videosShareValidator = [ |
11 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | 10 | param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
diff --git a/server/middlewares/validators/videos/video-watch.ts b/server/middlewares/validators/videos/video-watch.ts index 00c739d31..ef8b89ece 100644 --- a/server/middlewares/validators/videos/video-watch.ts +++ b/server/middlewares/validators/videos/video-watch.ts | |||
@@ -1,10 +1,9 @@ | |||
1 | import { body, param } from 'express-validator' | ||
2 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { body, param } from 'express-validator' | ||
3 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
3 | import { isIdOrUUIDValid, toIntOrNull } from '../../../helpers/custom-validators/misc' | 4 | import { isIdOrUUIDValid, toIntOrNull } from '../../../helpers/custom-validators/misc' |
4 | import { areValidationErrors } from '../utils' | ||
5 | import { logger } from '../../../helpers/logger' | 5 | import { logger } from '../../../helpers/logger' |
6 | import { doesVideoExist } from '../../../helpers/middlewares' | 6 | import { areValidationErrors, doesVideoExist } from '../shared' |
7 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | ||
8 | 7 | ||
9 | const videoWatchingValidator = [ | 8 | const videoWatchingValidator = [ |
10 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), | 9 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), |
diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index b7a9bcbe3..52e6c5762 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts | |||
@@ -22,7 +22,7 @@ import { | |||
22 | toValueOrNull | 22 | toValueOrNull |
23 | } from '../../../helpers/custom-validators/misc' | 23 | } from '../../../helpers/custom-validators/misc' |
24 | import { isBooleanBothQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search' | 24 | import { isBooleanBothQueryValid, isNumberArray, isStringArray } from '../../../helpers/custom-validators/search' |
25 | import { checkUserCanTerminateOwnershipChange, doesChangeVideoOwnershipExist } from '../../../helpers/custom-validators/video-ownership' | 25 | import { checkUserCanTerminateOwnershipChange } from '../../../helpers/custom-validators/video-ownership' |
26 | import { | 26 | import { |
27 | isScheduleVideoUpdatePrivacyValid, | 27 | isScheduleVideoUpdatePrivacyValid, |
28 | isVideoCategoryValid, | 28 | isVideoCategoryValid, |
@@ -42,12 +42,6 @@ import { | |||
42 | import { cleanUpReqFiles } from '../../../helpers/express-utils' | 42 | import { cleanUpReqFiles } from '../../../helpers/express-utils' |
43 | import { getDurationFromVideoFile } from '../../../helpers/ffprobe-utils' | 43 | import { getDurationFromVideoFile } from '../../../helpers/ffprobe-utils' |
44 | import { logger } from '../../../helpers/logger' | 44 | import { logger } from '../../../helpers/logger' |
45 | import { | ||
46 | checkUserCanManageVideo, | ||
47 | doesVideoChannelOfAccountExist, | ||
48 | doesVideoExist, | ||
49 | doesVideoFileOfVideoExist | ||
50 | } from '../../../helpers/middlewares' | ||
51 | import { deleteFileAndCatch } from '../../../helpers/utils' | 45 | import { deleteFileAndCatch } from '../../../helpers/utils' |
52 | import { getVideoWithAttributes } from '../../../helpers/video' | 46 | import { getVideoWithAttributes } from '../../../helpers/video' |
53 | import { CONFIG } from '../../../initializers/config' | 47 | import { CONFIG } from '../../../initializers/config' |
@@ -57,7 +51,14 @@ import { Hooks } from '../../../lib/plugins/hooks' | |||
57 | import { AccountModel } from '../../../models/account/account' | 51 | import { AccountModel } from '../../../models/account/account' |
58 | import { VideoModel } from '../../../models/video/video' | 52 | import { VideoModel } from '../../../models/video/video' |
59 | import { authenticatePromiseIfNeeded } from '../../auth' | 53 | import { authenticatePromiseIfNeeded } from '../../auth' |
60 | import { areValidationErrors } from '../utils' | 54 | import { |
55 | areValidationErrors, | ||
56 | checkUserCanManageVideo, | ||
57 | doesChangeVideoOwnershipExist, | ||
58 | doesVideoChannelOfAccountExist, | ||
59 | doesVideoExist, | ||
60 | doesVideoFileOfVideoExist | ||
61 | } from '../shared' | ||
61 | 62 | ||
62 | const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([ | 63 | const videosAddLegacyValidator = getCommonVideoEditAttributes().concat([ |
63 | body('videofile') | 64 | body('videofile') |
diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts index 097a5ece1..bcdd136c6 100644 --- a/server/middlewares/validators/webfinger.ts +++ b/server/middlewares/validators/webfinger.ts | |||
@@ -5,7 +5,7 @@ import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/w | |||
5 | import { getHostWithPort } from '../../helpers/express-utils' | 5 | import { getHostWithPort } from '../../helpers/express-utils' |
6 | import { logger } from '../../helpers/logger' | 6 | import { logger } from '../../helpers/logger' |
7 | import { ActorModel } from '../../models/actor/actor' | 7 | import { ActorModel } from '../../models/actor/actor' |
8 | import { areValidationErrors } from './utils' | 8 | import { areValidationErrors } from './shared' |
9 | 9 | ||
10 | const webfingerValidator = [ | 10 | const webfingerValidator = [ |
11 | query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'), | 11 | query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'), |