@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

Matthew Holden and committed by
epriestley
5b4fb3b1 50a39391

+65 -48
+22 -14
src/infrastructure/daemon/bot/adapter/PhabricatorBotBaseStreamingProtocolAdapter.php
··· 3 3 abstract class PhabricatorBotBaseStreamingProtocolAdapter 4 4 extends PhabricatorBaseProtocolAdapter { 5 5 6 + protected $readHandles; 7 + protected $multiHandle; 8 + protected $authtoken; 9 + 6 10 private $readBuffers; 7 - private $authtoken; 8 11 private $server; 9 - private $readHandles; 10 - private $multiHandle; 11 12 private $active; 12 13 private $inRooms = array(); 13 14 ··· 38 39 39 40 // Set up the curl stream for reading 40 41 $url = $this->buildStreamingUrl($room_id); 41 - $this->readHandle[$url] = curl_init(); 42 - curl_setopt($this->readHandle[$url], CURLOPT_URL, $url); 43 - curl_setopt($this->readHandle[$url], CURLOPT_RETURNTRANSFER, true); 44 - curl_setopt($this->readHandle[$url], CURLOPT_FOLLOWLOCATION, 1); 42 + $ch = $this->readHandles[$url] = curl_init(); 43 + 44 + curl_setopt($ch, CURLOPT_URL, $url); 45 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 46 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 45 47 curl_setopt( 46 - $this->readHandle[$url], 48 + $ch, 47 49 CURLOPT_USERPWD, 48 50 $this->authtoken.':x'); 51 + 49 52 curl_setopt( 50 - $this->readHandle[$url], 53 + $ch, 51 54 CURLOPT_HTTPHEADER, 52 55 array('Content-type: application/json')); 53 56 curl_setopt( 54 - $this->readHandle[$url], 57 + $ch, 55 58 CURLOPT_WRITEFUNCTION, 56 59 array($this, 'read')); 57 - curl_setopt($this->readHandle[$url], CURLOPT_BUFFERSIZE, 128); 58 - curl_setopt($this->readHandle[$url], CURLOPT_TIMEOUT, 0); 60 + curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); 61 + curl_setopt($ch, CURLOPT_TIMEOUT, 0); 59 62 60 - curl_multi_add_handle($this->multiHandle, $this->readHandle[$url]); 63 + curl_multi_add_handle($this->multiHandle, $ch); 61 64 62 65 // Initialize read buffer 63 66 $this->readBuffers[$url] = ''; ··· 153 156 } 154 157 155 158 protected function getAuthorizationHeader() { 156 - return 'Basic '.base64_encode($this->authtoken.':x'); 159 + return 'Basic '.$this->getEncodedAuthToken(); 160 + } 161 + 162 + protected function getEncodedAuthToken() { 163 + return base64_encode($this->authtoken.':x'); 157 164 } 158 165 159 166 abstract protected function buildStreamingUrl($channel); 160 167 161 168 abstract protected function processMessage($raw_object); 169 + 162 170 }
+43 -34
src/infrastructure/daemon/bot/adapter/PhabricatorBotFlowdockProtocolAdapter.php
··· 8 8 } 9 9 10 10 protected function buildStreamingUrl($channel) { 11 - $organization = $this->getConfig('organization'); 11 + $organization = $this->getConfig('flowdock.organization'); 12 + if (empty($organization)) { 13 + $this->getConfig('organization'); 14 + } 15 + if (empty($organization)) { 16 + throw new Exception( 17 + '"flowdock.organization" configuration variable not set'); 18 + } 19 + 20 + 12 21 $ssl = $this->getConfig('ssl'); 13 22 14 23 $url = ($ssl) ? 'https://' : 'http://'; 15 - $url .= "stream.flowdock.com/flows/{$organization}/{$channel}"; 16 - 24 + $url .= "{$this->authtoken}@stream.flowdock.com/flows/{$organization}/{$channel}"; 17 25 return $url; 18 26 } 19 27 20 28 protected function processMessage($m_obj) { 21 - $command = null; 22 - switch ($m_obj['event']) { 23 - case 'message': 24 - $command = 'MESSAGE'; 25 - break; 26 - default: 27 - // For now, ignore anything which we don't otherwise know about. 28 - break; 29 - } 29 + $command = null; 30 + switch ($m_obj['event']) { 31 + case 'message': 32 + $command = 'MESSAGE'; 33 + break; 34 + default: 35 + // For now, ignore anything which we don't otherwise know about. 36 + break; 37 + } 30 38 31 - if ($command === null) { 32 - return false; 33 - } 39 + if ($command === null) { 40 + return false; 41 + } 34 42 35 - // TODO: These should be usernames, not user IDs. 36 - $sender = id(new PhabricatorBotUser()) 37 - ->setName($m_obj['user']); 43 + // TODO: These should be usernames, not user IDs. 44 + $sender = id(new PhabricatorBotUser()) 45 + ->setName($m_obj['user']); 38 46 39 - $target = id(new PhabricatorBotChannel()) 40 - ->setName($m_obj['flow']); 47 + $target = id(new PhabricatorBotChannel()) 48 + ->setName($m_obj['flow']); 41 49 42 - return id(new PhabricatorBotMessage()) 43 - ->setCommand($command) 44 - ->setSender($sender) 45 - ->setTarget($target) 46 - ->setBody($m_obj['content']); 50 + return id(new PhabricatorBotMessage()) 51 + ->setCommand($command) 52 + ->setSender($sender) 53 + ->setTarget($target) 54 + ->setBody($m_obj['content']); 47 55 } 48 56 49 57 public function writeMessage(PhabricatorBotMessage $message) { ··· 59 67 private function speak( 60 68 $body, 61 69 PhabricatorBotTarget $flow) { 62 - 63 - list($organization, $room_id) = explode(':', $flow->getName()); 64 - 65 - $this->performPost( 66 - "/flows/{$organization}/{$room_id}/messages", 67 - array( 68 - 'event' => 'message', 69 - 'content' => $body)); 70 - } 70 + // The $flow->getName() returns the flow's UUID, 71 + // as such, the Flowdock API does not require the organization 72 + // to be specified in the URI 73 + $this->performPost( 74 + '/messages', 75 + array( 76 + 'flow' => $flow->getName(), 77 + 'event' => 'message', 78 + 'content' => $body)); 79 + } 71 80 72 81 public function __destruct() { 73 82 if ($this->readHandles) {