Mention probation: don't use time but number of messages in chan since previous mention;

This commit is contained in:
Benjamin Bouvier 2017-09-20 22:28:06 +02:00
parent cb720eafb8
commit dec545136b
2 changed files with 16 additions and 9 deletions

View File

@ -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'],

View File

@ -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')
// 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];