Sengi-Windows-MacOS-Linux/src/app/app.component.ts

267 lines
9.7 KiB
TypeScript
Raw Normal View History

2018-09-10 03:55:16 +02:00
import { Component, OnInit, OnDestroy } from '@angular/core';
2020-03-07 20:54:44 +01:00
import { ActivatedRoute, Router } from '@angular/router';
2019-03-10 03:49:07 +01:00
import { Subscription, Observable, Subject } from 'rxjs';
import { debounceTime, map } from 'rxjs/operators';
2020-03-07 20:54:44 +01:00
import { Select, Store } from '@ngxs/store';
2020-02-26 05:55:35 +01:00
import { faTimes } from "@fortawesome/free-solid-svg-icons";
2018-03-15 01:48:52 +01:00
2019-07-04 01:55:33 +02:00
import { NavigationService, LeftPanelType, OpenLeftPanelEvent } from './services/navigation.service';
2019-02-11 04:23:10 +01:00
import { StreamElement } from './states/streams.state';
2020-03-07 20:54:44 +01:00
import { AccountInfo, AddAccount } from "./states/accounts.state";
2019-02-26 05:38:15 +01:00
import { OpenMediaEvent } from './models/common.model';
2019-03-08 05:24:23 +01:00
import { ToolsService } from './services/tools.service';
import { MediaService } from './services/media.service';
import { ServiceWorkerService } from './services/service-worker.service';
2020-03-07 20:54:44 +01:00
import { AuthService, CurrentAuthProcess } from './services/auth.service';
import { MastodonWrapperService } from './services/mastodon-wrapper.service';
import { TokenData, Account } from './services/models/mastodon.interfaces';
import { NotificationService } from './services/notification.service';
import { AppInfo, RegisteredAppsStateModel } from './states/registered-apps.state';
import { HttpErrorResponse } from '@angular/common/http';
2018-03-15 01:48:52 +01:00
@Component({
2019-02-11 04:23:10 +01:00
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
2018-03-15 01:48:52 +01:00
})
2019-02-26 05:38:15 +01:00
export class AppComponent implements OnInit, OnDestroy {
2020-02-26 05:55:35 +01:00
faTimes = faTimes;
2019-02-11 04:23:10 +01:00
title = 'Sengi';
floatingColumnActive: boolean;
tutorialActive: boolean;
2019-02-26 05:38:15 +01:00
openedMediaEvent: OpenMediaEvent
2020-05-01 04:11:00 +02:00
restartNotificationLabel: string;
restartNotificationAvailable: boolean;
showRestartNotification: boolean;
2019-02-25 06:04:09 +01:00
2020-03-07 20:54:44 +01:00
private authStorageKey: string = 'tempAuth';
2019-02-11 04:23:10 +01:00
private columnEditorSub: Subscription;
2019-02-26 05:38:15 +01:00
private openMediaSub: Subscription;
private streamSub: Subscription;
2019-03-10 03:49:07 +01:00
private dragoverSub: Subscription;
2020-03-07 20:54:44 +01:00
private paramsSub: Subscription;
private restartNotificationSub: Subscription;
2020-05-01 04:11:00 +02:00
2019-02-11 04:23:10 +01:00
@Select(state => state.streamsstatemodel.streams) streamElements$: Observable<StreamElement[]>;
2019-03-08 05:24:23 +01:00
constructor(
2020-03-07 20:54:44 +01:00
private readonly router: Router,
private readonly notificationService: NotificationService,
private readonly store: Store,
private readonly mastodonService: MastodonWrapperService,
private readonly authService: AuthService,
private readonly activatedRoute: ActivatedRoute,
private readonly serviceWorkerService: ServiceWorkerService, // Ensure update checks
2019-03-08 05:24:23 +01:00
private readonly toolsService: ToolsService,
private readonly mediaService: MediaService,
private readonly navigationService: NavigationService) {
2019-02-11 04:23:10 +01:00
}
ngOnInit(): void {
2020-05-01 04:11:00 +02:00
this.paramsSub = this.activatedRoute.queryParams.subscribe(params => {
2020-03-07 20:54:44 +01:00
const code = params['code'];
if (!code) {
return;
}
const appDataWrapper = <CurrentAuthProcess>JSON.parse(localStorage.getItem(this.authStorageKey));
if (!appDataWrapper) {
this.notificationService.notify('', 400, 'Something when wrong in the authentication process. Please retry.', true);
this.router.navigate(['/']);
return;
}
const appInfo = this.getAllSavedApps().filter(x => x.instance === appDataWrapper.instance)[0];
let usedTokenData: TokenData;
this.authService.getToken(appDataWrapper.instance, appInfo.app.client_id, appInfo.app.client_secret, code, appInfo.app.redirect_uri)
.then((tokenData: TokenData) => {
2020-05-01 04:11:00 +02:00
if (tokenData.refresh_token && !tokenData.created_at) {
2020-03-07 20:54:44 +01:00
const nowEpoch = Date.now() / 1000 | 0;
2020-05-01 04:11:00 +02:00
tokenData.created_at = nowEpoch;
2020-03-07 20:54:44 +01:00
}
usedTokenData = tokenData;
return this.mastodonService.retrieveAccountDetails({ 'instance': appDataWrapper.instance, 'id': '', 'username': '', 'order': 0, 'isSelected': true, 'token': tokenData });
})
.then((account: Account) => {
2020-05-01 04:11:00 +02:00
var username = account.username.toLowerCase();
2020-03-07 20:54:44 +01:00
var instance = appDataWrapper.instance.toLowerCase();
2020-05-01 04:11:00 +02:00
if (this.isAccountAlreadyPresent(username, instance)) {
2020-03-07 20:54:44 +01:00
this.notificationService.notify(null, null, `Account @${username}@${instance} is already registered`, true);
this.router.navigate(['/']);
return;
}
const accountInfo = new AccountInfo();
2020-05-01 04:11:00 +02:00
accountInfo.username = username;
2020-03-07 20:54:44 +01:00
accountInfo.instance = instance;
accountInfo.token = usedTokenData;
this.store.dispatch([new AddAccount(accountInfo)])
.subscribe(() => {
localStorage.removeItem(this.authStorageKey);
this.router.navigate(['/']);
});
})
.catch((err: HttpErrorResponse) => {
this.notificationService.notifyHttpError(err, null);
this.router.navigate(['/']);
});
});
2019-02-26 05:38:15 +01:00
this.streamSub = this.streamElements$.subscribe((streams: StreamElement[]) => {
if (streams && streams.length === 0) {
2019-02-11 04:23:10 +01:00
this.tutorialActive = true;
} else {
this.tutorialActive = false;
}
});
2019-07-04 01:55:33 +02:00
this.columnEditorSub = this.navigationService.activatedPanelSubject.subscribe((event: OpenLeftPanelEvent) => {
if (event.type === LeftPanelType.Closed) {
2019-02-11 04:23:10 +01:00
this.floatingColumnActive = false;
this.checkEnhancedTutorial();
2019-02-11 04:23:10 +01:00
} else {
this.floatingColumnActive = true;
}
});
2019-02-26 05:38:15 +01:00
this.openMediaSub = this.navigationService.activatedMediaSubject.subscribe((openedMediaEvent: OpenMediaEvent) => {
if (openedMediaEvent) {
this.openedMediaEvent = openedMediaEvent;
// this.mediaViewerActive = true;
2019-03-08 04:33:10 +01:00
2019-02-26 05:38:15 +01:00
}
});
2019-03-10 03:49:07 +01:00
this.dragoverSub = this.dragoverSubject
.pipe(
debounceTime(1500)
2019-03-10 03:49:07 +01:00
)
.subscribe(() => {
2019-03-10 03:49:07 +01:00
this.drag = false;
});
this.restartNotificationSub = this.notificationService.restartNotificationStream.subscribe((label: string) => {
if (label) {
this.displayRestartNotification(label);
}
});
2019-02-11 04:23:10 +01:00
}
enhancedTutorialActive: boolean;
enhancedTutorialVisible: boolean;
private checkEnhancedTutorial() {
let enhancedTutorialDesactivated = JSON.parse(localStorage.getItem('tutorial'));
if (!this.floatingColumnActive && !this.tutorialActive && !enhancedTutorialDesactivated) {
setTimeout(() => {
this.enhancedTutorialActive = true;
setTimeout(() => {
this.enhancedTutorialVisible = true;
}, 100);
}, 500);
}
}
closeTutorial(){
2020-09-12 20:12:59 +02:00
localStorage.setItem('tutorial', JSON.stringify(true));
this.enhancedTutorialVisible = false;
setTimeout(() => {
this.enhancedTutorialActive = false;
2020-09-12 20:12:59 +02:00
}, 400);
}
2019-02-11 04:23:10 +01:00
ngOnDestroy(): void {
2019-02-26 05:38:15 +01:00
this.streamSub.unsubscribe();
2019-02-11 04:23:10 +01:00
this.columnEditorSub.unsubscribe();
2019-02-26 05:38:15 +01:00
this.openMediaSub.unsubscribe();
2019-03-10 03:49:07 +01:00
this.dragoverSub.unsubscribe();
2020-03-07 20:54:44 +01:00
this.paramsSub.unsubscribe();
this.restartNotificationSub.unsubscribe();
2019-02-26 05:38:15 +01:00
}
2019-03-08 04:33:10 +01:00
closeMedia() {
2019-02-26 05:38:15 +01:00
this.openedMediaEvent = null;
2019-02-11 04:23:10 +01:00
}
2019-03-10 03:49:07 +01:00
private dragoverSubject = new Subject<boolean>();
2019-03-08 04:33:10 +01:00
drag: boolean;
dragenter(event): boolean {
event.stopPropagation();
2019-03-08 05:24:23 +01:00
event.preventDefault();
2019-03-08 04:33:10 +01:00
this.drag = true;
return false;
}
dragleave(event): boolean {
event.stopPropagation();
2019-03-08 05:24:23 +01:00
event.preventDefault();
2019-03-08 04:33:10 +01:00
this.drag = false;
return false;
}
2019-03-08 05:24:23 +01:00
dragover(event): boolean {
2019-03-08 04:33:10 +01:00
event.stopPropagation();
event.preventDefault();
2019-03-10 03:49:07 +01:00
this.dragoverSubject.next(true);
2019-03-08 04:33:10 +01:00
return false;
}
2019-03-10 03:49:07 +01:00
drop(event): boolean {
2019-03-08 04:33:10 +01:00
event.stopPropagation();
2019-03-08 05:24:23 +01:00
event.preventDefault();
2019-03-08 04:33:10 +01:00
this.drag = false;
2019-03-08 05:24:23 +01:00
let files = <File[]>event.dataTransfer.files;
const selectedAccount = this.toolsService.getSelectedAccounts()[0];
2019-03-10 19:36:22 +01:00
this.mediaService.uploadMedia(selectedAccount, files);
2019-03-08 04:33:10 +01:00
return false;
}
2020-02-26 06:02:54 +01:00
loadNewVersion(): boolean {
document.location.reload();
// this.serviceWorkerService.loadNewAppVersion();
return false;
}
displayRestartNotification(label: string): boolean {
this.restartNotificationLabel = label;
this.showRestartNotification = true;
2020-05-01 04:11:00 +02:00
setTimeout(() => {
this.restartNotificationAvailable = true;
2020-05-01 04:11:00 +02:00
}, 200);
return false;
}
closeRestartNotification(): boolean {
this.restartNotificationAvailable = false;
2020-05-01 04:11:00 +02:00
setTimeout(() => {
this.showRestartNotification = false;
}, 250);
2020-02-26 06:02:54 +01:00
return false;
}
2020-03-07 20:54:44 +01:00
2020-05-01 04:11:00 +02:00
private isAccountAlreadyPresent(username: string, instance: string): boolean {
2020-03-07 20:54:44 +01:00
const accounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
for (let acc of accounts) {
2020-05-01 04:11:00 +02:00
if (acc.instance === instance && acc.username == username) {
2020-03-07 20:54:44 +01:00
return true;
}
}
return false;
}
private getAllSavedApps(): AppInfo[] {
const snapshot = <RegisteredAppsStateModel>this.store.snapshot().registeredapps;
return snapshot.apps;
}
2018-03-15 01:48:52 +01:00
}