2020-02-16 07:39:45 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
|
|
|
import { Subject } from 'rxjs';
|
2020-04-24 06:08:22 +02:00
|
|
|
import { StatusWrapper } from '../models/common.model';
|
2020-02-16 07:39:45 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2022-12-11 00:31:54 +01:00
|
|
|
export class StatusesStateService {
|
|
|
|
private cachedStatusText: { [statusId: string]: string } = {};
|
|
|
|
private cachedStatusStates: { [statusId: string]: { [accountId: string]: StatusState } } = {};
|
2020-02-16 07:39:45 +01:00
|
|
|
public stateNotification = new Subject<StatusState>();
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
getStateForStatus(statusId: string): StatusState[] {
|
2022-12-11 00:31:54 +01:00
|
|
|
if (!this.cachedStatusStates[statusId])
|
2020-02-16 07:39:45 +01:00
|
|
|
return null;
|
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
let results: StatusState[] = [];
|
|
|
|
Object.entries(this.cachedStatusStates[statusId]).forEach(
|
|
|
|
([key, value]) => {
|
|
|
|
results.push(value);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return results;
|
2020-02-16 07:39:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
statusFavoriteStatusChanged(statusId: string, accountId: string, isFavorited: boolean) {
|
|
|
|
if (!this.cachedStatusStates[statusId])
|
|
|
|
this.cachedStatusStates[statusId] = {};
|
|
|
|
|
|
|
|
if (!this.cachedStatusStates[statusId][accountId]) {
|
2022-12-11 00:55:20 +01:00
|
|
|
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, isFavorited, null, null, null, null);
|
2020-02-16 07:39:45 +01:00
|
|
|
} else {
|
|
|
|
this.cachedStatusStates[statusId][accountId].isFavorited = isFavorited;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
statusReblogStatusChanged(statusId: string, accountId: string, isRebloged: boolean) {
|
|
|
|
if (!this.cachedStatusStates[statusId])
|
|
|
|
this.cachedStatusStates[statusId] = {};
|
|
|
|
|
|
|
|
if (!this.cachedStatusStates[statusId][accountId]) {
|
2022-12-11 00:55:20 +01:00
|
|
|
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, null, isRebloged, null, null, null);
|
2020-02-16 07:39:45 +01:00
|
|
|
} else {
|
|
|
|
this.cachedStatusStates[statusId][accountId].isRebloged = isRebloged;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
|
|
|
|
}
|
2020-03-14 01:20:51 +01:00
|
|
|
|
|
|
|
statusBookmarkStatusChanged(statusId: string, accountId: string, isBookmarked: boolean) {
|
|
|
|
if (!this.cachedStatusStates[statusId])
|
|
|
|
this.cachedStatusStates[statusId] = {};
|
|
|
|
|
|
|
|
if (!this.cachedStatusStates[statusId][accountId]) {
|
2022-12-11 00:55:20 +01:00
|
|
|
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, null, null, isBookmarked, null, null);
|
2020-03-14 01:20:51 +01:00
|
|
|
} else {
|
|
|
|
this.cachedStatusStates[statusId][accountId].isBookmarked = isBookmarked;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
|
|
|
|
}
|
2020-04-24 06:08:22 +02:00
|
|
|
|
2022-12-11 00:31:54 +01:00
|
|
|
statusEditedStatusChanged(statusId: string, accountId: string, editedStatus: StatusWrapper) {
|
|
|
|
if (!this.cachedStatusStates[statusId])
|
|
|
|
this.cachedStatusStates[statusId] = {};
|
|
|
|
|
|
|
|
if (!this.cachedStatusStates[statusId][accountId]) {
|
2022-12-11 00:55:20 +01:00
|
|
|
this.cachedStatusStates[statusId][accountId] = new StatusState(statusId, accountId, null, null, null, true, editedStatus);
|
2022-12-11 00:31:54 +01:00
|
|
|
} else {
|
|
|
|
this.cachedStatusStates[statusId][accountId].isEdited = true;
|
|
|
|
this.cachedStatusStates[statusId][accountId].editedStatus = editedStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stateNotification.next(this.cachedStatusStates[statusId][accountId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
setStatusContent(data: string, replyingToStatus: StatusWrapper) {
|
|
|
|
if (replyingToStatus) {
|
2020-04-24 06:08:22 +02:00
|
|
|
this.cachedStatusText[replyingToStatus.status.uri] = data;
|
|
|
|
} else {
|
|
|
|
this.cachedStatusText['none'] = data;
|
2022-12-11 00:31:54 +01:00
|
|
|
}
|
2020-04-24 06:08:22 +02:00
|
|
|
}
|
|
|
|
|
2022-12-11 00:31:54 +01:00
|
|
|
getStatusContent(replyingToStatus: StatusWrapper): string {
|
2020-04-24 06:08:22 +02:00
|
|
|
let data: string;
|
2022-12-11 00:31:54 +01:00
|
|
|
if (replyingToStatus) {
|
2020-04-24 06:08:22 +02:00
|
|
|
data = this.cachedStatusText[replyingToStatus.status.uri];
|
|
|
|
} else {
|
|
|
|
data = this.cachedStatusText['none'];
|
|
|
|
}
|
|
|
|
|
2022-12-11 00:31:54 +01:00
|
|
|
if (!data) return '';
|
2020-04-24 06:08:22 +02:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2022-12-11 00:31:54 +01:00
|
|
|
resetStatusContent(replyingToStatus: StatusWrapper) {
|
|
|
|
if (replyingToStatus) {
|
2020-04-24 06:08:22 +02:00
|
|
|
this.cachedStatusText[replyingToStatus.status.uri] = '';
|
|
|
|
} else {
|
|
|
|
this.cachedStatusText['none'] = '';
|
2022-12-11 00:31:54 +01:00
|
|
|
}
|
2020-04-24 06:08:22 +02:00
|
|
|
}
|
2020-02-16 07:39:45 +01:00
|
|
|
}
|
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
export class StatusState {
|
2022-12-11 00:31:54 +01:00
|
|
|
|
2020-02-19 05:51:34 +01:00
|
|
|
constructor(
|
2022-12-11 00:31:54 +01:00
|
|
|
public statusId: string,
|
|
|
|
public accountId: string,
|
|
|
|
public isFavorited: boolean,
|
2020-03-14 01:20:51 +01:00
|
|
|
public isRebloged: boolean,
|
2022-12-11 00:31:54 +01:00
|
|
|
public isBookmarked: boolean,
|
2022-12-11 00:55:20 +01:00
|
|
|
public isEdited: boolean,
|
2022-12-11 00:31:54 +01:00
|
|
|
public editedStatus: StatusWrapper) {
|
2020-02-16 07:39:45 +01:00
|
|
|
}
|
|
|
|
}
|