2019-08-25 07:25:45 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { Store } from '@ngxs/store';
|
2019-08-25 08:37:54 +02:00
|
|
|
import { BehaviorSubject } from 'rxjs';
|
2019-08-25 07:25:45 +02:00
|
|
|
|
|
|
|
import { MastodonService } from './mastodon.service';
|
|
|
|
import { AccountInfo } from '../states/accounts.state';
|
|
|
|
import { ScheduledStatus } from './models/mastodon.interfaces';
|
2019-08-25 08:37:54 +02:00
|
|
|
import { NotificationService } from './notification.service';
|
2019-08-25 07:25:45 +02:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ScheduledStatusService {
|
|
|
|
scheduledStatuses = new BehaviorSubject<ScheduledStatusNotification[]>([]);
|
|
|
|
|
|
|
|
constructor(
|
2019-08-25 08:37:54 +02:00
|
|
|
private readonly notificationService: NotificationService,
|
2019-08-25 07:25:45 +02:00
|
|
|
private readonly mastodonService: MastodonService,
|
|
|
|
private readonly store: Store) {
|
|
|
|
|
|
|
|
this.fetchScheduledStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
private fetchScheduledStatus() {
|
|
|
|
let accounts = this.store.snapshot().registeredaccounts.accounts;
|
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
|
|
|
accounts.forEach((account: AccountInfo) => {
|
2019-09-07 03:35:54 +02:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-07 03:35:54 +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-09-07 23:52:07 +02:00
|
|
|
this.notificationService.notifyHttpError(err, account);
|
2019-09-07 03:35:54 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-25 07:25:45 +02:00
|
|
|
private processStatuses(account: AccountInfo, statuses: ScheduledStatus[]) {
|
2019-08-25 08:37:54 +02:00
|
|
|
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
|
|
|
|
2019-08-25 08:37:54 +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);
|
|
|
|
}
|
|
|
|
}
|
2019-09-07 03:35:54 +02:00
|
|
|
|
|
|
|
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[]) {
|
|
|
|
}
|
|
|
|
}
|