PeerTube/server/core/lib/moderation.ts

259 lines
7.6 KiB
TypeScript
Raw Normal View History

2024-02-14 10:20:02 +01:00
import express, { VideoLegacyUploadFile } from 'express'
import { PathLike } from 'fs-extra/esm'
import { Transaction } from 'sequelize'
import { AbuseAuditView, auditLoggerFactory } from '@server/helpers/audit-logger.js'
import { afterCommitIfTransaction } from '@server/helpers/database-utils.js'
import { logger } from '@server/helpers/logger.js'
import { AbuseModel } from '@server/models/abuse/abuse.js'
import { VideoAbuseModel } from '@server/models/abuse/video-abuse.js'
import { VideoCommentAbuseModel } from '@server/models/abuse/video-comment-abuse.js'
import { VideoFileModel } from '@server/models/video/video-file.js'
import { FilteredModelAttributes } from '@server/types/index.js'
2020-07-01 16:05:30 +02:00
import {
MAbuseFull,
MAccountDefault,
MAccountLight,
2022-09-23 11:38:18 +02:00
MComment,
2020-07-01 16:05:30 +02:00
MCommentAbuseAccountVideo,
MCommentOwnerVideo,
MUser,
2024-02-12 10:47:52 +01:00
MUserDefault,
2020-07-01 16:05:30 +02:00
MVideoAbuseVideoFull,
MVideoAccountLightBlacklistAllFiles
} from '@server/types/models/index.js'
import { LiveVideoCreate, VideoCommentCreate, VideoCreate, VideoImportCreate } from '@peertube/peertube-models'
import { UserModel } from '../models/user/user.js'
import { VideoCommentModel } from '../models/video/video-comment.js'
import { VideoModel } from '../models/video/video.js'
import { sendAbuse } from './activitypub/send/send-flag.js'
import { Notifier } from './notifier/index.js'
2019-07-18 14:28:37 +02:00
export type AcceptResult = {
accepted: boolean
errorMessage?: string
}
2022-09-23 11:38:18 +02:00
// ---------------------------------------------------------------------------
// Stub function that can be filtered by plugins
2023-07-19 16:02:49 +02:00
function isLocalVideoFileAccepted (object: {
2020-01-31 16:56:52 +01:00
videoBody: VideoCreate
2024-02-14 10:20:02 +01:00
videoFile: VideoLegacyUploadFile
2024-02-12 10:47:52 +01:00
user: MUserDefault
2019-07-18 14:28:37 +02:00
}): AcceptResult {
return { accepted: true }
}
2022-09-23 11:38:18 +02:00
// ---------------------------------------------------------------------------
// Stub function that can be filtered by plugins
2020-11-06 13:59:50 +01:00
function isLocalLiveVideoAccepted (object: {
liveVideoBody: LiveVideoCreate
user: UserModel
}): AcceptResult {
return { accepted: true }
}
2022-09-23 11:38:18 +02:00
// ---------------------------------------------------------------------------
// Stub function that can be filtered by plugins
2019-07-18 14:28:37 +02:00
function isLocalVideoThreadAccepted (_object: {
2022-09-23 15:32:56 +02:00
req: express.Request
2020-01-31 16:56:52 +01:00
commentBody: VideoCommentCreate
video: VideoModel
2019-07-18 14:28:37 +02:00
user: UserModel
}): AcceptResult {
return { accepted: true }
}
2022-09-23 11:38:18 +02:00
// Stub function that can be filtered by plugins
2019-07-18 14:28:37 +02:00
function isLocalVideoCommentReplyAccepted (_object: {
2022-09-23 15:32:56 +02:00
req: express.Request
2020-01-31 16:56:52 +01:00
commentBody: VideoCommentCreate
parentComment: VideoCommentModel
video: VideoModel
2019-07-18 14:28:37 +02:00
user: UserModel
}): AcceptResult {
return { accepted: true }
}
2022-09-23 11:38:18 +02:00
// ---------------------------------------------------------------------------
2019-07-18 14:28:37 +02:00
2022-09-23 11:38:18 +02:00
// Stub function that can be filtered by plugins
2019-07-18 14:28:37 +02:00
function isRemoteVideoCommentAccepted (_object: {
2022-09-23 11:38:18 +02:00
comment: MComment
2019-07-18 14:28:37 +02:00
}): AcceptResult {
return { accepted: true }
}
2022-09-23 11:38:18 +02:00
// ---------------------------------------------------------------------------
// Stub function that can be filtered by plugins
function isPreImportVideoAccepted (object: {
videoImportBody: VideoImportCreate
user: MUser
}): AcceptResult {
return { accepted: true }
}
2022-09-23 11:38:18 +02:00
// Stub function that can be filtered by plugins
function isPostImportVideoAccepted (object: {
videoFilePath: PathLike
videoFile: VideoFileModel
user: MUser
}): AcceptResult {
return { accepted: true }
}
2022-09-23 11:38:18 +02:00
// ---------------------------------------------------------------------------
2020-07-01 16:05:30 +02:00
async function createVideoAbuse (options: {
baseAbuse: FilteredModelAttributes<AbuseModel>
videoInstance: MVideoAccountLightBlacklistAllFiles
startAt: number
endAt: number
transaction: Transaction
reporterAccount: MAccountDefault
2021-12-09 14:27:32 +01:00
skipNotification: boolean
2020-07-01 16:05:30 +02:00
}) {
2021-12-09 14:27:32 +01:00
const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount, skipNotification } = options
2020-07-01 16:05:30 +02:00
const associateFun = async (abuseInstance: MAbuseFull) => {
const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({
abuseId: abuseInstance.id,
videoId: videoInstance.id,
2022-07-13 11:58:01 +02:00
startAt,
endAt
2020-07-01 16:05:30 +02:00
}, { transaction })
videoAbuseInstance.Video = videoInstance
abuseInstance.VideoAbuse = videoAbuseInstance
return { isOwned: videoInstance.isOwned() }
}
return createAbuse({
base: baseAbuse,
reporterAccount,
flaggedAccount: videoInstance.VideoChannel.Account,
transaction,
2021-12-09 14:27:32 +01:00
skipNotification,
2020-07-01 16:05:30 +02:00
associateFun
})
}
function createVideoCommentAbuse (options: {
baseAbuse: FilteredModelAttributes<AbuseModel>
commentInstance: MCommentOwnerVideo
transaction: Transaction
reporterAccount: MAccountDefault
2021-12-09 14:27:32 +01:00
skipNotification: boolean
2020-07-01 16:05:30 +02:00
}) {
2021-12-09 14:27:32 +01:00
const { baseAbuse, commentInstance, transaction, reporterAccount, skipNotification } = options
2020-07-01 16:05:30 +02:00
const associateFun = async (abuseInstance: MAbuseFull) => {
const commentAbuseInstance: MCommentAbuseAccountVideo = await VideoCommentAbuseModel.create({
abuseId: abuseInstance.id,
videoCommentId: commentInstance.id
}, { transaction })
commentAbuseInstance.VideoComment = commentInstance
abuseInstance.VideoCommentAbuse = commentAbuseInstance
return { isOwned: commentInstance.isOwned() }
}
return createAbuse({
base: baseAbuse,
reporterAccount,
flaggedAccount: commentInstance.Account,
transaction,
2021-12-09 14:27:32 +01:00
skipNotification,
2020-07-01 16:05:30 +02:00
associateFun
})
}
function createAccountAbuse (options: {
baseAbuse: FilteredModelAttributes<AbuseModel>
accountInstance: MAccountDefault
transaction: Transaction
reporterAccount: MAccountDefault
2021-12-09 14:27:32 +01:00
skipNotification: boolean
2020-07-01 16:05:30 +02:00
}) {
2021-12-09 14:27:32 +01:00
const { baseAbuse, accountInstance, transaction, reporterAccount, skipNotification } = options
2020-07-01 16:05:30 +02:00
2021-08-25 16:14:11 +02:00
const associateFun = () => {
return Promise.resolve({ isOwned: accountInstance.isOwned() })
2020-07-01 16:05:30 +02:00
}
return createAbuse({
base: baseAbuse,
reporterAccount,
flaggedAccount: accountInstance,
transaction,
2021-12-09 14:27:32 +01:00
skipNotification,
2020-07-01 16:05:30 +02:00
associateFun
})
}
2022-09-23 11:38:18 +02:00
// ---------------------------------------------------------------------------
2019-07-18 14:28:37 +02:00
export {
2020-11-06 13:59:50 +01:00
isLocalLiveVideoAccepted,
2023-07-19 16:02:49 +02:00
isLocalVideoFileAccepted,
2019-07-18 14:28:37 +02:00
isLocalVideoThreadAccepted,
isRemoteVideoCommentAccepted,
isLocalVideoCommentReplyAccepted,
isPreImportVideoAccepted,
2020-07-01 16:05:30 +02:00
isPostImportVideoAccepted,
createAbuse,
createVideoAbuse,
createVideoCommentAbuse,
createAccountAbuse
}
// ---------------------------------------------------------------------------
async function createAbuse (options: {
base: FilteredModelAttributes<AbuseModel>
reporterAccount: MAccountDefault
flaggedAccount: MAccountLight
2022-11-15 15:00:19 +01:00
associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean }>
2021-12-09 14:27:32 +01:00
skipNotification: boolean
2020-07-01 16:05:30 +02:00
transaction: Transaction
}) {
2021-12-09 14:27:32 +01:00
const { base, reporterAccount, flaggedAccount, associateFun, transaction, skipNotification } = options
2020-07-01 16:05:30 +02:00
const auditLogger = auditLoggerFactory('abuse')
const abuseAttributes = Object.assign({}, base, { flaggedAccountId: flaggedAccount.id })
const abuseInstance: MAbuseFull = await AbuseModel.create(abuseAttributes, { transaction })
abuseInstance.ReporterAccount = reporterAccount
abuseInstance.FlaggedAccount = flaggedAccount
const { isOwned } = await associateFun(abuseInstance)
if (isOwned === false) {
2021-06-15 09:17:19 +02:00
sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
2020-07-01 16:05:30 +02:00
}
2020-07-24 15:05:51 +02:00
const abuseJSON = abuseInstance.toFormattedAdminJSON()
2020-07-01 16:05:30 +02:00
auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON))
2021-12-09 14:27:32 +01:00
if (!skipNotification) {
afterCommitIfTransaction(transaction, () => {
Notifier.Instance.notifyOnNewAbuse({
abuse: abuseJSON,
abuseInstance,
reporter: reporterAccount.Actor.getIdentifier()
})
2021-03-04 16:12:46 +01:00
})
2021-12-09 14:27:32 +01:00
}
2020-07-01 16:05:30 +02:00
logger.info('Abuse report %d created.', abuseInstance.id)
return abuseJSON
2019-07-18 14:28:37 +02:00
}