Skip to content

Commit

Permalink
[newsfeed] Suppress unsightly animation cases when there are 0 or 1 l…
Browse files Browse the repository at this point in the history
…ive news items
  • Loading branch information
crazyscot committed Jan 13, 2024
1 parent 4430012 commit 59ac873
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ _This release is scheduled to be released on 2024-04-01._
### Fixed

- Skip changelog requirement when running tests for dependency updates (#3320)
- [newsfeed] Suppress unsightly animation cases when there are 0 or 1 active news items
- [newsfeed] Always compute the feed item URL using the same helper function

### Deleted

Expand Down
28 changes: 26 additions & 2 deletions modules/default/newsfeed/newsfeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,30 @@ Module.register("newsfeed", {
if (this.activeItem >= this.newsItems.length) {
this.activeItem = 0;
}
this.activeItemCount = this.newsItems.length;
// this.config.showFullArticle is a run-time configuration, triggered by optional notifications
if (this.config.showFullArticle) {
this.activeItemHash = this.newsItems[this.activeItem]?.hash;
return {
url: this.getActiveItemURL()
};
}
if (this.error) {
this.activeItemHash = undefined;
return {
error: this.error
};
}
if (this.newsItems.length === 0) {
this.activeItemHash = undefined;
return {
empty: true
};
}

const item = this.newsItems[this.activeItem];
this.activeItemHash = item.hash;

const items = this.newsItems.map(function (item) {
item.publishDate = moment(new Date(item.pubdate)).fromNow();
return item;
Expand Down Expand Up @@ -312,8 +318,26 @@ Module.register("newsfeed", {
if (this.timer) clearInterval(this.timer);

this.timer = setInterval(() => {
this.activeItem++;
this.updateDom(this.config.animationSpeed);
/*

Check failure on line 321 in modules/default/newsfeed/newsfeed.js

View workflow job for this annotation

GitHub Actions / test (18.x)

Expected line before comment

Check failure on line 321 in modules/default/newsfeed/newsfeed.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Expected line before comment

Check failure on line 321 in modules/default/newsfeed/newsfeed.js

View workflow job for this annotation

GitHub Actions / test (21.x)

Expected line before comment
* When animations are enabled, don't update the DOM unless we are actually changing what we are displaying.
* (Animating from a headline to itself is unsightly.)
* Cases:
*
* Number of items | Number of items | Display
* at last update | right now | Behaviour
* ----------------------------------------------------
* 0 | 0 | do not update
* 0 | >0 | update
* 1 | 0 or >1 | update
* 1 | 1 | update only if item details (hash value) changed
* >1 | any | update
*
* (N.B. We set activeItemCount and activeItemHash in getTemplateData().)
*/
if (this.newsItems.length !== this.activeItemCount || this.activeItemHash !== this.newsItems[0]?.hash) {
this.activeItem++; // this is OK if newsItems.Length==1; getTemplateData will wrap it around
this.updateDom(this.config.animationSpeed);
}

// Broadcast NewsFeed if needed
if (this.config.broadcastNewsFeeds) {
Expand Down
4 changes: 3 additions & 1 deletion modules/default/newsfeed/newsfeedfetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const iconv = require("iconv-lite");
const { htmlToText } = require("html-to-text");
const Log = require("logger");
const NodeHelper = require("node_helper");
const crypto = require("node:crypto");

Check failure on line 14 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (18.x)

`node:crypto` import should occur before import of `feedme`

Check failure on line 14 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (20.x)

`node:crypto` import should occur before import of `feedme`

Check failure on line 14 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (21.x)

`node:crypto` import should occur before import of `feedme`

/**
* Responsible for requesting an update on the set interval and broadcasting the data.
Expand Down Expand Up @@ -67,7 +68,8 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
description: description,
pubdate: pubdate,
url: url,
useCorsProxy: useCorsProxy
useCorsProxy: useCorsProxy,
hash: crypto.createHash('sha256').update(`${pubdate} :: ${title} :: ${url}`).digest('hex')

Check failure on line 72 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (18.x)

Strings must use doublequote

Check failure on line 72 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (18.x)

Strings must use doublequote

Check failure on line 72 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Strings must use doublequote

Check failure on line 72 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Strings must use doublequote

Check failure on line 72 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (21.x)

Strings must use doublequote

Check failure on line 72 in modules/default/newsfeed/newsfeedfetcher.js

View workflow job for this annotation

GitHub Actions / test (21.x)

Strings must use doublequote
});
} else if (logFeedWarnings) {
Log.warn("Can't parse feed item:");
Expand Down

0 comments on commit 59ac873

Please sign in to comment.