]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts
Support ICU in TS components
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-user-subscription / remote-subscribe.component.ts
index 286ecac029bdb1465e85b855e5be9d2b809daaba..36969271530da72394ee8e52931bab9a2cf43ded 100644 (file)
@@ -1,10 +1,12 @@
 import { Component, Input, OnInit } from '@angular/core'
-import { FormReactive, FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms'
+import { Notifier } from '@app/core'
+import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
+import { USER_HANDLE_VALIDATOR } from '../form-validators/user-validators'
 
 @Component({
   selector: 'my-remote-subscribe',
   templateUrl: './remote-subscribe.component.html',
-  styleUrls: ['./remote-subscribe.component.scss']
+  styleUrls: [ './remote-subscribe.component.scss' ]
 })
 export class RemoteSubscribeComponent extends FormReactive implements OnInit {
   @Input() uri: string
@@ -13,19 +15,19 @@ export class RemoteSubscribeComponent extends FormReactive implements OnInit {
 
   constructor (
     protected formValidatorService: FormValidatorService,
-    private userValidatorsService: UserValidatorsService
+    private notifier: Notifier
   ) {
     super()
   }
 
   ngOnInit () {
     this.buildForm({
-      text: this.userValidatorsService.USER_EMAIL
+      text: USER_HANDLE_VALIDATOR
     })
   }
 
   onValidKey () {
-    this.check()
+    this.forceCheck()
     if (!this.form.valid) return
 
     this.formValidated()
@@ -35,22 +37,31 @@ export class RemoteSubscribeComponent extends FormReactive implements OnInit {
     const address = this.form.value['text']
     const [ username, hostname ] = address.split('@')
 
+    const protocol = window.location.protocol
+
     // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5
-    fetch(`https://${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`)
+    fetch(`${protocol}//${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`)
       .then(response => response.json())
-      .then(data => new Promise((resolve, reject) => {
-        if (data && Array.isArray(data.links)) {
-          const link: { template: string } = data.links.find((link: any) => {
-            return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe'
-          })
-
-          if (link && link.template.includes('{uri}')) {
-            resolve(link.template.replace('{uri}', encodeURIComponent(this.uri)))
-          }
+      .then(data => {
+        if (!data || Array.isArray(data.links) === false) {
+          throw new Error('Not links in webfinger response')
+        }
+
+        const link: { template: string } = data.links.find((link: any) => {
+          return link && typeof link.template === 'string' && link.rel === 'http://ostatus.org/schema/1.0/subscribe'
+        })
+
+        if (link?.template.includes('{uri}')) {
+          return link.template.replace('{uri}', encodeURIComponent(this.uri))
         }
-        reject()
-      }))
+
+        throw new Error('No subscribe template in webfinger response')
+      })
       .then(window.open)
-      .catch(err => console.error(err))
+      .catch(err => {
+        console.error(err)
+
+        this.notifier.error($localize`Cannot fetch information of this remote account`)
+      })
   }
 }