1 import { VideoUploadFile } from 'express'
2 import { PathLike } from 'fs-extra'
3 import { Transaction } from 'sequelize/types'
4 import { AbuseAuditView, auditLoggerFactory } from '@server/helpers/audit-logger'
5 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
6 import { logger } from '@server/helpers/logger'
7 import { AbuseModel } from '@server/models/abuse/abuse'
8 import { VideoAbuseModel } from '@server/models/abuse/video-abuse'
9 import { VideoCommentAbuseModel } from '@server/models/abuse/video-comment-abuse'
10 import { VideoFileModel } from '@server/models/video/video-file'
11 import { FilteredModelAttributes } from '@server/types'
16 MCommentAbuseAccountVideo,
20 MVideoAccountLightBlacklistAllFiles
21 } from '@server/types/models'
22 import { ActivityCreate } from '../../shared/models/activitypub'
23 import { VideoObject } from '../../shared/models/activitypub/objects'
24 import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object'
25 import { LiveVideoCreate, VideoCreate, VideoImportCreate } from '../../shared/models/videos'
26 import { VideoCommentCreate } from '../../shared/models/videos/video-comment.model'
27 import { UserModel } from '../models/account/user'
28 import { ActorModel } from '../models/activitypub/actor'
29 import { VideoModel } from '../models/video/video'
30 import { VideoCommentModel } from '../models/video/video-comment'
31 import { sendAbuse } from './activitypub/send/send-flag'
32 import { Notifier } from './notifier'
34 export type AcceptResult = {
39 // Can be filtered by plugins
40 function isLocalVideoAccepted (object: {
41 videoBody: VideoCreate
42 videoFile: VideoUploadFile
45 return { accepted: true }
48 function isLocalLiveVideoAccepted (object: {
49 liveVideoBody: LiveVideoCreate
52 return { accepted: true }
55 function isLocalVideoThreadAccepted (_object: {
56 commentBody: VideoCommentCreate
60 return { accepted: true }
63 function isLocalVideoCommentReplyAccepted (_object: {
64 commentBody: VideoCommentCreate
65 parentComment: VideoCommentModel
69 return { accepted: true }
72 function isRemoteVideoAccepted (_object: {
73 activity: ActivityCreate
77 return { accepted: true }
80 function isRemoteVideoCommentAccepted (_object: {
81 activity: ActivityCreate
82 commentAP: VideoCommentObject
85 return { accepted: true }
88 function isPreImportVideoAccepted (object: {
89 videoImportBody: VideoImportCreate
92 return { accepted: true }
95 function isPostImportVideoAccepted (object: {
96 videoFilePath: PathLike
97 videoFile: VideoFileModel
100 return { accepted: true }
103 async function createVideoAbuse (options: {
104 baseAbuse: FilteredModelAttributes<AbuseModel>
105 videoInstance: MVideoAccountLightBlacklistAllFiles
108 transaction: Transaction
109 reporterAccount: MAccountDefault
111 const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount } = options
113 const associateFun = async (abuseInstance: MAbuseFull) => {
114 const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({
115 abuseId: abuseInstance.id,
116 videoId: videoInstance.id,
121 videoAbuseInstance.Video = videoInstance
122 abuseInstance.VideoAbuse = videoAbuseInstance
124 return { isOwned: videoInstance.isOwned() }
130 flaggedAccount: videoInstance.VideoChannel.Account,
136 function createVideoCommentAbuse (options: {
137 baseAbuse: FilteredModelAttributes<AbuseModel>
138 commentInstance: MCommentOwnerVideo
139 transaction: Transaction
140 reporterAccount: MAccountDefault
142 const { baseAbuse, commentInstance, transaction, reporterAccount } = options
144 const associateFun = async (abuseInstance: MAbuseFull) => {
145 const commentAbuseInstance: MCommentAbuseAccountVideo = await VideoCommentAbuseModel.create({
146 abuseId: abuseInstance.id,
147 videoCommentId: commentInstance.id
150 commentAbuseInstance.VideoComment = commentInstance
151 abuseInstance.VideoCommentAbuse = commentAbuseInstance
153 return { isOwned: commentInstance.isOwned() }
159 flaggedAccount: commentInstance.Account,
165 function createAccountAbuse (options: {
166 baseAbuse: FilteredModelAttributes<AbuseModel>
167 accountInstance: MAccountDefault
168 transaction: Transaction
169 reporterAccount: MAccountDefault
171 const { baseAbuse, accountInstance, transaction, reporterAccount } = options
173 const associateFun = async () => {
174 return { isOwned: accountInstance.isOwned() }
180 flaggedAccount: accountInstance,
187 isLocalLiveVideoAccepted,
189 isLocalVideoAccepted,
190 isLocalVideoThreadAccepted,
191 isRemoteVideoAccepted,
192 isRemoteVideoCommentAccepted,
193 isLocalVideoCommentReplyAccepted,
194 isPreImportVideoAccepted,
195 isPostImportVideoAccepted,
199 createVideoCommentAbuse,
203 // ---------------------------------------------------------------------------
205 async function createAbuse (options: {
206 base: FilteredModelAttributes<AbuseModel>
207 reporterAccount: MAccountDefault
208 flaggedAccount: MAccountLight
209 associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean} >
210 transaction: Transaction
212 const { base, reporterAccount, flaggedAccount, associateFun, transaction } = options
213 const auditLogger = auditLoggerFactory('abuse')
215 const abuseAttributes = Object.assign({}, base, { flaggedAccountId: flaggedAccount.id })
216 const abuseInstance: MAbuseFull = await AbuseModel.create(abuseAttributes, { transaction })
218 abuseInstance.ReporterAccount = reporterAccount
219 abuseInstance.FlaggedAccount = flaggedAccount
221 const { isOwned } = await associateFun(abuseInstance)
223 if (isOwned === false) {
224 await sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
227 const abuseJSON = abuseInstance.toFormattedAdminJSON()
228 auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON))
230 afterCommitIfTransaction(transaction, () => {
231 Notifier.Instance.notifyOnNewAbuse({
234 reporter: reporterAccount.Actor.getIdentifier()
238 logger.info('Abuse report %d created.', abuseInstance.id)