diff --git a/src/app/components/floating-column/add-new-account/add-new-account.component.html b/src/app/components/floating-column/add-new-account/add-new-account.component.html index e536d26f..76cee452 100644 --- a/src/app/components/floating-column/add-new-account/add-new-account.component.html +++ b/src/app/components/floating-column/add-new-account/add-new-account.component.html @@ -1,3 +1,12 @@ -

- add-new-account works! -

+
+

Add new account

+ +
+ + +
+ +
+ +
\ No newline at end of file diff --git a/src/app/components/floating-column/add-new-account/add-new-account.component.scss b/src/app/components/floating-column/add-new-account/add-new-account.component.scss index e69de29b..aca3ffd8 100644 --- a/src/app/components/floating-column/add-new-account/add-new-account.component.scss +++ b/src/app/components/floating-column/add-new-account/add-new-account.component.scss @@ -0,0 +1,2 @@ +@import "variables"; +@import "panel"; \ No newline at end of file diff --git a/src/app/components/floating-column/add-new-account/add-new-account.component.ts b/src/app/components/floating-column/add-new-account/add-new-account.component.ts index 390d36b5..6ba64280 100644 --- a/src/app/components/floating-column/add-new-account/add-new-account.component.ts +++ b/src/app/components/floating-column/add-new-account/add-new-account.component.ts @@ -1,15 +1,82 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, Input } from '@angular/core'; +import { RegisteredAppsStateModel, AppInfo, AddRegisteredApp } from '../../../states/registered-apps.state'; +import { AuthService, CurrentAuthProcess } from '../../../services/auth.service'; +import { Store } from '@ngxs/store'; +import { AppData } from '../../../services/models/mastodon.interfaces'; @Component({ - selector: 'app-add-new-account', - templateUrl: './add-new-account.component.html', - styleUrls: ['./add-new-account.component.scss'] + selector: 'app-add-new-account', + templateUrl: './add-new-account.component.html', + styleUrls: ['./add-new-account.component.scss'] }) export class AddNewAccountComponent implements OnInit { + @Input() mastodonFullHandle: string; - constructor() { } + constructor( + private readonly authService: AuthService, + private readonly store: Store) { } - ngOnInit() { - } + ngOnInit() { + } + onSubmit(): boolean { + let fullHandle = this.mastodonFullHandle.split('@').filter(x => x != null && x !== ''); + + const username = fullHandle[0]; + const instance = fullHandle[1]; + + this.checkAndCreateApplication(instance) + .then((appData: AppData) => { + this.redirectToInstanceAuthPage(username, instance, appData); + }); + + return false; + } + + private checkAndCreateApplication(instance: string): Promise { + const alreadyRegisteredApps = this.getAllSavedApps(); + const instanceApps = alreadyRegisteredApps.filter(x => x.instance === instance); + + if (instanceApps.length !== 0) { + console.log('instance already registered'); + return Promise.resolve(instanceApps[0].app); + } else { + console.log('instance not registered'); + const redirect_uri = this.getLocalHostname() + '/register'; + return this.authService.createNewApplication(instance, 'Sengi', redirect_uri, 'read write follow', 'https://github.com/NicolasConstant/sengi') + .then((appData: AppData) => { + return this.saveNewApp(instance, appData) + .then(() => { return appData; }); + }) + } + } + + private getAllSavedApps(): AppInfo[] { + const snapshot = this.store.snapshot().registeredapps; + return snapshot.apps; + } + + private redirectToInstanceAuthPage(username: string, instance: string, app: AppData) { + const appDataTemp = new CurrentAuthProcess(username, instance); + localStorage.setItem('tempAuth', JSON.stringify(appDataTemp)); + + let instanceUrl = this.authService.getInstanceLoginUrl(instance, app.client_id, app.redirect_uri); + + window.location.href = instanceUrl; + } + + private getLocalHostname(): string { + let localHostname = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : ''); + return localHostname; + } + + private saveNewApp(instance: string, app: AppData): Promise { + const appInfo = new AppInfo(); + appInfo.instance = instance; + appInfo.app = app; + + return this.store.dispatch([ + new AddRegisteredApp(appInfo) + ]).toPromise(); + } } diff --git a/src/app/components/floating-column/add-new-status/add-new-status.component.html b/src/app/components/floating-column/add-new-status/add-new-status.component.html index 6c6679ec..e5f5227f 100644 --- a/src/app/components/floating-column/add-new-status/add-new-status.component.html +++ b/src/app/components/floating-column/add-new-status/add-new-status.component.html @@ -1,3 +1,6 @@ -

- message-editor works! -

+
+

new message

+ + + +
diff --git a/src/app/components/floating-column/add-new-status/add-new-status.component.scss b/src/app/components/floating-column/add-new-status/add-new-status.component.scss index e69de29b..aca3ffd8 100644 --- a/src/app/components/floating-column/add-new-status/add-new-status.component.scss +++ b/src/app/components/floating-column/add-new-status/add-new-status.component.scss @@ -0,0 +1,2 @@ +@import "variables"; +@import "panel"; \ No newline at end of file diff --git a/src/app/components/floating-column/manage-account/manage-account.component.html b/src/app/components/floating-column/manage-account/manage-account.component.html index 1a3942af..ccc5fc35 100644 --- a/src/app/components/floating-column/manage-account/manage-account.component.html +++ b/src/app/components/floating-column/manage-account/manage-account.component.html @@ -1,5 +1,5 @@ -