]>
Commit | Line | Data |
---|---|---|
1 | import { Component, Input, OnInit } from '@angular/core' | |
2 | import { Notifier } from '@app/core' | |
3 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
4 | import { USER_HANDLE_VALIDATOR } from '../form-validators/user-validators' | |
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 { | |
12 | @Input() uri: string | |
13 | @Input() interact = false | |
14 | @Input() showHelp = false | |
15 | ||
16 | constructor ( | |
17 | protected formValidatorService: FormValidatorService, | |
18 | private notifier: Notifier | |
19 | ) { | |
20 | super() | |
21 | } | |
22 | ||
23 | ngOnInit () { | |
24 | this.buildForm({ | |
25 | text: USER_HANDLE_VALIDATOR | |
26 | }) | |
27 | } | |
28 | ||
29 | onValidKey () { | |
30 | this.forceCheck() | |
31 | if (!this.form.valid) return | |
32 | ||
33 | this.formValidated() | |
34 | } | |
35 | ||
36 | formValidated () { | |
37 | const address = this.form.value['text'] | |
38 | const [ username, hostname ] = address.split('@') | |
39 | ||
40 | const protocol = window.location.protocol | |
41 | ||
42 | // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5 | |
43 | fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`) | |
44 | .then(response => response.json()) | |
45 | .then(data => { | |
46 | if (!data || Array.isArray(data.links) === false) { | |
47 | throw new Error('Not links in webfinger response') | |
48 | } | |
49 | ||
50 | const link: { template: string } = data.links.find((link: any) => { | |
51 | return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe' | |
52 | }) | |
53 | ||
54 | if (link?.template.includes('{uri}')) { | |
55 | return link.template.replace('{uri}', encodeURIComponent(this.uri)) | |
56 | } | |
57 | ||
58 | throw new Error('No subscribe template in webfinger response') | |
59 | }) | |
60 | .then(window.open) | |
61 | .catch(err => { | |
62 | console.error(err) | |
63 | ||
64 | this.notifier.error($localize`Cannot fetch information of this remote account`) | |
65 | }) | |
66 | } | |
67 | } |