X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fmoderation.ts;h=4fc9cd747609a22d0d612caafc8e6c1fb944b566;hb=d95d15598847c7f020aa056e7e6e0c02d2bbf732;hp=55f7a985dee6b0ccf941dcdadab47cea01873467;hpb=a3b7421abb4192e215aa280418b62e96958c5e42;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/moderation.ts b/server/lib/moderation.ts index 55f7a985d..4fc9cd747 100644 --- a/server/lib/moderation.ts +++ b/server/lib/moderation.ts @@ -1,12 +1,33 @@ -import { VideoModel } from '../models/video/video' -import { VideoCommentModel } from '../models/video/video-comment' +import { PathLike } from 'fs-extra' +import { Transaction } from 'sequelize/types' +import { AbuseAuditView, auditLoggerFactory } from '@server/helpers/audit-logger' +import { logger } from '@server/helpers/logger' +import { AbuseModel } from '@server/models/abuse/abuse' +import { VideoAbuseModel } from '@server/models/abuse/video-abuse' +import { VideoCommentAbuseModel } from '@server/models/abuse/video-comment-abuse' +import { VideoFileModel } from '@server/models/video/video-file' +import { FilteredModelAttributes } from '@server/types' +import { + MAbuseFull, + MAccountDefault, + MAccountLight, + MCommentAbuseAccountVideo, + MCommentOwnerVideo, + MUser, + MVideoAbuseVideoFull, + MVideoAccountLightBlacklistAllFiles +} from '@server/types/models' +import { ActivityCreate } from '../../shared/models/activitypub' +import { VideoTorrentObject } from '../../shared/models/activitypub/objects' +import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object' +import { VideoCreate, VideoImportCreate } from '../../shared/models/videos' import { VideoCommentCreate } from '../../shared/models/videos/video-comment.model' -import { VideoCreate } from '../../shared/models/videos' import { UserModel } from '../models/account/user' -import { VideoTorrentObject } from '../../shared/models/activitypub/objects' -import { ActivityCreate } from '../../shared/models/activitypub' import { ActorModel } from '../models/activitypub/actor' -import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object' +import { VideoModel } from '../models/video/video' +import { VideoCommentModel } from '../models/video/video-comment' +import { sendAbuse } from './activitypub/send/send-flag' +import { Notifier } from './notifier' export type AcceptResult = { accepted: boolean @@ -55,10 +76,153 @@ function isRemoteVideoCommentAccepted (_object: { return { accepted: true } } +function isPreImportVideoAccepted (object: { + videoImportBody: VideoImportCreate + user: MUser +}): AcceptResult { + return { accepted: true } +} + +function isPostImportVideoAccepted (object: { + videoFilePath: PathLike + videoFile: VideoFileModel + user: MUser +}): AcceptResult { + return { accepted: true } +} + +async function createVideoAbuse (options: { + baseAbuse: FilteredModelAttributes + videoInstance: MVideoAccountLightBlacklistAllFiles + startAt: number + endAt: number + transaction: Transaction + reporterAccount: MAccountDefault +}) { + const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount } = options + + const associateFun = async (abuseInstance: MAbuseFull) => { + const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({ + abuseId: abuseInstance.id, + videoId: videoInstance.id, + startAt: startAt, + endAt: endAt + }, { transaction }) + + videoAbuseInstance.Video = videoInstance + abuseInstance.VideoAbuse = videoAbuseInstance + + return { isOwned: videoInstance.isOwned() } + } + + return createAbuse({ + base: baseAbuse, + reporterAccount, + flaggedAccount: videoInstance.VideoChannel.Account, + transaction, + associateFun + }) +} + +function createVideoCommentAbuse (options: { + baseAbuse: FilteredModelAttributes + commentInstance: MCommentOwnerVideo + transaction: Transaction + reporterAccount: MAccountDefault +}) { + const { baseAbuse, commentInstance, transaction, reporterAccount } = options + + 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, + associateFun + }) +} + +function createAccountAbuse (options: { + baseAbuse: FilteredModelAttributes + accountInstance: MAccountDefault + transaction: Transaction + reporterAccount: MAccountDefault +}) { + const { baseAbuse, accountInstance, transaction, reporterAccount } = options + + const associateFun = async () => { + return { isOwned: accountInstance.isOwned() } + } + + return createAbuse({ + base: baseAbuse, + reporterAccount, + flaggedAccount: accountInstance, + transaction, + associateFun + }) +} + export { isLocalVideoAccepted, isLocalVideoThreadAccepted, isRemoteVideoAccepted, isRemoteVideoCommentAccepted, - isLocalVideoCommentReplyAccepted + isLocalVideoCommentReplyAccepted, + isPreImportVideoAccepted, + isPostImportVideoAccepted, + + createAbuse, + createVideoAbuse, + createVideoCommentAbuse, + createAccountAbuse +} + +// --------------------------------------------------------------------------- + +async function createAbuse (options: { + base: FilteredModelAttributes + reporterAccount: MAccountDefault + flaggedAccount: MAccountLight + associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean} > + transaction: Transaction +}) { + const { base, reporterAccount, flaggedAccount, associateFun, transaction } = options + 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) { + await sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction) + } + + const abuseJSON = abuseInstance.toFormattedJSON() + auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON)) + + Notifier.Instance.notifyOnNewAbuse({ + abuse: abuseJSON, + abuseInstance, + reporter: reporterAccount.Actor.getIdentifier() + }) + + logger.info('Abuse report %d created.', abuseInstance.id) + + return abuseJSON }