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