From 66df78074b0d8a498508a96601beb127cb1dd4c0 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Tue, 2 Apr 2019 20:39:44 -0400 Subject: [PATCH] added new properties to detect new notifications --- .../left-side-bar/left-side-bar.component.ts | 4 -- .../services/models/mastodon.interfaces.ts | 2 +- src/app/services/notification.service.ts | 1 + src/app/services/user-notification.service.ts | 56 +++++++------------ src/app/states/settings.state.ts | 2 + 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/app/components/left-side-bar/left-side-bar.component.ts b/src/app/components/left-side-bar/left-side-bar.component.ts index 0b474703..03a16fb7 100644 --- a/src/app/components/left-side-bar/left-side-bar.component.ts +++ b/src/app/components/left-side-bar/left-side-bar.component.ts @@ -73,16 +73,12 @@ export class LeftSideBarComponent implements OnInit, OnDestroy { }); this.notificationSub = this.userNotificationServiceService.userNotifications.subscribe((notifications: UserNotification[]) => { - notifications.forEach((notification: UserNotification) => { const acc = this.accounts.find(x => x.info.id === notification.account.id); if(acc){ acc.hasActivityNotifications = notification.hasNewMentions || notification.hasNewNotifications; } }); - - console.warn('new notifications'); - console.warn(notifications); }); } diff --git a/src/app/services/models/mastodon.interfaces.ts b/src/app/services/models/mastodon.interfaces.ts index bcbf001e..d16e608c 100644 --- a/src/app/services/models/mastodon.interfaces.ts +++ b/src/app/services/models/mastodon.interfaces.ts @@ -115,7 +115,7 @@ export interface Mention { export interface Notification { id: string; type: 'mention' | 'reblog' | 'favourite' | 'follow'; - created_at: string; + created_at: Date; account: Account; status?: Status; } diff --git a/src/app/services/notification.service.ts b/src/app/services/notification.service.ts index 6881bbd4..842b24ec 100644 --- a/src/app/services/notification.service.ts +++ b/src/app/services/notification.service.ts @@ -15,6 +15,7 @@ export class NotificationService { } public notifyHttpError(err: HttpErrorResponse){ + console.error(err); console.error(err.message); // let message = `${err.status}: ${err.statusText}`; let message = `${err.statusText}`; diff --git a/src/app/services/user-notification.service.ts b/src/app/services/user-notification.service.ts index c1b6a5fd..114e9e27 100644 --- a/src/app/services/user-notification.service.ts +++ b/src/app/services/user-notification.service.ts @@ -98,57 +98,41 @@ export class UserNotificationService { const currentNotifications = userNotification.notifications; const currentMentions = userNotification.mentions; + 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]; + userNotification.notifications = [...newNotifications, ...currentNotifications]; userNotification.mentions = [...newMentions, ...currentMentions]; - + const accountSettings = this.toolsService.getAccountSettings(account); - if(accountSettings.lastMentionReadId && userNotification.mentions[0] && accountSettings.lastMentionReadId !== userNotification.mentions[0].id){ + if (accountSettings.lastMentionReadId && lastMention && accountSettings.lastMentionReadId !== lastMention.id && lastMentionNotification.created_at > accountSettings.lastMentionCreationDate) { userNotification.hasNewMentions = true; } else { userNotification.hasNewMentions = false; } - if(accountSettings.lastNotificationReadId && userNotification.notifications[0] && accountSettings.lastNotificationReadId !== userNotification.notifications[0].id){ + if (accountSettings.lastNotificationReadId && lastNotification && accountSettings.lastNotificationReadId !== lastNotification.id && lastNotification.created_at > accountSettings.lastNotificationCreationDate) { 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; + if ((!accountSettings.lastMentionReadId && !accountSettings.lastNotificationCreationDate) && lastMention && lastMentionNotification) { + accountSettings.lastMentionReadId = lastMention.id; + accountSettings.lastMentionCreationDate = lastMentionNotification.created_at; 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]; - // } + if((!accountSettings.lastNotificationReadId || !accountSettings.lastNotificationCreationDate) && lastNotification){ + accountSettings.lastNotificationReadId = lastNotification.id; + accountSettings.lastNotificationCreationDate = lastNotification.created_at; + this.toolsService.saveAccountSettings(accountSettings); + } return userNotification; } @@ -158,7 +142,7 @@ export class UserNotificationService { const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id); const lastMention = currentAccountNotifications.mentions[0]; - if(lastMention){ + 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; @@ -176,7 +160,7 @@ export class UserNotificationService { const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id); const lastNotification = currentAccountNotifications.notifications[0]; - if(lastNotification){ + if (lastNotification) { const settings = this.toolsService.getAccountSettings(account); settings.lastNotificationReadId = lastNotification.id; this.toolsService.saveAccountSettings(settings); diff --git a/src/app/states/settings.state.ts b/src/app/states/settings.state.ts index e86b3eb3..4c64150d 100644 --- a/src/app/states/settings.state.ts +++ b/src/app/states/settings.state.ts @@ -20,7 +20,9 @@ export class AccountSettings { displayMention: boolean = true; displayNotifications: boolean = true; lastMentionReadId: string; + lastMentionCreationDate: Date; lastNotificationReadId: string; + lastNotificationCreationDate: Date; } export class GlobalSettings {