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