From 5547b197275cbaa2f5ffeab17fa1a9d4ec2fd83f Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sun, 31 Mar 2019 18:53:11 -0400 Subject: [PATCH] wirering of notifications with settings state --- src/app/services/user-notification.service.ts | 93 +++++++++++++------ 1 file changed, 67 insertions(+), 26 deletions(-) diff --git a/src/app/services/user-notification.service.ts b/src/app/services/user-notification.service.ts index 34002a07..52e91620 100644 --- a/src/app/services/user-notification.service.ts +++ b/src/app/services/user-notification.service.ts @@ -6,6 +6,7 @@ import { Status, Notification } from './models/mastodon.interfaces'; import { MastodonService } from './mastodon.service'; import { AccountInfo } from '../states/accounts.state'; import { NotificationService } from './notification.service'; +import { ToolsService } from './tools.service'; @Injectable({ providedIn: 'root' @@ -16,6 +17,7 @@ export class UserNotificationService { private sinceIds: { [id: string]: string } = {}; constructor( + private readonly toolsService: ToolsService, private readonly notificationService: NotificationService, private readonly mastodonService: MastodonService, private readonly store: Store) { @@ -65,7 +67,7 @@ export class UserNotificationService { if (currentAccountNotifications) { currentAccountNotifications.allNotifications = [...notifications, ...currentAccountNotifications.allNotifications]; - currentAccountNotifications = this.analyseNotifications(currentAccountNotifications); + currentAccountNotifications = this.analyseNotifications(account, currentAccountNotifications); if (currentAccountNotifications.hasNewMentions || currentAccountNotifications.hasNewNotifications) { currentNotifications = currentNotifications.filter(x => x.account.id !== account.id); @@ -77,14 +79,14 @@ export class UserNotificationService { newNotifications.account = account; newNotifications.allNotifications = notifications; - newNotifications = this.analyseNotifications(newNotifications); + newNotifications = this.analyseNotifications(account, newNotifications); currentNotifications.push(newNotifications); this.userNotifications.next(currentNotifications); } } - private analyseNotifications(userNotification: UserNotification): UserNotification { + private analyseNotifications(account: AccountInfo, userNotification: UserNotification): UserNotification { if (userNotification.allNotifications.length > 30) { userNotification.allNotifications.length = 30; } @@ -96,34 +98,58 @@ export class UserNotificationService { const currentNotifications = userNotification.notifications; const currentMentions = userNotification.mentions; - if (!currentNotifications) { - userNotification.notifications = newNotifications; + userNotification.notifications = [...newNotifications, ...currentNotifications]; + userNotification.mentions = [...newMentions, ...currentMentions]; + + const accountSettings = this.toolsService.getAccountSettings(account); - } 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(accountSettings.lastMentionReadId && userNotification.mentions[0] && accountSettings.lastMentionReadId !== userNotification.mentions[0].id){ + userNotification.hasNewMentions = true; + } else { + userNotification.hasNewMentions = false; } - 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]; + if(accountSettings.lastNotificationReadId && userNotification.notifications[0] && accountSettings.lastNotificationReadId !== userNotification.notifications[0].id){ + userNotification.hasNewNotifications = true; + } else { + userNotification.hasNewNotifications = false; } + if((!accountSettings.lastMentionReadId && userNotification.mentions[0]) + || (!accountSettings.lastNotificationReadId && userNotification.notifications[0])){ + accountSettings.lastMentionReadId = userNotification.mentions[0].id; + accountSettings.lastNotificationReadId = userNotification.notifications[0].id; + this.toolsService.saveAccountSettings(accountSettings); + } + + // 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; } @@ -131,6 +157,14 @@ export class UserNotificationService { let currentNotifications = this.userNotifications.value; const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id); + const lastMention = currentAccountNotifications.mentions[0]; + if(lastMention){ + // 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); + } + if (currentAccountNotifications.hasNewMentions === true) { currentAccountNotifications.hasNewMentions = false; this.userNotifications.next(currentNotifications); @@ -141,6 +175,13 @@ export class UserNotificationService { let currentNotifications = this.userNotifications.value; const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id); + const lastNotification = currentAccountNotifications.notifications[0]; + if(lastNotification){ + const settings = this.toolsService.getAccountSettings(account); + settings.lastNotificationReadId = lastNotification.id; + this.toolsService.saveAccountSettings(settings); + } + if (currentAccountNotifications.hasNewNotifications === true) { currentAccountNotifications.hasNewNotifications = false; this.userNotifications.next(currentNotifications);