2019-08-10 01:08:18 +02:00
|
|
|
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
|
2019-02-12 04:33:54 +01:00
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
2018-10-02 06:19:11 +02:00
|
|
|
import { Store } from '@ngxs/store';
|
2018-10-13 17:38:23 +02:00
|
|
|
import { Observable, Subscription } from 'rxjs';
|
2020-03-13 01:23:53 +01:00
|
|
|
import { faWindowClose, faReply, faRetweet, faStar, faEllipsisH, faLock, faEnvelope, faBookmark } from "@fortawesome/free-solid-svg-icons";
|
2019-02-24 22:54:01 +01:00
|
|
|
import { faWindowClose as faWindowCloseRegular } from "@fortawesome/free-regular-svg-icons";
|
2018-10-02 06:19:11 +02:00
|
|
|
|
2019-10-02 06:14:40 +02:00
|
|
|
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
|
2018-10-02 06:19:11 +02:00
|
|
|
import { AccountInfo } from '../../../../states/accounts.state';
|
2019-07-06 05:41:03 +02:00
|
|
|
import { Status, Account, Results } from '../../../../services/models/mastodon.interfaces';
|
2020-03-14 01:37:21 +01:00
|
|
|
import { ToolsService, OpenThreadEvent, InstanceInfo } from '../../../../services/tools.service';
|
2019-02-12 04:33:54 +01:00
|
|
|
import { NotificationService } from '../../../../services/notification.service';
|
2019-03-06 04:46:50 +01:00
|
|
|
import { StatusWrapper } from '../../../../models/common.model';
|
2020-02-19 05:51:34 +01:00
|
|
|
import { StatusesStateService, StatusState } from '../../../../services/statuses-state.service';
|
2019-07-01 22:30:28 +02:00
|
|
|
|
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 {
|
2019-02-24 22:54:01 +01:00
|
|
|
faWindowClose = faWindowClose;
|
|
|
|
faReply = faReply;
|
|
|
|
faRetweet = faRetweet;
|
|
|
|
faStar = faStar;
|
|
|
|
faWindowCloseRegular = faWindowCloseRegular;
|
2019-06-01 20:22:12 +02:00
|
|
|
faEllipsisH = faEllipsisH;
|
|
|
|
faLock = faLock;
|
2019-07-04 05:47:06 +02:00
|
|
|
faEnvelope = faEnvelope;
|
2020-03-13 01:23:53 +01:00
|
|
|
faBookmark = faBookmark;
|
2018-10-03 06:37:41 +02:00
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
@Input() statusWrapper: StatusWrapper;
|
2018-10-13 07:10:43 +02:00
|
|
|
@Output() replyEvent = new EventEmitter();
|
2019-02-24 22:54:01 +01:00
|
|
|
@Output() cwIsActiveEvent = new EventEmitter<boolean>();
|
2018-10-02 05:46:12 +02:00
|
|
|
|
2019-07-04 02:23:23 +02:00
|
|
|
@Output() browseThreadEvent = new EventEmitter<OpenThreadEvent>();
|
|
|
|
|
2020-03-13 01:23:53 +01:00
|
|
|
isBookmarked: boolean;
|
2018-10-02 05:46:12 +02:00
|
|
|
isFavorited: boolean;
|
|
|
|
isBoosted: boolean;
|
2019-07-04 05:47:06 +02:00
|
|
|
isDM: boolean;
|
2018-10-03 06:37:41 +02:00
|
|
|
|
2018-10-02 06:19:11 +02:00
|
|
|
isBoostLocked: boolean;
|
|
|
|
isLocked: boolean;
|
2020-03-14 01:37:21 +01:00
|
|
|
isBookmarksAvailable: boolean;
|
2018-10-02 05:46:12 +02:00
|
|
|
|
2020-03-13 01:23:53 +01:00
|
|
|
bookmarkingIsLoading: boolean;
|
2019-04-07 20:18:10 +02:00
|
|
|
favoriteIsLoading: boolean;
|
|
|
|
boostIsLoading: boolean;
|
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
isContentWarningActive: boolean = false;
|
2019-02-24 22:54:01 +01:00
|
|
|
|
2019-08-10 01:08:18 +02:00
|
|
|
displayedStatus: Status;
|
2019-07-06 03:52:51 +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; } = {};
|
2020-03-14 01:20:51 +01:00
|
|
|
private bookmarkStatePerAccountId: { [id: string]: boolean; } = {};
|
2018-10-04 02:58:39 +02:00
|
|
|
|
2018-10-03 06:37:41 +02:00
|
|
|
private accounts$: Observable<AccountInfo[]>;
|
|
|
|
private accountSub: Subscription;
|
2020-02-19 05:51:34 +01:00
|
|
|
private statusStateSub: Subscription;
|
2018-10-03 06:37:41 +02:00
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
constructor(
|
2018-10-02 06:19:11 +02:00
|
|
|
private readonly store: Store,
|
2019-02-12 04:28:15 +01:00
|
|
|
private readonly toolsService: ToolsService,
|
2020-02-19 05:51:34 +01:00
|
|
|
private readonly statusStateService: StatusesStateService,
|
2019-10-02 06:14:40 +02:00
|
|
|
private readonly mastodonService: MastodonWrapperService,
|
2019-02-12 04:33:54 +01:00
|
|
|
private readonly notificationService: NotificationService) {
|
2018-10-03 06:37:41 +02:00
|
|
|
|
|
|
|
this.accounts$ = this.store.select(state => state.registeredaccounts.accounts);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-03-14 01:25:51 +01:00
|
|
|
this.displayedStatus = this.statusWrapper.status;
|
2018-10-04 02:58:39 +02:00
|
|
|
const account = this.statusWrapper.provider;
|
2020-04-07 03:54:31 +02:00
|
|
|
let accountId = 'remote';
|
|
|
|
if (account) {
|
|
|
|
accountId = account.id;
|
|
|
|
}
|
2019-04-07 21:51:32 +02:00
|
|
|
|
2020-03-14 02:02:37 +01:00
|
|
|
if (this.displayedStatus.reblog) {
|
2020-03-14 01:25:51 +01:00
|
|
|
this.displayedStatus = this.displayedStatus.reblog;
|
2019-07-03 23:53:53 +02:00
|
|
|
}
|
2018-10-04 02:58:39 +02:00
|
|
|
|
2020-04-07 03:54:31 +02:00
|
|
|
this.favoriteStatePerAccountId[accountId] = this.displayedStatus.favourited;
|
|
|
|
this.bootedStatePerAccountId[accountId] = this.displayedStatus.reblogged;
|
|
|
|
this.bookmarkStatePerAccountId[accountId] = this.displayedStatus.bookmarked;
|
2020-03-14 01:25:51 +01:00
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
this.analyseMemoryStatus();
|
|
|
|
|
2019-07-06 05:41:03 +02:00
|
|
|
if (this.displayedStatus.visibility === 'direct') {
|
2019-07-04 05:47:06 +02:00
|
|
|
this.isDM = true;
|
|
|
|
}
|
|
|
|
|
2018-10-03 06:37:41 +02:00
|
|
|
this.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => {
|
|
|
|
this.checkStatus(accounts);
|
|
|
|
});
|
2020-02-19 05:51:34 +01:00
|
|
|
|
|
|
|
this.statusStateSub = this.statusStateService.stateNotification.subscribe((state: StatusState) => {
|
|
|
|
if (state && state.statusId === this.displayedStatus.url) {
|
2020-03-14 02:02:37 +01:00
|
|
|
|
|
|
|
if (state.isFavorited) {
|
|
|
|
this.favoriteStatePerAccountId[state.accountId] = state.isFavorited;
|
|
|
|
}
|
|
|
|
if (state.isRebloged) {
|
|
|
|
this.bootedStatePerAccountId[state.accountId] = state.isRebloged;
|
|
|
|
}
|
|
|
|
if (state.isBookmarked) {
|
|
|
|
this.bookmarkStatePerAccountId[state.accountId] = state.isBookmarked;
|
|
|
|
}
|
2020-02-19 05:51:34 +01:00
|
|
|
|
|
|
|
this.checkIfFavorited();
|
|
|
|
this.checkIfBoosted();
|
2020-03-14 01:20:51 +01:00
|
|
|
this.checkIfBookmarked();
|
2020-02-19 05:51:34 +01:00
|
|
|
}
|
|
|
|
});
|
2018-10-03 06:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.accountSub.unsubscribe();
|
2020-02-19 05:51:34 +01:00
|
|
|
this.statusStateSub.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
private analyseMemoryStatus() {
|
|
|
|
let memoryStatusState = this.statusStateService.getStateForStatus(this.displayedStatus.url);
|
|
|
|
if (!memoryStatusState) return;
|
|
|
|
|
|
|
|
memoryStatusState.forEach((state: StatusState) => {
|
|
|
|
this.favoriteStatePerAccountId[state.accountId] = state.isFavorited;
|
|
|
|
this.bootedStatePerAccountId[state.accountId] = state.isRebloged;
|
2020-03-14 01:20:51 +01:00
|
|
|
this.bookmarkStatePerAccountId[state.accountId] = state.isBookmarked;
|
2020-02-19 05:51:34 +01:00
|
|
|
});
|
2018-10-03 06:37:41 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2020-04-07 03:54:31 +02:00
|
|
|
this.selectedAccounts = accounts.filter(x => x.isSelected);
|
|
|
|
|
|
|
|
if (!this.statusWrapper.isRemote) {
|
|
|
|
this.isProviderSelected = this.selectedAccounts.filter(x => x.id === provider.id).length > 0;
|
|
|
|
} else {
|
|
|
|
this.isProviderSelected = false;
|
|
|
|
}
|
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
|
|
|
|
2020-04-01 08:29:51 +02:00
|
|
|
this.isContentWarningActive = this.statusWrapper.applyCw;
|
2020-03-14 02:02:37 +01:00
|
|
|
|
2020-03-14 01:37:21 +01:00
|
|
|
this.checkIfBookmarksAreAvailable(this.selectedAccounts[0]);
|
2018-10-04 02:58:39 +02:00
|
|
|
this.checkIfFavorited();
|
|
|
|
this.checkIfBoosted();
|
2020-03-14 01:20:51 +01:00
|
|
|
this.checkIfBookmarked();
|
2018-10-02 05:46:12 +02:00
|
|
|
}
|
2020-03-14 02:02:37 +01:00
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
|
2019-02-24 22:54:01 +01:00
|
|
|
showContent(): boolean {
|
|
|
|
this.isContentWarningActive = false;
|
|
|
|
this.cwIsActiveEvent.next(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hideContent(): boolean {
|
|
|
|
this.isContentWarningActive = true;
|
|
|
|
this.cwIsActiveEvent.next(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-03 23:53:53 +02:00
|
|
|
if (this.boostIsLoading) return;
|
2019-04-07 20:18:10 +02:00
|
|
|
|
|
|
|
this.boostIsLoading = true;
|
|
|
|
const account = this.toolsService.getSelectedAccounts()[0];
|
|
|
|
const usableStatus = this.toolsService.getStatusUsableByAccount(account, this.statusWrapper);
|
|
|
|
usableStatus
|
|
|
|
.then((status: Status) => {
|
|
|
|
if (this.isBoosted && status.reblogged) {
|
|
|
|
return this.mastodonService.unreblog(account, status);
|
|
|
|
} else if (!this.isBoosted && !status.reblogged) {
|
|
|
|
return this.mastodonService.reblog(account, status);
|
|
|
|
} else {
|
|
|
|
return Promise.resolve(status);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then((boostedStatus: Status) => {
|
2019-07-03 23:53:53 +02:00
|
|
|
if (boostedStatus.pleroma) {
|
2019-04-13 23:04:24 +02:00
|
|
|
this.bootedStatePerAccountId[account.id] = boostedStatus.reblog !== null; //FIXME: when Pleroma will return the good status
|
|
|
|
} else {
|
2019-10-02 03:58:21 +02:00
|
|
|
let reblogged = boostedStatus.reblogged; //FIXME: when pixelfed will return the good status
|
2020-02-19 05:51:34 +01:00
|
|
|
if (reblogged === null) {
|
2019-10-02 03:58:21 +02:00
|
|
|
reblogged = !this.bootedStatePerAccountId[account.id];
|
|
|
|
}
|
|
|
|
this.bootedStatePerAccountId[account.id] = reblogged;
|
2019-07-03 23:53:53 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 20:18:10 +02:00
|
|
|
this.checkIfBoosted();
|
|
|
|
})
|
|
|
|
.catch((err: HttpErrorResponse) => {
|
2019-09-07 23:52:07 +02:00
|
|
|
this.notificationService.notifyHttpError(err, account);
|
2019-04-07 20:18:10 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
2020-02-19 05:51:34 +01:00
|
|
|
this.statusStateService.statusReblogStatusChanged(this.displayedStatus.url, account.id, this.bootedStatePerAccountId[account.id]);
|
2019-04-07 20:18:10 +02:00
|
|
|
this.boostIsLoading = false;
|
|
|
|
});
|
2018-10-04 02:58:39 +02:00
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
favorite(): boolean {
|
2019-07-03 23:53:53 +02:00
|
|
|
if (this.favoriteIsLoading) return;
|
2019-04-07 20:18:10 +02:00
|
|
|
|
|
|
|
this.favoriteIsLoading = true;
|
|
|
|
const account = this.toolsService.getSelectedAccounts()[0];
|
|
|
|
const usableStatus = this.toolsService.getStatusUsableByAccount(account, this.statusWrapper);
|
|
|
|
usableStatus
|
|
|
|
.then((status: Status) => {
|
|
|
|
if (this.isFavorited && status.favourited) {
|
|
|
|
return this.mastodonService.unfavorite(account, status);
|
|
|
|
} else if (!this.isFavorited && !status.favourited) {
|
|
|
|
return this.mastodonService.favorite(account, status);
|
|
|
|
} else {
|
|
|
|
return Promise.resolve(status);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then((favoritedStatus: Status) => {
|
2019-10-02 03:58:21 +02:00
|
|
|
let favourited = favoritedStatus.favourited; //FIXME: when pixelfed will return the good status
|
2020-02-19 05:51:34 +01:00
|
|
|
if (favourited === null) {
|
2019-10-02 03:58:21 +02:00
|
|
|
favourited = !this.favoriteStatePerAccountId[account.id];
|
|
|
|
}
|
|
|
|
this.favoriteStatePerAccountId[account.id] = favourited;
|
2019-04-07 20:18:10 +02:00
|
|
|
this.checkIfFavorited();
|
|
|
|
})
|
|
|
|
.catch((err: HttpErrorResponse) => {
|
2019-09-07 23:52:07 +02:00
|
|
|
this.notificationService.notifyHttpError(err, account);
|
2019-04-07 20:18:10 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
2020-02-19 05:51:34 +01:00
|
|
|
this.statusStateService.statusFavoriteStatusChanged(this.displayedStatus.url, account.id, this.favoriteStatePerAccountId[account.id]);
|
2019-04-07 20:18:10 +02:00
|
|
|
this.favoriteIsLoading = false;
|
|
|
|
});
|
|
|
|
|
2018-10-02 05:46:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:23:53 +01:00
|
|
|
bookmark(): boolean {
|
|
|
|
if (this.bookmarkingIsLoading) return;
|
|
|
|
|
|
|
|
this.bookmarkingIsLoading = true;
|
2020-03-14 01:20:51 +01:00
|
|
|
|
|
|
|
const account = this.toolsService.getSelectedAccounts()[0];
|
|
|
|
const usableStatus = this.toolsService.getStatusUsableByAccount(account, this.statusWrapper);
|
|
|
|
usableStatus
|
|
|
|
.then((status: Status) => {
|
|
|
|
if (this.isBookmarked && status.bookmarked) {
|
|
|
|
return this.mastodonService.unbookmark(account, status);
|
|
|
|
} else if (!this.isBookmarked && !status.bookmarked) {
|
|
|
|
return this.mastodonService.bookmark(account, status);
|
|
|
|
} else {
|
|
|
|
return Promise.resolve(status);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then((bookmarkedStatus: Status) => {
|
|
|
|
let bookmarked = bookmarkedStatus.bookmarked; //FIXME: when pixelfed will return the good status
|
|
|
|
if (bookmarked === null) {
|
|
|
|
bookmarked = !this.bookmarkStatePerAccountId[account.id];
|
|
|
|
}
|
|
|
|
this.bookmarkStatePerAccountId[account.id] = bookmarked;
|
|
|
|
this.checkIfBookmarked();
|
|
|
|
})
|
|
|
|
.catch((err: HttpErrorResponse) => {
|
|
|
|
this.notificationService.notifyHttpError(err, account);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
this.statusStateService.statusBookmarkStatusChanged(this.displayedStatus.url, account.id, this.bookmarkStatePerAccountId[account.id]);
|
|
|
|
this.bookmarkingIsLoading = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
// this.isBookmarked = !this.isBookmarked;
|
|
|
|
// this.bookmarkingIsLoading = false;
|
|
|
|
// }, 2000);
|
2020-03-13 01:23:53 +01: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;
|
|
|
|
}
|
2020-02-19 05:51:34 +01:00
|
|
|
}
|
2019-07-06 05:41:03 +02:00
|
|
|
|
2020-03-14 01:20:51 +01:00
|
|
|
private checkIfBookmarked() {
|
|
|
|
const selectedAccount = <AccountInfo>this.selectedAccounts[0];
|
|
|
|
|
|
|
|
if (selectedAccount) {
|
|
|
|
this.isBookmarked = this.bookmarkStatePerAccountId[selectedAccount.id];
|
|
|
|
} else {
|
|
|
|
this.isBookmarked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:37:21 +01:00
|
|
|
private checkIfBookmarksAreAvailable(account: AccountInfo) {
|
|
|
|
this.toolsService.getInstanceInfo(account)
|
|
|
|
.then((instance: InstanceInfo) => {
|
2020-03-14 02:02:37 +01:00
|
|
|
if (instance.major >= 3 && instance.minor >= 1) {
|
2020-03-14 01:37:21 +01:00
|
|
|
this.isBookmarksAvailable = true;
|
|
|
|
} else {
|
|
|
|
this.isBookmarksAvailable = false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.isBookmarksAvailable = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
browseThread(event: OpenThreadEvent) {
|
2019-08-10 01:08:18 +02:00
|
|
|
this.browseThreadEvent.next(event);
|
2019-07-06 05:41:03 +02:00
|
|
|
}
|
2018-10-02 05:46:12 +02:00
|
|
|
}
|