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';
|
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
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';
|
2018-10-03 06:37:41 +02:00
|
|
|
import { Observable, Subscription } from 'rxjs';
|
2018-10-04 02:13:31 +02:00
|
|
|
import { Status, Results } from '../../../../services/models/mastodon.interfaces';
|
2018-10-03 06:37:41 +02:00
|
|
|
// import { map } from "rxjs/operators";
|
2018-10-02 05:46:12 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-action-bar',
|
|
|
|
templateUrl: './action-bar.component.html',
|
|
|
|
styleUrls: ['./action-bar.component.scss']
|
|
|
|
})
|
2018-10-03 06:37:41 +02:00
|
|
|
export class ActionBarComponent implements OnInit, OnDestroy {
|
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
@Input() statusWrapper: StatusWrapper;
|
2018-10-13 07:10:43 +02:00
|
|
|
@Output() replyEvent = new EventEmitter();
|
2018-10-02 05:46:12 +02:00
|
|
|
|
|
|
|
isFavorited: boolean;
|
|
|
|
isBoosted: boolean;
|
2018-10-03 06:37:41 +02:00
|
|
|
|
2018-10-02 06:19:11 +02:00
|
|
|
isBoostLocked: boolean;
|
|
|
|
isLocked: boolean;
|
2018-10-02 05:46:12 +02:00
|
|
|
|
2018-10-03 06:37:41 +02:00
|
|
|
private isProviderSelected: boolean;
|
|
|
|
private selectedAccounts: AccountInfo[];
|
|
|
|
|
2018-10-04 02:58:39 +02:00
|
|
|
private favoriteStatePerAccountId: { [id: string]: boolean; } = {};
|
|
|
|
private bootedStatePerAccountId: { [id: string]: boolean; } = {};
|
|
|
|
|
2018-10-03 06:37:41 +02:00
|
|
|
private accounts$: Observable<AccountInfo[]>;
|
|
|
|
private accountSub: Subscription;
|
|
|
|
|
2018-10-02 06:19:11 +02:00
|
|
|
constructor(
|
|
|
|
private readonly store: Store,
|
2018-10-03 06:37:41 +02:00
|
|
|
private readonly mastodonService: MastodonService) {
|
|
|
|
|
|
|
|
this.accounts$ = this.store.select(state => state.registeredaccounts.accounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
// const selectedAccounts = this.getSelectedAccounts();
|
|
|
|
// this.checkStatus(selectedAccounts);
|
|
|
|
|
2018-10-04 02:58:39 +02:00
|
|
|
const status = this.statusWrapper.status;
|
|
|
|
const account = this.statusWrapper.provider;
|
|
|
|
this.favoriteStatePerAccountId[account.id] = status.favourited;
|
|
|
|
this.bootedStatePerAccountId[account.id] = status.reblogged;
|
|
|
|
|
2018-10-03 06:37:41 +02:00
|
|
|
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 {
|
2018-10-03 06:37:41 +02:00
|
|
|
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;
|
2018-10-02 05:46:12 +02:00
|
|
|
|
2018-10-03 06:37:41 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-10-04 02:58:39 +02:00
|
|
|
|
|
|
|
this.checkIfFavorited();
|
|
|
|
this.checkIfBoosted();
|
2018-10-02 05:46:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reply(): boolean {
|
2018-10-13 07:10:43 +02:00
|
|
|
this.replyEvent.emit();
|
2018-10-02 05:46:12 +02:00
|
|
|
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) => {
|
2018-10-04 02:58:39 +02:00
|
|
|
if (this.isBoosted) {
|
2018-10-04 02:13:31 +02:00
|
|
|
return this.mastodonService.unreblog(account, status);
|
|
|
|
} else {
|
|
|
|
return this.mastodonService.reblog(account, status);
|
|
|
|
}
|
|
|
|
})
|
2018-10-04 02:58:39 +02:00
|
|
|
.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);
|
|
|
|
});
|
|
|
|
});
|
2018-10-04 02:58:39 +02:00
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
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) => {
|
2018-10-04 02:58:39 +02:00
|
|
|
if (this.isFavorited) {
|
2018-10-04 02:13:31 +02:00
|
|
|
return this.mastodonService.unfavorite(account, status);
|
|
|
|
} else {
|
|
|
|
return this.mastodonService.favorite(account, status);
|
|
|
|
}
|
|
|
|
})
|
2018-10-04 02:58:39 +02:00
|
|
|
.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);
|
|
|
|
});
|
2018-10-04 02:58:39 +02:00
|
|
|
});
|
2018-10-02 05:46:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-04 02:58:39 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
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;
|
2018-10-03 06:37:41 +02:00
|
|
|
return regAccounts;
|
2018-10-02 06:19:11 +02:00
|
|
|
}
|
2018-10-02 05:46:12 +02:00
|
|
|
}
|