Page MenuHomePhabricator

D8485.id20150.diff
No OneTemporary

D8485.id20150.diff

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
@@ -210,6 +210,7 @@
'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 @@
'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
--- /dev/null
+++ b/src/applications/phame/conduit/ConduitAPI_phame_createpost_Method.php
@@ -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();
+ }
+
+}
diff --git a/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php b/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php
--- a/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php
+++ b/src/applications/phame/conduit/ConduitAPI_phame_queryposts_Method.php
@@ -95,23 +95,11 @@
$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
--- a/src/applications/phame/controller/post/PhamePostEditController.php
+++ b/src/applications/phame/controller/post/PhamePostEditController.php
@@ -46,12 +46,7 @@
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
--- a/src/applications/phame/storage/PhamePost.php
+++ b/src/applications/phame/storage/PhamePost.php
@@ -26,6 +26,19 @@
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 @@
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.',

File Metadata

Mime Type
text/plain
Expires
Fri, May 10, 4:19 PM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6283702
Default Alt Text
D8485.id20150.diff (7 KB)

Event Timeline