diff --git a/app/config.json b/app/config.json index ff568bc..77f27d4 100644 --- a/app/config.json +++ b/app/config.json @@ -1,6 +1,7 @@ { "saveIntervalSec": 60, "feedCheckIntervalSec": 30, + "maxCacheSize": 10, "commands": { "version": "version", "addFeed": "add-feed" diff --git a/app/index.js b/app/index.js index 0c62d60..a628344 100644 --- a/app/index.js +++ b/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 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 diff --git a/app/models/feed-data.js b/app/models/feed-data.js index d39ce45..8f9198e 100644 --- a/app/models/feed-data.js +++ b/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 }; }