2016-03-29 15:44:43 +02:00
|
|
|
/* Magic Mirror
|
|
|
|
* Module: NewsFeed
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2016-03-29 15:44:43 +02:00
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
Module.register("newsfeed", {
|
2016-03-29 15:44:43 +02:00
|
|
|
// Default module config.
|
|
|
|
defaults: {
|
2016-04-20 14:39:38 +02:00
|
|
|
feeds: [
|
|
|
|
{
|
|
|
|
title: "New York Times",
|
2020-04-28 23:05:28 +02:00
|
|
|
url: "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml",
|
2016-04-20 14:39:38 +02:00
|
|
|
encoding: "UTF-8" //ISO-8859-1
|
|
|
|
}
|
|
|
|
],
|
|
|
|
showSourceTitle: true,
|
2016-03-29 15:44:43 +02:00
|
|
|
showPublishDate: true,
|
2019-05-31 16:23:40 +02:00
|
|
|
broadcastNewsFeeds: true,
|
|
|
|
broadcastNewsUpdates: true,
|
2016-04-12 11:06:59 +02:00
|
|
|
showDescription: false,
|
2017-03-11 17:36:47 -06:00
|
|
|
wrapTitle: true,
|
|
|
|
wrapDescription: true,
|
2017-11-15 12:21:02 +07:00
|
|
|
truncDescription: true,
|
|
|
|
lengthDescription: 400,
|
2017-03-10 16:56:28 +01:00
|
|
|
hideLoading: false,
|
2017-11-15 12:21:02 +07:00
|
|
|
reloadInterval: 5 * 60 * 1000, // every 5 minutes
|
2016-04-12 11:06:59 +02:00
|
|
|
updateInterval: 10 * 1000,
|
2016-04-05 14:35:11 -04:00
|
|
|
animationSpeed: 2.5 * 1000,
|
2016-08-26 22:06:03 +02:00
|
|
|
maxNewsItems: 0, // 0 for unlimited
|
2017-03-21 19:39:51 +01:00
|
|
|
ignoreOldItems: false,
|
|
|
|
ignoreOlderThan: 24 * 60 * 60 * 1000, // 1 day
|
2016-12-01 19:48:53 -03:00
|
|
|
removeStartTags: "",
|
|
|
|
removeEndTags: "",
|
2016-08-27 00:32:47 +02:00
|
|
|
startTags: [],
|
Added ability to set a list of prohibited words that will be filtered out of newsfeed
Resolves #1071
`prohibitedWords` config parameter is an array of words. If set and case insensitive greedy match is found anywhere in the title then that newsfeed item will not be displayed. Readme updated with instructions.
Users should be careful on the words selection as careless setting may remove many or all items from the newsfeed. Some obvious mistakes like `space, comma, dot` etc. can be prevented programatically, but I left it out of this PR
Example:
with `prohibitedWords: ['dodgers']`
Original `newsItems`:
```
0:{title: "New York City, Russia, Los Angeles Dodgers: Your Wednesday Briefing", description: "Here’s what you need to know to start your day.", pubdate: "Wed, 01 Nov 2017 09:37:36 GMT", url: "https://www.nytimes.com/2017/11/01/briefing/new-yo…ssia-los-angeles-dodgers.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
1:{title: "A Mangled School Bus, Bodies Everywhere; ‘It Was Surreal’", description: "A truck ramming bicyclists. The driver emerging wi…e attack, it was as confusing as it was gruesome.", pubdate: "Wed, 01 Nov 2017 09:27:41 GMT", url: "https://www.nytimes.com/2017/10/31/nyregion/nyc-sc…r-attack-truck-witnesses.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
2:{title: "Dodgers 3, Astros 1 | Series tied, 3-3: With a Rally and a Romp, Dodgers Top Astros and Force Game 7", description: "Down by a run with just four innings left in Game …nst a dominating Justin Verlander. Game 7 awaits.", pubdate: "Wed, 01 Nov 2017 07:21:07 GMT", url: "https://www.nytimes.com/2017/11/01/sports/dodgers-win-game-6.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
3:{title: "José Andrés Fed Puerto Rico, and May Change How Aid Is Given", description: "The chef’s huge effort is just the latest led by c…ocally based way to feed people after a disaster.", pubdate: "Wed, 01 Nov 2017 06:40:09 GMT", url: "https://www.nytimes.com/2017/10/30/dining/jose-andres-puerto-rico.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
```
Filtered `newsItems`:
```
0:{title: "A Mangled School Bus, Bodies Everywhere; ‘It Was Surreal’", description: "A truck ramming bicyclists. The driver emerging wi…e attack, it was as confusing as it was gruesome.", pubdate: "Wed, 01 Nov 2017 09:27:41 GMT", url: "https://www.nytimes.com/2017/10/31/nyregion/nyc-sc…r-attack-truck-witnesses.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
1:{title: "José Andrés Fed Puerto Rico, and May Change How Aid Is Given", description: "The chef’s huge effort is just the latest led by c…ocally based way to feed people after a disaster.", pubdate: "Wed, 01 Nov 2017 06:40:09 GMT", url: "https://www.nytimes.com/2017/10/30/dining/jose
```
2017-11-01 03:40:32 -07:00
|
|
|
endTags: [],
|
2018-01-29 21:26:34 +01:00
|
|
|
prohibitedWords: [],
|
2018-06-26 20:01:28 -04:00
|
|
|
scrollLength: 500,
|
|
|
|
logFeedWarnings: false
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Define required scripts.
|
2020-05-11 22:22:32 +02:00
|
|
|
getScripts: function () {
|
2016-04-05 14:35:11 -04:00
|
|
|
return ["moment.js"];
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
2021-01-16 11:52:55 +01:00
|
|
|
//Define required styles.
|
|
|
|
getStyles: function () {
|
|
|
|
return ["newsfeed.css"];
|
|
|
|
},
|
|
|
|
|
2016-05-11 12:38:41 +02:00
|
|
|
// Define required translations.
|
2020-05-11 22:22:32 +02:00
|
|
|
getTranslations: function () {
|
2017-03-30 21:15:51 +02:00
|
|
|
// The translations for the default modules are defined in the core translation files.
|
|
|
|
// Therefor we can just return false. Otherwise we should have returned a dictionary.
|
|
|
|
// If you're trying to build your own module including translations, check out the documentation.
|
2016-05-11 12:38:41 +02:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2016-03-29 15:44:43 +02:00
|
|
|
// Define start sequence.
|
2020-05-11 22:22:32 +02:00
|
|
|
start: function () {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.info("Starting module: " + this.name);
|
2016-03-29 15:44:43 +02:00
|
|
|
|
|
|
|
// Set locale.
|
|
|
|
moment.locale(config.language);
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-03-29 15:44:43 +02:00
|
|
|
this.newsItems = [];
|
|
|
|
this.loaded = false;
|
2021-03-16 13:47:48 +01:00
|
|
|
this.error = null;
|
2016-03-29 15:44:43 +02:00
|
|
|
this.activeItem = 0;
|
2018-01-29 21:26:34 +01:00
|
|
|
this.scrollPosition = 0;
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
this.registerFeeds();
|
2016-03-31 13:05:23 +02:00
|
|
|
|
2018-05-15 20:32:02 -04:00
|
|
|
this.isShowingDescription = this.config.showDescription;
|
2016-03-30 12:20:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Override socket notification handler.
|
2020-05-11 22:22:32 +02:00
|
|
|
socketNotificationReceived: function (notification, payload) {
|
2016-04-05 14:35:11 -04:00
|
|
|
if (notification === "NEWS_ITEMS") {
|
2016-04-20 14:39:38 +02:00
|
|
|
this.generateFeed(payload);
|
2016-03-30 12:20:46 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
if (!this.loaded) {
|
2021-01-16 12:26:38 +01:00
|
|
|
if (this.config.hideLoading) {
|
|
|
|
this.show();
|
|
|
|
}
|
2016-04-20 14:39:38 +02:00
|
|
|
this.scheduleUpdateInterval();
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
2016-04-20 14:39:38 +02:00
|
|
|
|
2016-05-11 12:38:41 +02:00
|
|
|
this.loaded = true;
|
2021-03-16 13:47:48 +01:00
|
|
|
this.error = null;
|
2021-03-16 19:25:23 +01:00
|
|
|
} else if (notification === "INCORRECT_URL") {
|
2021-03-16 19:16:07 +01:00
|
|
|
this.error = `Incorrect url: ${payload.url}`;
|
2021-03-16 13:47:48 +01:00
|
|
|
this.scheduleUpdateInterval();
|
2016-03-30 12:20:46 +02:00
|
|
|
}
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
2021-01-16 13:37:18 +01:00
|
|
|
//Override fetching of template name
|
|
|
|
getTemplate: function () {
|
2016-04-20 14:39:38 +02:00
|
|
|
if (this.config.feedUrl) {
|
2021-01-16 13:37:18 +01:00
|
|
|
return "oldconfig.njk";
|
|
|
|
} else if (this.config.showFullArticle) {
|
|
|
|
return "fullarticle.njk";
|
2016-04-20 14:39:38 +02:00
|
|
|
}
|
2021-01-16 13:37:18 +01:00
|
|
|
return "newsfeed.njk";
|
|
|
|
},
|
2016-04-05 13:16:23 +02:00
|
|
|
|
2021-01-16 13:37:18 +01:00
|
|
|
//Override template data and return whats used for the current template
|
|
|
|
getTemplateData: function () {
|
|
|
|
// this.config.showFullArticle is a run-time configuration, triggered by optional notifications
|
|
|
|
if (this.config.showFullArticle) {
|
|
|
|
return {
|
|
|
|
url: this.getActiveItemURL()
|
|
|
|
};
|
2016-03-29 15:44:43 +02:00
|
|
|
}
|
2021-03-16 13:47:48 +01:00
|
|
|
if (this.error) {
|
|
|
|
return {
|
|
|
|
error: this.error
|
|
|
|
};
|
|
|
|
}
|
2021-01-16 14:06:07 +01:00
|
|
|
if (this.newsItems.length === 0) {
|
2021-01-16 13:37:18 +01:00
|
|
|
return {
|
|
|
|
loaded: false
|
|
|
|
};
|
2016-03-29 15:44:43 +02:00
|
|
|
}
|
2021-01-16 13:37:18 +01:00
|
|
|
if (this.activeItem >= this.newsItems.length) {
|
|
|
|
this.activeItem = 0;
|
|
|
|
}
|
2021-03-16 13:47:48 +01:00
|
|
|
|
2021-01-16 13:37:18 +01:00
|
|
|
const item = this.newsItems[this.activeItem];
|
|
|
|
|
|
|
|
return {
|
|
|
|
loaded: true,
|
|
|
|
config: this.config,
|
|
|
|
sourceTitle: item.sourceTitle,
|
|
|
|
publishDate: moment(new Date(item.pubdate)).fromNow(),
|
|
|
|
title: item.title,
|
|
|
|
description: item.description
|
|
|
|
};
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
getActiveItemURL: function () {
|
2020-03-15 15:49:34 +01:00
|
|
|
return typeof this.newsItems[this.activeItem].url === "string" ? this.newsItems[this.activeItem].url : this.newsItems[this.activeItem].url.href;
|
2019-03-25 01:08:59 +01:00
|
|
|
},
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
|
|
|
* Registers the feeds to be used by the backend.
|
2016-03-29 15:44:43 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
registerFeeds: function () {
|
2021-03-16 21:15:19 +01:00
|
|
|
for (let feed of this.config.feeds) {
|
2016-04-20 14:39:38 +02:00
|
|
|
this.sendSocketNotification("ADD_FEED", {
|
|
|
|
feed: feed,
|
|
|
|
config: this.config
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-04-20 14:39:38 +02:00
|
|
|
* Generate an ordered list of items for this configured module.
|
|
|
|
*
|
2020-08-03 11:36:29 +02:00
|
|
|
* @param {object} feeds An object with feeds returned by the node helper.
|
2016-04-20 14:39:38 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
generateFeed: function (feeds) {
|
2021-03-16 21:15:19 +01:00
|
|
|
let newsItems = [];
|
|
|
|
for (let feed in feeds) {
|
|
|
|
const feedItems = feeds[feed];
|
2016-04-20 14:39:38 +02:00
|
|
|
if (this.subscribedToFeed(feed)) {
|
2021-03-16 21:15:19 +01:00
|
|
|
for (let item of feedItems) {
|
2016-04-20 14:39:38 +02:00
|
|
|
item.sourceTitle = this.titleForFeed(feed);
|
2020-05-11 22:22:32 +02:00
|
|
|
if (!(this.config.ignoreOldItems && Date.now() - new Date(item.pubdate) > this.config.ignoreOlderThan)) {
|
2017-03-21 19:39:51 +01:00
|
|
|
newsItems.push(item);
|
|
|
|
}
|
2016-04-20 14:39:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 22:22:32 +02:00
|
|
|
newsItems.sort(function (a, b) {
|
2021-03-16 21:15:19 +01:00
|
|
|
const dateA = new Date(a.pubdate);
|
|
|
|
const dateB = new Date(b.pubdate);
|
2016-04-20 14:39:38 +02:00
|
|
|
return dateB - dateA;
|
2016-03-30 12:20:46 +02:00
|
|
|
});
|
2020-05-11 22:22:32 +02:00
|
|
|
if (this.config.maxNewsItems > 0) {
|
2016-06-06 00:16:49 +02:00
|
|
|
newsItems = newsItems.slice(0, this.config.maxNewsItems);
|
|
|
|
}
|
Added ability to set a list of prohibited words that will be filtered out of newsfeed
Resolves #1071
`prohibitedWords` config parameter is an array of words. If set and case insensitive greedy match is found anywhere in the title then that newsfeed item will not be displayed. Readme updated with instructions.
Users should be careful on the words selection as careless setting may remove many or all items from the newsfeed. Some obvious mistakes like `space, comma, dot` etc. can be prevented programatically, but I left it out of this PR
Example:
with `prohibitedWords: ['dodgers']`
Original `newsItems`:
```
0:{title: "New York City, Russia, Los Angeles Dodgers: Your Wednesday Briefing", description: "Here’s what you need to know to start your day.", pubdate: "Wed, 01 Nov 2017 09:37:36 GMT", url: "https://www.nytimes.com/2017/11/01/briefing/new-yo…ssia-los-angeles-dodgers.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
1:{title: "A Mangled School Bus, Bodies Everywhere; ‘It Was Surreal’", description: "A truck ramming bicyclists. The driver emerging wi…e attack, it was as confusing as it was gruesome.", pubdate: "Wed, 01 Nov 2017 09:27:41 GMT", url: "https://www.nytimes.com/2017/10/31/nyregion/nyc-sc…r-attack-truck-witnesses.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
2:{title: "Dodgers 3, Astros 1 | Series tied, 3-3: With a Rally and a Romp, Dodgers Top Astros and Force Game 7", description: "Down by a run with just four innings left in Game …nst a dominating Justin Verlander. Game 7 awaits.", pubdate: "Wed, 01 Nov 2017 07:21:07 GMT", url: "https://www.nytimes.com/2017/11/01/sports/dodgers-win-game-6.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
3:{title: "José Andrés Fed Puerto Rico, and May Change How Aid Is Given", description: "The chef’s huge effort is just the latest led by c…ocally based way to feed people after a disaster.", pubdate: "Wed, 01 Nov 2017 06:40:09 GMT", url: "https://www.nytimes.com/2017/10/30/dining/jose-andres-puerto-rico.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
```
Filtered `newsItems`:
```
0:{title: "A Mangled School Bus, Bodies Everywhere; ‘It Was Surreal’", description: "A truck ramming bicyclists. The driver emerging wi…e attack, it was as confusing as it was gruesome.", pubdate: "Wed, 01 Nov 2017 09:27:41 GMT", url: "https://www.nytimes.com/2017/10/31/nyregion/nyc-sc…r-attack-truck-witnesses.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
1:{title: "José Andrés Fed Puerto Rico, and May Change How Aid Is Given", description: "The chef’s huge effort is just the latest led by c…ocally based way to feed people after a disaster.", pubdate: "Wed, 01 Nov 2017 06:40:09 GMT", url: "https://www.nytimes.com/2017/10/30/dining/jose
```
2017-11-01 03:40:32 -07:00
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
if (this.config.prohibitedWords.length > 0) {
|
|
|
|
newsItems = newsItems.filter(function (value) {
|
2021-03-16 21:15:19 +01:00
|
|
|
for (let word of this.config.prohibitedWords) {
|
|
|
|
if (value["title"].toLowerCase().indexOf(word.toLowerCase()) > -1) {
|
Added ability to set a list of prohibited words that will be filtered out of newsfeed
Resolves #1071
`prohibitedWords` config parameter is an array of words. If set and case insensitive greedy match is found anywhere in the title then that newsfeed item will not be displayed. Readme updated with instructions.
Users should be careful on the words selection as careless setting may remove many or all items from the newsfeed. Some obvious mistakes like `space, comma, dot` etc. can be prevented programatically, but I left it out of this PR
Example:
with `prohibitedWords: ['dodgers']`
Original `newsItems`:
```
0:{title: "New York City, Russia, Los Angeles Dodgers: Your Wednesday Briefing", description: "Here’s what you need to know to start your day.", pubdate: "Wed, 01 Nov 2017 09:37:36 GMT", url: "https://www.nytimes.com/2017/11/01/briefing/new-yo…ssia-los-angeles-dodgers.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
1:{title: "A Mangled School Bus, Bodies Everywhere; ‘It Was Surreal’", description: "A truck ramming bicyclists. The driver emerging wi…e attack, it was as confusing as it was gruesome.", pubdate: "Wed, 01 Nov 2017 09:27:41 GMT", url: "https://www.nytimes.com/2017/10/31/nyregion/nyc-sc…r-attack-truck-witnesses.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
2:{title: "Dodgers 3, Astros 1 | Series tied, 3-3: With a Rally and a Romp, Dodgers Top Astros and Force Game 7", description: "Down by a run with just four innings left in Game …nst a dominating Justin Verlander. Game 7 awaits.", pubdate: "Wed, 01 Nov 2017 07:21:07 GMT", url: "https://www.nytimes.com/2017/11/01/sports/dodgers-win-game-6.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
3:{title: "José Andrés Fed Puerto Rico, and May Change How Aid Is Given", description: "The chef’s huge effort is just the latest led by c…ocally based way to feed people after a disaster.", pubdate: "Wed, 01 Nov 2017 06:40:09 GMT", url: "https://www.nytimes.com/2017/10/30/dining/jose-andres-puerto-rico.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
```
Filtered `newsItems`:
```
0:{title: "A Mangled School Bus, Bodies Everywhere; ‘It Was Surreal’", description: "A truck ramming bicyclists. The driver emerging wi…e attack, it was as confusing as it was gruesome.", pubdate: "Wed, 01 Nov 2017 09:27:41 GMT", url: "https://www.nytimes.com/2017/10/31/nyregion/nyc-sc…r-attack-truck-witnesses.html?partner=rss&emc=rss", sourceTitle: "New York Times"}
1:{title: "José Andrés Fed Puerto Rico, and May Change How Aid Is Given", description: "The chef’s huge effort is just the latest led by c…ocally based way to feed people after a disaster.", pubdate: "Wed, 01 Nov 2017 06:40:09 GMT", url: "https://www.nytimes.com/2017/10/30/dining/jose
```
2017-11-01 03:40:32 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
2021-01-16 14:06:07 +01:00
|
|
|
newsItems.forEach((item) => {
|
2021-01-16 11:10:53 +01:00
|
|
|
//Remove selected tags from the beginning of rss feed items (title or description)
|
|
|
|
if (this.config.removeStartTags === "title" || this.config.removeStartTags === "both") {
|
|
|
|
for (let f = 0; f < this.config.startTags.length; f++) {
|
|
|
|
if (item.title.slice(0, this.config.startTags[f].length) === this.config.startTags[f]) {
|
|
|
|
item.title = item.title.slice(this.config.startTags[f].length, item.title.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.removeStartTags === "description" || this.config.removeStartTags === "both") {
|
|
|
|
if (this.isShowingDescription) {
|
|
|
|
for (let f = 0; f < this.config.startTags.length; f++) {
|
|
|
|
if (item.description.slice(0, this.config.startTags[f].length) === this.config.startTags[f]) {
|
|
|
|
item.description = item.description.slice(this.config.startTags[f].length, item.description.length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove selected tags from the end of rss feed items (title or description)
|
|
|
|
|
|
|
|
if (this.config.removeEndTags) {
|
|
|
|
for (let f = 0; f < this.config.endTags.length; f++) {
|
|
|
|
if (item.title.slice(-this.config.endTags[f].length) === this.config.endTags[f]) {
|
|
|
|
item.title = item.title.slice(0, -this.config.endTags[f].length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isShowingDescription) {
|
|
|
|
for (let f = 0; f < this.config.endTags.length; f++) {
|
|
|
|
if (item.description.slice(-this.config.endTags[f].length) === this.config.endTags[f]) {
|
|
|
|
item.description = item.description.slice(0, -this.config.endTags[f].length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 14:06:07 +01:00
|
|
|
});
|
|
|
|
|
2019-05-31 16:23:40 +02:00
|
|
|
// get updated news items and broadcast them
|
2021-03-16 21:15:19 +01:00
|
|
|
const updatedItems = [];
|
2020-05-11 22:22:32 +02:00
|
|
|
newsItems.forEach((value) => {
|
|
|
|
if (this.newsItems.findIndex((value1) => value1 === value) === -1) {
|
2019-05-31 16:23:40 +02:00
|
|
|
// Add item to updated items list
|
2019-06-14 14:03:07 +02:00
|
|
|
updatedItems.push(value);
|
2019-05-31 16:23:40 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// check if updated items exist, if so and if we should broadcast these updates, then lets do so
|
|
|
|
if (this.config.broadcastNewsUpdates && updatedItems.length > 0) {
|
2020-05-11 22:22:32 +02:00
|
|
|
this.sendNotification("NEWS_FEED_UPDATE", { items: updatedItems });
|
2019-05-31 16:23:40 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
this.newsItems = newsItems;
|
|
|
|
},
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-04-20 14:39:38 +02:00
|
|
|
* Check if this module is configured to show this feed.
|
|
|
|
*
|
2020-08-03 11:36:29 +02:00
|
|
|
* @param {string} feedUrl Url of the feed to check.
|
|
|
|
* @returns {boolean} True if it is subscribed, false otherwise
|
2016-04-20 14:39:38 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
subscribedToFeed: function (feedUrl) {
|
2021-03-16 21:15:19 +01:00
|
|
|
for (let feed of this.config.feeds) {
|
2016-04-20 14:39:38 +02:00
|
|
|
if (feed.url === feedUrl) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
|
|
|
* Returns title for the specific feed url.
|
2016-04-20 14:39:38 +02:00
|
|
|
*
|
2020-08-03 11:36:29 +02:00
|
|
|
* @param {string} feedUrl Url of the feed
|
|
|
|
* @returns {string} The title of the feed
|
2016-04-20 14:39:38 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
titleForFeed: function (feedUrl) {
|
2021-03-16 21:15:19 +01:00
|
|
|
for (let feed of this.config.feeds) {
|
2016-04-20 14:39:38 +02:00
|
|
|
if (feed.url === feedUrl) {
|
2016-12-01 19:48:53 -03:00
|
|
|
return feed.title || "";
|
2016-04-20 14:39:38 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-01 19:48:53 -03:00
|
|
|
return "";
|
2016-03-29 15:44:43 +02:00
|
|
|
},
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-03-29 15:44:43 +02:00
|
|
|
* Schedule visual update.
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
scheduleUpdateInterval: function () {
|
2016-03-29 15:44:43 +02:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
self.updateDom(self.config.animationSpeed);
|
|
|
|
|
2019-07-09 10:20:02 +02:00
|
|
|
// Broadcast NewsFeed if needed
|
|
|
|
if (self.config.broadcastNewsFeeds) {
|
2020-05-11 22:22:32 +02:00
|
|
|
self.sendNotification("NEWS_FEED", { items: self.newsItems });
|
2019-07-09 10:20:02 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.timer = setInterval(function () {
|
2016-03-29 15:44:43 +02:00
|
|
|
self.activeItem++;
|
|
|
|
self.updateDom(self.config.animationSpeed);
|
2019-05-31 16:23:40 +02:00
|
|
|
|
|
|
|
// Broadcast NewsFeed if needed
|
|
|
|
if (self.config.broadcastNewsFeeds) {
|
2020-05-11 22:22:32 +02:00
|
|
|
self.sendNotification("NEWS_FEED", { items: self.newsItems });
|
2019-05-31 16:23:40 +02:00
|
|
|
}
|
2016-03-29 15:44:43 +02:00
|
|
|
}, this.config.updateInterval);
|
|
|
|
},
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
resetDescrOrFullArticleAndTimer: function () {
|
2018-05-15 20:32:02 -04:00
|
|
|
this.isShowingDescription = this.config.showDescription;
|
2017-01-08 14:38:16 +01:00
|
|
|
this.config.showFullArticle = false;
|
2018-01-29 21:26:34 +01:00
|
|
|
this.scrollPosition = 0;
|
|
|
|
// reset bottom bar alignment
|
2021-01-16 11:52:55 +01:00
|
|
|
document.getElementsByClassName("region bottom bar")[0].classList.remove("newsfeed-fullarticle");
|
2020-05-11 22:22:32 +02:00
|
|
|
if (!this.timer) {
|
2017-01-08 14:38:16 +01:00
|
|
|
this.scheduleUpdateInterval();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
notificationReceived: function (notification, payload, sender) {
|
2020-07-12 12:45:32 +02:00
|
|
|
const before = this.activeItem;
|
2021-01-16 12:26:38 +01:00
|
|
|
if (notification === "MODULE_DOM_CREATED" && this.config.hideLoading) {
|
|
|
|
this.hide();
|
|
|
|
} else if (notification === "ARTICLE_NEXT") {
|
2017-01-06 21:57:30 +01:00
|
|
|
this.activeItem++;
|
|
|
|
if (this.activeItem >= this.newsItems.length) {
|
|
|
|
this.activeItem = 0;
|
2017-01-04 10:46:31 +01:00
|
|
|
}
|
2017-01-08 14:38:16 +01:00
|
|
|
this.resetDescrOrFullArticleAndTimer();
|
2020-11-06 11:47:29 +01:00
|
|
|
Log.debug(this.name + " - going from article #" + before + " to #" + this.activeItem + " (of " + this.newsItems.length + ")");
|
2017-01-06 21:57:30 +01:00
|
|
|
this.updateDom(100);
|
2020-05-11 22:22:32 +02:00
|
|
|
} else if (notification === "ARTICLE_PREVIOUS") {
|
2017-01-06 21:57:30 +01:00
|
|
|
this.activeItem--;
|
|
|
|
if (this.activeItem < 0) {
|
|
|
|
this.activeItem = this.newsItems.length - 1;
|
2017-01-04 10:46:31 +01:00
|
|
|
}
|
2017-01-08 14:38:16 +01:00
|
|
|
this.resetDescrOrFullArticleAndTimer();
|
2020-11-06 11:47:29 +01:00
|
|
|
Log.debug(this.name + " - going from article #" + before + " to #" + this.activeItem + " (of " + this.newsItems.length + ")");
|
2017-01-06 21:57:30 +01:00
|
|
|
this.updateDom(100);
|
|
|
|
}
|
2017-01-08 14:38:16 +01:00
|
|
|
// if "more details" is received the first time: show article summary, on second time show full article
|
2020-05-11 22:22:32 +02:00
|
|
|
else if (notification === "ARTICLE_MORE_DETAILS") {
|
2018-01-29 21:26:34 +01:00
|
|
|
// full article is already showing, so scrolling down
|
2020-05-11 22:22:32 +02:00
|
|
|
if (this.config.showFullArticle === true) {
|
2018-01-29 21:26:34 +01:00
|
|
|
this.scrollPosition += this.config.scrollLength;
|
|
|
|
window.scrollTo(0, this.scrollPosition);
|
2020-11-06 11:47:29 +01:00
|
|
|
Log.debug(this.name + " - scrolling down");
|
|
|
|
Log.debug(this.name + " - ARTICLE_MORE_DETAILS, scroll position: " + this.config.scrollLength);
|
2020-05-11 22:22:32 +02:00
|
|
|
} else {
|
2018-07-07 16:35:07 +02:00
|
|
|
this.showFullArticle();
|
2018-01-29 21:26:34 +01:00
|
|
|
}
|
2020-05-11 22:22:32 +02:00
|
|
|
} else if (notification === "ARTICLE_SCROLL_UP") {
|
|
|
|
if (this.config.showFullArticle === true) {
|
2018-04-17 00:25:58 +02:00
|
|
|
this.scrollPosition -= this.config.scrollLength;
|
|
|
|
window.scrollTo(0, this.scrollPosition);
|
2020-11-06 11:47:29 +01:00
|
|
|
Log.debug(this.name + " - scrolling up");
|
|
|
|
Log.debug(this.name + " - ARTICLE_SCROLL_UP, scroll position: " + this.config.scrollLength);
|
2018-04-17 00:25:58 +02:00
|
|
|
}
|
2020-05-11 22:22:32 +02:00
|
|
|
} else if (notification === "ARTICLE_LESS_DETAILS") {
|
2017-01-08 14:38:16 +01:00
|
|
|
this.resetDescrOrFullArticleAndTimer();
|
2020-11-06 11:47:29 +01:00
|
|
|
Log.debug(this.name + " - showing only article titles again");
|
2017-01-06 21:57:30 +01:00
|
|
|
this.updateDom(100);
|
2020-05-11 22:22:32 +02:00
|
|
|
} else if (notification === "ARTICLE_TOGGLE_FULL") {
|
|
|
|
if (this.config.showFullArticle) {
|
2018-07-07 16:35:07 +02:00
|
|
|
this.activeItem++;
|
|
|
|
this.resetDescrOrFullArticleAndTimer();
|
|
|
|
} else {
|
|
|
|
this.showFullArticle();
|
|
|
|
}
|
2020-05-11 22:22:32 +02:00
|
|
|
} else if (notification === "ARTICLE_INFO_REQUEST") {
|
2019-03-25 01:08:59 +01:00
|
|
|
this.sendNotification("ARTICLE_INFO_RESPONSE", {
|
2020-05-11 22:22:32 +02:00
|
|
|
title: this.newsItems[this.activeItem].title,
|
2019-03-25 01:08:59 +01:00
|
|
|
source: this.newsItems[this.activeItem].sourceTitle,
|
2020-05-11 22:22:32 +02:00
|
|
|
date: this.newsItems[this.activeItem].pubdate,
|
|
|
|
desc: this.newsItems[this.activeItem].description,
|
|
|
|
url: this.getActiveItemURL()
|
2019-06-05 10:23:58 +02:00
|
|
|
});
|
2017-01-04 10:46:31 +01:00
|
|
|
}
|
|
|
|
},
|
2016-08-27 00:32:47 +02:00
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
showFullArticle: function () {
|
2018-07-07 16:35:07 +02:00
|
|
|
this.isShowingDescription = !this.isShowingDescription;
|
|
|
|
this.config.showFullArticle = !this.isShowingDescription;
|
|
|
|
// make bottom bar align to top to allow scrolling
|
2020-05-11 22:22:32 +02:00
|
|
|
if (this.config.showFullArticle === true) {
|
2021-01-16 11:52:55 +01:00
|
|
|
document.getElementsByClassName("region bottom bar")[0].classList.add("newsfeed-fullarticle");
|
2018-07-07 16:35:07 +02:00
|
|
|
}
|
2020-05-02 10:39:09 +02:00
|
|
|
clearInterval(this.timer);
|
|
|
|
this.timer = null;
|
2020-11-06 11:47:29 +01:00
|
|
|
Log.debug(this.name + " - showing " + this.isShowingDescription ? "article description" : "full article");
|
2018-07-07 16:35:07 +02:00
|
|
|
this.updateDom(100);
|
|
|
|
}
|
2016-03-29 15:44:43 +02:00
|
|
|
});
|