1 import { PathLike } from 'fs-extra'
2 import { Transaction } from 'sequelize/types'
3 import { AbuseAuditView, auditLoggerFactory } from '@server/helpers/audit-logger'
4 import { logger } from '@server/helpers/logger'
5 import { AbuseModel } from '@server/models/abuse/abuse'
6 import { VideoAbuseModel } from '@server/models/abuse/video-abuse'
7 import { VideoCommentAbuseModel } from '@server/models/abuse/video-comment-abuse'
8 import { VideoFileModel } from '@server/models/video/video-file'
9 import { FilteredModelAttributes } from '@server/types'
14 MCommentAbuseAccountVideo,
18 MVideoAccountLightBlacklistAllFiles
19 } from '@server/types/models'
20 import { ActivityCreate } from '../../shared/models/activitypub'
21 import { VideoTorrentObject } from '../../shared/models/activitypub/objects'
22 import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object'
23 import { VideoCreate, VideoImportCreate } from '../../shared/models/videos'
24 import { VideoCommentCreate } from '../../shared/models/videos/video-comment.model'
25 import { UserModel } from '../models/account/user'
26 import { ActorModel } from '../models/activitypub/actor'
27 import { VideoModel } from '../models/video/video'
28 import { VideoCommentModel } from '../models/video/video-comment'
29 import { sendAbuse } from './activitypub/send/send-flag'
30 import { Notifier } from './notifier'
32 export type AcceptResult = {
37 // Can be filtered by plugins
38 function isLocalVideoAccepted (object: {
39 videoBody: VideoCreate
40 videoFile: Express.Multer.File & { duration?: number }
43 return { accepted: true }
46 function isLocalVideoThreadAccepted (_object: {
47 commentBody: VideoCommentCreate
51 return { accepted: true }
54 function isLocalVideoCommentReplyAccepted (_object: {
55 commentBody: VideoCommentCreate
56 parentComment: VideoCommentModel
60 return { accepted: true }
63 function isRemoteVideoAccepted (_object: {
64 activity: ActivityCreate
65 videoAP: VideoTorrentObject
68 return { accepted: true }
71 function isRemoteVideoCommentAccepted (_object: {
72 activity: ActivityCreate
73 commentAP: VideoCommentObject
76 return { accepted: true }
79 function isPreImportVideoAccepted (object: {
80 videoImportBody: VideoImportCreate
83 return { accepted: true }
86 function isPostImportVideoAccepted (object: {
87 videoFilePath: PathLike
88 videoFile: VideoFileModel
91 return { accepted: true }
94 async function createVideoAbuse (options: {
95 baseAbuse: FilteredModelAttributes<AbuseModel>
96 videoInstance: MVideoAccountLightBlacklistAllFiles
99 transaction: Transaction
100 reporterAccount: MAccountDefault
102 const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount } = options
104 const associateFun = async (abuseInstance: MAbuseFull) => {
105 const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({
106 abuseId: abuseInstance.id,
107 videoId: videoInstance.id,
112 videoAbuseInstance.Video = videoInstance
113 abuseInstance.VideoAbuse = videoAbuseInstance
115 return { isOwned: videoInstance.isOwned() }
121 flaggedAccount: videoInstance.VideoChannel.Account,
127 function createVideoCommentAbuse (options: {
128 baseAbuse: FilteredModelAttributes<AbuseModel>
129 commentInstance: MCommentOwnerVideo
130 transaction: Transaction
131 reporterAccount: MAccountDefault
133 const { baseAbuse, commentInstance, transaction, reporterAccount } = options
135 const associateFun = async (abuseInstance: MAbuseFull) => {
136 const commentAbuseInstance: MCommentAbuseAccountVideo = await VideoCommentAbuseModel.create({
137 abuseId: abuseInstance.id,
138 videoCommentId: commentInstance.id
141 commentAbuseInstance.VideoComment = commentInstance
142 abuseInstance.VideoCommentAbuse = commentAbuseInstance
144 return { isOwned: commentInstance.isOwned() }
150 flaggedAccount: commentInstance.Account,
156 function createAccountAbuse (options: {
157 baseAbuse: FilteredModelAttributes<AbuseModel>
158 accountInstance: MAccountDefault
159 transaction: Transaction
160 reporterAccount: MAccountDefault
162 const { baseAbuse, accountInstance, transaction, reporterAccount } = options
164 const associateFun = async () => {
165 return { isOwned: accountInstance.isOwned() }
171 flaggedAccount: accountInstance,
178 isLocalVideoAccepted,
179 isLocalVideoThreadAccepted,
180 isRemoteVideoAccepted,
181 isRemoteVideoCommentAccepted,
182 isLocalVideoCommentReplyAccepted,
183 isPreImportVideoAccepted,
184 isPostImportVideoAccepted,
188 createVideoCommentAbuse,
192 // ---------------------------------------------------------------------------
194 async function createAbuse (options: {
195 base: FilteredModelAttributes<AbuseModel>
196 reporterAccount: MAccountDefault
197 flaggedAccount: MAccountLight
198 associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean} >
199 transaction: Transaction
201 const { base, reporterAccount, flaggedAccount, associateFun, transaction } = options
202 const auditLogger = auditLoggerFactory('abuse')
204 const abuseAttributes = Object.assign({}, base, { flaggedAccountId: flaggedAccount.id })
205 const abuseInstance: MAbuseFull = await AbuseModel.create(abuseAttributes, { transaction })
207 abuseInstance.ReporterAccount = reporterAccount
208 abuseInstance.FlaggedAccount = flaggedAccount
210 const { isOwned } = await associateFun(abuseInstance)
212 if (isOwned === false) {
213 await sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
216 const abuseJSON = abuseInstance.toFormattedAdminJSON()
217 auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON))
219 Notifier.Instance.notifyOnNewAbuse({
222 reporter: reporterAccount.Actor.getIdentifier()
225 logger.info('Abuse report %d created.', abuseInstance.id)