From 6db25aa41c9df16252666abef54292b788596a9f Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Fri, 7 Nov 2014 11:46:25 -0800 Subject: [PATCH] Phriction - add "create" conduit endpoint Summary: Fixes T6262. Ref T4029. Also gets us ready for T5873 for these end points. I can file something new about someday adding phriction.query, etc but I think we'll remember and can look at that post T5873. Test Plan: made a document via conduit and it worked! Reviewers: epriestley Reviewed By: epriestley Subscribers: Korvin, epriestley Maniphest Tasks: T6262, T4029 Differential Revision: https://secure.phabricator.com/D10813 --- src/__phutil_library_map__.php | 2 + .../PhrictionCreateConduitAPIMethod.php | 70 +++++++++++++++++++ .../conduit/PhrictionEditConduitAPIMethod.php | 12 +++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/applications/phriction/conduit/PhrictionCreateConduitAPIMethod.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 6e10991f2b..0b9ad35307 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2762,6 +2762,7 @@ phutil_register_library_map(array( 'PhrictionConstants' => 'applications/phriction/constants/PhrictionConstants.php', 'PhrictionContent' => 'applications/phriction/storage/PhrictionContent.php', 'PhrictionController' => 'applications/phriction/controller/PhrictionController.php', + 'PhrictionCreateConduitAPIMethod' => 'applications/phriction/conduit/PhrictionCreateConduitAPIMethod.php', 'PhrictionDAO' => 'applications/phriction/storage/PhrictionDAO.php', 'PhrictionDeleteController' => 'applications/phriction/controller/PhrictionDeleteController.php', 'PhrictionDiffController' => 'applications/phriction/controller/PhrictionDiffController.php', @@ -5966,6 +5967,7 @@ phutil_register_library_map(array( 'PhabricatorMarkupInterface', ), 'PhrictionController' => 'PhabricatorController', + 'PhrictionCreateConduitAPIMethod' => 'PhrictionConduitAPIMethod', 'PhrictionDAO' => 'PhabricatorLiskDAO', 'PhrictionDeleteController' => 'PhrictionController', 'PhrictionDiffController' => 'PhrictionController', diff --git a/src/applications/phriction/conduit/PhrictionCreateConduitAPIMethod.php b/src/applications/phriction/conduit/PhrictionCreateConduitAPIMethod.php new file mode 100644 index 0000000000..c0aaedf15f --- /dev/null +++ b/src/applications/phriction/conduit/PhrictionCreateConduitAPIMethod.php @@ -0,0 +1,70 @@ + 'required string', + 'title' => 'required string', + 'content' => 'required string', + 'description' => 'optional string', + ); + } + public function defineReturnType() { + return 'nonempty dict'; + } + public function defineErrorTypes() { + return array( + ); + } + protected function execute(ConduitAPIRequest $request) { + $slug = $request->getValue('slug'); + if (!strlen($slug)) { + throw new Exception(pht('No such document.')); + } + $doc = id(new PhrictionDocumentQuery()) + ->setViewer($request->getUser()) + ->withSlugs(array(PhabricatorSlug::normalize($slug))) + ->requireCapabilities( + array( + PhabricatorPolicyCapability::CAN_VIEW, + PhabricatorPolicyCapability::CAN_EDIT, + )) + ->executeOne(); + if ($doc) { + throw new Exception(pht('Document already exists!')); + } + + $doc = PhrictionDocument::initializeNewDocument( + $request->getUser(), + $slug); + + $xactions = array(); + $xactions[] = id(new PhrictionTransaction()) + ->setTransactionType(PhrictionTransaction::TYPE_TITLE) + ->setNewValue($request->getValue('title')); + $xactions[] = id(new PhrictionTransaction()) + ->setTransactionType(PhrictionTransaction::TYPE_CONTENT) + ->setNewValue($request->getValue('content')); + + $editor = id(new PhrictionTransactionEditor()) + ->setActor($request->getUser()) + ->setContentSourceFromConduitRequest($request) + ->setContinueOnNoEffect(true) + ->setDescription($request->getValue('description')); + + try { + $editor->applyTransactions($doc, $xactions); + } catch (PhabricatorApplicationTransactionValidationException $ex) { + // TODO - some magical hotness via T5873 + throw $ex; + } + + return $this->buildDocumentInfoDictionary($doc); + } +} diff --git a/src/applications/phriction/conduit/PhrictionEditConduitAPIMethod.php b/src/applications/phriction/conduit/PhrictionEditConduitAPIMethod.php index 97ffbe7801..1bf6b53395 100644 --- a/src/applications/phriction/conduit/PhrictionEditConduitAPIMethod.php +++ b/src/applications/phriction/conduit/PhrictionEditConduitAPIMethod.php @@ -7,7 +7,7 @@ final class PhrictionEditConduitAPIMethod extends PhrictionConduitAPIMethod { } public function getMethodDescription() { - return 'Update a Phriction document.'; + return pht('Update a Phriction document.'); } public function defineParamTypes() { @@ -57,8 +57,14 @@ final class PhrictionEditConduitAPIMethod extends PhrictionConduitAPIMethod { ->setActor($request->getUser()) ->setContentSourceFromConduitRequest($request) ->setContinueOnNoEffect(true) - ->setDescription($request->getValue('description')) - ->applyTransactions($doc, $xactions); + ->setDescription($request->getValue('description')); + + try { + $editor->applyTransactions($doc, $xactions); + } catch (PhabricatorApplicationTransactionValidationException $ex) { + // TODO - some magical hotness via T5873 + throw $ex; + } return $this->buildDocumentInfoDictionary($doc); } -- 2.51.2