5e48cf24fd8e220469e69b6deb641b509fd8bb45
[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
19 @Component({
20   selector: 'my-video-abuse-list',
21   templateUrl: './video-abuse-list.component.html',
22   styleUrls: [ '../moderation.component.scss']
23 })
24 export class VideoAbuseListComponent extends RestTable implements OnInit {
25   @ViewChild('moderationCommentModal', { static: true }) moderationCommentModal: ModerationCommentModalComponent
26
27   videoAbuses: (VideoAbuse & { moderationCommentHtml?: string, reasonHtml?: string })[] = []
28   totalRecords = 0
29   rowsPerPage = 10
30   sort: SortMeta = { field: 'createdAt', order: 1 }
31   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
32
33   videoAbuseActions: DropdownAction<VideoAbuse>[][] = []
34
35   constructor (
36     private notifier: Notifier,
37     private videoAbuseService: VideoAbuseService,
38     private blocklistService: BlocklistService,
39     private videoBlacklistService: VideoBlacklistService,
40     private confirmService: ConfirmService,
41     private i18n: I18n,
42     private markdownRenderer: MarkdownService,
43     private sanitizer: DomSanitizer
44   ) {
45     super()
46
47     this.videoAbuseActions = [
48       [
49         {
50           label: this.i18n('Internal actions'),
51           isHeader: true
52         },
53         {
54           label: this.i18n('Delete report'),
55           handler: videoAbuse => this.removeVideoAbuse(videoAbuse)
56         },
57         {
58           label: this.i18n('Add note'),
59           handler: videoAbuse => this.openModerationCommentModal(videoAbuse),
60           isDisplayed: videoAbuse => !videoAbuse.moderationComment
61         },
62         {
63           label: this.i18n('Update note'),
64           handler: videoAbuse => this.openModerationCommentModal(videoAbuse),
65           isDisplayed: videoAbuse => !!videoAbuse.moderationComment
66         },
67         {
68           label: this.i18n('Mark as accepted'),
69           handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED),
70           isDisplayed: videoAbuse => !this.isVideoAbuseAccepted(videoAbuse)
71         },
72         {
73           label: this.i18n('Mark as rejected'),
74           handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.REJECTED),
75           isDisplayed: videoAbuse => !this.isVideoAbuseRejected(videoAbuse)
76         }
77       ],
78       [
79         {
80           label: this.i18n('Actions for the video'),
81           isHeader: true
82         },
83         {
84           label: this.i18n('Blacklist video'),
85           handler: videoAbuse => {
86             this.videoBlacklistService.blacklistVideo(videoAbuse.video.id, undefined, true)
87               .subscribe(
88                 () => {
89                   this.notifier.success(this.i18n('Video blacklisted.'))
90
91                   this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED)
92                 },
93
94                 err => this.notifier.error(err.message)
95               )
96           }
97         }
98       ]
99     ]
100   }
101
102   ngOnInit () {
103     this.initialize()
104   }
105
106   getIdentifier () {
107     return 'VideoAbuseListComponent'
108   }
109
110   openModerationCommentModal (videoAbuse: VideoAbuse) {
111     this.moderationCommentModal.openModal(videoAbuse)
112   }
113
114   onModerationCommentUpdated () {
115     this.loadData()
116   }
117
118   createByString (account: Account) {
119     return Account.CREATE_BY_STRING(account.name, account.host)
120   }
121
122   isVideoAbuseAccepted (videoAbuse: VideoAbuse) {
123     return videoAbuse.state.id === VideoAbuseState.ACCEPTED
124   }
125
126   isVideoAbuseRejected (videoAbuse: VideoAbuse) {
127     return videoAbuse.state.id === VideoAbuseState.REJECTED
128   }
129
130   getVideoUrl (videoAbuse: VideoAbuse) {
131     return Video.buildClientUrl(videoAbuse.video.uuid)
132   }
133
134   getVideoEmbed (videoAbuse: VideoAbuse) {
135     const absoluteAPIUrl = 'http://localhost:9000' || getAbsoluteAPIUrl()
136     const embedUrl = buildVideoLink({
137       baseUrl: absoluteAPIUrl + '/videos/embed/' + videoAbuse.video.uuid,
138       warningTitle: false
139     })
140     return buildVideoEmbed(embedUrl)
141   }
142
143   switchToDefaultAvatar ($event: Event) {
144     ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
145   }
146
147   async removeVideoAbuse (videoAbuse: VideoAbuse) {
148     const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this abuse report?'), this.i18n('Delete'))
149     if (res === false) return
150
151     this.videoAbuseService.removeVideoAbuse(videoAbuse).subscribe(
152       () => {
153         this.notifier.success(this.i18n('Abuse deleted.'))
154         this.loadData()
155       },
156
157       err => this.notifier.error(err.message)
158     )
159   }
160
161   updateVideoAbuseState (videoAbuse: VideoAbuse, state: VideoAbuseState) {
162     this.videoAbuseService.updateVideoAbuse(videoAbuse, { state })
163       .subscribe(
164         () => this.loadData(),
165
166         err => this.notifier.error(err.message)
167       )
168
169   }
170
171   protected loadData () {
172     return this.videoAbuseService.getVideoAbuses(this.pagination, this.sort)
173                .subscribe(
174                  async resultList => {
175                    this.totalRecords = resultList.total
176
177                    this.videoAbuses = resultList.data
178
179                    for (const abuse of this.videoAbuses) {
180                      Object.assign(abuse, {
181                        reasonHtml: await this.toHtml(abuse.reason),
182                        moderationCommentHtml: await this.toHtml(abuse.moderationComment),
183                        embedHtml: this.sanitizer.bypassSecurityTrustHtml(this.getVideoEmbed(abuse))
184                      })
185                    }
186
187                  },
188
189                  err => this.notifier.error(err.message)
190                )
191   }
192
193   private toHtml (text: string) {
194     return this.markdownRenderer.textMarkdownToHTML(text)
195   }
196 }