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