1 import { Component, Input, OnInit } from '@angular/core'
2 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
3 import { USER_EMAIL_VALIDATOR } from '../form-validators/user-validators'
6 selector: 'my-remote-subscribe',
7 templateUrl: './remote-subscribe.component.html',
8 styleUrls: ['./remote-subscribe.component.scss']
10 export class RemoteSubscribeComponent extends FormReactive implements OnInit {
12 @Input() interact = false
13 @Input() showHelp = false
16 protected formValidatorService: FormValidatorService
23 text: USER_EMAIL_VALIDATOR
29 if (!this.form.valid) return
35 const address = this.form.value['text']
36 const [ username, hostname ] = address.split('@')
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 if (data && Array.isArray(data.links)) {
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'
47 if (link && link.template.includes('{uri}')) {
48 resolve(link.template.replace('{uri}', encodeURIComponent(this.uri)))
54 .catch(err => console.error(err))