]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts
Refactor actor avatar display
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-subscription / remote-subscribe.component.ts
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'
5
6 @Component({
7 selector: 'my-remote-subscribe',
8 templateUrl: './remote-subscribe.component.html',
9 styleUrls: ['./remote-subscribe.component.scss']
10 })
11 export class RemoteSubscribeComponent extends FormReactive implements OnInit {
12 @Input() uri: string
13 @Input() interact = false
14 @Input() showHelp = false
15
16 constructor (
17 protected formValidatorService: FormValidatorService,
18 private notifier: Notifier
19 ) {
20 super()
21 }
22
23 ngOnInit () {
24 this.buildForm({
25 text: USER_HANDLE_VALIDATOR
26 })
27 }
28
29 onValidKey () {
30 this.check()
31 if (!this.form.valid) return
32
33 this.formValidated()
34 }
35
36 formValidated () {
37 const address = this.form.value['text']
38 const [ username, hostname ] = address.split('@')
39
40 const protocol = window.location.protocol
41
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())
45 .then(data => new Promise((res, rej) => {
46 if (!data || Array.isArray(data.links) === false) return rej()
47
48 const link: { template: string } = data.links.find((link: any) => {
49 return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe'
50 })
51
52 if (link && link.template.includes('{uri}')) {
53 res(link.template.replace('{uri}', encodeURIComponent(this.uri)))
54 }
55 }))
56 .then(window.open)
57 .catch(err => {
58 console.error(err)
59
60 this.notifier.error($localize`Cannot fetch information of this remote account`)
61 })
62 }
63 }