Sengi-Windows-MacOS-Linux/src/app/components/floating-column/manage-account/my-account/my-account.component.ts

231 lines
8.8 KiB
TypeScript
Raw Normal View History

2019-04-08 02:34:33 +02:00
import { Component, OnInit, OnDestroy, Input } from '@angular/core';
import { Observable, Subscription } from 'rxjs';
import { Store, Select } from '@ngxs/store';
import { faCheckSquare } from "@fortawesome/free-regular-svg-icons";
2019-05-19 20:55:37 +02:00
import { faPenAlt, faTrash, faPlus, faCheck, faTimes } from "@fortawesome/free-solid-svg-icons";
import { NotificationService } from '../../../../services/notification.service';
import { StreamElement, StreamTypeEnum, AddStream, RemoveAllStreams, RemoveStream } from '../../../../states/streams.state';
import { AccountWrapper } from '../../../../models/account.models';
import { RemoveAccount } from '../../../../states/accounts.state';
import { NavigationService } from '../../../../services/navigation.service';
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
import { AccountSettings } from '../../../../states/settings.state';
import { SettingsService } from '../../../../services/settings.service';
@Component({
selector: 'app-my-account',
templateUrl: './my-account.component.html',
styleUrls: ['./my-account.component.scss']
})
2019-04-08 02:34:33 +02:00
export class MyAccountComponent implements OnInit, OnDestroy {
faPlus = faPlus;
faTrash = faTrash;
faPenAlt = faPenAlt;
2019-04-08 02:34:33 +02:00
faCheckSquare = faCheckSquare;
2019-05-19 20:55:37 +02:00
faCheck = faCheck;
faTimes = faTimes;
2019-06-23 22:49:55 +02:00
2019-11-16 23:32:58 +01:00
avatarNotificationDisabled: boolean;
2019-06-23 23:18:55 +02:00
customStatusLengthEnabled: boolean;
customStatusLength: number;
private accountSettings: AccountSettings;
2019-04-08 02:34:33 +02:00
availableStreams: StreamWrapper[] = [];
2019-05-19 02:44:36 +02:00
availableLists: StreamWrapper[] = [];
2019-04-08 02:34:33 +02:00
private _account: AccountWrapper;
@Input('account')
set account(acc: AccountWrapper) {
this._account = acc;
this.loadStreams(acc);
this.loadAccountSettings();
2019-04-08 02:34:33 +02:00
}
get account(): AccountWrapper {
return this._account;
}
@Select(state => state.streamsstatemodel.streams) streamElements$: Observable<StreamElement[]>;
private streamChangedSub: Subscription;
constructor(
private readonly settingsService: SettingsService,
private readonly store: Store,
private readonly navigationService: NavigationService,
private readonly mastodonService: MastodonWrapperService,
2019-05-19 02:44:36 +02:00
private readonly notificationService: NotificationService) { }
ngOnInit() {
2019-04-08 02:34:33 +02:00
this.streamChangedSub = this.streamElements$.subscribe((streams: StreamElement[]) => {
this.loadStreams(this.account);
});
2019-04-08 02:34:33 +02:00
}
ngOnDestroy(): void {
if(this.streamChangedSub) {
this.streamChangedSub.unsubscribe();
}
}
private loadAccountSettings(){
this.accountSettings = this.settingsService.getAccountSettings(this.account.info);
2019-11-16 23:32:58 +01:00
this.customStatusLengthEnabled = this.accountSettings.customStatusCharLengthEnabled;
this.customStatusLength = this.accountSettings.customStatusCharLength;
this.avatarNotificationDisabled = this.accountSettings.disableAvatarNotifications;
}
onCustomLengthEnabledChanged(): boolean {
this.accountSettings.customStatusCharLengthEnabled = this.customStatusLengthEnabled;
this.settingsService.saveAccountSettings(this.accountSettings);
return false;
}
customStatusLengthChanged(event): boolean{
this.accountSettings.customStatusCharLength = this.customStatusLength;
this.settingsService.saveAccountSettings(this.accountSettings);
2019-06-23 22:49:55 +02:00
return false;
}
2019-04-08 02:34:33 +02:00
private loadStreams(account: AccountWrapper){
const instance = account.info.instance;
this.availableStreams.length = 0;
2019-05-19 02:44:36 +02:00
this.availableStreams.push(new StreamWrapper(new StreamElement(StreamTypeEnum.global, 'Federated Timeline', account.info.id, null, null, null, instance)));
this.availableStreams.push(new StreamWrapper(new StreamElement(StreamTypeEnum.local, 'Local Timeline', account.info.id, null, null, null, instance)));
this.availableStreams.push(new StreamWrapper(new StreamElement(StreamTypeEnum.personnal, 'Home', account.info.id, null, null, null, instance)));
2019-11-17 04:57:33 +01:00
this.availableStreams.push(new StreamWrapper(new StreamElement(StreamTypeEnum.activity, 'Notifications', account.info.id, null, null, null, instance)));
2019-04-08 02:34:33 +02:00
const loadedStreams = <StreamElement[]>this.store.snapshot().streamsstatemodel.streams;
this.availableStreams.forEach(s => {
if(loadedStreams.find(x => x.id === s.id)){
s.isAdded = true;
} else {
s.isAdded = false;
}
});
2019-05-19 02:44:36 +02:00
2020-05-01 07:32:25 +02:00
// this.availableLists.length = 0;
2019-05-19 02:44:36 +02:00
this.mastodonService.getLists(account.info)
.then((streams: StreamElement[]) => {
2020-05-01 07:32:25 +02:00
// this.availableLists.length = 0;
2019-05-19 02:44:36 +02:00
for (let stream of streams) {
2020-05-01 07:32:25 +02:00
let wrappedStream = this.availableLists.find(x => x.id === stream.id);
if(!wrappedStream){
wrappedStream = new StreamWrapper(stream);
this.availableLists.push(wrappedStream);
}
2019-05-19 02:44:36 +02:00
if(loadedStreams.find(x => x.id == stream.id)){
wrappedStream.isAdded = true;
} else {
wrappedStream.isAdded = false;
2020-05-01 07:32:25 +02:00
}
2019-05-19 02:44:36 +02:00
}
})
.then(_ => {
this.availableLists.sort((a,b) => {
if (a.name < b.name) {
return -1;
}
if (a.name > b.name) {
return 1;
}
return 0;
});
})
2019-05-19 02:44:36 +02:00
.catch(err => {
2019-09-07 23:52:07 +02:00
this.notificationService.notifyHttpError(err, this.account.info);
2019-05-19 02:44:36 +02:00
});
}
2019-04-08 02:34:33 +02:00
addStream(stream: StreamWrapper): boolean {
if (stream && !stream.isAdded) {
this.store.dispatch([new AddStream(stream)]).toPromise()
.then(() => {
2019-04-08 02:34:33 +02:00
stream.isAdded = true;
});
}
return false;
}
2020-05-01 08:51:41 +02:00
removeStream(stream: StreamWrapper): boolean {
if (stream && stream.isAdded) {
this.store.dispatch([new RemoveStream(stream.id)]).toPromise()
.then(() => {
stream.isAdded = false;
});
}
return false;
}
removeAccount(): boolean {
const accountId = this.account.info.id;
this.store.dispatch([new RemoveAllStreams(accountId), new RemoveAccount(accountId)]);
this.navigationService.closePanel();
return false;
}
2019-05-19 08:09:30 +02:00
listTitle: string;
2019-05-19 08:26:46 +02:00
creationLoading: boolean;
2019-05-19 08:09:30 +02:00
createList(): boolean {
if(this.creationLoading || !this.listTitle || this.listTitle == '') return false;
this.creationLoading = true;
this.mastodonService.createList(this.account.info, this.listTitle)
.then((stream: StreamElement) => {
this.listTitle = null;
let wrappedStream = new StreamWrapper(stream);
this.availableLists.push(wrappedStream);
})
.catch(err => {
2019-09-07 23:52:07 +02:00
this.notificationService.notifyHttpError(err, this.account.info);
2019-05-19 08:09:30 +02:00
})
.then(() => {
this.creationLoading = false;
});
return false;
}
editList(list: StreamWrapper): boolean {
list.editList = !list.editList;
2019-05-19 08:09:30 +02:00
return false;
}
2019-05-19 20:55:37 +02:00
openCloseDeleteConfirmation(list: StreamWrapper, state: boolean): boolean {
list.confirmDeletion = state;
return false;
}
2019-05-19 08:09:30 +02:00
2019-05-19 20:55:37 +02:00
deleteList(list: StreamWrapper): boolean {
this.mastodonService.deleteList(this.account.info, list.listId)
.then(() => {
const isAdded = this.availableLists.find(x => x.id === list.id).isAdded;
if(isAdded){
this.store.dispatch([new RemoveStream(list.id)]);
}
2019-05-19 20:55:37 +02:00
this.availableLists = this.availableLists.filter(x => x.id !== list.id);
})
.catch(err => {
2019-09-07 23:52:07 +02:00
this.notificationService.notifyHttpError(err, this.account.info);
2019-05-19 20:55:37 +02:00
});
2019-05-19 08:09:30 +02:00
return false;
}
2019-11-16 23:32:58 +01:00
onDisableAvatarNotificationChanged() {
let settings = this.settingsService.getAccountSettings(this.account.info);
2019-11-16 23:32:58 +01:00
settings.disableAvatarNotifications = this.avatarNotificationDisabled;
this.settingsService.saveAccountSettings(settings);
2019-11-16 23:32:58 +01:00
}
}
2019-04-08 02:34:33 +02:00
export class StreamWrapper extends StreamElement {
2019-04-08 02:34:33 +02:00
constructor(stream: StreamElement) {
2019-05-19 02:44:36 +02:00
super(stream.type, stream.name, stream.accountId, stream.tag, stream.list, stream.listId, stream.instance);
2019-04-08 02:34:33 +02:00
}
isAdded: boolean;
2019-05-19 08:09:30 +02:00
confirmDeletion: boolean;
editList: boolean;
2019-04-08 02:34:33 +02:00
}