]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts
Bumped to version v5.2.1
[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 () {
c17a55fb
C
38 let address = this.form.value['text'] || ''
39 address = address.replace(/^@/, '')
40
583cd0d2
AC
41 const [ username, hostname ] = address.split('@')
42
d43c6b1f
C
43 const protocol = window.location.protocol
44
f0ad4710 45 // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5
d43c6b1f 46 fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`)
583cd0d2 47 .then(response => response.json())
9df52d66
C
48 .then(data => {
49 if (!data || Array.isArray(data.links) === false) {
50 throw new Error('Not links in webfinger response')
51 }
d43c6b1f
C
52
53 const link: { template: string } = data.links.find((link: any) => {
54 return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe'
55 })
56
9df52d66
C
57 if (link?.template.includes('{uri}')) {
58 return link.template.replace('{uri}', encodeURIComponent(this.uri))
583cd0d2 59 }
9df52d66
C
60
61 throw new Error('No subscribe template in webfinger response')
62 })
583cd0d2 63 .then(window.open)
d43c6b1f 64 .catch(err => {
42b40636 65 logger.error(err)
d43c6b1f
C
66
67 this.notifier.error($localize`Cannot fetch information of this remote account`)
68 })
660d11e9
RK
69 }
70}