]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-moderation/report-modals/video-report.component.ts
Reorganize shared models
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / report-modals / video-report.component.ts
1 import { mapValues, pickBy } from 'lodash-es'
2 import { buildVideoEmbed, buildVideoLink } from 'src/assets/player/utils'
3 import { Component, Input, OnInit, ViewChild } from '@angular/core'
4 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
5 import { Notifier } from '@app/core'
6 import { AbuseValidatorsService, FormReactive, FormValidatorService } from '@app/shared/shared-forms'
7 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
8 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
11 import { AbusePredefinedReasonsString } from '@shared/models'
12 import { Video } from '../../shared-main'
13 import { AbuseService } from '../abuse.service'
14
15 @Component({
16 selector: 'my-video-report',
17 templateUrl: './video-report.component.html',
18 styleUrls: [ './report.component.scss' ]
19 })
20 export class VideoReportComponent extends FormReactive implements OnInit {
21 @Input() video: Video = null
22
23 @ViewChild('modal', { static: true }) modal: NgbModal
24
25 error: string = null
26 predefinedReasons: { id: AbusePredefinedReasonsString, label: string, description?: string, help?: string }[] = []
27 embedHtml: SafeHtml
28
29 private openedModal: NgbModalRef
30
31 constructor (
32 protected formValidatorService: FormValidatorService,
33 private modalService: NgbModal,
34 private abuseValidatorsService: AbuseValidatorsService,
35 private abuseService: AbuseService,
36 private notifier: Notifier,
37 private sanitizer: DomSanitizer,
38 private i18n: I18n
39 ) {
40 super()
41 }
42
43 get currentHost () {
44 return window.location.host
45 }
46
47 get originHost () {
48 if (this.isRemote()) {
49 return this.video.account.host
50 }
51
52 return ''
53 }
54
55 get timestamp () {
56 return this.form.get('timestamp').value
57 }
58
59 getVideoEmbed () {
60 return this.sanitizer.bypassSecurityTrustHtml(
61 buildVideoEmbed(
62 buildVideoLink({
63 baseUrl: this.video.embedUrl,
64 title: false,
65 warningTitle: false
66 })
67 )
68 )
69 }
70
71 ngOnInit () {
72 this.buildForm({
73 reason: this.abuseValidatorsService.ABUSE_REASON,
74 predefinedReasons: mapValues(abusePredefinedReasonsMap, r => null),
75 timestamp: {
76 hasStart: null,
77 startAt: null,
78 hasEnd: null,
79 endAt: null
80 }
81 })
82
83 this.predefinedReasons = this.abuseService.getPrefefinedReasons('video')
84
85 this.embedHtml = this.getVideoEmbed()
86 }
87
88 show () {
89 this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false, size: 'lg' })
90 }
91
92 hide () {
93 this.openedModal.close()
94 this.openedModal = null
95 }
96
97 report () {
98 const reason = this.form.get('reason').value
99 const predefinedReasons = Object.keys(pickBy(this.form.get('predefinedReasons').value)) as AbusePredefinedReasonsString[]
100 const { hasStart, startAt, hasEnd, endAt } = this.form.get('timestamp').value
101
102 this.abuseService.reportVideo({
103 reason,
104 predefinedReasons,
105 video: {
106 id: this.video.id,
107 startAt: hasStart && startAt ? startAt : undefined,
108 endAt: hasEnd && endAt ? endAt : undefined
109 }
110 }).subscribe(
111 () => {
112 this.notifier.success(this.i18n('Video reported.'))
113 this.hide()
114 },
115
116 err => this.notifier.error(err.message)
117 )
118 }
119
120 isRemote () {
121 return !this.video.isLocal
122 }
123 }