Lazy import some modules
[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 '../../../shared/account/account.model'
3 import { Notifier } from '@app/core'
4 import { SortMeta } from 'primeng/components/common/sortmeta'
5 import { VideoAbuse, VideoAbuseState } from '../../../../../../shared'
6 import { RestPagination, RestTable, VideoAbuseService } 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
14 @Component({
15   selector: 'my-video-abuse-list',
16   templateUrl: './video-abuse-list.component.html',
17   styleUrls: [ '../moderation.component.scss']
18 })
19 export class VideoAbuseListComponent extends RestTable implements OnInit {
20   @ViewChild('moderationCommentModal') moderationCommentModal: ModerationCommentModalComponent
21
22   videoAbuses: (VideoAbuse & { moderationCommentHtml?: string, reasonHtml?: string })[] = []
23   totalRecords = 0
24   rowsPerPage = 10
25   sort: SortMeta = { field: 'createdAt', order: 1 }
26   pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
27
28   videoAbuseActions: DropdownAction<VideoAbuse>[] = []
29
30   constructor (
31     private notifier: Notifier,
32     private videoAbuseService: VideoAbuseService,
33     private confirmService: ConfirmService,
34     private i18n: I18n,
35     private markdownRenderer: MarkdownService
36   ) {
37     super()
38
39     this.videoAbuseActions = [
40       {
41         label: this.i18n('Delete this report'),
42         handler: videoAbuse => this.removeVideoAbuse(videoAbuse)
43       },
44       {
45         label: this.i18n('Update moderation comment'),
46         handler: videoAbuse => this.openModerationCommentModal(videoAbuse)
47       },
48       {
49         label: this.i18n('Mark as accepted'),
50         handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.ACCEPTED),
51         isDisplayed: videoAbuse => !this.isVideoAbuseAccepted(videoAbuse)
52       },
53       {
54         label: this.i18n('Mark as rejected'),
55         handler: videoAbuse => this.updateVideoAbuseState(videoAbuse, VideoAbuseState.REJECTED),
56         isDisplayed: videoAbuse => !this.isVideoAbuseRejected(videoAbuse)
57       }
58     ]
59   }
60
61   ngOnInit () {
62     this.initialize()
63   }
64
65   openModerationCommentModal (videoAbuse: VideoAbuse) {
66     this.moderationCommentModal.openModal(videoAbuse)
67   }
68
69   onModerationCommentUpdated () {
70     this.loadData()
71   }
72
73   createByString (account: Account) {
74     return Account.CREATE_BY_STRING(account.name, account.host)
75   }
76
77   isVideoAbuseAccepted (videoAbuse: VideoAbuse) {
78     return videoAbuse.state.id === VideoAbuseState.ACCEPTED
79   }
80
81   isVideoAbuseRejected (videoAbuse: VideoAbuse) {
82     return videoAbuse.state.id === VideoAbuseState.REJECTED
83   }
84
85   getVideoUrl (videoAbuse: VideoAbuse) {
86     return Video.buildClientUrl(videoAbuse.video.uuid)
87   }
88
89   async removeVideoAbuse (videoAbuse: VideoAbuse) {
90     const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this abuse report?'), this.i18n('Delete'))
91     if (res === false) return
92
93     this.videoAbuseService.removeVideoAbuse(videoAbuse).subscribe(
94       () => {
95         this.notifier.success(this.i18n('Abuse deleted.'))
96         this.loadData()
97       },
98
99       err => this.notifier.error(err.message)
100     )
101   }
102
103   updateVideoAbuseState (videoAbuse: VideoAbuse, state: VideoAbuseState) {
104     this.videoAbuseService.updateVideoAbuse(videoAbuse, { state })
105       .subscribe(
106         () => this.loadData(),
107
108         err => this.notifier.error(err.message)
109       )
110
111   }
112
113   protected loadData () {
114     return this.videoAbuseService.getVideoAbuses(this.pagination, this.sort)
115                .subscribe(
116                  async resultList => {
117                    this.totalRecords = resultList.total
118
119                    this.videoAbuses = resultList.data
120
121                    for (const abuse of this.videoAbuses) {
122                      Object.assign(abuse, {
123                        reasonHtml: await this.toHtml(abuse.reason),
124                        moderationCommentHtml: await this.toHtml(abuse.moderationComment)
125                      })
126                    }
127
128                  },
129
130                  err => this.notifier.error(err.message)
131                )
132   }
133
134   private toHtml (text: string) {
135     return this.markdownRenderer.textMarkdownToHTML(text)
136   }
137 }