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