diff options
Diffstat (limited to 'internal/handlers/handlers.go')
| -rw-r--r-- | internal/handlers/handlers.go | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 147db07..caf7daa 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -325,9 +325,19 @@ func (h *Handler) fetchMeals(ctx context.Context, forceRefresh bool) ([]models.M return fetcher.FetchWithCache(ctx, forceRefresh) } -// fetchCalendarEvents fetches Google Calendar events from cache or API +// fetchCalendarEvents fetches calendar events from cache or source. +// +// Two sources feed the one cache: Google Calendar, and any subscribed .ics +// feeds. They are merged here rather than downstream so the timeline, the +// widget and everything else keep seeing a single list of calendar events. +// +// A Google failure is still returned as an error, unchanged — an outage there +// should surface rather than be masked by a partially-populated calendar. A +// single failing .ics feed, by contrast, is recorded against that feed and +// skipped, because one dead feed URL should not take the calendar down. func (h *Handler) fetchCalendarEvents(ctx context.Context, forceRefresh bool) ([]models.CalendarEvent, error) { - if h.googleCalendarClient == nil { + icsFeeds, _ := h.store.GetEnabledICSFeeds() + if h.googleCalendarClient == nil && len(icsFeeds) == 0 { return nil, nil } @@ -355,20 +365,30 @@ func (h *Handler) fetchCalendarEvents(ctx context.Context, forceRefresh bool) ([ enabledIDs = append(enabledIDs, trimmed) } } - } else { - // Configs exist but all disabled — respect that + } else if len(icsFeeds) == 0 { + // Configs exist but all disabled, and no .ics feeds either. return nil, nil } } - h.googleCalendarClient.SetCalendarIDs(enabledIDs) + if h.googleCalendarClient != nil { + h.googleCalendarClient.SetCalendarIDs(enabledIDs) + } fetcher := &CacheFetcher[models.CalendarEvent]{ Store: h.store, CacheKey: store.CacheKeyGoogleCalendar, TTLMinutes: h.config.CacheTTLMinutes, Fetch: func(ctx context.Context) ([]models.CalendarEvent, error) { - return h.googleCalendarClient.GetUpcomingEvents(ctx, 50) + var events []models.CalendarEvent + if h.googleCalendarClient != nil && len(enabledIDs) > 0 { + googleEvents, err := h.googleCalendarClient.GetUpcomingEvents(ctx, 50) + if err != nil { + return nil, err + } + events = append(events, googleEvents...) + } + return append(events, h.fetchICSFeedEvents(ctx)...), nil }, GetFromCache: h.store.GetCalendarEvents, SaveToCache: h.store.SaveCalendarEvents, |
