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 06:21:21 +02:00
|
|
|
import { faPenAlt, faTrash, faPlus } from "@fortawesome/free-solid-svg-icons";
|
2019-03-20 04:13:50 +01:00
|
|
|
|
|
|
|
import { NotificationService } from '../../../../services/notification.service';
|
|
|
|
import { StreamElement, StreamTypeEnum, AddStream, RemoveAllStreams } from '../../../../states/streams.state';
|
|
|
|
import { AccountWrapper } from '../../../../models/account.models';
|
|
|
|
import { RemoveAccount } from '../../../../states/accounts.state';
|
|
|
|
import { NavigationService } from '../../../../services/navigation.service';
|
2019-05-19 02:44:36 +02:00
|
|
|
import { MastodonService } from '../../../../services/mastodon.service';
|
2019-03-20 03:25:04 +01:00
|
|
|
|
|
|
|
@Component({
|
2019-03-20 04:13:50 +01:00
|
|
|
selector: 'app-my-account',
|
|
|
|
templateUrl: './my-account.component.html',
|
|
|
|
styleUrls: ['./my-account.component.scss']
|
2019-03-20 03:25:04 +01:00
|
|
|
})
|
2019-04-08 02:34:33 +02:00
|
|
|
export class MyAccountComponent implements OnInit, OnDestroy {
|
2019-05-19 06:21:21 +02:00
|
|
|
faPlus = faPlus;
|
|
|
|
faTrash = faTrash;
|
|
|
|
faPenAlt = faPenAlt;
|
2019-04-08 02:34:33 +02:00
|
|
|
faCheckSquare = faCheckSquare;
|
2019-03-20 04:13:50 +01:00
|
|
|
|
2019-04-08 02:34:33 +02:00
|
|
|
availableStreams: StreamWrapper[] = [];
|
2019-05-19 02:44:36 +02:00
|
|
|
availableLists: StreamWrapper[] = [];
|
2019-03-20 04:13:50 +01:00
|
|
|
|
2019-04-08 02:34:33 +02:00
|
|
|
private _account: AccountWrapper;
|
|
|
|
@Input('account')
|
|
|
|
set account(acc: AccountWrapper) {
|
|
|
|
this._account = acc;
|
|
|
|
this.loadStreams(acc);
|
|
|
|
}
|
|
|
|
get account(): AccountWrapper {
|
|
|
|
return this._account;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Select(state => state.streamsstatemodel.streams) streamElements$: Observable<StreamElement[]>;
|
|
|
|
private streamChangedSub: Subscription;
|
2019-03-20 04:13:50 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private readonly store: Store,
|
|
|
|
private readonly navigationService: NavigationService,
|
2019-05-19 02:44:36 +02:00
|
|
|
private readonly mastodonService: MastodonService,
|
|
|
|
private readonly notificationService: NotificationService) { }
|
2019-03-20 03:25:04 +01:00
|
|
|
|
2019-03-20 04:13:50 +01:00
|
|
|
ngOnInit() {
|
2019-04-08 02:34:33 +02:00
|
|
|
this.streamChangedSub = this.streamElements$.subscribe((streams: StreamElement[]) => {
|
|
|
|
this.loadStreams(this.account);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
if(this.streamChangedSub) {
|
|
|
|
this.streamChangedSub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadStreams(account: AccountWrapper){
|
|
|
|
const instance = account.info.instance;
|
2019-03-20 04:13:50 +01:00
|
|
|
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-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
|
|
|
|
|
|
|
this.availableLists.length = 0;
|
|
|
|
this.mastodonService.getLists(account.info)
|
|
|
|
.then((streams: StreamElement[]) => {
|
|
|
|
this.availableLists.length = 0;
|
|
|
|
for (let stream of streams) {
|
|
|
|
let wrappedStream = new StreamWrapper(stream);
|
|
|
|
if(loadedStreams.find(x => x.id == stream.id)){
|
|
|
|
wrappedStream.isAdded = true;
|
|
|
|
} else {
|
|
|
|
wrappedStream.isAdded = false;
|
|
|
|
}
|
|
|
|
this.availableLists.push(wrappedStream);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
|
|
|
|
});
|
2019-03-20 04:13:50 +01:00
|
|
|
}
|
2019-03-20 03:25:04 +01:00
|
|
|
|
2019-04-08 02:34:33 +02:00
|
|
|
addStream(stream: StreamWrapper): boolean {
|
|
|
|
if (stream && !stream.isAdded) {
|
2019-03-20 04:13:50 +01:00
|
|
|
this.store.dispatch([new AddStream(stream)]).toPromise()
|
|
|
|
.then(() => {
|
2019-04-08 02:34:33 +02:00
|
|
|
stream.isAdded = true;
|
|
|
|
});
|
2019-03-20 04:13:50 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-20 03:25:04 +01:00
|
|
|
|
2019-03-20 04:13:50 +01:00
|
|
|
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 => {
|
|
|
|
this.notificationService.notifyHttpError(err);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.creationLoading = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
editList(list: StreamWrapper): boolean {
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteList(list: StreamWrapper): boolean {
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-20 03:25:04 +01:00
|
|
|
}
|
2019-04-08 02:34:33 +02:00
|
|
|
|
|
|
|
class StreamWrapper extends StreamElement {
|
|
|
|
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;
|
2019-04-08 02:34:33 +02:00
|
|
|
}
|