1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 22:10:55 +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
extends PhabricatorBaseProtocolAdapter {
protected $readHandles;
protected $multiHandle;
protected $authtoken;
private $readBuffers;
private $authtoken;
private $server;
private $readHandles;
private $multiHandle;
private $active;
private $inRooms = array();
@ -38,26 +39,28 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
// Set up the curl stream for reading
$url = $this->buildStreamingUrl($room_id);
$this->readHandle[$url] = curl_init();
curl_setopt($this->readHandle[$url], CURLOPT_URL, $url);
curl_setopt($this->readHandle[$url], CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->readHandle[$url], CURLOPT_FOLLOWLOCATION, 1);
$ch = $this->readHandles[$url] = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt(
$this->readHandle[$url],
$ch,
CURLOPT_USERPWD,
$this->authtoken.':x');
curl_setopt(
$this->readHandle[$url],
$ch,
CURLOPT_HTTPHEADER,
array('Content-type: application/json'));
curl_setopt(
$this->readHandle[$url],
$ch,
CURLOPT_WRITEFUNCTION,
array($this, 'read'));
curl_setopt($this->readHandle[$url], CURLOPT_BUFFERSIZE, 128);
curl_setopt($this->readHandle[$url], CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
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
$this->readBuffers[$url] = '';
@ -153,10 +156,15 @@ abstract class PhabricatorBotBaseStreamingProtocolAdapter
}
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 processMessage($raw_object);
}

View file

@ -8,12 +8,20 @@ final class PhabricatorBotFlowdockProtocolAdapter
}
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');
$url = ($ssl) ? 'https://' : 'http://';
$url .= "stream.flowdock.com/flows/{$organization}/{$channel}";
$url .= "{$this->authtoken}@stream.flowdock.com/flows/{$organization}/{$channel}";
return $url;
}
@ -59,12 +67,13 @@ final class PhabricatorBotFlowdockProtocolAdapter
private function speak(
$body,
PhabricatorBotTarget $flow) {
list($organization, $room_id) = explode(':', $flow->getName());
// The $flow->getName() returns the flow's UUID,
// as such, the Flowdock API does not require the organization
// to be specified in the URI
$this->performPost(
"/flows/{$organization}/{$room_id}/messages",
'/messages',
array(
'flow' => $flow->getName(),
'event' => 'message',
'content' => $body));
}