From 62d0bc47b333135f31abea90b4f5a3c28e608733 Mon Sep 17 00:00:00 2001
From: Shpuld Shpuldson <shp@cock.li>
Date: Wed, 1 Jul 2020 14:15:04 +0300
Subject: [PATCH 1/4] remove unnecessary fetchAndUpdate, change notifications
 fetcher to not double fetch

---
 src/components/notifications/notifications.js              | 5 -----
 src/modules/api.js                                         | 3 ---
 .../backend_interactor_service.js                          | 4 ----
 .../notifications_fetcher/notifications_fetcher.service.js | 7 +++++--
 4 files changed, 5 insertions(+), 14 deletions(-)

diff --git a/src/components/notifications/notifications.js b/src/components/notifications/notifications.js
index 26ffbab6..e999a18e 100644
--- a/src/components/notifications/notifications.js
+++ b/src/components/notifications/notifications.js
@@ -56,11 +56,6 @@ const Notifications = {
   components: {
     Notification
   },
-  created () {
-    const { dispatch } = this.$store
-
-    dispatch('fetchAndUpdateNotifications')
-  },
   watch: {
     unseenCount (count) {
       if (count > 0) {
diff --git a/src/modules/api.js b/src/modules/api.js
index 748570e5..04ef6ab4 100644
--- a/src/modules/api.js
+++ b/src/modules/api.js
@@ -138,9 +138,6 @@ const api = {
       if (!fetcher) return
       store.commit('removeFetcher', { fetcherName: 'notifications', fetcher })
     },
-    fetchAndUpdateNotifications (store) {
-      store.state.backendInteractor.fetchAndUpdateNotifications({ store })
-    },
 
     // Follow requests
     startFetchingFollowRequests (store) {
diff --git a/src/services/backend_interactor_service/backend_interactor_service.js b/src/services/backend_interactor_service/backend_interactor_service.js
index e1c32860..45e6bd0e 100644
--- a/src/services/backend_interactor_service/backend_interactor_service.js
+++ b/src/services/backend_interactor_service/backend_interactor_service.js
@@ -12,10 +12,6 @@ const backendInteractorService = credentials => ({
     return notificationsFetcher.startFetching({ store, credentials })
   },
 
-  fetchAndUpdateNotifications ({ store }) {
-    return notificationsFetcher.fetchAndUpdate({ store, credentials })
-  },
-
   startFetchingFollowRequests ({ store }) {
     return followRequestFetcher.startFetching({ store, credentials })
   },
diff --git a/src/services/notifications_fetcher/notifications_fetcher.service.js b/src/services/notifications_fetcher/notifications_fetcher.service.js
index 64499a1b..581931f5 100644
--- a/src/services/notifications_fetcher/notifications_fetcher.service.js
+++ b/src/services/notifications_fetcher/notifications_fetcher.service.js
@@ -31,8 +31,11 @@ const fetchAndUpdate = ({ store, credentials, older = false }) => {
     const notifications = timelineData.data
     const readNotifsIds = notifications.filter(n => n.seen).map(n => n.id)
     if (readNotifsIds.length) {
-      args['since'] = Math.max(...readNotifsIds)
-      fetchNotifications({ store, args, older })
+      const possibleMax = Math.max(...readNotifsIds)
+      if (possibleMax !== timelineData.maxId) {
+        args['since'] = possibleMax
+        fetchNotifications({ store, args, older })
+      }
     }
 
     return result

From a3e370e9f816e3c98028bf82e8ac020b8e294466 Mon Sep 17 00:00:00 2001
From: Shpuld Shpuldson <shp@cock.li>
Date: Wed, 1 Jul 2020 15:19:45 +0300
Subject: [PATCH 2/4] add initial fetching back in a more streamlined way

---
 src/components/notifications/notifications.js | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/components/notifications/notifications.js b/src/components/notifications/notifications.js
index e999a18e..30187072 100644
--- a/src/components/notifications/notifications.js
+++ b/src/components/notifications/notifications.js
@@ -27,6 +27,11 @@ const Notifications = {
       seenToDisplayCount: DEFAULT_SEEN_TO_DISPLAY_COUNT
     }
   },
+  created () {
+    const store = this.$store
+    const credentials = store.state.users.currentUser.credentials
+    notificationsFetcher.fetchAndUpdate({ store: this.$store, credentials })
+  },
   computed: {
     mainClass () {
       return this.minimalMode ? '' : 'panel panel-default'

From 3ebd4e4429a9680046daeac2ed8753471365be9e Mon Sep 17 00:00:00 2001
From: Shpuld Shpuldson <shp@cock.li>
Date: Wed, 1 Jul 2020 17:55:42 +0300
Subject: [PATCH 3/4] document the 'mark-as-read-detection' system

---
 .../notifications_fetcher.service.js            | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/services/notifications_fetcher/notifications_fetcher.service.js b/src/services/notifications_fetcher/notifications_fetcher.service.js
index 581931f5..c2552480 100644
--- a/src/services/notifications_fetcher/notifications_fetcher.service.js
+++ b/src/services/notifications_fetcher/notifications_fetcher.service.js
@@ -27,17 +27,18 @@ const fetchAndUpdate = ({ store, credentials, older = false }) => {
     }
     const result = fetchNotifications({ store, args, older })
 
-    // load unread notifications repeatedly to provide consistency between browser tabs
+    // If there's any unread notifications, try fetch notifications since
+    // the newest read notification to check if any of the unread notifs
+    // have changed their 'seen' state (marked as read in another session), so
+    // we can update the state in this session to mark them as read as well.
+    // The normal maxId-check does not tell if older notifications have changed
     const notifications = timelineData.data
     const readNotifsIds = notifications.filter(n => n.seen).map(n => n.id)
-    if (readNotifsIds.length) {
-      const possibleMax = Math.max(...readNotifsIds)
-      if (possibleMax !== timelineData.maxId) {
-        args['since'] = possibleMax
-        fetchNotifications({ store, args, older })
-      }
+    const numUnseenNotifs = notifications.length - readNotifsIds.length
+    if (numUnseenNotifs > 0) {
+      args['since'] = Math.max(...readNotifsIds)
+      fetchNotifications({ store, args, older })
     }
-
     return result
   }
 }

From 9178908c1ea8d0ad99b4a631abb9594edc3765bf Mon Sep 17 00:00:00 2001
From: Shpuld Shpludson <shp@cock.li>
Date: Sun, 5 Jul 2020 06:54:12 +0000
Subject: [PATCH 4/4] Apply suggestion to
 src/components/notifications/notifications.js

---
 src/components/notifications/notifications.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/components/notifications/notifications.js b/src/components/notifications/notifications.js
index 30187072..d8a327b0 100644
--- a/src/components/notifications/notifications.js
+++ b/src/components/notifications/notifications.js
@@ -30,7 +30,7 @@ const Notifications = {
   created () {
     const store = this.$store
     const credentials = store.state.users.currentUser.credentials
-    notificationsFetcher.fetchAndUpdate({ store: this.$store, credentials })
+    notificationsFetcher.fetchAndUpdate({ store, credentials })
   },
   computed: {
     mainClass () {