]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
bd45d503 | 2 | import { logger } from '@server/helpers/logger' |
d95d1559 | 3 | import { createAccountAbuse, createVideoAbuse, createVideoCommentAbuse } from '@server/lib/moderation' |
bd45d503 | 4 | import { Notifier } from '@server/lib/notifier' |
d95d1559 | 5 | import { AbuseModel } from '@server/models/abuse/abuse' |
edbc9325 | 6 | import { AbuseMessageModel } from '@server/models/abuse/abuse-message' |
d95d1559 | 7 | import { getServerActor } from '@server/models/application/application' |
bd45d503 | 8 | import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse' |
d17c7b4e | 9 | import { AbuseCreate, AbuseState, HttpStatusCode, UserRight } from '@shared/models' |
d95d1559 C |
10 | import { getFormattedObjects } from '../../helpers/utils' |
11 | import { sequelizeTypescript } from '../../initializers/database' | |
12 | import { | |
13 | abuseGetValidator, | |
edbc9325 | 14 | abuseListForAdminsValidator, |
d95d1559 C |
15 | abuseReportValidator, |
16 | abusesSortValidator, | |
17 | abuseUpdateValidator, | |
edbc9325 | 18 | addAbuseMessageValidator, |
e915cde3 | 19 | apiRateLimiter, |
d95d1559 C |
20 | asyncMiddleware, |
21 | asyncRetryTransactionMiddleware, | |
22 | authenticate, | |
94148c90 | 23 | checkAbuseValidForMessagesValidator, |
edbc9325 | 24 | deleteAbuseMessageValidator, |
d95d1559 | 25 | ensureUserHasRight, |
edbc9325 | 26 | getAbuseValidator, |
1333ab1f | 27 | openapiOperationDoc, |
d95d1559 C |
28 | paginationValidator, |
29 | setDefaultPagination, | |
30 | setDefaultSort | |
31 | } from '../../middlewares' | |
32 | import { AccountModel } from '../../models/account/account' | |
33 | ||
34 | const abuseRouter = express.Router() | |
35 | ||
e915cde3 C |
36 | abuseRouter.use(apiRateLimiter) |
37 | ||
57f6896f | 38 | abuseRouter.get('/', |
1333ab1f | 39 | openapiOperationDoc({ operationId: 'getAbuses' }), |
d95d1559 C |
40 | authenticate, |
41 | ensureUserHasRight(UserRight.MANAGE_ABUSES), | |
42 | paginationValidator, | |
43 | abusesSortValidator, | |
44 | setDefaultSort, | |
45 | setDefaultPagination, | |
edbc9325 C |
46 | abuseListForAdminsValidator, |
47 | asyncMiddleware(listAbusesForAdmins) | |
d95d1559 | 48 | ) |
57f6896f | 49 | abuseRouter.put('/:id', |
d95d1559 C |
50 | authenticate, |
51 | ensureUserHasRight(UserRight.MANAGE_ABUSES), | |
52 | asyncMiddleware(abuseUpdateValidator), | |
53 | asyncRetryTransactionMiddleware(updateAbuse) | |
54 | ) | |
57f6896f | 55 | abuseRouter.post('/', |
d95d1559 C |
56 | authenticate, |
57 | asyncMiddleware(abuseReportValidator), | |
58 | asyncRetryTransactionMiddleware(reportAbuse) | |
59 | ) | |
57f6896f | 60 | abuseRouter.delete('/:id', |
d95d1559 C |
61 | authenticate, |
62 | ensureUserHasRight(UserRight.MANAGE_ABUSES), | |
63 | asyncMiddleware(abuseGetValidator), | |
64 | asyncRetryTransactionMiddleware(deleteAbuse) | |
65 | ) | |
66 | ||
edbc9325 C |
67 | abuseRouter.get('/:id/messages', |
68 | authenticate, | |
69 | asyncMiddleware(getAbuseValidator), | |
94148c90 | 70 | checkAbuseValidForMessagesValidator, |
edbc9325 C |
71 | asyncRetryTransactionMiddleware(listAbuseMessages) |
72 | ) | |
73 | ||
74 | abuseRouter.post('/:id/messages', | |
75 | authenticate, | |
76 | asyncMiddleware(getAbuseValidator), | |
94148c90 | 77 | checkAbuseValidForMessagesValidator, |
edbc9325 C |
78 | addAbuseMessageValidator, |
79 | asyncRetryTransactionMiddleware(addAbuseMessage) | |
80 | ) | |
81 | ||
82 | abuseRouter.delete('/:id/messages/:messageId', | |
83 | authenticate, | |
84 | asyncMiddleware(getAbuseValidator), | |
94148c90 | 85 | checkAbuseValidForMessagesValidator, |
edbc9325 C |
86 | asyncMiddleware(deleteAbuseMessageValidator), |
87 | asyncRetryTransactionMiddleware(deleteAbuseMessage) | |
88 | ) | |
89 | ||
d95d1559 C |
90 | // --------------------------------------------------------------------------- |
91 | ||
92 | export { | |
7a4ea932 | 93 | abuseRouter |
d95d1559 C |
94 | } |
95 | ||
96 | // --------------------------------------------------------------------------- | |
97 | ||
edbc9325 | 98 | async function listAbusesForAdmins (req: express.Request, res: express.Response) { |
d95d1559 C |
99 | const user = res.locals.oauth.token.user |
100 | const serverActor = await getServerActor() | |
101 | ||
edbc9325 | 102 | const resultList = await AbuseModel.listForAdminApi({ |
d95d1559 C |
103 | start: req.query.start, |
104 | count: req.query.count, | |
105 | sort: req.query.sort, | |
106 | id: req.query.id, | |
57f6896f | 107 | filter: req.query.filter, |
d95d1559 C |
108 | predefinedReason: req.query.predefinedReason, |
109 | search: req.query.search, | |
110 | state: req.query.state, | |
111 | videoIs: req.query.videoIs, | |
112 | searchReporter: req.query.searchReporter, | |
113 | searchReportee: req.query.searchReportee, | |
114 | searchVideo: req.query.searchVideo, | |
115 | searchVideoChannel: req.query.searchVideoChannel, | |
116 | serverAccountId: serverActor.Account.id, | |
117 | user | |
118 | }) | |
119 | ||
edbc9325 C |
120 | return res.json({ |
121 | total: resultList.total, | |
122 | data: resultList.data.map(d => d.toFormattedAdminJSON()) | |
123 | }) | |
d95d1559 C |
124 | } |
125 | ||
126 | async function updateAbuse (req: express.Request, res: express.Response) { | |
127 | const abuse = res.locals.abuse | |
594d3e48 | 128 | let stateUpdated = false |
d95d1559 C |
129 | |
130 | if (req.body.moderationComment !== undefined) abuse.moderationComment = req.body.moderationComment | |
594d3e48 C |
131 | |
132 | if (req.body.state !== undefined) { | |
133 | abuse.state = req.body.state | |
134 | stateUpdated = true | |
135 | } | |
d95d1559 C |
136 | |
137 | await sequelizeTypescript.transaction(t => { | |
138 | return abuse.save({ transaction: t }) | |
139 | }) | |
140 | ||
594d3e48 C |
141 | if (stateUpdated === true) { |
142 | AbuseModel.loadFull(abuse.id) | |
143 | .then(abuseFull => Notifier.Instance.notifyOnAbuseStateChange(abuseFull)) | |
144 | .catch(err => logger.error('Cannot notify on abuse state change', { err })) | |
145 | } | |
edbc9325 | 146 | |
310b5219 | 147 | // Do not send the delete to other instances, we updated OUR copy of this abuse |
d95d1559 | 148 | |
76148b27 | 149 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
d95d1559 C |
150 | } |
151 | ||
152 | async function deleteAbuse (req: express.Request, res: express.Response) { | |
153 | const abuse = res.locals.abuse | |
154 | ||
155 | await sequelizeTypescript.transaction(t => { | |
156 | return abuse.destroy({ transaction: t }) | |
157 | }) | |
158 | ||
310b5219 | 159 | // Do not send the delete to other instances, we delete OUR copy of this abuse |
d95d1559 | 160 | |
76148b27 | 161 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
d95d1559 C |
162 | } |
163 | ||
164 | async function reportAbuse (req: express.Request, res: express.Response) { | |
165 | const videoInstance = res.locals.videoAll | |
166 | const commentInstance = res.locals.videoCommentFull | |
167 | const accountInstance = res.locals.account | |
168 | ||
169 | const body: AbuseCreate = req.body | |
170 | ||
171 | const { id } = await sequelizeTypescript.transaction(async t => { | |
9e847c17 C |
172 | const user = res.locals.oauth.token.User |
173 | // Don't send abuse notification if reporter is an admin/moderator | |
174 | const skipNotification = user.hasRight(UserRight.MANAGE_ABUSES) | |
175 | ||
176 | const reporterAccount = await AccountModel.load(user.Account.id, t) | |
d95d1559 C |
177 | const predefinedReasons = body.predefinedReasons?.map(r => abusePredefinedReasonsMap[r]) |
178 | ||
179 | const baseAbuse = { | |
180 | reporterAccountId: reporterAccount.id, | |
181 | reason: body.reason, | |
182 | state: AbuseState.PENDING, | |
183 | predefinedReasons | |
184 | } | |
185 | ||
186 | if (body.video) { | |
187 | return createVideoAbuse({ | |
188 | baseAbuse, | |
189 | videoInstance, | |
190 | reporterAccount, | |
191 | transaction: t, | |
192 | startAt: body.video.startAt, | |
9e847c17 C |
193 | endAt: body.video.endAt, |
194 | skipNotification | |
d95d1559 C |
195 | }) |
196 | } | |
197 | ||
198 | if (body.comment) { | |
199 | return createVideoCommentAbuse({ | |
200 | baseAbuse, | |
201 | commentInstance, | |
202 | reporterAccount, | |
9e847c17 C |
203 | transaction: t, |
204 | skipNotification | |
d95d1559 C |
205 | }) |
206 | } | |
207 | ||
208 | // Account report | |
209 | return createAccountAbuse({ | |
210 | baseAbuse, | |
211 | accountInstance, | |
212 | reporterAccount, | |
9e847c17 C |
213 | transaction: t, |
214 | skipNotification | |
d95d1559 C |
215 | }) |
216 | }) | |
217 | ||
218 | return res.json({ abuse: { id } }) | |
219 | } | |
edbc9325 C |
220 | |
221 | async function listAbuseMessages (req: express.Request, res: express.Response) { | |
222 | const abuse = res.locals.abuse | |
223 | ||
224 | const resultList = await AbuseMessageModel.listForApi(abuse.id) | |
225 | ||
226 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
227 | } | |
228 | ||
229 | async function addAbuseMessage (req: express.Request, res: express.Response) { | |
230 | const abuse = res.locals.abuse | |
231 | const user = res.locals.oauth.token.user | |
232 | ||
233 | const abuseMessage = await AbuseMessageModel.create({ | |
234 | message: req.body.message, | |
235 | byModerator: abuse.reporterAccountId !== user.Account.id, | |
236 | accountId: user.Account.id, | |
237 | abuseId: abuse.id | |
238 | }) | |
239 | ||
594d3e48 C |
240 | AbuseModel.loadFull(abuse.id) |
241 | .then(abuseFull => Notifier.Instance.notifyOnAbuseMessage(abuseFull, abuseMessage)) | |
242 | .catch(err => logger.error('Cannot notify on new abuse message', { err })) | |
edbc9325 C |
243 | |
244 | return res.json({ | |
245 | abuseMessage: { | |
246 | id: abuseMessage.id | |
247 | } | |
248 | }) | |
249 | } | |
250 | ||
251 | async function deleteAbuseMessage (req: express.Request, res: express.Response) { | |
252 | const abuseMessage = res.locals.abuseMessage | |
253 | ||
254 | await sequelizeTypescript.transaction(t => { | |
255 | return abuseMessage.destroy({ transaction: t }) | |
256 | }) | |
257 | ||
76148b27 | 258 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
edbc9325 | 259 | } |