Files

100 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2020-05-25 23:03:19 +02: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 NewsfeedFetcher = require("./newsfeedfetcher");
2016-03-30 14:49:37 +02:00
module.exports = NodeHelper.create({
2020-05-25 23:03:19 +02:00
// Override start method.
2024-01-01 15:38:08 +01:00
start () {
2023-04-04 20:44:32 +02:00
Log.log(`Starting node helper for: ${this.name}`);
2016-03-30 14:49:37 +02:00
this.fetchers = [];
},
2020-05-25 23:03:19 +02:00
// Override socketNotificationReceived received.
2024-01-01 15:38:08 +01:00
socketNotificationReceived (notification, payload) {
2016-04-05 14:35:11 -04:00
if (notification === "ADD_FEED") {
this.createFetcher(payload.feed, payload.config);
2026-04-01 12:35:16 +02:00
} else if (notification === "CHECK_ARTICLE_URL") {
this.checkArticleUrl(payload.url);
}
},
/**
* Checks whether a URL can be displayed in an iframe by inspecting
* X-Frame-Options and Content-Security-Policy headers server-side.
* @param {string} url The article URL to check.
*/
async checkArticleUrl (url) {
try {
const response = await fetch(url, { method: "HEAD" });
const xfo = response.headers.get("x-frame-options");
const csp = response.headers.get("content-security-policy");
// sameorigin also blocks since the article is on a different origin than MM
const blockedByXFO = xfo && ["deny", "sameorigin"].includes(xfo.toLowerCase().trim());
const blockedByCSP = csp && (/frame-ancestors\s+['"]?none['"]?/).test(csp);
this.sendSocketNotification("ARTICLE_URL_STATUS", { url, canFrame: !blockedByXFO && !blockedByCSP });
} catch {
// Network error or HEAD not supported — let the browser try the iframe anyway
this.sendSocketNotification("ARTICLE_URL_STATUS", { url, canFrame: true });
2016-03-30 14:49:37 +02:00
}
},
2020-08-03 11:36:29 +02:00
/**
* Creates a fetcher for a new feed if it doesn't exist yet.
* Otherwise it reuses the existing one.
2021-05-15 14:49:04 +02:00
* @param {object} feed The feed object
* @param {object} config The configuration object
*/
2024-01-01 15:38:08 +01:00
createFetcher (feed, config) {
2020-07-12 12:45:32 +02:00
const url = feed.url || "";
const encoding = feed.encoding || "UTF-8";
const reloadInterval = feed.reloadInterval || config.reloadInterval || 5 * 60 * 1000;
2026-04-01 12:35:16 +02:00
const useCorsProxy = feed.useCorsProxy ?? true;
try {
new URL(url);
} catch (error) {
2026-01-01 08:45:36 -06:00
Log.error("Error: Malformed newsfeed url: ", url, error);
this.sendSocketNotification("NEWSFEED_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
return;
}
2016-03-29 15:44:43 +02:00
2020-07-12 12:45:32 +02:00
let fetcher;
if (typeof this.fetchers[url] === "undefined") {
2023-04-04 20:44:32 +02:00
Log.log(`Create new newsfetcher for url: ${url} - Interval: ${reloadInterval}`);
2023-01-01 18:09:08 +01:00
fetcher = new NewsfeedFetcher(url, reloadInterval, encoding, config.logFeedWarnings, useCorsProxy);
2016-04-03 19:52:13 +02:00
2020-07-12 12:45:32 +02:00
fetcher.onReceive(() => {
this.broadcastFeeds();
2016-03-30 14:49:37 +02:00
});
2026-04-01 12:35:16 +02:00
fetcher.onError((fetcher, errorInfo) => {
Log.error("Error: Could not fetch newsfeed: ", fetcher.url, errorInfo.message || errorInfo);
this.sendSocketNotification("NEWSFEED_ERROR", {
2026-04-01 12:35:16 +02:00
error_type: errorInfo.translationKey
2016-03-30 14:49:37 +02:00
});
});
2020-07-12 12:45:32 +02:00
this.fetchers[url] = fetcher;
2016-03-30 14:49:37 +02:00
} else {
2023-04-04 20:44:32 +02:00
Log.log(`Use existing newsfetcher for url: ${url}`);
2020-07-12 12:45:32 +02:00
fetcher = this.fetchers[url];
2016-03-30 14:49:37 +02:00
fetcher.setReloadInterval(reloadInterval);
fetcher.broadcastItems();
}
2016-03-29 15:44:43 +02:00
2016-03-30 14:49:37 +02:00
fetcher.startFetch();
},
2020-08-03 11:36:29 +02:00
/**
* Creates an object with all feed items of the different registered feeds,
* and broadcasts these using sendSocketNotification.
*/
2024-01-01 15:38:08 +01:00
broadcastFeeds () {
2021-03-16 21:15:19 +01:00
const feeds = {};
2026-04-01 12:35:16 +02:00
for (const url in this.fetchers) {
feeds[url] = this.fetchers[url].items;
}
this.sendSocketNotification("NEWS_ITEMS", feeds);
2016-03-30 14:49:37 +02:00
}
});