]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/follows/following-add/following-add.component.ts
Refractor notification service
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / follows / following-add / following-add.component.ts
1 import { Component } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { Notifier } from '@app/core'
4 import { ConfirmService } from '../../../core'
5 import { validateHost } from '../../../shared'
6 import { FollowService } from '../shared'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8
9 @Component({
10 selector: 'my-following-add',
11 templateUrl: './following-add.component.html',
12 styleUrls: [ './following-add.component.scss' ]
13 })
14 export class FollowingAddComponent {
15 hostsString = ''
16 hostsError: string = null
17 error: string = null
18
19 constructor (
20 private router: Router,
21 private notifier: Notifier,
22 private confirmService: ConfirmService,
23 private followService: FollowService,
24 private i18n: I18n
25 ) {}
26
27 httpEnabled () {
28 return window.location.protocol === 'https:'
29 }
30
31 onHostsChanged () {
32 this.hostsError = null
33
34 const newHostsErrors = []
35 const hosts = this.getNotEmptyHosts()
36
37 for (const host of hosts) {
38 if (validateHost(host) === false) {
39 newHostsErrors.push(this.i18n('{{host}} is not valid', { host }))
40 }
41 }
42
43 if (newHostsErrors.length !== 0) {
44 this.hostsError = newHostsErrors.join('. ')
45 }
46 }
47
48 async addFollowing () {
49 this.error = ''
50
51 const hosts = this.getNotEmptyHosts()
52 if (hosts.length === 0) {
53 this.error = this.i18n('You need to specify hosts to follow.')
54 }
55
56 if (!this.isHostsUnique(hosts)) {
57 this.error = this.i18n('Hosts need to be unique.')
58 return
59 }
60
61 const confirmMessage = this.i18n('If you confirm, you will send a follow request to:<br /> - ') + hosts.join('<br /> - ')
62 const res = await this.confirmService.confirm(confirmMessage, this.i18n('Follow new server(s)'))
63 if (res === false) return
64
65 this.followService.follow(hosts).subscribe(
66 () => {
67 this.notifier.success(this.i18n('Follow request(s) sent!'))
68
69 setTimeout(() => this.router.navigate([ '/admin/follows/following-list' ]), 500)
70 },
71
72 err => this.notifier.error(err.message)
73 )
74 }
75
76 private isHostsUnique (hosts: string[]) {
77 return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host))
78 }
79
80 private getNotEmptyHosts () {
81 return this.hostsString
82 .split('\n')
83 .filter(host => host && host.length !== 0) // Eject empty hosts
84 }
85 }