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