mirror of
https://github.com/NicolasConstant/sengi
synced 2025-01-24 07:22:50 +01:00
added optimization for notification retrieval
This commit is contained in:
parent
07318dbe15
commit
f4ed9c4482
@ -13,6 +13,8 @@ import { NotificationService } from './notification.service';
|
|||||||
export class UserNotificationService {
|
export class UserNotificationService {
|
||||||
userNotifications = new BehaviorSubject<UserNotification[]>([]);
|
userNotifications = new BehaviorSubject<UserNotification[]>([]);
|
||||||
|
|
||||||
|
private sinceIds: { [id: string]: string } = {};
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly notificationService: NotificationService,
|
private readonly notificationService: NotificationService,
|
||||||
private readonly mastodonService: MastodonService,
|
private readonly mastodonService: MastodonService,
|
||||||
@ -25,8 +27,13 @@ export class UserNotificationService {
|
|||||||
let accounts = this.store.snapshot().registeredaccounts.accounts;
|
let accounts = this.store.snapshot().registeredaccounts.accounts;
|
||||||
let promises: Promise<any>[] = [];
|
let promises: Promise<any>[] = [];
|
||||||
|
|
||||||
accounts.forEach(account => {
|
accounts.forEach((account: AccountInfo) => {
|
||||||
let getNotificationPromise = this.mastodonService.getNotifications(account, null, null, null, 30)
|
let sinceId = null;
|
||||||
|
if(this.sinceIds[account.id]){
|
||||||
|
sinceId = this.sinceIds[account.id];
|
||||||
|
}
|
||||||
|
|
||||||
|
let getNotificationPromise = this.mastodonService.getNotifications(account, null, null, sinceId, 30)
|
||||||
.then((notifications: Notification[]) => {
|
.then((notifications: Notification[]) => {
|
||||||
this.processNotifications(account, notifications);
|
this.processNotifications(account, notifications);
|
||||||
})
|
})
|
||||||
@ -45,6 +52,10 @@ export class UserNotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private processNotifications(account: AccountInfo, notifications: Notification[]) {
|
private processNotifications(account: AccountInfo, notifications: Notification[]) {
|
||||||
|
if(notifications.length === 0){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let currentNotifications = this.userNotifications.value;
|
let currentNotifications = this.userNotifications.value;
|
||||||
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
|
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
|
||||||
|
|
||||||
@ -52,6 +63,8 @@ export class UserNotificationService {
|
|||||||
const userMentions = notifications.filter(x => x.type === 'mention').map(x => x.status);
|
const userMentions = notifications.filter(x => x.type === 'mention').map(x => x.status);
|
||||||
|
|
||||||
const lastId = notifications[notifications.length - 1].id;
|
const lastId = notifications[notifications.length - 1].id;
|
||||||
|
const sinceId = notifications[0].id;
|
||||||
|
this.sinceIds[account.id] = sinceId;
|
||||||
|
|
||||||
if (currentAccountNotifications) {
|
if (currentAccountNotifications) {
|
||||||
const currentUserNotifications = currentAccountNotifications.notifications;
|
const currentUserNotifications = currentAccountNotifications.notifications;
|
||||||
|
Loading…
Reference in New Issue
Block a user