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