]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/moderation.ts
Use 3 tables to represent abuses
[github/Chocobozzz/PeerTube.git] / server / lib / moderation.ts
CommitLineData
d95d1559
C
1import { PathLike } from 'fs-extra'
2import { Transaction } from 'sequelize/types'
3import { AbuseAuditView, auditLoggerFactory } from '@server/helpers/audit-logger'
4import { logger } from '@server/helpers/logger'
5import { AbuseModel } from '@server/models/abuse/abuse'
6import { VideoAbuseModel } from '@server/models/abuse/video-abuse'
7import { VideoCommentAbuseModel } from '@server/models/abuse/video-comment-abuse'
8import { VideoFileModel } from '@server/models/video/video-file'
9import { FilteredModelAttributes } from '@server/types'
10import {
11 MAbuseFull,
12 MAccountDefault,
13 MAccountLight,
14 MCommentAbuseAccountVideo,
15 MCommentOwnerVideo,
16 MUser,
17 MVideoAbuseVideoFull,
18 MVideoAccountLightBlacklistAllFiles
19} from '@server/types/models'
20import { ActivityCreate } from '../../shared/models/activitypub'
21import { VideoTorrentObject } from '../../shared/models/activitypub/objects'
22import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object'
2158ac90 23import { VideoCreate, VideoImportCreate } from '../../shared/models/videos'
d95d1559 24import { VideoCommentCreate } from '../../shared/models/videos/video-comment.model'
b4055e1c 25import { UserModel } from '../models/account/user'
b4055e1c 26import { ActorModel } from '../models/activitypub/actor'
d95d1559
C
27import { VideoModel } from '../models/video/video'
28import { VideoCommentModel } from '../models/video/video-comment'
29import { sendAbuse } from './activitypub/send/send-flag'
30import { Notifier } from './notifier'
b4055e1c
C
31
32export type AcceptResult = {
33 accepted: boolean
34 errorMessage?: string
35}
36
37// Can be filtered by plugins
38function isLocalVideoAccepted (object: {
a1587156
C
39 videoBody: VideoCreate
40 videoFile: Express.Multer.File & { duration?: number }
b4055e1c
C
41 user: UserModel
42}): AcceptResult {
43 return { accepted: true }
44}
45
46function isLocalVideoThreadAccepted (_object: {
a1587156
C
47 commentBody: VideoCommentCreate
48 video: VideoModel
b4055e1c
C
49 user: UserModel
50}): AcceptResult {
51 return { accepted: true }
52}
53
54function isLocalVideoCommentReplyAccepted (_object: {
a1587156
C
55 commentBody: VideoCommentCreate
56 parentComment: VideoCommentModel
57 video: VideoModel
b4055e1c
C
58 user: UserModel
59}): AcceptResult {
60 return { accepted: true }
61}
62
63function isRemoteVideoAccepted (_object: {
a1587156
C
64 activity: ActivityCreate
65 videoAP: VideoTorrentObject
b4055e1c
C
66 byActor: ActorModel
67}): AcceptResult {
68 return { accepted: true }
69}
70
71function isRemoteVideoCommentAccepted (_object: {
a1587156
C
72 activity: ActivityCreate
73 commentAP: VideoCommentObject
b4055e1c
C
74 byActor: ActorModel
75}): AcceptResult {
76 return { accepted: true }
77}
78
2158ac90
RK
79function isPreImportVideoAccepted (object: {
80 videoImportBody: VideoImportCreate
81 user: MUser
82}): AcceptResult {
83 return { accepted: true }
84}
85
86function isPostImportVideoAccepted (object: {
87 videoFilePath: PathLike
88 videoFile: VideoFileModel
89 user: MUser
90}): AcceptResult {
91 return { accepted: true }
92}
93
d95d1559
C
94async 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
127function 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
156function 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
b4055e1c
C
177export {
178 isLocalVideoAccepted,
179 isLocalVideoThreadAccepted,
180 isRemoteVideoAccepted,
181 isRemoteVideoCommentAccepted,
2158ac90
RK
182 isLocalVideoCommentReplyAccepted,
183 isPreImportVideoAccepted,
d95d1559
C
184 isPostImportVideoAccepted,
185
186 createAbuse,
187 createVideoAbuse,
188 createVideoCommentAbuse,
189 createAccountAbuse
190}
191
192// ---------------------------------------------------------------------------
193
194async 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
b4055e1c 228}