From 67ed6552b831df66713bac9e672738796128d33f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Jun 2020 14:10:17 +0200 Subject: Reorganize client shared modules --- .../remote-subscribe.component.ts | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts (limited to 'client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts') diff --git a/client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts b/client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts new file mode 100644 index 000000000..09164a5d3 --- /dev/null +++ b/client/src/app/shared/shared-user-subscription/remote-subscribe.component.ts @@ -0,0 +1,58 @@ +import { Component, Input, OnInit } from '@angular/core' +import { FormReactive, FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms' + +@Component({ + selector: 'my-remote-subscribe', + templateUrl: './remote-subscribe.component.html', + styleUrls: ['./remote-subscribe.component.scss'] +}) +export class RemoteSubscribeComponent extends FormReactive implements OnInit { + @Input() uri: string + @Input() interact = false + @Input() showHelp = false + + constructor ( + protected formValidatorService: FormValidatorService, + private userValidatorsService: UserValidatorsService + ) { + super() + } + + ngOnInit () { + this.buildForm({ + text: this.userValidatorsService.USER_EMAIL + }) + } + + onValidKey () { + this.check() + if (!this.form.valid) return + + this.formValidated() + } + + formValidated () { + const address = this.form.value['text'] + const [ username, hostname ] = address.split('@') + + // Should not have CORS error because https://tools.ietf.org/html/rfc7033#section-5 + fetch(`https://${hostname}/.well-known/webfinger?resource=acct:${username}@${hostname}`) + .then(response => response.json()) + .then(data => new Promise((resolve, reject) => { + console.log(data) + + 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))) + } + } + reject() + })) + .then(window.open) + .catch(err => console.error(err)) + } +} -- cgit v1.2.3