2019-03-24 22:52:34 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { BehaviorSubject, Subject, Observable, Subscription } from 'rxjs';
|
|
|
|
import { Store } from '@ngxs/store';
|
|
|
|
|
|
|
|
import { Status, Notification } from './models/mastodon.interfaces';
|
|
|
|
import { MastodonService } from './mastodon.service';
|
|
|
|
import { AccountInfo } from '../states/accounts.state';
|
|
|
|
import { NotificationService } from './notification.service';
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2019-03-25 06:09:53 +01:00
|
|
|
export class UserNotificationService {
|
2019-03-24 22:52:34 +01:00
|
|
|
userNotifications = new BehaviorSubject<UserNotification[]>([]);
|
|
|
|
|
2019-03-25 06:22:55 +01:00
|
|
|
private sinceIds: { [id: string]: string } = {};
|
|
|
|
|
2019-03-24 22:52:34 +01:00
|
|
|
constructor(
|
|
|
|
private readonly notificationService: NotificationService,
|
|
|
|
private readonly mastodonService: MastodonService,
|
|
|
|
private readonly store: Store) {
|
|
|
|
|
|
|
|
this.fetchNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
private fetchNotifications() {
|
|
|
|
let accounts = this.store.snapshot().registeredaccounts.accounts;
|
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
2019-03-25 06:22:55 +01:00
|
|
|
accounts.forEach((account: AccountInfo) => {
|
|
|
|
let sinceId = null;
|
2019-03-28 05:46:37 +01:00
|
|
|
if (this.sinceIds[account.id]) {
|
2019-03-25 06:22:55 +01:00
|
|
|
sinceId = this.sinceIds[account.id];
|
|
|
|
}
|
|
|
|
|
|
|
|
let getNotificationPromise = this.mastodonService.getNotifications(account, null, null, sinceId, 30)
|
2019-03-24 22:52:34 +01:00
|
|
|
.then((notifications: Notification[]) => {
|
|
|
|
this.processNotifications(account, notifications);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
this.notificationService.notifyHttpError(err);
|
|
|
|
});
|
|
|
|
promises.push(getNotificationPromise);
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.all(promises)
|
|
|
|
.then(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.fetchNotifications();
|
|
|
|
}, 15 * 1000);
|
2019-03-28 05:46:37 +01:00
|
|
|
});
|
2019-03-24 22:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private processNotifications(account: AccountInfo, notifications: Notification[]) {
|
2019-03-28 05:46:37 +01:00
|
|
|
if (notifications.length === 0) {
|
2019-03-25 06:22:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-24 22:52:34 +01:00
|
|
|
let currentNotifications = this.userNotifications.value;
|
2019-03-28 05:46:37 +01:00
|
|
|
let currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
|
2019-03-24 22:52:34 +01:00
|
|
|
|
2019-03-25 06:22:55 +01:00
|
|
|
const sinceId = notifications[0].id;
|
|
|
|
this.sinceIds[account.id] = sinceId;
|
2019-03-25 05:52:30 +01:00
|
|
|
|
2019-03-24 22:52:34 +01:00
|
|
|
if (currentAccountNotifications) {
|
2019-03-28 05:46:37 +01:00
|
|
|
currentAccountNotifications.allNotifications = [...notifications, ...currentAccountNotifications.allNotifications];
|
2019-03-24 22:52:34 +01:00
|
|
|
|
2019-03-28 05:46:37 +01:00
|
|
|
currentAccountNotifications = this.analyseNotifications(currentAccountNotifications);
|
2019-03-24 22:52:34 +01:00
|
|
|
|
2019-03-28 05:46:37 +01:00
|
|
|
if (currentAccountNotifications.hasNewMentions || currentAccountNotifications.hasNewNotifications) {
|
2019-03-24 22:52:34 +01:00
|
|
|
currentNotifications = currentNotifications.filter(x => x.account.id !== account.id);
|
|
|
|
currentNotifications.push(currentAccountNotifications);
|
|
|
|
this.userNotifications.next(currentNotifications);
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-28 05:46:37 +01:00
|
|
|
let newNotifications = new UserNotification();
|
2019-03-24 22:52:34 +01:00
|
|
|
newNotifications.account = account;
|
2019-03-28 05:46:37 +01:00
|
|
|
newNotifications.allNotifications = notifications;
|
|
|
|
|
|
|
|
newNotifications = this.analyseNotifications(newNotifications);
|
2019-03-24 22:52:34 +01:00
|
|
|
|
|
|
|
currentNotifications.push(newNotifications);
|
|
|
|
this.userNotifications.next(currentNotifications);
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 05:52:30 +01:00
|
|
|
|
2019-03-28 05:46:37 +01:00
|
|
|
private analyseNotifications(userNotification: UserNotification): UserNotification {
|
|
|
|
if(userNotification.allNotifications.length > 30){
|
|
|
|
userNotification.allNotifications.length = 30;
|
|
|
|
}
|
|
|
|
userNotification.lastId = userNotification.allNotifications[userNotification.allNotifications.length - 1].id;
|
|
|
|
|
|
|
|
const newNotifications = userNotification.allNotifications.filter(x => x.type !== 'mention');
|
|
|
|
const newMentions = userNotification.allNotifications.filter(x => x.type === 'mention').map(x => x.status);
|
|
|
|
|
|
|
|
const currentNotifications = userNotification.notifications;
|
|
|
|
const currentMentions = userNotification.mentions;
|
|
|
|
|
|
|
|
if (!currentNotifications) {
|
|
|
|
userNotification.notifications = newNotifications;
|
|
|
|
|
|
|
|
} else if (currentNotifications.length === 0) {
|
|
|
|
if (newNotifications.length > 0) {
|
|
|
|
userNotification.hasNewNotifications = true;
|
|
|
|
}
|
|
|
|
userNotification.notifications = newNotifications;
|
|
|
|
|
|
|
|
} else if (newNotifications.length > 0) {
|
|
|
|
userNotification.hasNewNotifications = currentNotifications[0].id !== newNotifications[0].id;
|
|
|
|
userNotification.notifications = [...newNotifications, ...currentNotifications];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentNotifications) {
|
|
|
|
userNotification.mentions = newMentions;
|
|
|
|
|
|
|
|
} else if (currentMentions.length === 0) {
|
|
|
|
if (newMentions.length > 0) {
|
|
|
|
userNotification.hasNewMentions = true;
|
|
|
|
}
|
|
|
|
userNotification.mentions = newMentions;
|
|
|
|
|
|
|
|
} else if (newMentions.length > 0) {
|
|
|
|
userNotification.hasNewMentions = currentMentions[0].id !== newMentions[0].id;
|
|
|
|
userNotification.mentions = [...newMentions, ...currentMentions];
|
|
|
|
}
|
|
|
|
|
|
|
|
return userNotification;
|
|
|
|
}
|
|
|
|
|
2019-03-25 05:52:30 +01:00
|
|
|
markMentionsAsRead(account: AccountInfo) {
|
|
|
|
let currentNotifications = this.userNotifications.value;
|
|
|
|
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
|
|
|
|
currentAccountNotifications.hasNewMentions = false;
|
|
|
|
this.userNotifications.next(currentNotifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
markNotificationAsRead(account: AccountInfo) {
|
|
|
|
let currentNotifications = this.userNotifications.value;
|
|
|
|
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
|
|
|
|
currentAccountNotifications.hasNewNotifications = false;
|
|
|
|
this.userNotifications.next(currentNotifications);
|
|
|
|
}
|
2019-03-24 22:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class UserNotification {
|
|
|
|
account: AccountInfo;
|
2019-03-28 05:46:37 +01:00
|
|
|
allNotifications: Notification[] = [];
|
|
|
|
|
2019-03-24 22:52:34 +01:00
|
|
|
hasNewNotifications: boolean;
|
|
|
|
hasNewMentions: boolean;
|
2019-03-28 05:46:37 +01:00
|
|
|
|
|
|
|
notifications: Notification[];
|
|
|
|
mentions: Status[];
|
2019-03-25 05:52:30 +01:00
|
|
|
lastId: string;
|
2019-03-24 22:52:34 +01:00
|
|
|
}
|