From 91ee5237bd1df72b11054909fd9a75db44bf6b58 Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Mon, 28 Aug 2023 15:39:58 +0200
Subject: [PATCH] Fix calendar polling and first fetch behaviour on feed change

---
 .../frontend/components/calendar/Calendar.vue | 46 ++++++++++++++-----
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/aleksis/core/frontend/components/calendar/Calendar.vue b/aleksis/core/frontend/components/calendar/Calendar.vue
index 3d78fa530..3baa57dea 100644
--- a/aleksis/core/frontend/components/calendar/Calendar.vue
+++ b/aleksis/core/frontend/components/calendar/Calendar.vue
@@ -104,18 +104,20 @@ export default {
   },
   computed: {
     events() {
-      return this.calendar.calendarFeeds.flatMap((cf) =>
-        cf.events.map((event) => ({
-          ...event,
-          category: cf.verboseName,
-          calendarFeedName: cf.name,
-          start: new Date(event.start),
-          end: new Date(event.end),
-          color: event.color ? event.color : cf.color,
-          timed: !event.allDay,
-          meta: JSON.parse(event.meta),
-        }))
-      );
+      return this.calendar.calendarFeeds
+        .filter((c) => this.calendarFeeds.map((cf) => cf.name).includes(c.name))
+        .flatMap((cf) =>
+          cf.events.map((event) => ({
+            ...event,
+            category: cf.verboseName,
+            calendarFeedName: cf.name,
+            start: new Date(event.start),
+            end: new Date(event.end),
+            color: event.color ? event.color : cf.color,
+            timed: !event.allDay,
+            meta: JSON.parse(event.meta),
+          }))
+        );
     },
     paramsForSend() {
       if (this.params !== null) {
@@ -173,6 +175,26 @@ export default {
         this.$apollo.queries.calendar.refetch(this.queryVariables);
       }
     },
+    calendarFeeds(newFeeds, oldFeeds) {
+      this.updateMinTime();
+
+      if (
+        !newFeeds
+          .map((ncf) => ncf.name)
+          .every((n) => oldFeeds.map((ocf) => ocf.name).includes(n))
+      ) {
+        this.$apollo.queries.calendar.stopPolling();
+        clearInterval(this.fetchMoreInterval);
+
+        this.$apollo.queries.calendar.setVariables(this.queryVariables);
+        this.$apollo.queries.calendar.startPolling(30000);
+
+        // Reset fetched date ranges to newly fetched date range
+        this.fetchedDateRanges = [
+          { start: this.queryVariables.start, end: this.queryVariables.end },
+        ];
+      }
+    },
     range: {
       handler() {
         this.updateMinTime();
-- 
GitLab