From 0f6e5ced5bce6dc69e0e9938a440656672cb6cd2 Mon Sep 17 00:00:00 2001 From: Alex Quach Date: Fri, 7 Jun 2013 15:08:34 -0700 Subject: [PATCH] Adding macro create method. --- src/__phutil_library_map__.php | 2 + .../ConduitAPI_macro_creatememe_Method.php | 57 +++++++++++++++++++ .../PhabricatorMacroMemeController.php | 18 +++++- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 src/applications/macro/conduit/ConduitAPI_macro_creatememe_Method.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index dee3337316..82d0bde3f3 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -183,6 +183,7 @@ phutil_register_library_map(array( 'ConduitAPI_flag_edit_Method' => 'applications/flag/conduit/ConduitAPI_flag_edit_Method.php', 'ConduitAPI_flag_query_Method' => 'applications/flag/conduit/ConduitAPI_flag_query_Method.php', 'ConduitAPI_macro_Method' => 'applications/macro/conduit/ConduitAPI_macro_Method.php', + 'ConduitAPI_macro_creatememe_Method' => 'applications/macro/conduit/ConduitAPI_macro_creatememe_Method.php', 'ConduitAPI_macro_query_Method' => 'applications/macro/conduit/ConduitAPI_macro_query_Method.php', 'ConduitAPI_maniphest_Method' => 'applications/maniphest/conduit/ConduitAPI_maniphest_Method.php', 'ConduitAPI_maniphest_createtask_Method' => 'applications/maniphest/conduit/ConduitAPI_maniphest_createtask_Method.php', @@ -2121,6 +2122,7 @@ phutil_register_library_map(array( 'ConduitAPI_flag_edit_Method' => 'ConduitAPI_flag_Method', 'ConduitAPI_flag_query_Method' => 'ConduitAPI_flag_Method', 'ConduitAPI_macro_Method' => 'ConduitAPIMethod', + 'ConduitAPI_macro_creatememe_Method' => 'ConduitAPI_macro_Method', 'ConduitAPI_macro_query_Method' => 'ConduitAPI_macro_Method', 'ConduitAPI_maniphest_Method' => 'ConduitAPIMethod', 'ConduitAPI_maniphest_createtask_Method' => 'ConduitAPI_maniphest_Method', diff --git a/src/applications/macro/conduit/ConduitAPI_macro_creatememe_Method.php b/src/applications/macro/conduit/ConduitAPI_macro_creatememe_Method.php new file mode 100644 index 0000000000..04f10fd31b --- /dev/null +++ b/src/applications/macro/conduit/ConduitAPI_macro_creatememe_Method.php @@ -0,0 +1,57 @@ + 'string', + 'upperText' => 'optional string', + 'lowerText' => 'optional string', + ); + } + + public function defineReturnType() { + return 'string'; + } + + public function defineErrorTypes() { + return array( + 'ERR-NOT-FOUND' => 'Macro was not found.', + ); + } + + protected function execute(ConduitAPIRequest $request) { + $user = $request->getUser(); + + $macro_name = $request->getValue('macroName'); + $upper_text = $request->getValue('upperText'); + $lower_text = $request->getValue('lowerText'); + + $uri = PhabricatorMacroMemeController::generateMacro( + $user, + $macro_name, + $upper_text, + $lower_text); + + if (!$uri) { + throw new ConduitException('ERR-NOT-FOUND'); + } + + return array( + 'uri' => $uri, + ); + } + +} diff --git a/src/applications/macro/controller/PhabricatorMacroMemeController.php b/src/applications/macro/controller/PhabricatorMacroMemeController.php index 4446f48f93..e876db8068 100644 --- a/src/applications/macro/controller/PhabricatorMacroMemeController.php +++ b/src/applications/macro/controller/PhabricatorMacroMemeController.php @@ -9,12 +9,23 @@ final class PhabricatorMacroMemeController $upper_text = $request->getStr('uppertext'); $lower_text = $request->getStr('lowertext'); $user = $request->getUser(); + + $uri = PhabricatorMacroMemeController::generateMacro($user, $macro_name, + $upper_text, $lower_text); + if ($uri === false) { + return new Aphront404Response(); + } + return id(new AphrontRedirectResponse())->setURI($uri); + } + + public static function generateMacro($user, $macro_name, $upper_text, + $lower_text) { $macro = id(new PhabricatorMacroQuery()) ->setViewer($user) ->withNames(array($macro_name)) ->executeOne(); if (!$macro) { - return new Aphront404Response(); + return false; } $file = $macro->getFile(); @@ -29,7 +40,7 @@ final class PhabricatorMacroMemeController if ($xform) { $memefile = id(new PhabricatorFile())->loadOneWhere( 'phid = %s', $xform->getTransformedPHID()); - return id(new AphrontRedirectResponse())->setURI($memefile->getBestURI()); + return $memefile->getBestURI(); } $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $transformers = (new PhabricatorImageTransformer()); @@ -40,6 +51,7 @@ final class PhabricatorMacroMemeController $xfile->setTransformedPHID($newfile->getPHID()); $xfile->setTransform($hash); $xfile->save(); - return id(new AphrontRedirectResponse())->setURI($newfile->getBestURI()); + + return $newfile->getBestURI(); } }