]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/moderation.ts
Fix moderation notification
[github/Chocobozzz/PeerTube.git] / server / lib / moderation.ts
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'
10 import {
11 MAbuseFull,
12 MAccountDefault,
13 MAccountLight,
14 MCommentAbuseAccountVideo,
15 MCommentOwnerVideo,
16 MUser,
17 MVideoAbuseVideoFull,
18 MVideoAccountLightBlacklistAllFiles
19 } from '@server/types/models'
20 import { ActivityCreate } from '../../shared/models/activitypub'
21 import { VideoObject } from '../../shared/models/activitypub/objects'
22 import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object'
23 import { LiveVideoCreate, 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'
31 import { afterCommitIfTransaction } from '@server/helpers/database-utils'
32
33 export type AcceptResult = {
34 accepted: boolean
35 errorMessage?: string
36 }
37
38 // Can be filtered by plugins
39 function isLocalVideoAccepted (object: {
40 videoBody: VideoCreate
41 videoFile: Express.Multer.File & { duration?: number }
42 user: UserModel
43 }): AcceptResult {
44 return { accepted: true }
45 }
46
47 function isLocalLiveVideoAccepted (object: {
48 liveVideoBody: LiveVideoCreate
49 user: UserModel
50 }): AcceptResult {
51 return { accepted: true }
52 }
53
54 function isLocalVideoThreadAccepted (_object: {
55 commentBody: VideoCommentCreate
56 video: VideoModel
57 user: UserModel
58 }): AcceptResult {
59 return { accepted: true }
60 }
61
62 function isLocalVideoCommentReplyAccepted (_object: {
63 commentBody: VideoCommentCreate
64 parentComment: VideoCommentModel
65 video: VideoModel
66 user: UserModel
67 }): AcceptResult {
68 return { accepted: true }
69 }
70
71 function isRemoteVideoAccepted (_object: {
72 activity: ActivityCreate
73 videoAP: VideoObject
74 byActor: ActorModel
75 }): AcceptResult {
76 return { accepted: true }
77 }
78
79 function isRemoteVideoCommentAccepted (_object: {
80 activity: ActivityCreate
81 commentAP: VideoCommentObject
82 byActor: ActorModel
83 }): AcceptResult {
84 return { accepted: true }
85 }
86
87 function isPreImportVideoAccepted (object: {
88 videoImportBody: VideoImportCreate
89 user: MUser
90 }): AcceptResult {
91 return { accepted: true }
92 }
93
94 function isPostImportVideoAccepted (object: {
95 videoFilePath: PathLike
96 videoFile: VideoFileModel
97 user: MUser
98 }): AcceptResult {
99 return { accepted: true }
100 }
101
102 async function createVideoAbuse (options: {
103 baseAbuse: FilteredModelAttributes<AbuseModel>
104 videoInstance: MVideoAccountLightBlacklistAllFiles
105 startAt: number
106 endAt: number
107 transaction: Transaction
108 reporterAccount: MAccountDefault
109 }) {
110 const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount } = options
111
112 const associateFun = async (abuseInstance: MAbuseFull) => {
113 const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({
114 abuseId: abuseInstance.id,
115 videoId: videoInstance.id,
116 startAt: startAt,
117 endAt: endAt
118 }, { transaction })
119
120 videoAbuseInstance.Video = videoInstance
121 abuseInstance.VideoAbuse = videoAbuseInstance
122
123 return { isOwned: videoInstance.isOwned() }
124 }
125
126 return createAbuse({
127 base: baseAbuse,
128 reporterAccount,
129 flaggedAccount: videoInstance.VideoChannel.Account,
130 transaction,
131 associateFun
132 })
133 }
134
135 function createVideoCommentAbuse (options: {
136 baseAbuse: FilteredModelAttributes<AbuseModel>
137 commentInstance: MCommentOwnerVideo
138 transaction: Transaction
139 reporterAccount: MAccountDefault
140 }) {
141 const { baseAbuse, commentInstance, transaction, reporterAccount } = options
142
143 const associateFun = async (abuseInstance: MAbuseFull) => {
144 const commentAbuseInstance: MCommentAbuseAccountVideo = await VideoCommentAbuseModel.create({
145 abuseId: abuseInstance.id,
146 videoCommentId: commentInstance.id
147 }, { transaction })
148
149 commentAbuseInstance.VideoComment = commentInstance
150 abuseInstance.VideoCommentAbuse = commentAbuseInstance
151
152 return { isOwned: commentInstance.isOwned() }
153 }
154
155 return createAbuse({
156 base: baseAbuse,
157 reporterAccount,
158 flaggedAccount: commentInstance.Account,
159 transaction,
160 associateFun
161 })
162 }
163
164 function createAccountAbuse (options: {
165 baseAbuse: FilteredModelAttributes<AbuseModel>
166 accountInstance: MAccountDefault
167 transaction: Transaction
168 reporterAccount: MAccountDefault
169 }) {
170 const { baseAbuse, accountInstance, transaction, reporterAccount } = options
171
172 const associateFun = async () => {
173 return { isOwned: accountInstance.isOwned() }
174 }
175
176 return createAbuse({
177 base: baseAbuse,
178 reporterAccount,
179 flaggedAccount: accountInstance,
180 transaction,
181 associateFun
182 })
183 }
184
185 export {
186 isLocalLiveVideoAccepted,
187
188 isLocalVideoAccepted,
189 isLocalVideoThreadAccepted,
190 isRemoteVideoAccepted,
191 isRemoteVideoCommentAccepted,
192 isLocalVideoCommentReplyAccepted,
193 isPreImportVideoAccepted,
194 isPostImportVideoAccepted,
195
196 createAbuse,
197 createVideoAbuse,
198 createVideoCommentAbuse,
199 createAccountAbuse
200 }
201
202 // ---------------------------------------------------------------------------
203
204 async function createAbuse (options: {
205 base: FilteredModelAttributes<AbuseModel>
206 reporterAccount: MAccountDefault
207 flaggedAccount: MAccountLight
208 associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean} >
209 transaction: Transaction
210 }) {
211 const { base, reporterAccount, flaggedAccount, associateFun, transaction } = options
212 const auditLogger = auditLoggerFactory('abuse')
213
214 const abuseAttributes = Object.assign({}, base, { flaggedAccountId: flaggedAccount.id })
215 const abuseInstance: MAbuseFull = await AbuseModel.create(abuseAttributes, { transaction })
216
217 abuseInstance.ReporterAccount = reporterAccount
218 abuseInstance.FlaggedAccount = flaggedAccount
219
220 const { isOwned } = await associateFun(abuseInstance)
221
222 if (isOwned === false) {
223 await sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
224 }
225
226 const abuseJSON = abuseInstance.toFormattedAdminJSON()
227 auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON))
228
229 afterCommitIfTransaction(transaction, () => {
230 Notifier.Instance.notifyOnNewAbuse({
231 abuse: abuseJSON,
232 abuseInstance,
233 reporter: reporterAccount.Actor.getIdentifier()
234 })
235 })
236
237 logger.info('Abuse report %d created.', abuseInstance.id)
238
239 return abuseJSON
240 }