]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts
Refactor form reactive
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-subscription / remote-subscribe.component.ts
CommitLineData
660d11e9 1import { Component, Input, OnInit } from '@angular/core'
d43c6b1f 2import { Notifier } from '@app/core'
5c5bcea2 3import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
42b40636 4import { logger } from '@root-helpers/logger'
d43c6b1f 5import { USER_HANDLE_VALIDATOR } from '../form-validators/user-validators'
660d11e9
RK
6
7@Component({
8 selector: 'my-remote-subscribe',
9 templateUrl: './remote-subscribe.component.html',
9df52d66 10 styleUrls: [ './remote-subscribe.component.scss' ]
660d11e9
RK
11})
12export class RemoteSubscribeComponent extends FormReactive implements OnInit {
3ddb1ec5 13 @Input() uri: string
660d11e9
RK
14 @Input() interact = false
15 @Input() showHelp = false
16
17 constructor (
5c5bcea2 18 protected formReactiveService: FormReactiveService,
d43c6b1f 19 private notifier: Notifier
660d11e9
RK
20 ) {
21 super()
22 }
23
24 ngOnInit () {
25 this.buildForm({
d43c6b1f 26 text: USER_HANDLE_VALIDATOR
660d11e9
RK
27 })
28 }
29
30 onValidKey () {
cc4bf76c 31 this.forceCheck()
660d11e9
RK
32 if (!this.form.valid) return
33
34 this.formValidated()
35 }
36
37 formValidated () {
38 const address = this.form.value['text']
583cd0d2
AC
39 const [ username, hostname ] = address.split('@')
40
d43c6b1f
C
41 const protocol = window.location.protocol
42
f0ad4710 43 // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5
d43c6b1f 44 fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`)
583cd0d2 45 .then(response => response.json())
9df52d66
C
46 .then(data => {
47 if (!data || Array.isArray(data.links) === false) {
48 throw new Error('Not links in webfinger response')
49 }
d43c6b1f
C
50
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'
53 })
54
9df52d66
C
55 if (link?.template.includes('{uri}')) {
56 return link.template.replace('{uri}', encodeURIComponent(this.uri))
583cd0d2 57 }
9df52d66
C
58
59 throw new Error('No subscribe template in webfinger response')
60 })
583cd0d2 61 .then(window.open)
d43c6b1f 62 .catch(err => {
42b40636 63 logger.error(err)
d43c6b1f
C
64
65 this.notifier.error($localize`Cannot fetch information of this remote account`)
66 })
660d11e9
RK
67 }
68}