]>
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 | let address = this.form.value['text'] || '' | |
39 | address = address.replace(/^@/, '') | |
40 | ||
41 | const [ username, hostname ] = address.split('@') | |
42 | ||
43 | const protocol = window.location.protocol | |
44 | ||
45 | // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5 | |
46 | fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`) | |
47 | .then(response => response.json()) | |
48 | .then(data => { | |
49 | if (!data || Array.isArray(data.links) === false) { | |
50 | throw new Error('Not links in webfinger response') | |
51 | } | |
52 | ||
53 | const link: { template: string } = data.links.find((link: any) => { | |
54 | return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe' | |
55 | }) | |
56 | ||
57 | if (link?.template.includes('{uri}')) { | |
58 | return link.template.replace('{uri}', encodeURIComponent(this.uri)) | |
59 | } | |
60 | ||
61 | throw new Error('No subscribe template in webfinger response') | |
62 | }) | |
63 | .then(window.open) | |
64 | .catch(err => { | |
65 | logger.error(err) | |
66 | ||
67 | this.notifier.error($localize`Cannot fetch information of this remote account`) | |
68 | }) | |
69 | } | |
70 | } |