From 4ea0035479405a6a770b88d78b3daa7a5ebb7a8e Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sun, 8 Jan 2017 16:10:07 +0000 Subject: [PATCH] Added prevention for the same user being (un)subscribed multiple times --- feed-bot.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/feed-bot.js b/feed-bot.js index 8aa3b44..b6838ff 100644 --- a/feed-bot.js +++ b/feed-bot.js @@ -136,14 +136,18 @@ var Subscriptions = { }); }, subscribe: function (userID, user) { - this.subscribers.push(userID); - this.writeToFile(); - Log.event("Subscribed user " + (user ? user + "(" + userID + ")" : userID)); + if (this.subscribers.indexOf(userID) === -1) { + this.subscribers.push(userID); //subscribe the user if they aren't already subscribed + this.writeToFile(); + Log.event("Subscribed user " + (user ? user + "(" + userID + ")" : userID)); + } }, unsubscribe: function (userID, user) { - this.subscribers.splice(this.subscribers.indexOf(userID)); - this.writeToFile(); - Log.event("Unsubscribed user " + (user ? user + "(" + userID + ")" : userID)); + if (this.subscribers.indexOf(userID) > -1) { + this.subscribers.splice(this.subscribers.indexOf(userID)); + this.writeToFile(); + Log.event("Unsubscribed user " + (user ? user + "(" + userID + ")" : userID)); + } }, writeToFile: function () { JsonFile.writeFile(Config.subscribersFile, this.subscribers, (err) => { if (err) Log.error("Unable to write subscribers to json file", err); });