Sengi-Windows-MacOS-Linux/src/app/components/stream/status/action-bar/action-bar.component.ts

192 lines
6.5 KiB
TypeScript
Raw Normal View History

2018-10-13 07:10:43 +02:00
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
2018-10-02 06:19:11 +02:00
import { Store } from '@ngxs/store';
import { StatusWrapper } from '../../stream.component';
2018-10-02 06:19:11 +02:00
import { MastodonService } from '../../../../services/mastodon.service';
import { AccountInfo } from '../../../../states/accounts.state';
import { Observable, Subscription } from 'rxjs';
2018-10-04 02:13:31 +02:00
import { Status, Results } from '../../../../services/models/mastodon.interfaces';
// import { map } from "rxjs/operators";
@Component({
selector: 'app-action-bar',
templateUrl: './action-bar.component.html',
styleUrls: ['./action-bar.component.scss']
})
export class ActionBarComponent implements OnInit, OnDestroy {
@Input() statusWrapper: StatusWrapper;
2018-10-13 07:10:43 +02:00
@Output() replyEvent = new EventEmitter();
isFavorited: boolean;
isBoosted: boolean;
2018-10-02 06:19:11 +02:00
isBoostLocked: boolean;
isLocked: boolean;
private isProviderSelected: boolean;
private selectedAccounts: AccountInfo[];
private favoriteStatePerAccountId: { [id: string]: boolean; } = {};
private bootedStatePerAccountId: { [id: string]: boolean; } = {};
private accounts$: Observable<AccountInfo[]>;
private accountSub: Subscription;
2018-10-02 06:19:11 +02:00
constructor(
private readonly store: Store,
private readonly mastodonService: MastodonService) {
this.accounts$ = this.store.select(state => state.registeredaccounts.accounts);
}
ngOnInit() {
// const selectedAccounts = this.getSelectedAccounts();
// this.checkStatus(selectedAccounts);
const status = this.statusWrapper.status;
const account = this.statusWrapper.provider;
this.favoriteStatePerAccountId[account.id] = status.favourited;
this.bootedStatePerAccountId[account.id] = status.reblogged;
this.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => {
this.checkStatus(accounts);
});
}
ngOnDestroy(): void {
this.accountSub.unsubscribe();
}
2018-10-04 02:13:31 +02:00
private checkStatus(accounts: AccountInfo[]): void {
const status = this.statusWrapper.status;
const provider = this.statusWrapper.provider;
this.selectedAccounts = accounts.filter(x => x.isSelected);
this.isProviderSelected = this.selectedAccounts.filter(x => x.id === provider.id).length > 0;
if (status.visibility === 'direct' || status.visibility === 'private') {
this.isBoostLocked = true;
} else {
this.isBoostLocked = false;
}
if ((status.visibility === 'direct' || status.visibility === 'private') && !this.isProviderSelected) {
this.isLocked = true;
} else {
this.isLocked = false;
}
this.checkIfFavorited();
this.checkIfBoosted();
}
reply(): boolean {
2018-10-13 07:10:43 +02:00
this.replyEvent.emit();
return false;
}
boost(): boolean {
2018-10-04 02:13:31 +02:00
this.selectedAccounts.forEach((account: AccountInfo) => {
const isProvider = this.statusWrapper.provider.id === account.id;
let pipeline: Promise<Status> = Promise.resolve(this.statusWrapper.status);
if (!isProvider) {
pipeline = pipeline.then((foreignStatus: Status) => {
const statusUrl = foreignStatus.url;
return this.mastodonService.search(account, statusUrl)
.then((results: Results) => {
//TODO check and type errors
return results.statuses[0];
});
});
}
pipeline
.then((status: Status) => {
if (this.isBoosted) {
2018-10-04 02:13:31 +02:00
return this.mastodonService.unreblog(account, status);
} else {
return this.mastodonService.reblog(account, status);
}
})
.then((boostedStatus: Status) => {
this.bootedStatePerAccountId[account.id] = boostedStatus.reblogged;
this.checkIfBoosted();
// this.isBoosted = !this.isBoosted;
2018-10-04 02:13:31 +02:00
})
.catch(err => {
console.error(err);
});
});
return false;
}
favorite(): boolean {
2018-10-04 02:13:31 +02:00
this.selectedAccounts.forEach((account: AccountInfo) => {
const isProvider = this.statusWrapper.provider.id === account.id;
let pipeline: Promise<Status> = Promise.resolve(this.statusWrapper.status);
if (!isProvider) {
pipeline = pipeline.then((foreignStatus: Status) => {
const statusUrl = foreignStatus.url;
return this.mastodonService.search(account, statusUrl)
.then((results: Results) => {
//TODO check and type errors
return results.statuses[0];
});
});
}
pipeline
.then((status: Status) => {
if (this.isFavorited) {
2018-10-04 02:13:31 +02:00
return this.mastodonService.unfavorite(account, status);
} else {
return this.mastodonService.favorite(account, status);
}
})
.then((favoritedStatus: Status) => {
this.favoriteStatePerAccountId[account.id] = favoritedStatus.favourited;
this.checkIfFavorited();
// this.isFavorited = !this.isFavorited;
2018-10-04 02:13:31 +02:00
})
.catch(err => {
console.error(err);
});
});
return false;
}
private checkIfBoosted() {
const selectedAccount = <AccountInfo>this.selectedAccounts[0];
if (selectedAccount) {
this.isBoosted = this.bootedStatePerAccountId[selectedAccount.id];
} else {
this.isBoosted = false;
}
}
private checkIfFavorited() {
const selectedAccount = <AccountInfo>this.selectedAccounts[0];
if (selectedAccount) {
this.isFavorited = this.favoriteStatePerAccountId[selectedAccount.id];
} else {
this.isFavorited = false;
}
}
more(): boolean {
console.warn('more');
return false;
}
2018-10-02 06:19:11 +02:00
private getSelectedAccounts(): AccountInfo[] {
var regAccounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
return regAccounts;
2018-10-02 06:19:11 +02:00
}
}