]>
Commit | Line | Data |
---|---|---|
660d11e9 | 1 | import { Component, Input, OnInit } from '@angular/core' |
d43c6b1f | 2 | import { Notifier } from '@app/core' |
7ed1edbb | 3 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' |
d43c6b1f | 4 | import { USER_HANDLE_VALIDATOR } from '../form-validators/user-validators' |
660d11e9 RK |
5 | |
6 | @Component({ | |
7 | selector: 'my-remote-subscribe', | |
8 | templateUrl: './remote-subscribe.component.html', | |
9 | styleUrls: ['./remote-subscribe.component.scss'] | |
10 | }) | |
11 | export class RemoteSubscribeComponent extends FormReactive implements OnInit { | |
3ddb1ec5 | 12 | @Input() uri: string |
660d11e9 RK |
13 | @Input() interact = false |
14 | @Input() showHelp = false | |
15 | ||
16 | constructor ( | |
d43c6b1f C |
17 | protected formValidatorService: FormValidatorService, |
18 | private notifier: Notifier | |
660d11e9 RK |
19 | ) { |
20 | super() | |
21 | } | |
22 | ||
23 | ngOnInit () { | |
24 | this.buildForm({ | |
d43c6b1f | 25 | text: USER_HANDLE_VALIDATOR |
660d11e9 RK |
26 | }) |
27 | } | |
28 | ||
29 | onValidKey () { | |
3866f1a0 | 30 | this.check() |
660d11e9 RK |
31 | if (!this.form.valid) return |
32 | ||
33 | this.formValidated() | |
34 | } | |
35 | ||
36 | formValidated () { | |
37 | const address = this.form.value['text'] | |
583cd0d2 AC |
38 | const [ username, hostname ] = address.split('@') |
39 | ||
d43c6b1f C |
40 | const protocol = window.location.protocol |
41 | ||
f0ad4710 | 42 | // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5 |
d43c6b1f | 43 | fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`) |
583cd0d2 | 44 | .then(response => response.json()) |
d43c6b1f C |
45 | .then(data => new Promise((res, rej) => { |
46 | if (!data || Array.isArray(data.links) === false) return rej() | |
47 | ||
48 | const link: { template: string } = data.links.find((link: any) => { | |
49 | return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe' | |
50 | }) | |
51 | ||
52 | if (link && link.template.includes('{uri}')) { | |
53 | res(link.template.replace('{uri}', encodeURIComponent(this.uri))) | |
583cd0d2 | 54 | } |
583cd0d2 AC |
55 | })) |
56 | .then(window.open) | |
d43c6b1f C |
57 | .catch(err => { |
58 | console.error(err) | |
59 | ||
60 | this.notifier.error($localize`Cannot fetch information of this remote account`) | |
61 | }) | |
660d11e9 RK |
62 | } |
63 | } |