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';
|
2019-04-01 00:53:11 +02:00
|
|
|
import { ToolsService } from './tools.service';
|
2019-03-24 22:52:34 +01:00
|
|
|
|
|
|
|
@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(
|
2019-04-01 00:53:11 +02:00
|
|
|
private readonly toolsService: ToolsService,
|
2019-03-24 22:52:34 +01:00
|
|
|
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-04-01 00:53:11 +02:00
|
|
|
currentAccountNotifications = this.analyseNotifications(account, 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;
|
|
|
|
|
2019-04-01 00:53:11 +02:00
|
|
|
newNotifications = this.analyseNotifications(account, 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-04-01 00:53:11 +02:00
|
|
|
private analyseNotifications(account: AccountInfo, userNotification: UserNotification): UserNotification {
|
2019-03-28 05:57:09 +01:00
|
|
|
if (userNotification.allNotifications.length > 30) {
|
2019-03-28 05:46:37 +01:00
|
|
|
userNotification.allNotifications.length = 30;
|
|
|
|
}
|
2019-03-28 05:57:09 +01:00
|
|
|
userNotification.lastId = userNotification.allNotifications[userNotification.allNotifications.length - 1].id;
|
2019-03-28 05:46:37 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2019-04-03 02:39:44 +02:00
|
|
|
const lastMention = userNotification.mentions[0];
|
|
|
|
let lastMentionNotification: Notification;
|
|
|
|
if (lastMention) {
|
|
|
|
lastMentionNotification = userNotification.allNotifications.find(x => x.type === 'mention' && x.status.id === lastMention.id);
|
|
|
|
}
|
|
|
|
const lastNotification = userNotification.notifications[0];
|
|
|
|
|
2019-04-01 00:53:11 +02:00
|
|
|
userNotification.notifications = [...newNotifications, ...currentNotifications];
|
|
|
|
userNotification.mentions = [...newMentions, ...currentMentions];
|
2019-04-03 02:39:44 +02:00
|
|
|
|
2019-04-01 00:53:11 +02:00
|
|
|
const accountSettings = this.toolsService.getAccountSettings(account);
|
2019-03-28 05:46:37 +01:00
|
|
|
|
2019-04-03 02:39:44 +02:00
|
|
|
if (accountSettings.lastMentionReadId && lastMention && accountSettings.lastMentionReadId !== lastMention.id && lastMentionNotification.created_at > accountSettings.lastMentionCreationDate) {
|
2019-04-01 00:53:11 +02:00
|
|
|
userNotification.hasNewMentions = true;
|
|
|
|
} else {
|
|
|
|
userNotification.hasNewMentions = false;
|
|
|
|
}
|
|
|
|
|
2019-04-03 02:39:44 +02:00
|
|
|
if (accountSettings.lastNotificationReadId && lastNotification && accountSettings.lastNotificationReadId !== lastNotification.id && lastNotification.created_at > accountSettings.lastNotificationCreationDate) {
|
2019-04-01 00:53:11 +02:00
|
|
|
userNotification.hasNewNotifications = true;
|
|
|
|
} else {
|
|
|
|
userNotification.hasNewNotifications = false;
|
|
|
|
}
|
2019-03-28 05:46:37 +01:00
|
|
|
|
2019-04-03 02:39:44 +02:00
|
|
|
if ((!accountSettings.lastMentionReadId && !accountSettings.lastNotificationCreationDate) && lastMention && lastMentionNotification) {
|
|
|
|
accountSettings.lastMentionReadId = lastMention.id;
|
|
|
|
accountSettings.lastMentionCreationDate = lastMentionNotification.created_at;
|
2019-04-01 00:53:11 +02:00
|
|
|
this.toolsService.saveAccountSettings(accountSettings);
|
2019-03-28 05:46:37 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 02:39:44 +02:00
|
|
|
if((!accountSettings.lastNotificationReadId || !accountSettings.lastNotificationCreationDate) && lastNotification){
|
|
|
|
accountSettings.lastNotificationReadId = lastNotification.id;
|
|
|
|
accountSettings.lastNotificationCreationDate = lastNotification.created_at;
|
|
|
|
this.toolsService.saveAccountSettings(accountSettings);
|
|
|
|
}
|
2019-03-28 05:46:37 +01:00
|
|
|
|
|
|
|
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);
|
2019-03-28 05:57:09 +01:00
|
|
|
|
2019-04-01 00:53:11 +02:00
|
|
|
const lastMention = currentAccountNotifications.mentions[0];
|
2019-04-03 02:39:44 +02:00
|
|
|
if (lastMention) {
|
2019-04-01 00:53:11 +02:00
|
|
|
// const lastNotification = currentAccountNotifications.allNotifications.find(x => x.status && x.status.id === lastMention.id);
|
|
|
|
const settings = this.toolsService.getAccountSettings(account);
|
|
|
|
settings.lastMentionReadId = lastMention.id;
|
|
|
|
this.toolsService.saveAccountSettings(settings);
|
|
|
|
}
|
|
|
|
|
2019-03-28 05:57:09 +01:00
|
|
|
if (currentAccountNotifications.hasNewMentions === true) {
|
|
|
|
currentAccountNotifications.hasNewMentions = false;
|
|
|
|
this.userNotifications.next(currentNotifications);
|
|
|
|
}
|
2019-03-25 05:52:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
markNotificationAsRead(account: AccountInfo) {
|
|
|
|
let currentNotifications = this.userNotifications.value;
|
|
|
|
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
|
2019-03-28 05:57:09 +01:00
|
|
|
|
2019-04-01 00:53:11 +02:00
|
|
|
const lastNotification = currentAccountNotifications.notifications[0];
|
2019-04-03 02:39:44 +02:00
|
|
|
if (lastNotification) {
|
2019-04-01 00:53:11 +02:00
|
|
|
const settings = this.toolsService.getAccountSettings(account);
|
|
|
|
settings.lastNotificationReadId = lastNotification.id;
|
|
|
|
this.toolsService.saveAccountSettings(settings);
|
|
|
|
}
|
|
|
|
|
2019-03-28 05:57:09 +01:00
|
|
|
if (currentAccountNotifications.hasNewNotifications === true) {
|
|
|
|
currentAccountNotifications.hasNewNotifications = false;
|
|
|
|
this.userNotifications.next(currentNotifications);
|
|
|
|
}
|
2019-03-25 05:52:30 +01:00
|
|
|
}
|
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
|
|
|
|
2019-04-01 02:32:13 +02:00
|
|
|
notifications: Notification[] = [];
|
|
|
|
mentions: Status[] = [];
|
2019-03-25 05:52:30 +01:00
|
|
|
lastId: string;
|
2019-03-24 22:52:34 +01:00
|
|
|
}
|