From d95d15598847c7f020aa056e7e6e0c02d2bbf732 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 1 Jul 2020 16:05:30 +0200 Subject: Use 3 tables to represent abuses --- server/helpers/audit-logger.ts | 27 +++++------ server/helpers/custom-validators/abuses.ts | 54 +++++++++++++++++++++ .../helpers/custom-validators/activitypub/flag.ts | 4 +- server/helpers/custom-validators/video-abuses.ts | 56 ---------------------- server/helpers/middlewares/abuses.ts | 38 +++++++++++++++ server/helpers/middlewares/index.ts | 2 +- server/helpers/middlewares/video-abuses.ts | 32 ------------- 7 files changed, 107 insertions(+), 106 deletions(-) create mode 100644 server/helpers/custom-validators/abuses.ts delete mode 100644 server/helpers/custom-validators/video-abuses.ts create mode 100644 server/helpers/middlewares/abuses.ts delete mode 100644 server/helpers/middlewares/video-abuses.ts (limited to 'server/helpers') diff --git a/server/helpers/audit-logger.ts b/server/helpers/audit-logger.ts index 0bbfbc753..954b0b69d 100644 --- a/server/helpers/audit-logger.ts +++ b/server/helpers/audit-logger.ts @@ -1,15 +1,15 @@ -import * as path from 'path' -import * as express from 'express' import { diff } from 'deep-object-diff' -import { chain } from 'lodash' +import * as express from 'express' import * as flatten from 'flat' +import { chain } from 'lodash' +import * as path from 'path' import * as winston from 'winston' -import { jsonLoggerFormat, labelFormatter } from './logger' -import { User, VideoAbuse, VideoChannel, VideoDetails, VideoImport } from '../../shared' -import { VideoComment } from '../../shared/models/videos/video-comment.model' +import { AUDIT_LOG_FILENAME } from '@server/initializers/constants' +import { Abuse, User, VideoChannel, VideoDetails, VideoImport } from '../../shared' import { CustomConfig } from '../../shared/models/server/custom-config.model' +import { VideoComment } from '../../shared/models/videos/video-comment.model' import { CONFIG } from '../initializers/config' -import { AUDIT_LOG_FILENAME } from '@server/initializers/constants' +import { jsonLoggerFormat, labelFormatter } from './logger' function getAuditIdFromRes (res: express.Response) { return res.locals.oauth.token.User.username @@ -212,18 +212,15 @@ class VideoChannelAuditView extends EntityAuditView { } } -const videoAbuseKeysToKeep = [ +const abuseKeysToKeep = [ 'id', 'reason', 'reporterAccount', - 'video-id', - 'video-name', - 'video-uuid', 'createdAt' ] -class VideoAbuseAuditView extends EntityAuditView { - constructor (private readonly videoAbuse: VideoAbuse) { - super(videoAbuseKeysToKeep, 'abuse', videoAbuse) +class AbuseAuditView extends EntityAuditView { + constructor (private readonly abuse: Abuse) { + super(abuseKeysToKeep, 'abuse', abuse) } } @@ -274,6 +271,6 @@ export { CommentAuditView, UserAuditView, VideoAuditView, - VideoAbuseAuditView, + AbuseAuditView, CustomConfigAuditView } diff --git a/server/helpers/custom-validators/abuses.ts b/server/helpers/custom-validators/abuses.ts new file mode 100644 index 000000000..a6a895c65 --- /dev/null +++ b/server/helpers/custom-validators/abuses.ts @@ -0,0 +1,54 @@ +import validator from 'validator' +import { abusePredefinedReasonsMap, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models' +import { CONSTRAINTS_FIELDS, ABUSE_STATES } from '../../initializers/constants' +import { exists, isArray } from './misc' + +const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES + +function isAbuseReasonValid (value: string) { + return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) +} + +function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) { + return exists(value) && value in abusePredefinedReasonsMap +} + +function isAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) { + return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap) +} + +function isAbuseTimestampValid (value: number) { + return value === null || (exists(value) && validator.isInt('' + value, { min: 0 })) +} + +function isAbuseTimestampCoherent (endAt: number, { req }) { + return exists(req.body.startAt) && endAt > req.body.startAt +} + +function isAbuseModerationCommentValid (value: string) { + return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT) +} + +function isAbuseStateValid (value: string) { + return exists(value) && ABUSE_STATES[value] !== undefined +} + +function isAbuseVideoIsValid (value: AbuseVideoIs) { + return exists(value) && ( + value === 'deleted' || + value === 'blacklisted' + ) +} + +// --------------------------------------------------------------------------- + +export { + isAbuseReasonValid, + isAbusePredefinedReasonValid, + isAbusePredefinedReasonsValid, + isAbuseTimestampValid, + isAbuseTimestampCoherent, + isAbuseModerationCommentValid, + isAbuseStateValid, + isAbuseVideoIsValid +} diff --git a/server/helpers/custom-validators/activitypub/flag.ts b/server/helpers/custom-validators/activitypub/flag.ts index 6452e297c..dc90b3667 100644 --- a/server/helpers/custom-validators/activitypub/flag.ts +++ b/server/helpers/custom-validators/activitypub/flag.ts @@ -1,9 +1,9 @@ import { isActivityPubUrlValid } from './misc' -import { isVideoAbuseReasonValid } from '../video-abuses' +import { isAbuseReasonValid } from '../abuses' function isFlagActivityValid (activity: any) { return activity.type === 'Flag' && - isVideoAbuseReasonValid(activity.content) && + isAbuseReasonValid(activity.content) && isActivityPubUrlValid(activity.object) } diff --git a/server/helpers/custom-validators/video-abuses.ts b/server/helpers/custom-validators/video-abuses.ts deleted file mode 100644 index 0c2c34268..000000000 --- a/server/helpers/custom-validators/video-abuses.ts +++ /dev/null @@ -1,56 +0,0 @@ -import validator from 'validator' - -import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants' -import { exists, isArray } from './misc' -import { VideoAbuseVideoIs } from '@shared/models/videos/abuse/video-abuse-video-is.type' -import { VideoAbusePredefinedReasonsString, videoAbusePredefinedReasonsMap } from '@shared/models/videos/abuse/video-abuse-reason.model' - -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 isVideoAbusePredefinedReasonValid (value: VideoAbusePredefinedReasonsString) { - return exists(value) && value in videoAbusePredefinedReasonsMap -} - -function isVideoAbusePredefinedReasonsValid (value: VideoAbusePredefinedReasonsString[]) { - return exists(value) && isArray(value) && value.every(v => v in videoAbusePredefinedReasonsMap) -} - -function isVideoAbuseTimestampValid (value: number) { - return value === null || (exists(value) && validator.isInt('' + value, { min: 0 })) -} - -function isVideoAbuseTimestampCoherent (endAt: number, { req }) { - return exists(req.body.startAt) && endAt > req.body.startAt -} - -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 -} - -function isAbuseVideoIsValid (value: VideoAbuseVideoIs) { - return exists(value) && ( - value === 'deleted' || - value === 'blacklisted' - ) -} - -// --------------------------------------------------------------------------- - -export { - isVideoAbuseReasonValid, - isVideoAbusePredefinedReasonValid, - isVideoAbusePredefinedReasonsValid, - isVideoAbuseTimestampValid, - isVideoAbuseTimestampCoherent, - isVideoAbuseModerationCommentValid, - isVideoAbuseStateValid, - isAbuseVideoIsValid -} diff --git a/server/helpers/middlewares/abuses.ts b/server/helpers/middlewares/abuses.ts new file mode 100644 index 000000000..3906f6760 --- /dev/null +++ b/server/helpers/middlewares/abuses.ts @@ -0,0 +1,38 @@ +import { Response } from 'express' +import { AbuseModel } from '../../models/abuse/abuse' +import { fetchVideo } from '../video' + +// FIXME: deprecated in 2.3. Remove this function +async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) { + const abuseId = parseInt(abuseIdArg + '', 10) + let abuse = await AbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID) + + if (!abuse) { + const userId = res.locals.oauth?.token.User.id + const video = await fetchVideo(videoUUID, 'all', userId) + + if (video) abuse = await AbuseModel.loadByIdAndVideoId(abuseId, video.id) + } + + if (abuse === null) { + res.status(404) + .json({ error: 'Video abuse not found' }) + .end() + + return false + } + + res.locals.abuse = abuse + return true +} + +async function doesAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) { + +} + +// --------------------------------------------------------------------------- + +export { + doesAbuseExist, + doesVideoAbuseExist +} diff --git a/server/helpers/middlewares/index.ts b/server/helpers/middlewares/index.ts index f91aeaa12..f57f3ad31 100644 --- a/server/helpers/middlewares/index.ts +++ b/server/helpers/middlewares/index.ts @@ -1,5 +1,5 @@ +export * from './abuses' export * from './accounts' -export * from './video-abuses' export * from './video-blacklists' export * from './video-captions' export * from './video-channels' diff --git a/server/helpers/middlewares/video-abuses.ts b/server/helpers/middlewares/video-abuses.ts deleted file mode 100644 index 97a5724b6..000000000 --- a/server/helpers/middlewares/video-abuses.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { Response } from 'express' -import { VideoAbuseModel } from '../../models/video/video-abuse' -import { fetchVideo } from '../video' - -async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) { - const abuseId = parseInt(abuseIdArg + '', 10) - let videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID) - - if (!videoAbuse) { - const userId = res.locals.oauth?.token.User.id - const video = await fetchVideo(videoUUID, 'all', userId) - - if (video) videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, video.id) - } - - if (videoAbuse === null) { - res.status(404) - .json({ error: 'Video abuse not found' }) - .end() - - return false - } - - res.locals.videoAbuse = videoAbuse - return true -} - -// --------------------------------------------------------------------------- - -export { - doesVideoAbuseExist -} -- cgit v1.2.3