Sengi-Windows-MacOS-Linux/src/app/services/scheduled-status.service.ts

95 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-08-25 07:25:45 +02:00
import { Injectable } from '@angular/core';
import { Store } from '@ngxs/store';
import { BehaviorSubject } from 'rxjs';
2019-08-25 07:25:45 +02:00
import { MastodonWrapperService } from './mastodon-wrapper.service';
2019-08-25 07:25:45 +02:00
import { AccountInfo } from '../states/accounts.state';
import { ScheduledStatus } from './models/mastodon.interfaces';
import { NotificationService } from './notification.service';
2019-08-25 07:25:45 +02:00
@Injectable({
providedIn: 'root'
})
export class ScheduledStatusService {
scheduledStatuses = new BehaviorSubject<ScheduledStatusNotification[]>([]);
constructor(
private readonly notificationService: NotificationService,
private readonly mastodonService: MastodonWrapperService,
2019-08-25 07:25:45 +02:00
private readonly store: Store) {
this.fetchScheduledStatus();
}
private fetchScheduledStatus() {
let accounts = this.store.snapshot().registeredaccounts.accounts;
let promises: Promise<any>[] = [];
accounts.forEach((account: AccountInfo) => {
let promise = this.getStatusFromAccount(account);
2019-08-25 07:25:45 +02:00
promises.push(promise);
});
Promise.all(promises)
.then(() => {
setTimeout(() => {
this.fetchScheduledStatus();
2019-09-07 22:31:37 +02:00
}, 130 * 1000);
2019-08-25 07:25:45 +02:00
});
}
private getStatusFromAccount(account: AccountInfo): Promise<any> {
return this.mastodonService.getScheduledStatuses(account)
.then((statuses: ScheduledStatus[]) => {
if (statuses) {
this.processStatuses(account, statuses);
}
})
.catch(err => {
2019-10-02 03:49:40 +02:00
//this.notificationService.notifyHttpError(err, account);
});
}
2019-08-25 07:25:45 +02:00
private processStatuses(account: AccountInfo, statuses: ScheduledStatus[]) {
let previousStatuses: ScheduledStatus[] = [];
const notification = this.scheduledStatuses.value.find(x => x.account.id === account.id);
if (notification) {
previousStatuses = notification.statuses;
}
let uniques: string[] = [];
if (statuses && previousStatuses) {
uniques = [...new Set([...statuses, ...previousStatuses].map(x => x.id))];
}
2019-08-25 07:25:45 +02:00
if (uniques.length !== previousStatuses.length) {
2019-08-25 07:25:45 +02:00
const currentStatuses = new ScheduledStatusNotification(account, statuses);
const otherNotifications = this.scheduledStatuses.value.filter(x => x.account.id !== account.id);
const currentNotifications = [...otherNotifications, currentStatuses];
this.scheduledStatuses.next(currentNotifications);
}
}
statusAdded(account: AccountInfo) {
this.getStatusFromAccount(account);
}
removeStatus(account: AccountInfo, statusId: string) {
const notification = this.scheduledStatuses.value.find(x => x.account.id === account.id);
notification.statuses = notification.statuses.filter(x => x.id !== statusId);
const otherNotifications = this.scheduledStatuses.value.filter(x => x.account.id !== account.id);
const currentNotifications = [...otherNotifications, notification];
this.scheduledStatuses.next(currentNotifications);
}
2019-08-25 07:25:45 +02:00
}
export class ScheduledStatusNotification {
constructor(
public readonly account: AccountInfo,
public statuses: ScheduledStatus[]) {
}
}