2021-08-27 07:32:44 -05:00
|
|
|
import express from 'express'
|
2020-07-01 09:05:30 -05:00
|
|
|
import { body, param, query } from 'express-validator'
|
|
|
|
import {
|
2020-11-10 07:41:20 -06:00
|
|
|
areAbusePredefinedReasonsValid,
|
2020-07-07 03:57:04 -05:00
|
|
|
isAbuseFilterValid,
|
2020-07-24 08:05:51 -05:00
|
|
|
isAbuseMessageValid,
|
2020-07-01 09:05:30 -05:00
|
|
|
isAbuseModerationCommentValid,
|
|
|
|
isAbusePredefinedReasonValid,
|
|
|
|
isAbuseReasonValid,
|
|
|
|
isAbuseStateValid,
|
|
|
|
isAbuseTimestampCoherent,
|
|
|
|
isAbuseTimestampValid,
|
|
|
|
isAbuseVideoIsValid
|
|
|
|
} from '@server/helpers/custom-validators/abuses'
|
2021-06-28 10:30:59 -05:00
|
|
|
import { exists, isIdOrUUIDValid, isIdValid, toCompleteUUID, toIntOrNull } from '@server/helpers/custom-validators/misc'
|
2020-07-01 09:05:30 -05:00
|
|
|
import { logger } from '@server/helpers/logger'
|
2020-07-24 08:05:51 -05:00
|
|
|
import { AbuseMessageModel } from '@server/models/abuse/abuse-message'
|
|
|
|
import { AbuseCreate, UserRight } from '@shared/models'
|
2021-07-16 03:42:24 -05:00
|
|
|
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { areValidationErrors, doesAbuseExist, doesAccountIdExist, doesCommentIdExist, doesVideoExist } from './shared'
|
2022-11-15 07:41:55 -06:00
|
|
|
import { forceNumber } from '@shared/core-utils'
|
2020-07-01 09:05:30 -05:00
|
|
|
|
|
|
|
const abuseReportValidator = [
|
2020-07-07 03:57:04 -05:00
|
|
|
body('account.id')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isIdValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
|
|
|
|
body('video.id')
|
|
|
|
.optional()
|
2021-06-28 10:30:59 -05:00
|
|
|
.customSanitizer(toCompleteUUID)
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isIdOrUUIDValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
body('video.startAt')
|
2020-07-01 09:05:30 -05:00
|
|
|
.optional()
|
|
|
|
.customSanitizer(toIntOrNull)
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseTimestampValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
body('video.endAt')
|
2020-07-01 09:05:30 -05:00
|
|
|
.optional()
|
|
|
|
.customSanitizer(toIntOrNull)
|
|
|
|
.custom(isAbuseTimestampValid)
|
|
|
|
.bail()
|
|
|
|
.custom(isAbuseTimestampCoherent)
|
|
|
|
.withMessage('Should have a startAt timestamp beginning before endAt'),
|
|
|
|
|
2020-07-07 03:57:04 -05:00
|
|
|
body('comment.id')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isIdValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
|
|
|
|
body('reason')
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseReasonValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
|
|
|
|
body('predefinedReasons')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(areAbusePredefinedReasonsValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
|
2020-07-01 09:05:30 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
2020-07-07 03:57:04 -05:00
|
|
|
const body: AbuseCreate = req.body
|
|
|
|
|
|
|
|
if (body.video?.id && !await doesVideoExist(body.video.id, res)) return
|
|
|
|
if (body.account?.id && !await doesAccountIdExist(body.account.id, res)) return
|
|
|
|
if (body.comment?.id && !await doesCommentIdExist(body.comment.id, res)) return
|
|
|
|
|
|
|
|
if (!body.video?.id && !body.account?.id && !body.comment?.id) {
|
2021-05-31 18:36:53 -05:00
|
|
|
res.fail({ message: 'video id or account id or comment id is required.' })
|
2020-07-07 03:57:04 -05:00
|
|
|
return
|
|
|
|
}
|
2020-07-01 09:05:30 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const abuseGetValidator = [
|
2022-08-17 07:27:04 -05:00
|
|
|
param('id')
|
|
|
|
.custom(isIdValid),
|
2020-07-01 09:05:30 -05:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2020-07-07 03:57:04 -05:00
|
|
|
if (!await doesAbuseExist(req.params.id, res)) return
|
2020-07-01 09:05:30 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const abuseUpdateValidator = [
|
2022-08-17 07:27:04 -05:00
|
|
|
param('id')
|
|
|
|
.custom(isIdValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
|
2020-07-01 09:05:30 -05:00
|
|
|
body('state')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseStateValid),
|
2020-07-01 09:05:30 -05:00
|
|
|
body('moderationComment')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseModerationCommentValid),
|
2020-07-01 09:05:30 -05:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
2020-07-07 03:57:04 -05:00
|
|
|
if (!await doesAbuseExist(req.params.id, res)) return
|
2020-07-01 09:05:30 -05:00
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-07-24 08:05:51 -05:00
|
|
|
const abuseListForAdminsValidator = [
|
2020-07-01 09:05:30 -05:00
|
|
|
query('id')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isIdValid),
|
2020-07-07 03:57:04 -05:00
|
|
|
query('filter')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseFilterValid),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('predefinedReason')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbusePredefinedReasonValid),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('search')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(exists),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('state')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseStateValid),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('videoIs')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseVideoIsValid),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('searchReporter')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(exists),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('searchReportee')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(exists),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('searchVideo')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(exists),
|
2020-07-01 09:05:30 -05:00
|
|
|
query('searchVideoChannel')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(exists),
|
2020-07-01 09:05:30 -05:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-07-24 08:05:51 -05:00
|
|
|
const abuseListForUserValidator = [
|
|
|
|
query('id')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isIdValid),
|
2020-07-24 08:05:51 -05:00
|
|
|
|
|
|
|
query('search')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(exists),
|
2020-07-24 08:05:51 -05:00
|
|
|
|
|
|
|
query('state')
|
|
|
|
.optional()
|
2022-08-17 07:27:04 -05:00
|
|
|
.custom(isAbuseStateValid),
|
2020-07-24 08:05:51 -05:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const getAbuseValidator = [
|
2022-08-17 07:27:04 -05:00
|
|
|
param('id')
|
|
|
|
.custom(isIdValid),
|
2020-07-24 08:05:51 -05:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await doesAbuseExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const user = res.locals.oauth.token.user
|
|
|
|
const abuse = res.locals.abuse
|
|
|
|
|
|
|
|
if (user.hasRight(UserRight.MANAGE_ABUSES) !== true && abuse.reporterAccountId !== user.Account.id) {
|
|
|
|
const message = `User ${user.username} does not have right to get abuse ${abuse.id}`
|
|
|
|
logger.warn(message)
|
|
|
|
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message
|
|
|
|
})
|
2020-07-24 08:05:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-07-27 04:40:30 -05:00
|
|
|
const checkAbuseValidForMessagesValidator = [
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
const abuse = res.locals.abuse
|
|
|
|
if (abuse.ReporterAccount.isOwned() === false) {
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({ message: 'This abuse was created by a user of your instance.' })
|
2020-07-27 04:40:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-07-24 08:05:51 -05:00
|
|
|
const addAbuseMessageValidator = [
|
2022-08-17 07:27:04 -05:00
|
|
|
body('message')
|
|
|
|
.custom(isAbuseMessageValid),
|
2020-07-24 08:05:51 -05:00
|
|
|
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const deleteAbuseMessageValidator = [
|
2022-08-17 07:27:04 -05:00
|
|
|
param('messageId')
|
|
|
|
.custom(isIdValid),
|
2020-07-24 08:05:51 -05:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
const user = res.locals.oauth.token.user
|
|
|
|
const abuse = res.locals.abuse
|
|
|
|
|
2022-11-15 07:41:55 -06:00
|
|
|
const messageId = forceNumber(req.params.messageId)
|
2020-07-24 08:05:51 -05:00
|
|
|
const abuseMessage = await AbuseMessageModel.loadByIdAndAbuseId(messageId, abuse.id)
|
|
|
|
|
|
|
|
if (!abuseMessage) {
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Abuse message not found'
|
|
|
|
})
|
2020-07-24 08:05:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (user.hasRight(UserRight.MANAGE_ABUSES) !== true && abuseMessage.accountId !== user.Account.id) {
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: 'Cannot delete this abuse message'
|
|
|
|
})
|
2020-07-24 08:05:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.abuseMessage = abuseMessage
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-07-01 09:05:30 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2020-07-24 08:05:51 -05:00
|
|
|
abuseListForAdminsValidator,
|
2020-07-01 09:05:30 -05:00
|
|
|
abuseReportValidator,
|
|
|
|
abuseGetValidator,
|
2020-07-24 08:05:51 -05:00
|
|
|
addAbuseMessageValidator,
|
2020-07-27 04:40:30 -05:00
|
|
|
checkAbuseValidForMessagesValidator,
|
2020-07-01 09:05:30 -05:00
|
|
|
abuseUpdateValidator,
|
2020-07-24 08:05:51 -05:00
|
|
|
deleteAbuseMessageValidator,
|
|
|
|
abuseListForUserValidator,
|
2020-11-10 07:41:20 -06:00
|
|
|
getAbuseValidator
|
2020-07-01 09:05:30 -05:00
|
|
|
}
|