about summary refs log tree commit diff
path: root/src/state/models/me.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/models/me.ts')
-rw-r--r--src/state/models/me.ts36
1 files changed, 24 insertions, 12 deletions
diff --git a/src/state/models/me.ts b/src/state/models/me.ts
index da46c60cf..0d0c1d1de 100644
--- a/src/state/models/me.ts
+++ b/src/state/models/me.ts
@@ -4,6 +4,7 @@ import {RootStoreModel} from './root-store'
 import {FeedModel} from './feed-view'
 import {NotificationsViewModel} from './notifications-view'
 import {isObj, hasProp} from '../lib/type-guards'
+import {displayNotificationFromModel} from '../../view/lib/notifee'
 
 export class MeModel {
   did: string = ''
@@ -125,19 +126,30 @@ export class MeModel {
       this.notificationCount = res.data.count
       notifee.setBadgeCount(this.notificationCount)
       if (newNotifications) {
-        // trigger pre-emptive fetch on new notifications
-        let oldMostRecent = this.notifications.mostRecentNotification
-        this.notifications.refresh().then(() => {
-          // if a new most recent notification is found, trigger a notification card
-          const mostRecent = this.notifications.mostRecentNotification
-          if (mostRecent && oldMostRecent?.uri !== mostRecent?.uri) {
-            const notifeeOpts = mostRecent.toNotifeeOpts()
-            if (notifeeOpts) {
-              notifee.displayNotification(notifeeOpts)
-            }
-          }
-        })
+        this.notifications.refresh()
       }
     })
   }
+
+  async bgFetchNotifications() {
+    const res = await this.rootStore.api.app.bsky.notification.getCount()
+    // NOTE we don't update this.notificationCount to avoid repaints during bg
+    //      this means `newNotifications` may not be accurate, so we rely on
+    //      `mostRecent` to determine if there really is a new notif to show -prf
+    const newNotifications = this.notificationCount !== res.data.count
+    notifee.setBadgeCount(res.data.count)
+    this.rootStore.log.debug(
+      `Background fetch received unread count = ${res.data.count}`,
+    )
+    if (newNotifications) {
+      this.rootStore.log.debug(
+        'Background fetch detected potentially a new notification',
+      )
+      const mostRecent = await this.notifications.getNewMostRecent()
+      if (mostRecent) {
+        this.rootStore.log.debug('Got the notification, triggering a push')
+        displayNotificationFromModel(mostRecent)
+      }
+    }
+  }
 }