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'
17 MCommentAbuseAccountVideo,
21 MVideoAccountLightBlacklistAllFiles
22 } from '@server/types/models'
23 import { LiveVideoCreate, VideoCreate, VideoImportCreate } from '../../shared/models/videos'
24 import { VideoCommentCreate } from '../../shared/models/videos/comment'
25 import { UserModel } from '../models/user/user'
26 import { VideoModel } from '../models/video/video'
27 import { VideoCommentModel } from '../models/video/video-comment'
28 import { sendAbuse } from './activitypub/send/send-flag'
29 import { Notifier } from './notifier'
31 export type AcceptResult = {
36 // ---------------------------------------------------------------------------
38 // Stub function that can be filtered by plugins
39 function isLocalVideoAccepted (object: {
40 videoBody: VideoCreate
41 videoFile: VideoUploadFile
44 return { accepted: true }
47 // ---------------------------------------------------------------------------
49 // Stub function that can be filtered by plugins
50 function isLocalLiveVideoAccepted (object: {
51 liveVideoBody: LiveVideoCreate
54 return { accepted: true }
57 // ---------------------------------------------------------------------------
59 // Stub function that can be filtered by plugins
60 function isLocalVideoThreadAccepted (_object: {
61 commentBody: VideoCommentCreate
65 return { accepted: true }
68 // Stub function that can be filtered by plugins
69 function isLocalVideoCommentReplyAccepted (_object: {
70 commentBody: VideoCommentCreate
71 parentComment: VideoCommentModel
75 return { accepted: true }
78 // ---------------------------------------------------------------------------
80 // Stub function that can be filtered by plugins
81 function isRemoteVideoCommentAccepted (_object: {
84 return { accepted: true }
87 // ---------------------------------------------------------------------------
89 // Stub function that can be filtered by plugins
90 function isPreImportVideoAccepted (object: {
91 videoImportBody: VideoImportCreate
94 return { accepted: true }
97 // Stub function that can be filtered by plugins
98 function isPostImportVideoAccepted (object: {
99 videoFilePath: PathLike
100 videoFile: VideoFileModel
103 return { accepted: true }
106 // ---------------------------------------------------------------------------
108 async function createVideoAbuse (options: {
109 baseAbuse: FilteredModelAttributes<AbuseModel>
110 videoInstance: MVideoAccountLightBlacklistAllFiles
113 transaction: Transaction
114 reporterAccount: MAccountDefault
115 skipNotification: boolean
117 const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount, skipNotification } = options
119 const associateFun = async (abuseInstance: MAbuseFull) => {
120 const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({
121 abuseId: abuseInstance.id,
122 videoId: videoInstance.id,
127 videoAbuseInstance.Video = videoInstance
128 abuseInstance.VideoAbuse = videoAbuseInstance
130 return { isOwned: videoInstance.isOwned() }
136 flaggedAccount: videoInstance.VideoChannel.Account,
143 function createVideoCommentAbuse (options: {
144 baseAbuse: FilteredModelAttributes<AbuseModel>
145 commentInstance: MCommentOwnerVideo
146 transaction: Transaction
147 reporterAccount: MAccountDefault
148 skipNotification: boolean
150 const { baseAbuse, commentInstance, transaction, reporterAccount, skipNotification } = options
152 const associateFun = async (abuseInstance: MAbuseFull) => {
153 const commentAbuseInstance: MCommentAbuseAccountVideo = await VideoCommentAbuseModel.create({
154 abuseId: abuseInstance.id,
155 videoCommentId: commentInstance.id
158 commentAbuseInstance.VideoComment = commentInstance
159 abuseInstance.VideoCommentAbuse = commentAbuseInstance
161 return { isOwned: commentInstance.isOwned() }
167 flaggedAccount: commentInstance.Account,
174 function createAccountAbuse (options: {
175 baseAbuse: FilteredModelAttributes<AbuseModel>
176 accountInstance: MAccountDefault
177 transaction: Transaction
178 reporterAccount: MAccountDefault
179 skipNotification: boolean
181 const { baseAbuse, accountInstance, transaction, reporterAccount, skipNotification } = options
183 const associateFun = () => {
184 return Promise.resolve({ isOwned: accountInstance.isOwned() })
190 flaggedAccount: accountInstance,
197 // ---------------------------------------------------------------------------
200 isLocalLiveVideoAccepted,
202 isLocalVideoAccepted,
203 isLocalVideoThreadAccepted,
204 isRemoteVideoCommentAccepted,
205 isLocalVideoCommentReplyAccepted,
206 isPreImportVideoAccepted,
207 isPostImportVideoAccepted,
211 createVideoCommentAbuse,
215 // ---------------------------------------------------------------------------
217 async function createAbuse (options: {
218 base: FilteredModelAttributes<AbuseModel>
219 reporterAccount: MAccountDefault
220 flaggedAccount: MAccountLight
221 associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean} >
222 skipNotification: boolean
223 transaction: Transaction
225 const { base, reporterAccount, flaggedAccount, associateFun, transaction, skipNotification } = options
226 const auditLogger = auditLoggerFactory('abuse')
228 const abuseAttributes = Object.assign({}, base, { flaggedAccountId: flaggedAccount.id })
229 const abuseInstance: MAbuseFull = await AbuseModel.create(abuseAttributes, { transaction })
231 abuseInstance.ReporterAccount = reporterAccount
232 abuseInstance.FlaggedAccount = flaggedAccount
234 const { isOwned } = await associateFun(abuseInstance)
236 if (isOwned === false) {
237 sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
240 const abuseJSON = abuseInstance.toFormattedAdminJSON()
241 auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON))
243 if (!skipNotification) {
244 afterCommitIfTransaction(transaction, () => {
245 Notifier.Instance.notifyOnNewAbuse({
248 reporter: reporterAccount.Actor.getIdentifier()
253 logger.info('Abuse report %d created.', abuseInstance.id)