]>
Commit | Line | Data |
---|---|---|
1 | import * as debug from 'debug' | |
2 | import truncate from 'lodash-es/truncate' | |
3 | import { SortMeta } from 'primeng/api' | |
4 | import { Component, Input, OnInit, ViewChild } from '@angular/core' | |
5 | import { ActivatedRoute, Router } from '@angular/router' | |
6 | import { ConfirmService, MarkdownService, Notifier, RestPagination, RestTable } from '@app/core' | |
7 | import { Account, Actor, DropdownAction, Video, VideoService } from '@app/shared/shared-main' | |
8 | import { AbuseService, BlocklistService, VideoBlockService } from '@app/shared/shared-moderation' | |
9 | import { VideoCommentService } from '@app/shared/shared-video-comment' | |
10 | import { logger } from '@root-helpers/logger' | |
11 | import { AbuseState, AdminAbuse } from '@shared/models' | |
12 | import { AdvancedInputFilter } from '../shared-forms' | |
13 | import { AbuseMessageModalComponent } from './abuse-message-modal.component' | |
14 | import { ModerationCommentModalComponent } from './moderation-comment-modal.component' | |
15 | import { ProcessedAbuse } from './processed-abuse.model' | |
16 | ||
17 | const debugLogger = debug('peertube:moderation:AbuseListTableComponent') | |
18 | ||
19 | @Component({ | |
20 | selector: 'my-abuse-list-table', | |
21 | templateUrl: './abuse-list-table.component.html', | |
22 | styleUrls: [ '../shared-moderation/moderation.scss', './abuse-list-table.component.scss' ] | |
23 | }) | |
24 | export class AbuseListTableComponent extends RestTable implements OnInit { | |
25 | @Input() viewType: 'admin' | 'user' | |
26 | ||
27 | @ViewChild('abuseMessagesModal', { static: true }) abuseMessagesModal: AbuseMessageModalComponent | |
28 | @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent | |
29 | ||
30 | abuses: ProcessedAbuse[] = [] | |
31 | totalRecords = 0 | |
32 | sort: SortMeta = { field: 'createdAt', order: 1 } | |
33 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } | |
34 | ||
35 | abuseActions: DropdownAction<ProcessedAbuse>[][] = [] | |
36 | ||
37 | inputFilters: AdvancedInputFilter[] = [ | |
38 | { | |
39 | title: $localize`Advanced filters`, | |
40 | children: [ | |
41 | { | |
42 | value: 'state:pending', | |
43 | label: $localize`Unsolved reports` | |
44 | }, | |
45 | { | |
46 | value: 'state:accepted', | |
47 | label: $localize`Accepted reports` | |
48 | }, | |
49 | { | |
50 | value: 'state:rejected', | |
51 | label: $localize`Refused reports` | |
52 | }, | |
53 | { | |
54 | value: 'videoIs:blacklisted', | |
55 | label: $localize`Reports with blocked videos` | |
56 | }, | |
57 | { | |
58 | value: 'videoIs:deleted', | |
59 | label: $localize`Reports with deleted videos` | |
60 | } | |
61 | ] | |
62 | } | |
63 | ] | |
64 | ||
65 | constructor ( | |
66 | protected route: ActivatedRoute, | |
67 | protected router: Router, | |
68 | private notifier: Notifier, | |
69 | private abuseService: AbuseService, | |
70 | private blocklistService: BlocklistService, | |
71 | private commentService: VideoCommentService, | |
72 | private videoService: VideoService, | |
73 | private videoBlocklistService: VideoBlockService, | |
74 | private confirmService: ConfirmService, | |
75 | private markdownRenderer: MarkdownService | |
76 | ) { | |
77 | super() | |
78 | } | |
79 | ||
80 | ngOnInit () { | |
81 | this.abuseActions = [ | |
82 | this.buildInternalActions(), | |
83 | ||
84 | this.buildFlaggedAccountActions(), | |
85 | ||
86 | this.buildCommentActions(), | |
87 | ||
88 | this.buildVideoActions(), | |
89 | ||
90 | this.buildAccountActions() | |
91 | ] | |
92 | ||
93 | this.initialize() | |
94 | } | |
95 | ||
96 | isAdminView () { | |
97 | return this.viewType === 'admin' | |
98 | } | |
99 | ||
100 | getIdentifier () { | |
101 | return 'AbuseListTableComponent' | |
102 | } | |
103 | ||
104 | openModerationCommentModal (abuse: AdminAbuse) { | |
105 | this.moderationCommentModal.openModal(abuse) | |
106 | } | |
107 | ||
108 | onModerationCommentUpdated () { | |
109 | this.reloadData() | |
110 | } | |
111 | ||
112 | isAbuseAccepted (abuse: AdminAbuse) { | |
113 | return abuse.state.id === AbuseState.ACCEPTED | |
114 | } | |
115 | ||
116 | isAbuseRejected (abuse: AdminAbuse) { | |
117 | return abuse.state.id === AbuseState.REJECTED | |
118 | } | |
119 | ||
120 | getVideoUrl (abuse: AdminAbuse) { | |
121 | return Video.buildWatchUrl(abuse.video) | |
122 | } | |
123 | ||
124 | getCommentUrl (abuse: AdminAbuse) { | |
125 | return Video.buildWatchUrl(abuse.comment.video) + ';threadId=' + abuse.comment.threadId | |
126 | } | |
127 | ||
128 | getAccountUrl (abuse: ProcessedAbuse) { | |
129 | return '/a/' + abuse.flaggedAccount.nameWithHost | |
130 | } | |
131 | ||
132 | async removeAbuse (abuse: AdminAbuse) { | |
133 | const res = await this.confirmService.confirm($localize`Do you really want to delete this abuse report?`, $localize`Delete`) | |
134 | if (res === false) return | |
135 | ||
136 | this.abuseService.removeAbuse(abuse) | |
137 | .subscribe({ | |
138 | next: () => { | |
139 | this.notifier.success($localize`Abuse deleted.`) | |
140 | this.reloadData() | |
141 | }, | |
142 | ||
143 | error: err => this.notifier.error(err.message) | |
144 | }) | |
145 | } | |
146 | ||
147 | updateAbuseState (abuse: AdminAbuse, state: AbuseState) { | |
148 | this.abuseService.updateAbuse(abuse, { state }) | |
149 | .subscribe({ | |
150 | next: () => this.reloadData(), | |
151 | ||
152 | error: err => this.notifier.error(err.message) | |
153 | }) | |
154 | } | |
155 | ||
156 | onCountMessagesUpdated (event: { abuseId: number, countMessages: number }) { | |
157 | const abuse = this.abuses.find(a => a.id === event.abuseId) | |
158 | ||
159 | if (!abuse) { | |
160 | logger.error(`Cannot find abuse ${event.abuseId}`) | |
161 | return | |
162 | } | |
163 | ||
164 | abuse.countMessages = event.countMessages | |
165 | } | |
166 | ||
167 | openAbuseMessagesModal (abuse: AdminAbuse) { | |
168 | this.abuseMessagesModal.openModal(abuse) | |
169 | } | |
170 | ||
171 | isLocalAbuse (abuse: AdminAbuse) { | |
172 | if (this.viewType === 'user') return true | |
173 | if (!abuse.reporterAccount) return false | |
174 | ||
175 | return Actor.IS_LOCAL(abuse.reporterAccount.host) | |
176 | } | |
177 | ||
178 | protected reloadDataInternal () { | |
179 | debugLogger('Loading data.') | |
180 | ||
181 | const options = { | |
182 | pagination: this.pagination, | |
183 | sort: this.sort, | |
184 | search: this.search | |
185 | } | |
186 | ||
187 | const observable = this.viewType === 'admin' | |
188 | ? this.abuseService.getAdminAbuses(options) | |
189 | : this.abuseService.getUserAbuses(options) | |
190 | ||
191 | return observable.subscribe({ | |
192 | next: async resultList => { | |
193 | this.totalRecords = resultList.total | |
194 | ||
195 | this.abuses = [] | |
196 | ||
197 | for (const a of resultList.data) { | |
198 | const abuse = a as ProcessedAbuse | |
199 | ||
200 | abuse.reasonHtml = await this.toHtml(abuse.reason) | |
201 | ||
202 | if (abuse.moderationComment) { | |
203 | abuse.moderationCommentHtml = await this.toHtml(abuse.moderationComment) | |
204 | } | |
205 | ||
206 | if (abuse.video) { | |
207 | if (abuse.video.channel?.ownerAccount) { | |
208 | abuse.video.channel.ownerAccount = new Account(abuse.video.channel.ownerAccount) | |
209 | } | |
210 | } | |
211 | ||
212 | if (abuse.comment) { | |
213 | if (abuse.comment.deleted) { | |
214 | abuse.truncatedCommentHtml = abuse.commentHtml = $localize`Deleted comment` | |
215 | } else { | |
216 | const truncated = truncate(abuse.comment.text, { length: 100 }) | |
217 | abuse.truncatedCommentHtml = await this.markdownRenderer.textMarkdownToHTML({ markdown: truncated, withHtml: true }) | |
218 | abuse.commentHtml = await this.markdownRenderer.textMarkdownToHTML({ markdown: abuse.comment.text, withHtml: true }) | |
219 | } | |
220 | } | |
221 | ||
222 | if (abuse.reporterAccount) { | |
223 | abuse.reporterAccount = new Account(abuse.reporterAccount) | |
224 | } | |
225 | ||
226 | if (abuse.flaggedAccount) { | |
227 | abuse.flaggedAccount = new Account(abuse.flaggedAccount) | |
228 | } | |
229 | ||
230 | if (abuse.updatedAt === abuse.createdAt) delete abuse.updatedAt | |
231 | ||
232 | this.abuses.push(abuse) | |
233 | } | |
234 | }, | |
235 | ||
236 | error: err => this.notifier.error(err.message) | |
237 | }) | |
238 | } | |
239 | ||
240 | private buildInternalActions (): DropdownAction<ProcessedAbuse>[] { | |
241 | return [ | |
242 | { | |
243 | label: $localize`Internal actions`, | |
244 | isHeader: true | |
245 | }, | |
246 | { | |
247 | label: this.isAdminView() | |
248 | ? $localize`Messages with reporter` | |
249 | : $localize`Messages with moderators`, | |
250 | handler: abuse => this.openAbuseMessagesModal(abuse), | |
251 | isDisplayed: abuse => this.isLocalAbuse(abuse) | |
252 | }, | |
253 | { | |
254 | label: $localize`Update internal note`, | |
255 | handler: abuse => this.openModerationCommentModal(abuse), | |
256 | isDisplayed: abuse => this.isAdminView() && !!abuse.moderationComment | |
257 | }, | |
258 | { | |
259 | label: $localize`Mark as accepted`, | |
260 | handler: abuse => this.updateAbuseState(abuse, AbuseState.ACCEPTED), | |
261 | isDisplayed: abuse => this.isAdminView() && !this.isAbuseAccepted(abuse) | |
262 | }, | |
263 | { | |
264 | label: $localize`Mark as rejected`, | |
265 | handler: abuse => this.updateAbuseState(abuse, AbuseState.REJECTED), | |
266 | isDisplayed: abuse => this.isAdminView() && !this.isAbuseRejected(abuse) | |
267 | }, | |
268 | { | |
269 | label: $localize`Add internal note`, | |
270 | handler: abuse => this.openModerationCommentModal(abuse), | |
271 | isDisplayed: abuse => this.isAdminView() && !abuse.moderationComment | |
272 | }, | |
273 | { | |
274 | label: $localize`Delete report`, | |
275 | handler: abuse => this.removeAbuse(abuse), | |
276 | isDisplayed: () => this.isAdminView() | |
277 | } | |
278 | ] | |
279 | } | |
280 | ||
281 | private buildFlaggedAccountActions (): DropdownAction<ProcessedAbuse>[] { | |
282 | if (!this.isAdminView()) return [] | |
283 | ||
284 | return [ | |
285 | { | |
286 | label: $localize`Actions for the flagged account`, | |
287 | isHeader: true, | |
288 | isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video | |
289 | }, | |
290 | ||
291 | { | |
292 | label: $localize`Mute account`, | |
293 | isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video, | |
294 | handler: abuse => this.muteAccountHelper(abuse.flaggedAccount) | |
295 | }, | |
296 | ||
297 | { | |
298 | label: $localize`Mute server account`, | |
299 | isDisplayed: abuse => abuse.flaggedAccount && !abuse.comment && !abuse.video, | |
300 | handler: abuse => this.muteServerHelper(abuse.flaggedAccount.host) | |
301 | } | |
302 | ] | |
303 | } | |
304 | ||
305 | private buildAccountActions (): DropdownAction<ProcessedAbuse>[] { | |
306 | if (!this.isAdminView()) return [] | |
307 | ||
308 | return [ | |
309 | { | |
310 | label: $localize`Actions for the reporter`, | |
311 | isHeader: true, | |
312 | isDisplayed: abuse => !!abuse.reporterAccount | |
313 | }, | |
314 | ||
315 | { | |
316 | label: $localize`Mute reporter`, | |
317 | isDisplayed: abuse => !!abuse.reporterAccount, | |
318 | handler: abuse => this.muteAccountHelper(abuse.reporterAccount) | |
319 | }, | |
320 | ||
321 | { | |
322 | label: $localize`Mute server`, | |
323 | isDisplayed: abuse => abuse.reporterAccount && !abuse.reporterAccount.userId, | |
324 | handler: abuse => this.muteServerHelper(abuse.reporterAccount.host) | |
325 | } | |
326 | ] | |
327 | } | |
328 | ||
329 | private buildVideoActions (): DropdownAction<ProcessedAbuse>[] { | |
330 | if (!this.isAdminView()) return [] | |
331 | ||
332 | return [ | |
333 | { | |
334 | label: $localize`Actions for the video`, | |
335 | isHeader: true, | |
336 | isDisplayed: abuse => abuse.video && !abuse.video.deleted | |
337 | }, | |
338 | { | |
339 | label: $localize`Block video`, | |
340 | isDisplayed: abuse => abuse.video && !abuse.video.deleted && !abuse.video.blacklisted, | |
341 | handler: abuse => { | |
342 | this.videoBlocklistService.blockVideo([ { videoId: abuse.video.id, unfederate: abuse.video.channel.isLocal } ]) | |
343 | .subscribe({ | |
344 | next: () => { | |
345 | this.notifier.success($localize`Video blocked.`) | |
346 | ||
347 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
348 | }, | |
349 | ||
350 | error: err => this.notifier.error(err.message) | |
351 | }) | |
352 | } | |
353 | }, | |
354 | { | |
355 | label: $localize`Unblock video`, | |
356 | isDisplayed: abuse => abuse.video && !abuse.video.deleted && abuse.video.blacklisted, | |
357 | handler: abuse => { | |
358 | this.videoBlocklistService.unblockVideo(abuse.video.id) | |
359 | .subscribe({ | |
360 | next: () => { | |
361 | this.notifier.success($localize`Video unblocked.`) | |
362 | ||
363 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
364 | }, | |
365 | ||
366 | error: err => this.notifier.error(err.message) | |
367 | }) | |
368 | } | |
369 | }, | |
370 | { | |
371 | label: $localize`Delete video`, | |
372 | isDisplayed: abuse => abuse.video && !abuse.video.deleted, | |
373 | handler: async abuse => { | |
374 | const res = await this.confirmService.confirm( | |
375 | $localize`Do you really want to delete this video?`, | |
376 | $localize`Delete` | |
377 | ) | |
378 | if (res === false) return | |
379 | ||
380 | this.videoService.removeVideo(abuse.video.id) | |
381 | .subscribe({ | |
382 | next: () => { | |
383 | this.notifier.success($localize`Video deleted.`) | |
384 | ||
385 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
386 | }, | |
387 | ||
388 | error: err => this.notifier.error(err.message) | |
389 | }) | |
390 | } | |
391 | } | |
392 | ] | |
393 | } | |
394 | ||
395 | private buildCommentActions (): DropdownAction<ProcessedAbuse>[] { | |
396 | if (!this.isAdminView()) return [] | |
397 | ||
398 | return [ | |
399 | { | |
400 | label: $localize`Actions for the comment`, | |
401 | isHeader: true, | |
402 | isDisplayed: abuse => abuse.comment && !abuse.comment.deleted | |
403 | }, | |
404 | ||
405 | { | |
406 | label: $localize`Delete comment`, | |
407 | isDisplayed: abuse => abuse.comment && !abuse.comment.deleted, | |
408 | handler: async abuse => { | |
409 | const res = await this.confirmService.confirm( | |
410 | $localize`Do you really want to delete this comment?`, | |
411 | $localize`Delete` | |
412 | ) | |
413 | if (res === false) return | |
414 | ||
415 | this.commentService.deleteVideoComment(abuse.comment.video.id, abuse.comment.id) | |
416 | .subscribe({ | |
417 | next: () => { | |
418 | this.notifier.success($localize`Comment deleted.`) | |
419 | ||
420 | this.updateAbuseState(abuse, AbuseState.ACCEPTED) | |
421 | }, | |
422 | ||
423 | error: err => this.notifier.error(err.message) | |
424 | }) | |
425 | } | |
426 | } | |
427 | ] | |
428 | } | |
429 | ||
430 | private muteAccountHelper (account: Account) { | |
431 | this.blocklistService.blockAccountByInstance(account) | |
432 | .subscribe({ | |
433 | next: () => { | |
434 | this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`) | |
435 | account.mutedByInstance = true | |
436 | }, | |
437 | ||
438 | error: err => this.notifier.error(err.message) | |
439 | }) | |
440 | } | |
441 | ||
442 | private muteServerHelper (host: string) { | |
443 | this.blocklistService.blockServerByInstance(host) | |
444 | .subscribe({ | |
445 | next: () => { | |
446 | this.notifier.success($localize`Server ${host} muted by the instance.`) | |
447 | }, | |
448 | ||
449 | error: err => this.notifier.error(err.message) | |
450 | }) | |
451 | } | |
452 | ||
453 | private toHtml (text: string) { | |
454 | return this.markdownRenderer.textMarkdownToHTML({ markdown: text }) | |
455 | } | |
456 | } |