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