diff --git a/src/applications/macro/conduit/MacroQueryConduitAPIMethod.php b/src/applications/macro/conduit/MacroQueryConduitAPIMethod.php index 0b2ff99ba..7f680ebdb 100644 --- a/src/applications/macro/conduit/MacroQueryConduitAPIMethod.php +++ b/src/applications/macro/conduit/MacroQueryConduitAPIMethod.php @@ -1,82 +1,83 @@ 'optional list', 'phids' => 'optional list', 'ids' => 'optional list', 'names' => 'optional list', 'nameLike' => 'optional string', ); } public function defineReturnType() { return 'list'; } public function defineErrorTypes() { return array( ); } protected function execute(ConduitAPIRequest $request) { $query = new PhabricatorMacroQuery(); $query->setViewer($request->getUser()); $author_phids = $request->getValue('authorPHIDs'); $phids = $request->getValue('phids'); $ids = $request->getValue('ids'); $name_like = $request->getValue('nameLike'); $names = $request->getValue('names'); if ($author_phids) { $query->withAuthorPHIDs($author_phids); } if ($phids) { $query->withPHIDs($phids); } if ($ids) { $query->withIDs($ids); } if ($name_like) { $query->withNameLike($name_like); } if ($names) { $query->withNames($names); } $macros = $query->execute(); if (!$macros) { return array(); } $results = array(); foreach ($macros as $macro) { $file = $macro->getFile(); $results[$macro->getName()] = array( 'uri' => $file->getBestURI(), 'phid' => $macro->getPHID(), 'authorPHID' => $file->getAuthorPHID(), - 'dateCreated' => $file->getDateCreated(), + 'dateCreated' => $file->getDateCreated(), + 'filePHID' => $file->getPHID(), ); } return $results; } } diff --git a/src/infrastructure/daemon/bot/handler/PhabricatorBotMacroHandler.php b/src/infrastructure/daemon/bot/handler/PhabricatorBotMacroHandler.php index bc60d9d9f..9e7a051a8 100644 --- a/src/infrastructure/daemon/bot/handler/PhabricatorBotMacroHandler.php +++ b/src/infrastructure/daemon/bot/handler/PhabricatorBotMacroHandler.php @@ -1,166 +1,176 @@ macros === false) { return false; } if ($this->macros !== null) { return true; } $macros = $this->getConduit()->callMethodSynchronous( 'macro.query', array()); // If we have no macros, cache `false` (meaning "no macros") and return // immediately. if (!$macros) { $this->macros = false; return false; } $regexp = array(); foreach ($macros as $macro_name => $macro) { $regexp[] = preg_quote($macro_name, '/'); } $regexp = '/^('.implode('|', $regexp).')\z/'; $this->macros = $macros; $this->regexp = $regexp; return true; } public function receiveMessage(PhabricatorBotMessage $message) { if (!$this->init()) { return; } switch ($message->getCommand()) { case 'MESSAGE': $message_body = $message->getBody(); $matches = null; if (!preg_match($this->regexp, trim($message_body), $matches)) { return; } $macro = $matches[1]; $ascii = idx($this->macros[$macro], 'ascii'); if ($ascii === false) { return; } if (!$ascii) { $this->macros[$macro]['ascii'] = $this->rasterize( $this->macros[$macro], $this->getConfig('macro.size', 48), $this->getConfig('macro.aspect', 0.66)); $ascii = $this->macros[$macro]['ascii']; } if ($ascii === false) { // If we failed to rasterize the macro, bail out. return; } $target_name = $message->getTarget()->getName(); foreach ($ascii as $line) { $this->replyTo($message, $line); } break; } } public function rasterize($macro, $size, $aspect) { - $image = HTTPSFuture::loadContent($macro['uri']); + try { + $image = $this->getConduit()->callMethodSynchronous( + 'file.download', + array( + 'phid' => $macro['filePHID'], + )); + $image = base64_decode($image); + } catch (Exception $ex) { + return false; + } + if (!$image) { return false; } $img = @imagecreatefromstring($image); if (!$img) { return false; } $sx = imagesx($img); $sy = imagesy($img); if ($sx > $size || $sy > $size) { $scale = max($sx, $sy) / $size; $dx = floor($sx / $scale); $dy = floor($sy / $scale); } else { $dx = $sx; $dy = $sy; } $dy = floor($dy * $aspect); $dst = imagecreatetruecolor($dx, $dy); if (!$dst) { return false; } imagealphablending($dst, false); $ok = imagecopyresampled( $dst, $img, 0, 0, 0, 0, $dx, $dy, $sx, $sy); if (!$ok) { return false; } $map = array( ' ', '.', ',', ':', ';', '!', '|', '*', '=', '@', '$', '#', ); $lines = array(); for ($ii = 0; $ii < $dy; $ii++) { $buf = ''; for ($jj = 0; $jj < $dx; $jj++) { $c = imagecolorat($dst, $jj, $ii); $a = ($c >> 24) & 0xFF; $r = ($c >> 16) & 0xFF; $g = ($c >> 8) & 0xFF; $b = ($c) & 0xFF; $luma = (255 - ((0.30 * $r) + (0.59 * $g) + (0.11 * $b))) / 256; $luma *= ((127 - $a) / 127); $char = $map[max(0, floor($luma * count($map)))]; $buf .= $char; } $lines[] = $buf; } return $lines; } }