From 0ad2b526bcf4d3eddf32caed9f0d256ef33a25e2 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 9 Jun 2011 13:16:54 -0700 Subject: [PATCH] Don't mention the same object in IRC more than once every 10 minutes Summary: phabot gets spammy when discussing a specific revision. Test Plan: Added debugging code and produced a reasonable-looking chatlog: [1:15pm] phabotlocal joined the chat room. [1:15pm] epriestley: D5 [1:15pm] phabotlocal: D5 quack - http://local.aphront.com/D5 [1:15pm] epriestley: D5 [1:15pm] phabotlocal: Declining to mention PHID-DREV-d7940d0fe081e3ae5267 again (59s left). [1:15pm] epriestley: D5 [1:15pm] phabotlocal: Declining to mention PHID-DREV-d7940d0fe081e3ae5267 again (57s left). [1:15pm] CodeBlock: nice :D [1:15pm] epriestley: I should just leave that code in for maximum irony. [1:15pm] epriestley: D5 [1:15pm] phabotlocal: Declining to mention PHID-DREV-d7940d0fe081e3ae5267 again (9s left). [1:16pm] epriestley: D5 [1:16pm] phabotlocal: Declining to mention PHID-DREV-d7940d0fe081e3ae5267 again (2s left). [1:16pm] epriestley: D5 [1:16pm] phabotlocal: D5 quack - http://local.aphront.com/D5 [1:16pm] epriestley: D5 [1:16pm] phabotlocal: Declining to mention PHID-DREV-d7940d0fe081e3ae5267 again (57s left). [1:16pm] epriestley: rJUI100 [1:16pm] phabotlocal: http://local.aphront.com/rJUI100 [1:16pm] epriestley: rJUI100 [1:16pm] phabotlocal: Declining to mention PHID-CMIT-8fb731b171a27b8d477c again (55s left). [1:16pm] phabotlocal left the chat room. (Remote host closed the connection) Reviewed By: aran Reviewers: aran, codeblock, hsb, mroch CC: aran Differential Revision: 420 --- .../PhabricatorIRCObjectNameHandler.php | 22 ++++++++++++++++--- .../irc/handler/objectname/__init__.php | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php b/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php index 0851e426e0..0629c06df8 100644 --- a/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php +++ b/src/infrastructure/daemon/irc/handler/objectname/PhabricatorIRCObjectNameHandler.php @@ -23,6 +23,12 @@ */ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler { + /** + * Map of PHIDs to the last mention of them (as an epoch timestamp); prevents + * us from spamming chat when a single object is discussed. + */ + private $recentlyMentioned = array(); + public function receiveMessage(PhabricatorIRCMessage $message) { switch ($message->getCommand()) { @@ -82,7 +88,7 @@ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler { 'guids' => $revision_ids, )); foreach ($revisions as $revision) { - $output[] = + $output[$revision['phid']] = 'D'.$revision['id'].' '.$revision['name'].' - '. $revision['uri']; } @@ -100,11 +106,21 @@ class PhabricatorIRCObjectNameHandler extends PhabricatorIRCHandler { if (isset($commit['error'])) { continue; } - $output[] = $commit['uri']; + $output[$commit['commitPHID']] = $commit['uri']; } } - foreach ($output as $description) { + foreach ($output as $phid => $description) { + + // Don't mention the same object more than once every 10 minutes, so + // we avoid spamming the chat over and over again for discsussions of + // a specific revision, for example. + $quiet_until = idx($this->recentlyMentioned, $phid, 0) + (60 * 10); + if (time() < $quiet_until) { + continue; + } + $this->recentlyMentioned[$phid] = time(); + $this->write('PRIVMSG', "{$channel} :{$description}"); } break; diff --git a/src/infrastructure/daemon/irc/handler/objectname/__init__.php b/src/infrastructure/daemon/irc/handler/objectname/__init__.php index 1b2f54cc85..71b4cd099e 100644 --- a/src/infrastructure/daemon/irc/handler/objectname/__init__.php +++ b/src/infrastructure/daemon/irc/handler/objectname/__init__.php @@ -8,5 +8,7 @@ phutil_require_module('phabricator', 'infrastructure/daemon/irc/handler/base'); +phutil_require_module('phutil', 'utils'); + phutil_require_source('PhabricatorIRCObjectNameHandler.php');