fix reblog and fav state to be global

This commit is contained in:
Nicolas Constant 2020-02-18 23:51:34 -05:00
parent 21bade1dc9
commit 71e6e1ed18
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
3 changed files with 53 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import { Status, Account, Results } from '../../../../services/models/mastodon.i
import { ToolsService, OpenThreadEvent } from '../../../../services/tools.service';
import { NotificationService } from '../../../../services/notification.service';
import { StatusWrapper } from '../../../../models/common.model';
import { StatusesStateService, StatusState } from '../../../../services/statuses-state.service';
@Component({
selector: 'app-action-bar',
@ -43,7 +44,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
favoriteIsLoading: boolean;
boostIsLoading: boolean;
isContentWarningActive: boolean = false;
isContentWarningActive: boolean = false;
displayedStatus: Status;
@ -55,10 +56,12 @@ export class ActionBarComponent implements OnInit, OnDestroy {
private accounts$: Observable<AccountInfo[]>;
private accountSub: Subscription;
private statusStateSub: Subscription;
constructor(
constructor(
private readonly store: Store,
private readonly toolsService: ToolsService,
private readonly statusStateService: StatusesStateService,
private readonly mastodonService: MastodonWrapperService,
private readonly notificationService: NotificationService) {
@ -79,6 +82,8 @@ export class ActionBarComponent implements OnInit, OnDestroy {
this.displayedStatus = status;
}
this.analyseMemoryStatus();
if (this.displayedStatus.visibility === 'direct') {
this.isDM = true;
}
@ -86,10 +91,31 @@ export class ActionBarComponent implements OnInit, OnDestroy {
this.accountSub = this.accounts$.subscribe((accounts: AccountInfo[]) => {
this.checkStatus(accounts);
});
this.statusStateSub = this.statusStateService.stateNotification.subscribe((state: StatusState) => {
if (state && state.statusId === this.displayedStatus.url) {
this.favoriteStatePerAccountId[state.accountId] = state.isFavorited;
this.bootedStatePerAccountId[state.accountId] = state.isRebloged;
this.checkIfFavorited();
this.checkIfBoosted();
}
});
}
ngOnDestroy(): void {
this.accountSub.unsubscribe();
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;
});
}
private checkStatus(accounts: AccountInfo[]): void {
@ -156,7 +182,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
this.bootedStatePerAccountId[account.id] = boostedStatus.reblog !== null; //FIXME: when Pleroma will return the good status
} else {
let reblogged = boostedStatus.reblogged; //FIXME: when pixelfed will return the good status
if(reblogged === null){
if (reblogged === null) {
reblogged = !this.bootedStatePerAccountId[account.id];
}
this.bootedStatePerAccountId[account.id] = reblogged;
@ -168,6 +194,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
this.notificationService.notifyHttpError(err, account);
})
.then(() => {
this.statusStateService.statusReblogStatusChanged(this.displayedStatus.url, account.id, this.bootedStatePerAccountId[account.id]);
this.boostIsLoading = false;
});
@ -192,7 +219,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
})
.then((favoritedStatus: Status) => {
let favourited = favoritedStatus.favourited; //FIXME: when pixelfed will return the good status
if(favourited === null){
if (favourited === null) {
favourited = !this.favoriteStatePerAccountId[account.id];
}
this.favoriteStatePerAccountId[account.id] = favourited;
@ -202,6 +229,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
this.notificationService.notifyHttpError(err, account);
})
.then(() => {
this.statusStateService.statusFavoriteStatusChanged(this.displayedStatus.url, account.id, this.favoriteStatePerAccountId[account.id]);
this.favoriteIsLoading = false;
});
@ -225,9 +253,9 @@ export class ActionBarComponent implements OnInit, OnDestroy {
} else {
this.isFavorited = false;
}
}
}
browseThread(event: OpenThreadEvent){
browseThread(event: OpenThreadEvent) {
this.browseThreadEvent.next(event);
}
}

View File

@ -17,7 +17,7 @@ describe('StatusesStateService', () => {
const service: StatusesStateService = TestBed.get(StatusesStateService);
service.statusFavoriteStatusChanged(statusId, accountId, true);
let result = service.getStateForStatus(statusId, accountId);
let result = service.getStateForStatus(statusId).find(x => x.accountId === accountId);
expect(result.isFavorited).toBeTruthy();
});
@ -28,7 +28,7 @@ describe('StatusesStateService', () => {
const service: StatusesStateService = TestBed.get(StatusesStateService);
service.statusReblogStatusChanged(statusId, accountId, true);
let result = service.getStateForStatus(statusId, accountId);
let result = service.getStateForStatus(statusId).find(x => x.accountId === accountId);
expect(result.isRebloged).toBeTruthy();
});
@ -40,7 +40,7 @@ describe('StatusesStateService', () => {
const service: StatusesStateService = TestBed.get(StatusesStateService);
service.statusFavoriteStatusChanged(statusId, accountId, true);
service.statusFavoriteStatusChanged(statusId, accountId, false);
let result = service.getStateForStatus(statusId, accountId);
let result = service.getStateForStatus(statusId).find(x => x.accountId === accountId);
expect(result.isFavorited).toBeFalsy();
});
@ -52,7 +52,7 @@ describe('StatusesStateService', () => {
const service: StatusesStateService = TestBed.get(StatusesStateService);
service.statusReblogStatusChanged(statusId, accountId, true);
service.statusReblogStatusChanged(statusId, accountId, false);
let result = service.getStateForStatus(statusId, accountId);
let result = service.getStateForStatus(statusId).find(x => x.accountId === accountId);
expect(result.isRebloged).toBeFalsy();
});

View File

@ -11,11 +11,17 @@ export class StatusesStateService {
constructor() { }
getStateForStatus(statusId: string, accountId: string): StatusState {
if(!this.cachedStatusStates[statusId] || !this.cachedStatusStates[statusId][accountId])
getStateForStatus(statusId: string): StatusState[] {
if(!this.cachedStatusStates[statusId])
return null;
return this.cachedStatusStates[statusId][accountId];
let results: StatusState[] = [];
Object.entries(this.cachedStatusStates[statusId]).forEach(
([key, value]) => {
results.push(value);
}
);
return results;
}
statusFavoriteStatusChanged(statusId: string, accountId: string, isFavorited: boolean) {
@ -45,8 +51,11 @@ export class StatusesStateService {
}
}
class StatusState {
constructor(public statusId: string, public accountId: string, public isFavorited: boolean, public isRebloged: boolean) {
export class StatusState {
constructor(
public statusId: string,
public accountId: string,
public isFavorited: boolean,
public isRebloged: boolean) {
}
}