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