From cb720eafb8af966641eea9070a03dabebcc5aa55 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 20 Sep 2017 21:58:55 +0200 Subject: [PATCH] Add a probation period for mentions; --- config.example.js | 5 +++++ index.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/config.example.js b/config.example.js index 70138a7..2583d79 100644 --- a/config.example.js +++ b/config.example.js @@ -8,8 +8,13 @@ module.exports = { 'somebody': ['push', 'merge_request', 'issue', 'build'] }, + // Project URL used for mentions of MR (e.g. !123) or issues (#122). projectUrl: "https://framagit.org/bnjbvr/kresus/", + // Amout of time (in ms) during which no other mentions to a same MR/issue + // will be done. + cacheDuration: 10000, + branches: ['master'], // IRC nick/names for the bot diff --git a/index.js b/index.js index 51e25bf..c94f79f 100644 --- a/index.js +++ b/index.js @@ -218,7 +218,19 @@ app.post('/', function(req, res) { res.sendStatus(200); }); +var cache = {}; +function makeCacheKey(isIssue, id, chan) { + return chan + '-' + (isIssue ? 'issue' : 'mr') + id; +} + function fetch_and_say(isIssue, id, from, chan) { + var cacheKey = makeCacheKey(isIssue, id, chan); + + // Don't mention if it's been already mentioned in the last + // config.cacheDuration ms. + if (typeof cache[cacheKey] !== 'undefined') + return; + var path, text_prefix; if (isIssue) { path = 'issues/'; @@ -240,6 +252,9 @@ function fetch_and_say(isIssue, id, from, chan) { } else { client.say(to, text_prefix + id + ": " + url); } + cache[cacheKey] = setTimeout(function() { + delete cache[cacheKey]; + }, config.cacheDuration); } }); }