diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1062,6 +1062,7 @@ 'PasteCreateConduitAPIMethod' => 'applications/paste/conduit/PasteCreateConduitAPIMethod.php', 'PasteCreateMailReceiver' => 'applications/paste/mail/PasteCreateMailReceiver.php', 'PasteDefaultViewCapability' => 'applications/paste/capability/PasteDefaultViewCapability.php', + 'PasteEditConduitAPIMethod' => 'applications/paste/conduit/PasteEditConduitAPIMethod.php', 'PasteEmbedView' => 'applications/paste/view/PasteEmbedView.php', 'PasteInfoConduitAPIMethod' => 'applications/paste/conduit/PasteInfoConduitAPIMethod.php', 'PasteMockMailReceiver' => 'applications/paste/mail/PasteMockMailReceiver.php', @@ -3851,6 +3852,7 @@ 'PasteCreateConduitAPIMethod' => 'PasteConduitAPIMethod', 'PasteCreateMailReceiver' => 'PhabricatorMailReceiver', 'PasteDefaultViewCapability' => 'PhabricatorPolicyCapability', + 'PasteEditConduitAPIMethod' => 'PasteConduitAPIMethod', 'PasteEmbedView' => 'AphrontView', 'PasteInfoConduitAPIMethod' => 'PasteConduitAPIMethod', 'PasteMockMailReceiver' => 'PhabricatorObjectMailReceiver', diff --git a/src/applications/paste/conduit/PasteEditConduitAPIMethod.php b/src/applications/paste/conduit/PasteEditConduitAPIMethod.php new file mode 100644 --- /dev/null +++ b/src/applications/paste/conduit/PasteEditConduitAPIMethod.php @@ -0,0 +1,102 @@ + 'required string', + 'paste_id' => 'required id', + 'title' => 'optional string', + 'language' => 'optional string', + ); + } + + public function defineReturnType() { + return 'nonempty dict'; + } + + public function defineErrorTypes() { + return array( + 'ERR-NO-PASTE' => 'Paste may not be empty.', + 'ERR-NO-ACCESS' => 'You do not have permission to edit this paste.', + 'ERR-BAD-PASTE' => 'No such paste exists', + ); + } + + protected function execute(ConduitAPIRequest $request) { + $content = $request->getValue('content'); + $title = $request->getValue('title'); + $language = $request->getValue('language'); + $paste_id = $request->getValue('paste_id'); + + if (!strlen($content)) { + throw new ConduitException('ERR-NO-PASTE'); + } + + $viewer = $request->getUser(); + + try { + $paste = id(new PhabricatorPasteQuery()) + ->setViewer($request->getUser()) + ->requireCapabilities( + array( + PhabricatorPolicyCapability::CAN_VIEW, + PhabricatorPolicyCapability::CAN_EDIT, + )) + ->withIDs(array($paste_id)) + ->needRawContent(true) + ->executeOne(); + } catch (PhabricatorPolicyException $ex) { + throw new ConduitException('ERR-NO-ACCESS'); + } + + if (!$paste) { + throw new ConduitException('ERR-BAD-PASTE'); + } + + $xactions = array(); + + if ($content != $paste->getRawContent()) { + $file = PhabricatorPasteEditor::initializeFileForPaste( + $viewer, + $title, + $content); + + $xactions[] = id(new PhabricatorPasteTransaction()) + ->setTransactionType(PhabricatorPasteTransaction::TYPE_CONTENT) + ->setNewValue($file->getPHID()); + } + + if ($title && $title != $paste->getTitle()) { + $xactions[] = id(new PhabricatorPasteTransaction()) + ->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE) + ->setNewValue($title); + } + + if ($language && $language != $paste->getLanguage()) { + $xactions[] = id(new PhabricatorPasteTransaction()) + ->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE) + ->setNewValue($language); + } + + if (count($xactions) > 0) { + $editor = id(new PhabricatorPasteEditor()) + ->setActor($viewer) + ->setContentSourceFromConduitRequest($request); + + $xactions = $editor->applyTransactions($paste, $xactions); + + $paste->attachRawContent($content); + } + return $this->buildPasteInfoDictionary($paste); + } + +}