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