From a1faac0a21b88bacc242fe2083acc4b799b6737f Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Tue, 11 Mar 2014 15:51:53 -0700 Subject: [PATCH] Phame - create conduit API to create posts Summary: nothing too crazy here. try to be smart about some defaults (i.e. phame title is optional and can be derived from title; post as not a draft by default; etc). Fixes T3695. also do a little re-factoring to centralizing initializing new posts and turning posts into dictionaries. also change blogs => posts in another conduit method so it makes sense and stuff. Test Plan: made some posts via conduit. testing trying to specify blogger, phame title, and isDraft, all worked nicely Reviewers: chad, epriestley Reviewed By: epriestley Subscribers: aran, epriestley, Korvin Maniphest Tasks: T3695 Differential Revision: https://secure.phabricator.com/D8485 --- src/__phutil_library_map__.php | 2 + .../ConduitAPI_phame_createpost_Method.php | 97 +++++++++++++++++++ .../ConduitAPI_phame_queryposts_Method.php | 18 +--- .../post/PhamePostEditController.php | 7 +- src/applications/phame/storage/PhamePost.php | 29 ++++++ 5 files changed, 132 insertions(+), 21 deletions(-) create mode 100644 src/applications/phame/conduit/ConduitAPI_phame_createpost_Method.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index b59bbbc548..84c5e6286e 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -210,6 +210,7 @@ phutil_register_library_map(array( 'ConduitAPI_paste_info_Method' => 'applications/paste/conduit/ConduitAPI_paste_info_Method.php', 'ConduitAPI_paste_query_Method' => 'applications/paste/conduit/ConduitAPI_paste_query_Method.php', 'ConduitAPI_phame_Method' => 'applications/phame/conduit/ConduitAPI_phame_Method.php', + 'ConduitAPI_phame_createpost_Method' => 'applications/phame/conduit/ConduitAPI_phame_createpost_Method.php', 'ConduitAPI_phame_query_Method' => 'applications/phame/conduit/ConduitAPI_phame_query_Method.php', 'ConduitAPI_phame_queryposts_Method' => 'applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php', 'ConduitAPI_phid_Method' => 'applications/phid/conduit/ConduitAPI_phid_Method.php', @@ -2738,6 +2739,7 @@ phutil_register_library_map(array( 'ConduitAPI_paste_info_Method' => 'ConduitAPI_paste_Method', 'ConduitAPI_paste_query_Method' => 'ConduitAPI_paste_Method', 'ConduitAPI_phame_Method' => 'ConduitAPIMethod', + 'ConduitAPI_phame_createpost_Method' => 'ConduitAPI_phame_Method', 'ConduitAPI_phame_query_Method' => 'ConduitAPI_phame_Method', 'ConduitAPI_phame_queryposts_Method' => 'ConduitAPI_phame_Method', 'ConduitAPI_phid_Method' => 'ConduitAPIMethod', diff --git a/src/applications/phame/conduit/ConduitAPI_phame_createpost_Method.php b/src/applications/phame/conduit/ConduitAPI_phame_createpost_Method.php new file mode 100644 index 0000000000..b35deef77f --- /dev/null +++ b/src/applications/phame/conduit/ConduitAPI_phame_createpost_Method.php @@ -0,0 +1,97 @@ + 'required phid', + 'title' => 'required string', + 'body' => 'required string', + 'phameTitle' => 'optional string', + 'bloggerPHID' => 'optional phid', + 'isDraft' => 'optional bool', + ); + } + + public function defineReturnType() { + return 'list'; + } + + public function defineErrorTypes() { + return array( + 'ERR-INVALID-PARAMETER' => + pht('Missing or malformed parameter.'), + 'ERR-INVALID-BLOG' => + pht('Invalid blog PHID or user can not post to blog.'), + ); + } + + protected function execute(ConduitAPIRequest $request) { + $user = $request->getUser(); + $blog_phid = $request->getValue('blogPHID'); + $title = $request->getValue('title'); + $body = $request->getValue('body'); + $exception_description = array(); + if (!$blog_phid) { + $exception_description[] = pht('No blog phid.'); + } + if (!strlen($title)) { + $exception_description[] = pht('No post title.'); + } + if (!strlen($body)) { + $exception_description[] = pht('No post body.'); + } + if ($exception_description) { + throw id(new ConduitException('ERR-INVALID-PARAMETER')) + ->setErrorDescription(implode("\n", $exception_description)); + } + + $blogger_phid = $request->getValue('bloggerPHID'); + if ($blogger_phid) { + $blogger = id(new PhabricatorPeopleQuery()) + ->setViewer($user) + ->withPHIDs(array($blogger_phid)) + ->executeOne(); + } else { + $blogger = $user; + } + + $blog = id(new PhameBlogQuery()) + ->setViewer($blogger) + ->withPHIDs(array($blog_phid)) + ->requireCapabilities( + array( + PhabricatorPolicyCapability::CAN_JOIN, + )) + ->executeOne(); + + if (!$blog) { + throw new ConduitException('ERR-INVALID-BLOG'); + } + + $post = PhamePost::initializePost($blogger, $blog); + $is_draft = $request->getValue('isDraft', false); + if (!$is_draft) { + $post->setDatePublished(time()); + $post->setVisibility(PhamePost::VISIBILITY_PUBLISHED); + } + $post->setTitle($title); + $phame_title = $request->getValue( + 'phameTitle', + phutil_utf8_shorten($title, 64)); + $post->setPhameTitle(PhabricatorSlug::normalize($phame_title)); + $post->setBody($body); + $post->save(); + + return $post->toDictionary(); + } + +} diff --git a/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php b/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php index b4cce2a454..2c6d8cc683 100644 --- a/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php +++ b/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php @@ -95,23 +95,11 @@ final class ConduitAPI_phame_queryposts_Method extends ConduitAPI_phame_Method { $query->setLimit($limit); } - $blogs = $query->execute(); + $posts = $query->execute(); $results = array(); - foreach ($blogs as $blog) { - $results[] = array( - 'id' => $blog->getID(), - 'phid' => $blog->getPHID(), - 'blogPHID' => $blog->getBlogPHID(), - 'bloggerPHID' => $blog->getBloggerPHID(), - 'viewURI' => $blog->getViewURI(), - 'title' => $blog->getTitle(), - 'phameTitle' => $blog->getPhameTitle(), - 'body' => $blog->getBody(), - 'summary' => PhabricatorMarkupEngine::summarize($blog->getBody()), - 'datePublished' => $blog->getDatePublished(), - 'published' => !$blog->isDraft(), - ); + foreach ($posts as $post) { + $results[] = $post->toDictionary(); } return $results; diff --git a/src/applications/phame/controller/post/PhamePostEditController.php b/src/applications/phame/controller/post/PhamePostEditController.php index b780fca017..2c763ac2dc 100644 --- a/src/applications/phame/controller/post/PhamePostEditController.php +++ b/src/applications/phame/controller/post/PhamePostEditController.php @@ -46,12 +46,7 @@ final class PhamePostEditController return new Aphront404Response(); } - $post = id(new PhamePost()) - ->setBloggerPHID($user->getPHID()) - ->setBlogPHID($blog->getPHID()) - ->setBlog($blog) - ->setDatePublished(0) - ->setVisibility(PhamePost::VISIBILITY_DRAFT); + $post = PhamePost::initializePost($user, $blog); $cancel_uri = $this->getApplicationURI('/blog/view/'.$blog->getID().'/'); $submit_button = pht('Save Draft'); diff --git a/src/applications/phame/storage/PhamePost.php b/src/applications/phame/storage/PhamePost.php index 5ae396f4c8..009e6ccb46 100644 --- a/src/applications/phame/storage/PhamePost.php +++ b/src/applications/phame/storage/PhamePost.php @@ -26,6 +26,19 @@ final class PhamePost extends PhameDAO private $blog; + public static function initializePost( + PhabricatorUser $blogger, + PhameBlog $blog) { + + $post = id(new PhamePost()) + ->setBloggerPHID($blogger->getPHID()) + ->setBlogPHID($blog->getPHID()) + ->setBlog($blog) + ->setDatePublished(0) + ->setVisibility(self::VISIBILITY_DRAFT); + return $post; + } + public function setBlog(PhameBlog $blog) { $this->blog = $blog; return $this; @@ -82,6 +95,22 @@ final class PhamePost extends PhameDAO PhabricatorPhamePHIDTypePost::TYPECONST); } + public function toDictionary() { + return array( + 'id' => $this->getID(), + 'phid' => $this->getPHID(), + 'blogPHID' => $this->getBlogPHID(), + 'bloggerPHID' => $this->getBloggerPHID(), + 'viewURI' => $this->getViewURI(), + 'title' => $this->getTitle(), + 'phameTitle' => $this->getPhameTitle(), + 'body' => $this->getBody(), + 'summary' => PhabricatorMarkupEngine::summarize($this->getBody()), + 'datePublished' => $this->getDatePublished(), + 'published' => !$this->isDraft(), + ); + } + public static function getVisibilityOptionsForSelect() { return array( self::VISIBILITY_DRAFT => 'Draft: visible only to me.',