From dec545136b594017d9d06a40a24be19165663388 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 20 Sep 2017 22:28:06 +0200 Subject: [PATCH] Mention probation: don't use time but number of messages in chan since previous mention; --- config.example.js | 4 ++-- index.js | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/config.example.js b/config.example.js index 2583d79..c96f7f3 100644 --- a/config.example.js +++ b/config.example.js @@ -11,9 +11,9 @@ module.exports = { // 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 + // Amout of messages between which no other mentions to a same MR/issue // will be done. - cacheDuration: 10000, + cacheDuration: 15, branches: ['master'], diff --git a/index.js b/index.js index c94f79f..666aa67 100644 --- a/index.js +++ b/index.js @@ -218,7 +218,10 @@ app.post('/', function(req, res) { res.sendStatus(200); }); -var cache = {}; +var chanMessageCounters = {}; + +var mentionCache = {}; + function makeCacheKey(isIssue, id, chan) { return chan + '-' + (isIssue ? 'issue' : 'mr') + id; } @@ -227,9 +230,13 @@ 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; + // config.cacheDuration messages. + var mentionCounter = mentionCache[cacheKey]; + if (typeof mentionCounter !== 'undefined') { + if (chanMessageCounters[chan] - mentionCounter <= config.cacheDuration) { + return; + } + } var path, text_prefix; if (isIssue) { @@ -252,9 +259,7 @@ 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); + mentionCache[cacheKey] = chanMessageCounters[chan]; } }); } @@ -294,6 +299,8 @@ app.listen(config.port, config.hostname, function() { console.log('gitlab-to-irc running.'); client.on('message', function(from, chan, message) { + chanMessageCounters[chan] = (chanMessageCounters[chan] || 0) + 1; + var matches = null; while ((matches = issueRegexp.exec(message)) !== null) { var issueId = matches[1];