Browse Source

Add maximum link cache size

shorten
benji7425 4 years ago
parent
commit
bf9fbdcdae
  1. 1
      app/config.json
  2. 7
      app/index.js
  3. 8
      app/models/feed-data.js

1
app/config.json

@ -1,6 +1,7 @@
{
"saveIntervalSec": 60,
"feedCheckIntervalSec": 30,
"maxCacheSize": 10,
"commands": {
"version": "version",
"addFeed": "add-feed"

7
app/index.js

@ -53,7 +53,7 @@ const HandleMessage = {
message.reply("v" + require("../package.json").version);
break;
case config.commands.addFeed:
addFeed(client, guildsData, message);
addFeed(client, guildsData, message, config.maxCacheSize);
break;
}
}
@ -66,7 +66,7 @@ const HandleMessage = {
}
};
function addFeed(client, guildsData, message) {
function addFeed(client, guildsData, message, maxCacheSize) {
const parameters = message.content.split(" "); //expect !addfeed <url> <channelName> <roleName>
const feedUrl = [...GetUrls(message.content)][0];
@ -80,7 +80,8 @@ function addFeed(client, guildsData, message) {
const feedData = new FeedData({
url: feedUrl,
channelName: channel.name,
roleName: role ? role.name : null
roleName: role ? role.name : null,
maxCacheSize: maxCacheSize
});
//ask the user if they're happy with the details they set up, save if yes, don't if no

8
app/models/feed-data.js

@ -8,17 +8,21 @@ const FeedRead = require("feed-read"); //for extracing new links from RSS feeds
const GetUrls = require("get-urls"); //for extracting urls from messages
module.exports = class FeedData {
constructor({ url, channelName, roleName, cachedLinks }) {
constructor({ url, channelName, roleName, cachedLinks, maxCacheSize }) {
this.url = url;
this.channelName = channelName;
this.roleName = roleName;
this.cachedLinks = cachedLinks || [];
this.maxCacheSize = maxCacheSize || 10;
this.cachedLinks.push = (...elements) => {
const unique = elements
.map(el => normaliseUrl(el)) //normalise all the urls
.filter(el => !this.cachedLinks.includes(el)); //filter out any already cached
Array.prototype.push.apply(this.cachedLinks, unique);
Array.prototype.push.apply(this.cachedLinks, unique);
if(this.cachedLinks.length > this.maxCacheSize)
this.cachedLinks.splice(0, this.cachedLinks.length - this.maxCacheSize); //remove the # of elements above the max from the beginning
};
}

Loading…
Cancel
Save