From 268eebed921ac13a9ce0f4717f4923aa24190657 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Aug 2018 16:54:01 +0200 Subject: Add state and moderationComment for abuses on server side --- .../custom-validators/activitypub/videos.ts | 2 +- server/helpers/custom-validators/video-abuses.ts | 43 ++++++++++++++++++++++ server/helpers/custom-validators/videos.ts | 7 +--- 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 server/helpers/custom-validators/video-abuses.ts (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index b8075f3c7..702c09842 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -3,7 +3,6 @@ import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers' import { peertubeTruncate } from '../../core-utils' import { exists, isBooleanValid, isDateValid, isUUIDValid } from '../misc' import { - isVideoAbuseReasonValid, isVideoDurationValid, isVideoNameValid, isVideoStateValid, @@ -13,6 +12,7 @@ import { } from '../videos' import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' import { VideoState } from '../../../../shared/models/videos' +import { isVideoAbuseReasonValid } from '../video-abuses' function sanitizeAndCheckVideoTorrentCreateActivity (activity: any) { return isBaseActivityValid(activity, 'Create') && diff --git a/server/helpers/custom-validators/video-abuses.ts b/server/helpers/custom-validators/video-abuses.ts new file mode 100644 index 000000000..290efb149 --- /dev/null +++ b/server/helpers/custom-validators/video-abuses.ts @@ -0,0 +1,43 @@ +import { Response } from 'express' +import * as validator from 'validator' +import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers' +import { exists } from './misc' +import { VideoAbuseModel } from '../../models/video/video-abuse' + +const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES + +function isVideoAbuseReasonValid (value: string) { + return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) +} + +function isVideoAbuseModerationCommentValid (value: string) { + return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT) +} + +function isVideoAbuseStateValid (value: string) { + return exists(value) && VIDEO_ABUSE_STATES[ value ] !== undefined +} + +async function isVideoAbuseExist (abuseId: number, videoId: number, res: Response) { + const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId) + + if (videoAbuse === null) { + res.status(404) + .json({ error: 'Video abuse not found' }) + .end() + + return false + } + + res.locals.videoAbuse = videoAbuse + return true +} + +// --------------------------------------------------------------------------- + +export { + isVideoAbuseExist, + isVideoAbuseStateValid, + isVideoAbuseReasonValid, + isVideoAbuseModerationCommentValid +} diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index f4c1c8b07..5e6cfe217 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -6,6 +6,7 @@ import * as validator from 'validator' import { UserRight, VideoPrivacy, VideoRateType } from '../../../shared' import { CONSTRAINTS_FIELDS, + VIDEO_ABUSE_STATES, VIDEO_CATEGORIES, VIDEO_LICENCES, VIDEO_MIMETYPE_EXT, @@ -18,6 +19,7 @@ import { exists, isArray, isFileValid } from './misc' import { VideoChannelModel } from '../../models/video/video-channel' import { UserModel } from '../../models/account/user' import * as magnetUtil from 'magnet-uri' +import { VideoAbuseModel } from '../../models/video/video-abuse' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES @@ -71,10 +73,6 @@ function isVideoTagsValid (tags: string[]) { ) } -function isVideoAbuseReasonValid (value: string) { - return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) -} - function isVideoViewsValid (value: string) { return exists(value) && validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.VIEWS) } @@ -220,7 +218,6 @@ export { isVideoTagsValid, isVideoFPSResolutionValid, isScheduleVideoUpdatePrivacyValid, - isVideoAbuseReasonValid, isVideoFile, isVideoMagnetUriValid, isVideoStateValid, -- cgit v1.2.3