Fetch maximumEntries of current events (and all past events if broadcastPastEvents is true)

This commit is contained in:
Johan Alvinger 2020-11-25 21:53:34 +01:00
parent 8a5e87b116
commit d00c25e107

View File

@ -376,17 +376,24 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
return a.startDate - b.startDate;
});
if (includePastEvents) {
// Include all events
events = newEvents;
} else {
// All events from startOfToday are fetched but we only want the ones that haven't ended yet
const now = moment();
var entries = -1;
var pastEntries = 0;
for (var ne of newEvents) {
if (moment(ne.endDate, "x").isAfter(now)) events.push(ne);
if (!includePastEvents && moment(ne.endDate, "x").isBefore(now)) {
// Events has ended and past events should not be included
pastEntries++;
continue;
}
entries++;
// If max events has been saved, skip the rest
if (entries > maximumEntries) break;
}
entries += pastEntries; // Total number of entries should include pastEntries
if (entries > 0) {
events = newEvents.slice(0, entries);
} else {
events = [];
}
events = events.slice(0, maximumEntries);
self.broadcastEvents();
scheduleTimer();