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'
8 selector: 'my-remote-subscribe',
9 templateUrl: './remote-subscribe.component.html',
10 styleUrls: [ './remote-subscribe.component.scss' ]
12 export class RemoteSubscribeComponent extends FormReactive implements OnInit {
14 @Input() interact = false
15 @Input() showHelp = false
18 protected formReactiveService: FormReactiveService,
19 private notifier: Notifier
26 text: USER_HANDLE_VALIDATOR
32 if (!this.form.valid) return
38 const address = this.form.value['text']
39 const [ username, hostname ] = address.split('@')
41 const protocol = window.location.protocol
43 // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5
44 fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`)
45 .then(response => response.json())
47 if (!data || Array.isArray(data.links) === false) {
48 throw new Error('Not links in webfinger response')
51 const link: { template: string } = data.links.find((link: any) => {
52 return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe'
55 if (link?.template.includes('{uri}')) {
56 return link.template.replace('{uri}', encodeURIComponent(this.uri))
59 throw new Error('No subscribe template in webfinger response')
65 this.notifier.error($localize`Cannot fetch information of this remote account`)