1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +01:00

Repair Flowdock adapter for ChatBot

Summary:
Restores functionality for Flowdock->Chatbot adapter.

Most likely the result of API changes in the year since the original patch was contributed,
the flowdock adapter no longer worked.

This makes a few tweaks to both the base streaming adapter class and the flowdock adpater. I took care to not disturb the functionality of the campfire adapter, but I don't have any way to test it.

Test Plan: I am new here and I have no idea what to write other than sarcastic things but I'll most like amend this after review.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D10168
This commit is contained in:
Matthew Holden 2014-08-06 14:30:07 -07:00 committed by epriestley
parent 50a393913c
commit 5b4fb3b155
2 changed files with 65 additions and 48 deletions

View file

@ -3,11 +3,12 @@
abstract class PhabricatorBotBaseStreamingProtocolAdapter abstract class PhabricatorBotBaseStreamingProtocolAdapter
extends PhabricatorBaseProtocolAdapter { extends PhabricatorBaseProtocolAdapter {
protected $readHandles;
protected $multiHandle;
protected $authtoken;
private $readBuffers; private $readBuffers;
private $authtoken;
private $server; private $server;
private $readHandles;
private $multiHandle;
private $active; private $active;
private $inRooms = array(); private $inRooms = array();
@ -38,26 +39,28 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
// Set up the curl stream for reading // Set up the curl stream for reading
$url = $this->buildStreamingUrl($room_id); $url = $this->buildStreamingUrl($room_id);
$this->readHandle[$url] = curl_init(); $ch = $this->readHandles[$url] = curl_init();
curl_setopt($this->readHandle[$url], CURLOPT_URL, $url);
curl_setopt($this->readHandle[$url], CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($this->readHandle[$url], CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( curl_setopt(
$this->readHandle[$url], $ch,
CURLOPT_USERPWD, CURLOPT_USERPWD,
$this->authtoken.':x'); $this->authtoken.':x');
curl_setopt( curl_setopt(
$this->readHandle[$url], $ch,
CURLOPT_HTTPHEADER, CURLOPT_HTTPHEADER,
array('Content-type: application/json')); array('Content-type: application/json'));
curl_setopt( curl_setopt(
$this->readHandle[$url], $ch,
CURLOPT_WRITEFUNCTION, CURLOPT_WRITEFUNCTION,
array($this, 'read')); array($this, 'read'));
curl_setopt($this->readHandle[$url], CURLOPT_BUFFERSIZE, 128); curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($this->readHandle[$url], CURLOPT_TIMEOUT, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_multi_add_handle($this->multiHandle, $this->readHandle[$url]); curl_multi_add_handle($this->multiHandle, $ch);
// Initialize read buffer // Initialize read buffer
$this->readBuffers[$url] = ''; $this->readBuffers[$url] = '';
@ -153,10 +156,15 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
} }
protected function getAuthorizationHeader() { protected function getAuthorizationHeader() {
return 'Basic '.base64_encode($this->authtoken.':x'); return 'Basic '.$this->getEncodedAuthToken();
}
protected function getEncodedAuthToken() {
return base64_encode($this->authtoken.':x');
} }
abstract protected function buildStreamingUrl($channel); abstract protected function buildStreamingUrl($channel);
abstract protected function processMessage($raw_object); abstract protected function processMessage($raw_object);
} }

View file

@ -8,42 +8,50 @@ final class PhabricatorBotFlowdockProtocolAdapter
} }
protected function buildStreamingUrl($channel) { protected function buildStreamingUrl($channel) {
$organization = $this->getConfig('organization'); $organization = $this->getConfig('flowdock.organization');
if (empty($organization)) {
$this->getConfig('organization');
}
if (empty($organization)) {
throw new Exception(
'"flowdock.organization" configuration variable not set');
}
$ssl = $this->getConfig('ssl'); $ssl = $this->getConfig('ssl');
$url = ($ssl) ? 'https://' : 'http://'; $url = ($ssl) ? 'https://' : 'http://';
$url .= "stream.flowdock.com/flows/{$organization}/{$channel}"; $url .= "{$this->authtoken}@stream.flowdock.com/flows/{$organization}/{$channel}";
return $url; return $url;
} }
protected function processMessage($m_obj) { protected function processMessage($m_obj) {
$command = null; $command = null;
switch ($m_obj['event']) { switch ($m_obj['event']) {
case 'message': case 'message':
$command = 'MESSAGE'; $command = 'MESSAGE';
break; break;
default: default:
// For now, ignore anything which we don't otherwise know about. // For now, ignore anything which we don't otherwise know about.
break; break;
} }
if ($command === null) { if ($command === null) {
return false; return false;
} }
// TODO: These should be usernames, not user IDs. // TODO: These should be usernames, not user IDs.
$sender = id(new PhabricatorBotUser()) $sender = id(new PhabricatorBotUser())
->setName($m_obj['user']); ->setName($m_obj['user']);
$target = id(new PhabricatorBotChannel()) $target = id(new PhabricatorBotChannel())
->setName($m_obj['flow']); ->setName($m_obj['flow']);
return id(new PhabricatorBotMessage()) return id(new PhabricatorBotMessage())
->setCommand($command) ->setCommand($command)
->setSender($sender) ->setSender($sender)
->setTarget($target) ->setTarget($target)
->setBody($m_obj['content']); ->setBody($m_obj['content']);
} }
public function writeMessage(PhabricatorBotMessage $message) { public function writeMessage(PhabricatorBotMessage $message) {
@ -59,15 +67,16 @@ final class PhabricatorBotFlowdockProtocolAdapter
private function speak( private function speak(
$body, $body,
PhabricatorBotTarget $flow) { PhabricatorBotTarget $flow) {
// The $flow->getName() returns the flow's UUID,
list($organization, $room_id) = explode(':', $flow->getName()); // as such, the Flowdock API does not require the organization
// to be specified in the URI
$this->performPost( $this->performPost(
"/flows/{$organization}/{$room_id}/messages", '/messages',
array( array(
'event' => 'message', 'flow' => $flow->getName(),
'content' => $body)); 'event' => 'message',
} 'content' => $body));
}
public function __destruct() { public function __destruct() {
if ($this->readHandles) { if ($this->readHandles) {