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 '@app/shared/instance/follow.service'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
10 selector: 'my-following-add',
11 templateUrl: './following-add.component.html',
12 styleUrls: [ './following-add.component.scss' ]
14 export class FollowingAddComponent {
16 hostsError: string = null
20 private router: Router,
21 private notifier: Notifier,
22 private confirmService: ConfirmService,
23 private followService: FollowService,
28 return window.location.protocol === 'https:'
32 this.hostsError = null
34 const newHostsErrors = []
35 const hosts = this.getNotEmptyHosts()
37 for (const host of hosts) {
38 if (validateHost(host) === false) {
39 newHostsErrors.push(this.i18n('{{host}} is not valid', { host }))
43 if (newHostsErrors.length !== 0) {
44 this.hostsError = newHostsErrors.join('. ')
48 async addFollowing () {
51 const hosts = this.getNotEmptyHosts()
52 if (hosts.length === 0) {
53 this.error = this.i18n('You need to specify hosts to follow.')
56 if (!this.isHostsUnique(hosts)) {
57 this.error = this.i18n('Hosts need to be unique.')
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
65 this.followService.follow(hosts).subscribe(
67 this.notifier.success(this.i18n('Follow request(s) sent!'))
69 setTimeout(() => this.router.navigate([ '/admin/follows/following-list' ]), 500)
72 err => this.notifier.error(err.message)
76 private isHostsUnique (hosts: string[]) {
77 return hosts.every(host => hosts.indexOf(host) === hosts.lastIndexOf(host))
80 private getNotEmptyHosts () {
81 return this.hostsString
83 .filter(host => host && host.length !== 0) // Eject empty hosts