2025-12-18 13:57:24 -06:00
|
|
|
const zlib = require("node:zlib");
|
2020-06-20 11:18:37 -07:00
|
|
|
const NodeHelper = require("node_helper");
|
2021-02-18 19:14:53 +01:00
|
|
|
const Log = require("logger");
|
2023-04-04 20:44:32 +02:00
|
|
|
const CalendarFetcher = require("./calendarfetcher");
|
2016-03-31 11:05:32 +02:00
|
|
|
|
|
|
|
|
module.exports = NodeHelper.create({
|
|
|
|
|
// Override start method.
|
2024-01-01 15:38:08 +01:00
|
|
|
start () {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`Starting node helper for: ${this.name}`);
|
2016-03-31 11:05:32 +02:00
|
|
|
this.fetchers = [];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Override socketNotificationReceived method.
|
2024-01-01 15:38:08 +01:00
|
|
|
socketNotificationReceived (notification, payload) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (notification === "ADD_CALENDAR") {
|
2021-02-21 11:32:03 +01:00
|
|
|
this.createFetcher(payload.url, payload.fetchInterval, payload.excludedEvents, payload.maximumEntries, payload.maximumNumberOfDays, payload.auth, payload.broadcastPastEvents, payload.selfSignedCert, payload.id);
|
2022-07-12 14:35:04 +02:00
|
|
|
} else if (notification === "FETCH_CALENDAR") {
|
2022-08-06 10:47:37 +02:00
|
|
|
const key = payload.id + payload.url;
|
2022-07-12 14:35:04 +02:00
|
|
|
if (typeof this.fetchers[key] === "undefined") {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.error("No fetcher exists with key: ", key);
|
2022-07-12 14:35:04 +02:00
|
|
|
this.sendSocketNotification("CALENDAR_ERROR", { error_type: "MODULE_ERROR_UNSPECIFIED" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-11-14 20:14:23 +01:00
|
|
|
this.fetchers[key].fetchCalendar();
|
2016-03-31 11:05:32 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2020-08-03 11:19:54 +02:00
|
|
|
/**
|
2016-06-04 20:32:55 -06:00
|
|
|
* Creates a fetcher for a new url if it doesn't exist yet.
|
|
|
|
|
* Otherwise it reuses the existing one.
|
2020-08-03 11:19:54 +02:00
|
|
|
* @param {string} url The url of the calendar
|
|
|
|
|
* @param {number} fetchInterval How often does the calendar needs to be fetched in ms
|
|
|
|
|
* @param {string[]} excludedEvents An array of words / phrases from event titles that will be excluded from being shown.
|
|
|
|
|
* @param {number} maximumEntries The maximum number of events fetched.
|
|
|
|
|
* @param {number} maximumNumberOfDays The maximum number of days an event should be in the future.
|
|
|
|
|
* @param {object} auth The object containing options for authentication against the calendar.
|
|
|
|
|
* @param {boolean} broadcastPastEvents If true events from the past maximumNumberOfDays will be included in event broadcasts
|
2021-02-21 11:32:03 +01:00
|
|
|
* @param {boolean} selfSignedCert If true, the server certificate is not verified against the list of supplied CAs.
|
2020-08-03 11:19:54 +02:00
|
|
|
* @param {string} identifier ID of the module
|
2016-03-31 11:05:32 +02:00
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
createFetcher (url, fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert, identifier) {
|
2021-03-16 19:25:23 +01:00
|
|
|
try {
|
|
|
|
|
new URL(url);
|
|
|
|
|
} catch (error) {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.error("Malformed calendar url: ", url, error);
|
2021-05-02 10:28:11 +02:00
|
|
|
this.sendSocketNotification("CALENDAR_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
|
2016-03-31 11:05:32 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 20:15:04 +01:00
|
|
|
let fetcher;
|
2024-03-21 13:43:04 +01:00
|
|
|
let fetchIntervalCorrected;
|
2020-12-23 20:15:04 +01:00
|
|
|
if (typeof this.fetchers[identifier + url] === "undefined") {
|
2024-03-21 13:43:04 +01:00
|
|
|
if (fetchInterval < 60000) {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.warn(`fetchInterval for url ${url} must be >= 60000`);
|
2024-03-21 13:43:04 +01:00
|
|
|
fetchIntervalCorrected = 60000;
|
|
|
|
|
}
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`Create new calendarfetcher for url: ${url} - Interval: ${fetchIntervalCorrected || fetchInterval}`);
|
2024-03-21 13:43:04 +01:00
|
|
|
fetcher = new CalendarFetcher(url, fetchIntervalCorrected || fetchInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, broadcastPastEvents, selfSignedCert);
|
2020-06-20 11:01:37 -07:00
|
|
|
|
2020-12-23 20:15:04 +01:00
|
|
|
fetcher.onReceive((fetcher) => {
|
|
|
|
|
this.broadcastEvents(fetcher, identifier);
|
2016-03-31 11:05:32 +02:00
|
|
|
});
|
|
|
|
|
|
2026-01-22 19:24:37 +01:00
|
|
|
fetcher.onError((fetcher, errorInfo) => {
|
|
|
|
|
Log.error("Calendar Error. Could not fetch calendar: ", fetcher.url, errorInfo.message || errorInfo);
|
2021-05-02 10:28:11 +02:00
|
|
|
this.sendSocketNotification("CALENDAR_ERROR", {
|
2021-05-02 14:43:12 +02:00
|
|
|
id: identifier,
|
2026-01-22 19:24:37 +01:00
|
|
|
error_type: errorInfo.translationKey
|
2016-03-31 11:05:32 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-12-23 20:15:04 +01:00
|
|
|
this.fetchers[identifier + url] = fetcher;
|
2025-12-08 10:07:04 +01:00
|
|
|
fetcher.fetchCalendar();
|
2016-03-31 11:05:32 +02:00
|
|
|
} else {
|
2025-10-22 22:50:31 +02:00
|
|
|
Log.log(`Use existing calendarfetcher for url: ${url}`);
|
2020-12-23 20:15:04 +01:00
|
|
|
fetcher = this.fetchers[identifier + url];
|
2025-12-08 10:07:04 +01:00
|
|
|
// Check if calendar data is stale and needs refresh
|
|
|
|
|
if (fetcher.shouldRefetch()) {
|
|
|
|
|
Log.log(`Calendar data is stale, fetching fresh data for url: ${url}`);
|
|
|
|
|
fetcher.fetchCalendar();
|
|
|
|
|
} else {
|
|
|
|
|
fetcher.broadcastEvents();
|
|
|
|
|
}
|
2016-03-31 11:05:32 +02:00
|
|
|
}
|
2020-12-23 20:15:04 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2021-01-02 21:08:53 +01:00
|
|
|
* @param {object} fetcher the fetcher associated with the calendar
|
|
|
|
|
* @param {string} identifier the identifier of the calendar
|
2020-12-23 20:15:04 +01:00
|
|
|
*/
|
2024-01-01 15:38:08 +01:00
|
|
|
broadcastEvents (fetcher, identifier) {
|
2025-12-18 13:57:24 -06:00
|
|
|
const checksum = zlib.crc32(Buffer.from(JSON.stringify(fetcher.events), "utf8"));
|
2020-12-23 20:15:04 +01:00
|
|
|
this.sendSocketNotification("CALENDAR_EVENTS", {
|
|
|
|
|
id: identifier,
|
2025-11-14 20:14:23 +01:00
|
|
|
url: fetcher.url,
|
2025-12-18 13:57:24 -06:00
|
|
|
events: fetcher.events,
|
|
|
|
|
checksum: checksum
|
2020-12-23 20:15:04 +01:00
|
|
|
});
|
2016-03-31 11:05:32 +02:00
|
|
|
}
|
|
|
|
|
});
|