Sengi-Windows-MacOS-Linux/src/app/components/floating-column/add-new-account/add-new-account.component.ts

186 lines
7.0 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, Input } from '@angular/core';
2019-02-12 05:41:21 +01:00
import { HttpErrorResponse } from '@angular/common/http';
import { Store } from '@ngxs/store';
import { RegisteredAppsStateModel, AppInfo, AddRegisteredApp } from '../../../states/registered-apps.state';
import { AuthService, CurrentAuthProcess } from '../../../services/auth.service';
import { AppData } from '../../../services/models/mastodon.interfaces';
2019-02-12 05:41:21 +01:00
import { NotificationService } from '../../../services/notification.service';
2023-08-20 03:28:20 +02:00
import { ToolsService } from '../../../services/tools.service';
2018-09-22 06:22:51 +02:00
@Component({
selector: 'app-add-new-account',
templateUrl: './add-new-account.component.html',
styleUrls: ['./add-new-account.component.scss']
2018-09-22 06:22:51 +02:00
})
2023-08-20 03:28:20 +02:00
export class AddNewAccountComponent implements OnInit {
2019-07-10 02:45:10 +02:00
private blockList = ['gab.com', 'gab.ai', 'cyzed.com'];
private comradeList = ['juche.town'];
2019-06-01 05:54:45 +02:00
2019-07-07 02:56:19 +02:00
isComrade: boolean;
2020-01-01 19:09:02 +01:00
isLoading: boolean;
private instance: string;
2019-07-07 02:13:05 +02:00
@Input()
set setInstance(value: string) {
2020-04-15 07:18:33 +02:00
this.instance = value.replace('http://', '').replace('https://', '').replace('/', '').toLowerCase().trim();
2019-07-07 02:13:05 +02:00
this.checkComrad();
2023-08-20 03:28:20 +02:00
this.checkInstanceMultiAccount(value);
2019-07-07 02:13:05 +02:00
}
get setInstance(): string {
return this.instance;
2019-07-07 02:13:05 +02:00
}
2018-09-22 06:22:51 +02:00
constructor(
2023-08-20 03:28:20 +02:00
private readonly toolsService: ToolsService,
2019-02-12 05:41:21 +01:00
private readonly notificationService: NotificationService,
private readonly authService: AuthService,
private readonly store: Store) { }
2018-09-22 06:22:51 +02:00
ngOnInit() {
}
2018-09-22 06:22:51 +02:00
2019-07-07 02:13:05 +02:00
checkComrad(): any {
if (this.instance) {
2019-07-07 02:13:05 +02:00
let cleanInstance = this.instance.replace('http://', '').replace('https://', '').toLowerCase();
2019-07-10 02:45:10 +02:00
for (let b of this.comradeList) {
2019-07-07 02:13:05 +02:00
if (cleanInstance == b || cleanInstance.includes(`.${b}`)) {
2019-07-07 02:56:19 +02:00
this.isComrade = true;
2019-07-07 02:13:05 +02:00
return;
}
}
}
2019-07-07 02:56:19 +02:00
this.isComrade = false;
2019-07-07 02:13:05 +02:00
}
2023-08-20 03:28:20 +02:00
isInstanceMultiAccount: boolean;
isInstanceMultiAccountLoading: boolean;
checkInstanceMultiAccount(value: string) {
if(value) {
const instances: string[] = this.toolsService.getAllAccounts().map(x => x.instance);
if(instances && instances.indexOf(value) > -1){
this.isInstanceMultiAccount = true;
this.isInstanceMultiAccountLoading = true;
setTimeout(() => {
this.isInstanceMultiAccountLoading = false;
}, 2000);
} else {
this.isInstanceMultiAccount = false;
this.isInstanceMultiAccountLoading = false;
}
}
}
2019-07-07 02:13:05 +02:00
onSubmit(): boolean {
2023-08-20 03:28:20 +02:00
if(this.isLoading || !this.instance || this.isInstanceMultiAccountLoading) return false;
2020-02-15 03:01:07 +01:00
this.isLoading = true;
2019-06-01 05:54:45 +02:00
2020-02-15 03:01:07 +01:00
this.checkBlockList(this.instance);
2019-07-07 02:13:05 +02:00
this.checkAndCreateApplication(this.instance)
.then((appData: AppData) => {
this.redirectToInstanceAuthPage(this.instance, appData);
2019-02-12 05:41:21 +01:00
})
2020-01-01 19:09:02 +01:00
.then(x => {
setTimeout(() => {
this.isLoading = false;
}, 1000);
})
2019-02-12 05:41:21 +01:00
.catch((err: HttpErrorResponse) => {
2020-01-01 19:09:02 +01:00
this.isLoading = false;
if (err instanceof HttpErrorResponse) {
2019-09-07 23:52:07 +02:00
this.notificationService.notifyHttpError(err, null);
2019-06-01 05:54:45 +02:00
} else if ((<Error>err).message === 'CORS') {
2019-09-07 23:52:07 +02:00
this.notificationService.notify(null, null, 'Connection Error. It\'s usually a CORS issue with the server you\'re connecting to. Please check in the console and if so, contact your administrator with those informations.', true);
} else {
2019-09-07 23:52:07 +02:00
this.notificationService.notify(null, null, 'Unkown error', true);
}
});
return false;
}
2019-07-07 02:13:05 +02:00
private checkBlockList(instance: string) {
2019-06-01 21:50:08 +02:00
let cleanInstance = instance.replace('http://', '').replace('https://', '').toLowerCase();
2019-06-01 05:54:45 +02:00
for (let b of this.blockList) {
2019-07-07 02:13:05 +02:00
if (cleanInstance == b || cleanInstance.includes(`.${b}`)) {
2019-06-01 05:54:45 +02:00
let content = '<div style="width:100%; height:100%; background-color: black;"><iframe style="pointer-events: none;" width="100%" height="100%" src="https://www.youtube.com/embed/dQw4w9WgXcQ?rel=0&autoplay=1&showinfo=0&controls=0" allow="autoplay; fullscreen"></div>';
document.open();
document.write(content);
document.close();
throw Error('Oh Noz!');
}
}
}
private checkAndCreateApplication(instance: string): Promise<AppData> {
const alreadyRegisteredApps = this.getAllSavedApps();
const instanceApps = alreadyRegisteredApps.filter(x => x.instance === instance);
if (instanceApps.length !== 0) {
return Promise.resolve(instanceApps[0].app);
} else {
2019-12-01 03:16:25 +01:00
let redirect_uri = this.getLocalHostname();
2020-01-01 18:35:15 +01:00
2020-02-28 07:03:16 +01:00
// let userAgent = navigator.userAgent.toLowerCase();
// console.log(`userAgent ${userAgent}`);
2020-01-01 18:35:15 +01:00
2020-02-28 07:03:16 +01:00
// if (userAgent.includes(' electron/')) {
// redirect_uri += '/register';
// }
2019-12-01 03:16:25 +01:00
2019-05-16 05:48:29 +02:00
return this.authService.createNewApplication(instance, 'Sengi', redirect_uri, 'read write follow', 'https://nicolasconstant.github.io/sengi/')
.then((appData: AppData) => {
return this.saveNewApp(instance, appData)
2020-01-01 18:35:15 +01:00
.then(() => {
return new Promise<AppData>(resolve => setTimeout(resolve, 1000, appData));
});
})
.catch((err: HttpErrorResponse) => {
if (err.status === 0) {
throw Error('CORS');
} else {
throw err;
}
});
}
}
private getAllSavedApps(): AppInfo[] {
const snapshot = <RegisteredAppsStateModel>this.store.snapshot().registeredapps;
return snapshot.apps;
}
private redirectToInstanceAuthPage(instance: string, app: AppData) {
const appDataTemp = new CurrentAuthProcess(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 {
2019-11-02 05:47:04 +01:00
let href = window.location.href;
if(href.includes('/home')){
return href.split('/home')[0];
} else {
return location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
}
}
private saveNewApp(instance: string, app: AppData): Promise<any> {
const appInfo = new AppInfo();
appInfo.instance = instance;
appInfo.app = app;
return this.store.dispatch([
new AddRegisteredApp(appInfo)
]).toPromise();
}
2018-09-22 06:22:51 +02:00
}