mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-20 04:20:55 +01:00
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
This commit is contained in:
parent
46cf263e9d
commit
a1faac0a21
5 changed files with 132 additions and 21 deletions
|
@ -210,6 +210,7 @@ phutil_register_library_map(array(
|
||||||
'ConduitAPI_paste_info_Method' => 'applications/paste/conduit/ConduitAPI_paste_info_Method.php',
|
'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_paste_query_Method' => 'applications/paste/conduit/ConduitAPI_paste_query_Method.php',
|
||||||
'ConduitAPI_phame_Method' => 'applications/phame/conduit/ConduitAPI_phame_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_query_Method' => 'applications/phame/conduit/ConduitAPI_phame_query_Method.php',
|
||||||
'ConduitAPI_phame_queryposts_Method' => 'applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php',
|
'ConduitAPI_phame_queryposts_Method' => 'applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php',
|
||||||
'ConduitAPI_phid_Method' => 'applications/phid/conduit/ConduitAPI_phid_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_info_Method' => 'ConduitAPI_paste_Method',
|
||||||
'ConduitAPI_paste_query_Method' => 'ConduitAPI_paste_Method',
|
'ConduitAPI_paste_query_Method' => 'ConduitAPI_paste_Method',
|
||||||
'ConduitAPI_phame_Method' => 'ConduitAPIMethod',
|
'ConduitAPI_phame_Method' => 'ConduitAPIMethod',
|
||||||
|
'ConduitAPI_phame_createpost_Method' => 'ConduitAPI_phame_Method',
|
||||||
'ConduitAPI_phame_query_Method' => 'ConduitAPI_phame_Method',
|
'ConduitAPI_phame_query_Method' => 'ConduitAPI_phame_Method',
|
||||||
'ConduitAPI_phame_queryposts_Method' => 'ConduitAPI_phame_Method',
|
'ConduitAPI_phame_queryposts_Method' => 'ConduitAPI_phame_Method',
|
||||||
'ConduitAPI_phid_Method' => 'ConduitAPIMethod',
|
'ConduitAPI_phid_Method' => 'ConduitAPIMethod',
|
||||||
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
final class ConduitAPI_phame_createpost_Method extends ConduitAPI_phame_Method {
|
||||||
|
|
||||||
|
public function getMethodDescription() {
|
||||||
|
return pht('Create a phame post.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMethodStatus() {
|
||||||
|
return self::METHOD_STATUS_UNSTABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function defineParamTypes() {
|
||||||
|
return array(
|
||||||
|
'blogPHID' => 'required phid',
|
||||||
|
'title' => 'required string',
|
||||||
|
'body' => 'required string',
|
||||||
|
'phameTitle' => 'optional string',
|
||||||
|
'bloggerPHID' => 'optional phid',
|
||||||
|
'isDraft' => 'optional bool',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function defineReturnType() {
|
||||||
|
return 'list<dict>';
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -95,23 +95,11 @@ final class ConduitAPI_phame_queryposts_Method extends ConduitAPI_phame_Method {
|
||||||
$query->setLimit($limit);
|
$query->setLimit($limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
$blogs = $query->execute();
|
$posts = $query->execute();
|
||||||
|
|
||||||
$results = array();
|
$results = array();
|
||||||
foreach ($blogs as $blog) {
|
foreach ($posts as $post) {
|
||||||
$results[] = array(
|
$results[] = $post->toDictionary();
|
||||||
'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(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
|
|
|
@ -46,12 +46,7 @@ final class PhamePostEditController
|
||||||
return new Aphront404Response();
|
return new Aphront404Response();
|
||||||
}
|
}
|
||||||
|
|
||||||
$post = id(new PhamePost())
|
$post = PhamePost::initializePost($user, $blog);
|
||||||
->setBloggerPHID($user->getPHID())
|
|
||||||
->setBlogPHID($blog->getPHID())
|
|
||||||
->setBlog($blog)
|
|
||||||
->setDatePublished(0)
|
|
||||||
->setVisibility(PhamePost::VISIBILITY_DRAFT);
|
|
||||||
$cancel_uri = $this->getApplicationURI('/blog/view/'.$blog->getID().'/');
|
$cancel_uri = $this->getApplicationURI('/blog/view/'.$blog->getID().'/');
|
||||||
|
|
||||||
$submit_button = pht('Save Draft');
|
$submit_button = pht('Save Draft');
|
||||||
|
|
|
@ -26,6 +26,19 @@ final class PhamePost extends PhameDAO
|
||||||
|
|
||||||
private $blog;
|
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) {
|
public function setBlog(PhameBlog $blog) {
|
||||||
$this->blog = $blog;
|
$this->blog = $blog;
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -82,6 +95,22 @@ final class PhamePost extends PhameDAO
|
||||||
PhabricatorPhamePHIDTypePost::TYPECONST);
|
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() {
|
public static function getVisibilityOptionsForSelect() {
|
||||||
return array(
|
return array(
|
||||||
self::VISIBILITY_DRAFT => 'Draft: visible only to me.',
|
self::VISIBILITY_DRAFT => 'Draft: visible only to me.',
|
||||||
|
|
Loading…
Reference in a new issue