Add nth abuse count for a given video, add reporter/reportee reports stats
[oweals/peertube.git] / client / src / app / +admin / moderation / video-abuse-list / video-abuse-list.component.ts
1 import { Component, OnInit, ViewChild } from '@angular/core'
2 import { Account } from '@app/shared/account/account.model'
3 import { Notifier } from '@app/core'
4 import { SortMeta } from 'primeng/api'
5 import { VideoAbuse, VideoAbuseState } from '../../../../../../shared'
6 import { RestPagination, RestTable, VideoAbuseService, VideoBlacklistService } from '../../../shared'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { DropdownAction } from '../../../shared/buttons/action-dropdown.component'
9 import { ConfirmService } from '../../../core/index'
10 import { ModerationCommentModalComponent } from './moderation-comment-modal.component'
11 import { Video } from '../../../shared/video/video.model'
12 import { MarkdownService } from '@app/shared/renderer'
13 import { Actor } from '@app/shared/actor/actor.model'
14 import { buildVideoLink, buildVideoEmbed } from 'src/assets/player/utils'
15 import { getAbsoluteAPIUrl } from '@app/shared/misc/utils'
16 import { DomSanitizer } from '@angular/platform-browser'
17 import { BlocklistService } from '@app/shared/blocklist'
18 import { VideoService } from '@app/shared/video/video.service'
19 import { ActivatedRoute } from '@angular/router'
20 import { first } from 'rxjs/operators'
21
22 @Component({
23   selector: 'my-video-abuse-list',
24   templateUrl: './video-abuse-list.component.html',
25   styleUrls: [ '../moderation.component.scss', './video-abuse-list.component.scss' ]
26 })
27 export class VideoAbuseListComponent extends RestTable implements OnInit {
28   @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent
29
30   videoAbuses: (VideoAbuse & { moderationCommentHtml?: string, reasonHtml?: string })[] = []
31   totalRecords = 0
32   rowsPerPageOptions = [ 20, 50, 100 ]
33   rowsPerPage = this.rowsPerPageOptions[0]
34   sort: SortMeta = { field: 'createdAt', order: 1 }
35   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
36
37   videoAbuseActions: DropdownAction<VideoAbuse>[][] = []
38
39   constructor (
40     private notifier: Notifier,
41     private videoAbuseService: VideoAbuseService,
42     private blocklistService: BlocklistService,
43     private videoService: VideoService,
44     private videoBlacklistService: VideoBlacklistService,
45     private confirmService: ConfirmService,
46     private i18n: I18n,
47     private markdownRenderer: MarkdownService,
48     private sanitizer: DomSanitizer,
49     private route: ActivatedRoute
50   ) {
51     super()
52
53     this.videoAbuseActions = [
54       [
55         {
56           label: this.i18n('Internal actions'),
57           isHeader: true
58         },
59         {
60           label: this.i18n('Delete report'),
61           handler: videoAbuse => this.removeVideoAbuse(videoAbuse)
62         },
63         {
64           label: this.i18n('Add note'),
65           handler: videoAbuse => this.openModerationCommentModal(videoAbuse),
66           isDisplayed: videoAbuse => !videoAbuse.moderationComment
67         },
68         {
69           label: this.i18n('Update note'),
70           handler: videoAbuse => this.openModerationCommentModal(videoAbuse),
71           isDisplayed: videoAbuse => !!videoAbuse.moderationComment
72         },
73         {
74           label: this.i18n('Mark as accepted'),
75           handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED),
76           isDisplayed: videoAbuse => !this.isVideoAbuseAccepted(videoAbuse)
77         },
78         {
79           label: this.i18n('Mark as rejected'),
80           handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.REJECTED),
81           isDisplayed: videoAbuse => !this.isVideoAbuseRejected(videoAbuse)
82         }
83       ],
84       [
85         {
86           label: this.i18n('Actions for the video'),
87           isHeader: true,
88           isDisplayed: videoAbuse => !videoAbuse.video.deleted
89         },
90         {
91           label: this.i18n('Blacklist video'),
92           isDisplayed: videoAbuse => !videoAbuse.video.deleted && !videoAbuse.video.blacklisted,
93           handler: videoAbuse => {
94             this.videoBlacklistService.blacklistVideo(videoAbuse.video.id, undefined, true)
95               .subscribe(
96                 () => {
97                   this.notifier.success(this.i18n('Video blacklisted.'))
98
99                   this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED)
100                 },
101
102                 err => this.notifier.error(err.message)
103               )
104           }
105         },
106         {
107           label: this.i18n('Unblacklist video'),
108           isDisplayed: videoAbuse => !videoAbuse.video.deleted && videoAbuse.video.blacklisted,
109           handler: videoAbuse => {
110             this.videoBlacklistService.removeVideoFromBlacklist(videoAbuse.video.id)
111               .subscribe(
112                 () => {
113                   this.notifier.success(this.i18n('Video unblacklisted.'))
114
115                   this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED)
116                 },
117
118                 err => this.notifier.error(err.message)
119               )
120           }
121         },
122         {
123           label: this.i18n('Delete video'),
124           isDisplayed: videoAbuse => !videoAbuse.video.deleted,
125           handler: async videoAbuse => {
126             const res = await this.confirmService.confirm(
127               this.i18n('Do you really want to delete this video?'),
128               this.i18n('Delete')
129             )
130             if (res === false) return
131
132             this.videoService.removeVideo(videoAbuse.video.id)
133               .subscribe(
134                 () => {
135                   this.notifier.success(this.i18n('Video deleted.'))
136
137                   this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED)
138                 },
139
140                 err => this.notifier.error(err.message)
141               )
142           }
143         }
144       ],
145       [
146         {
147           label: this.i18n('Actions for the reporter'),
148           isHeader: true
149         },
150         {
151           label: this.i18n('Mute reporter'),
152           handler: async videoAbuse => {
153             const account = videoAbuse.reporterAccount as Account
154
155             this.blocklistService.blockAccountByInstance(account)
156               .subscribe(
157                 () => {
158                   this.notifier.success(
159                     this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost })
160                   )
161
162                   account.mutedByInstance = true
163                 },
164
165                 err => this.notifier.error(err.message)
166               )
167           }
168         },
169         {
170           label: this.i18n('Mute server'),
171           isDisplayed: videoAbuse => !videoAbuse.reporterAccount.userId,
172           handler: async videoAbuse => {
173             this.blocklistService.blockServerByInstance(videoAbuse.reporterAccount.host)
174               .subscribe(
175                 () => {
176                   this.notifier.success(
177                     this.i18n('Server {{host}} muted by the instance.', { host: videoAbuse.reporterAccount.host })
178                   )
179                 },
180
181                 err => this.notifier.error(err.message)
182               )
183           }
184         }
185       ]
186     ]
187   }
188
189   ngOnInit () {
190     this.initialize()
191
192     this.route.queryParams
193       .pipe(first(params => params.search !== undefined && params.search !== null))
194       .subscribe(params => this.search = params.search)
195   }
196
197   getIdentifier () {
198     return 'VideoAbuseListComponent'
199   }
200
201   openModerationCommentModal (videoAbuse: VideoAbuse) {
202     this.moderationCommentModal.openModal(videoAbuse)
203   }
204
205   onModerationCommentUpdated () {
206     this.loadData()
207   }
208
209   createByString (account: Account) {
210     return Account.CREATE_BY_STRING(account.name, account.host)
211   }
212
213   isVideoAbuseAccepted (videoAbuse: VideoAbuse) {
214     return videoAbuse.state.id === VideoAbuseState.ACCEPTED
215   }
216
217   isVideoAbuseRejected (videoAbuse: VideoAbuse) {
218     return videoAbuse.state.id === VideoAbuseState.REJECTED
219   }
220
221   getVideoUrl (videoAbuse: VideoAbuse) {
222     return Video.buildClientUrl(videoAbuse.video.uuid)
223   }
224
225   getVideoEmbed (videoAbuse: VideoAbuse) {
226     const absoluteAPIUrl = 'http://localhost:9000' || getAbsoluteAPIUrl() // TODO
227     const embedUrl = buildVideoLink({
228       baseUrl: absoluteAPIUrl + '/videos/embed/' + videoAbuse.video.uuid,
229       warningTitle: false
230     })
231     return buildVideoEmbed(embedUrl)
232   }
233
234   switchToDefaultAvatar ($event: Event) {
235     ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
236   }
237
238   async removeVideoAbuse (videoAbuse: VideoAbuse) {
239     const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this abuse report?'), this.i18n('Delete'))
240     if (res === false) return
241
242     this.videoAbuseService.removeVideoAbuse(videoAbuse).subscribe(
243       () => {
244         this.notifier.success(this.i18n('Abuse deleted.'))
245         this.loadData()
246       },
247
248       err => this.notifier.error(err.message)
249     )
250   }
251
252   updateVideoAbuseState (videoAbuse: VideoAbuse, state: VideoAbuseState) {
253     this.videoAbuseService.updateVideoAbuse(videoAbuse, { state })
254       .subscribe(
255         () => this.loadData(),
256
257         err => this.notifier.error(err.message)
258       )
259
260   }
261
262   protected loadData () {
263     return this.videoAbuseService.getVideoAbuses({
264       pagination: this.pagination,
265       sort: this.sort,
266       search: this.search
267     }).subscribe(
268         async resultList => {
269           this.totalRecords = resultList.total
270
271           this.videoAbuses = resultList.data
272
273           for (const abuse of this.videoAbuses) {
274             Object.assign(abuse, {
275               reasonHtml: await this.toHtml(abuse.reason),
276               moderationCommentHtml: await this.toHtml(abuse.moderationComment),
277               embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse)),
278               reporterAccount: new Account(abuse.reporterAccount)
279             })
280           }
281
282         },
283
284         err => this.notifier.error(err.message)
285       )
286   }
287
288   private toHtml (text: string) {
289     return this.markdownRenderer.textMarkdownToHTML(text)
290   }
291 }