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