1 import { Component, Input, OnInit } from '@angular/core'
2 import { Notifier } from '@app/core'
3 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
4 import { USER_HANDLE_VALIDATOR } from '../form-validators/user-validators'
7 selector: 'my-remote-subscribe',
8 templateUrl: './remote-subscribe.component.html',
9 styleUrls: [ './remote-subscribe.component.scss' ]
11 export class RemoteSubscribeComponent extends FormReactive implements OnInit {
13 @Input() interact = false
14 @Input() showHelp = false
17 protected formValidatorService: FormValidatorService,
18 private notifier: Notifier
25 text: USER_HANDLE_VALIDATOR
31 if (!this.form.valid) return
37 const address = this.form.value['text']
38 const [ username, hostname ] = address.split('@')
40 const protocol = window.location.protocol
42 // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5
43 fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`)
44 .then(response => response.json())
46 if (!data || Array.isArray(data.links) === false) {
47 throw new Error('Not links in webfinger response')
50 const link: { template: string } = data.links.find((link: any) => {
51 return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe'
54 if (link?.template.includes('{uri}')) {
55 return link.template.replace('{uri}', encodeURIComponent(this.uri))
58 throw new Error('No subscribe template in webfinger response')
64 this.notifier.error($localize`Cannot fetch information of this remote account`)