1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-27 09:12:41 +01:00

Make PonderQuestionEditor use ApplicationTransactions

Summary:
Ref T3373. Make PonderQuestions editable and use transactions.

This temporarily disables some stuff:

  - email;
  - feed;
  - comments;
  - voting.

I'll restore those in followups and wait to land this until they're at least mostly back online.

The transactions themselves also need more string/color/icon work.

Test Plan: Created and edited questions. Viewed transactions.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T3373

Differential Revision: https://secure.phabricator.com/D6601
This commit is contained in:
epriestley 2013-07-28 15:02:18 -07:00
parent e4d0f33146
commit 5a3e3994f5
8 changed files with 231 additions and 102 deletions

View file

@ -1895,8 +1895,8 @@ phutil_register_library_map(array(
'PonderPHIDTypeQuestion' => 'applications/ponder/phid/PonderPHIDTypeQuestion.php', 'PonderPHIDTypeQuestion' => 'applications/ponder/phid/PonderPHIDTypeQuestion.php',
'PonderPostBodyView' => 'applications/ponder/view/PonderPostBodyView.php', 'PonderPostBodyView' => 'applications/ponder/view/PonderPostBodyView.php',
'PonderQuestion' => 'applications/ponder/storage/PonderQuestion.php', 'PonderQuestion' => 'applications/ponder/storage/PonderQuestion.php',
'PonderQuestionAskController' => 'applications/ponder/controller/PonderQuestionAskController.php',
'PonderQuestionDetailView' => 'applications/ponder/view/PonderQuestionDetailView.php', 'PonderQuestionDetailView' => 'applications/ponder/view/PonderQuestionDetailView.php',
'PonderQuestionEditController' => 'applications/ponder/controller/PonderQuestionEditController.php',
'PonderQuestionEditor' => 'applications/ponder/editor/PonderQuestionEditor.php', 'PonderQuestionEditor' => 'applications/ponder/editor/PonderQuestionEditor.php',
'PonderQuestionListController' => 'applications/ponder/controller/PonderQuestionListController.php', 'PonderQuestionListController' => 'applications/ponder/controller/PonderQuestionListController.php',
'PonderQuestionMailReceiver' => 'applications/ponder/mail/PonderQuestionMailReceiver.php', 'PonderQuestionMailReceiver' => 'applications/ponder/mail/PonderQuestionMailReceiver.php',
@ -1907,6 +1907,7 @@ phutil_register_library_map(array(
'PonderQuestionStatusController' => 'applications/ponder/controller/PonderQuestionStatusController.php', 'PonderQuestionStatusController' => 'applications/ponder/controller/PonderQuestionStatusController.php',
'PonderQuestionTransaction' => 'applications/ponder/storage/PonderQuestionTransaction.php', 'PonderQuestionTransaction' => 'applications/ponder/storage/PonderQuestionTransaction.php',
'PonderQuestionTransactionComment' => 'applications/ponder/storage/PonderQuestionTransactionComment.php', 'PonderQuestionTransactionComment' => 'applications/ponder/storage/PonderQuestionTransactionComment.php',
'PonderQuestionTransactionQuery' => 'applications/ponder/query/PonderQuestionTransactionQuery.php',
'PonderQuestionViewController' => 'applications/ponder/controller/PonderQuestionViewController.php', 'PonderQuestionViewController' => 'applications/ponder/controller/PonderQuestionViewController.php',
'PonderRemarkupRule' => 'applications/ponder/remarkup/PonderRemarkupRule.php', 'PonderRemarkupRule' => 'applications/ponder/remarkup/PonderRemarkupRule.php',
'PonderReplyHandler' => 'applications/ponder/mail/PonderReplyHandler.php', 'PonderReplyHandler' => 'applications/ponder/mail/PonderReplyHandler.php',
@ -4025,9 +4026,9 @@ phutil_register_library_map(array(
4 => 'PhabricatorPolicyInterface', 4 => 'PhabricatorPolicyInterface',
5 => 'PhabricatorTokenReceiverInterface', 5 => 'PhabricatorTokenReceiverInterface',
), ),
'PonderQuestionAskController' => 'PonderController',
'PonderQuestionDetailView' => 'AphrontView', 'PonderQuestionDetailView' => 'AphrontView',
'PonderQuestionEditor' => 'PhabricatorEditor', 'PonderQuestionEditController' => 'PonderController',
'PonderQuestionEditor' => 'PhabricatorApplicationTransactionEditor',
'PonderQuestionListController' => 'PonderQuestionListController' =>
array( array(
0 => 'PonderController', 0 => 'PonderController',
@ -4041,6 +4042,7 @@ phutil_register_library_map(array(
'PonderQuestionStatusController' => 'PonderController', 'PonderQuestionStatusController' => 'PonderController',
'PonderQuestionTransaction' => 'PhabricatorApplicationTransaction', 'PonderQuestionTransaction' => 'PhabricatorApplicationTransaction',
'PonderQuestionTransactionComment' => 'PhabricatorApplicationTransactionComment', 'PonderQuestionTransactionComment' => 'PhabricatorApplicationTransactionComment',
'PonderQuestionTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
'PonderQuestionViewController' => 'PonderController', 'PonderQuestionViewController' => 'PonderController',
'PonderRemarkupRule' => 'PhabricatorRemarkupRuleObject', 'PonderRemarkupRule' => 'PhabricatorRemarkupRuleObject',
'PonderReplyHandler' => 'PhabricatorMailReplyHandler', 'PonderReplyHandler' => 'PhabricatorMailReplyHandler',

View file

@ -52,7 +52,7 @@ final class PhabricatorApplicationPonder extends PhabricatorApplication {
'(?:query/(?P<queryKey>[^/]+)/)?' => 'PonderQuestionListController', '(?:query/(?P<queryKey>[^/]+)/)?' => 'PonderQuestionListController',
'answer/add/' => 'PonderAnswerSaveController', 'answer/add/' => 'PonderAnswerSaveController',
'answer/preview/' => 'PonderAnswerPreviewController', 'answer/preview/' => 'PonderAnswerPreviewController',
'question/ask/' => 'PonderQuestionAskController', 'question/edit/(?:(?P<id>\d+)/)?' => 'PonderQuestionEditController',
'question/preview/' => 'PonderQuestionPreviewController', 'question/preview/' => 'PonderQuestionPreviewController',
'question/(?P<status>open|close)/(?P<id>[1-9]\d*)/' => 'question/(?P<status>open|close)/(?P<id>[1-9]\d*)/' =>
'PonderQuestionStatusController', 'PonderQuestionStatusController',

View file

@ -23,7 +23,7 @@ abstract class PonderController extends PhabricatorController {
->addAction( ->addAction(
id(new PHUIListItemView()) id(new PHUIListItemView())
->setName(pht('Create Question')) ->setName(pht('Create Question'))
->setHref('/ponder/question/ask/') ->setHref('/ponder/question/edit/')
->setIcon('create')); ->setIcon('create'));
return $crumbs; return $crumbs;

View file

@ -1,25 +1,49 @@
<?php <?php
final class PonderQuestionAskController extends PonderController { final class PonderQuestionEditController extends PonderController {
private $id;
public function willProcessRequest(array $data) {
$this->id = idx($data, 'id');
}
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
if ($this->id) {
$question = id(new PonderQuestionQuery())
->setViewer($user)
->withIDs(array($this->id))
->requireCapabilities(
array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
))
->executeOne();
if (!$question) {
return new Aphront404Response();
}
} else {
$question = id(new PonderQuestion()) $question = id(new PonderQuestion())
->setStatus(PonderQuestionStatus::STATUS_OPEN)
->setAuthorPHID($user->getPHID()) ->setAuthorPHID($user->getPHID())
->setVoteCount(0) ->setVoteCount(0)
->setAnswerCount(0) ->setAnswerCount(0)
->setHeat(0.0); ->setHeat(0.0);
}
$v_title = $question->getTitle();
$v_content = $question->getContent();
$errors = array(); $errors = array();
$e_title = true; $e_title = true;
if ($request->isFormPost()) { if ($request->isFormPost()) {
$question->setTitle($request->getStr('title')); $v_title = $request->getStr('title');
$question->setContent($request->getStr('content')); $v_content = $request->getStr('content');
$question->setStatus(PonderQuestionStatus::STATUS_OPEN);
$len = phutil_utf8_strlen($question->getTitle()); $len = phutil_utf8_strlen($v_title);
if ($len < 1) { if ($len < 1) {
$errors[] = pht('Title must not be empty.'); $errors[] = pht('Title must not be empty.');
$e_title = pht('Required'); $e_title = pht('Required');
@ -29,17 +53,23 @@ final class PonderQuestionAskController extends PonderController {
} }
if (!$errors) { if (!$errors) {
$content_source = PhabricatorContentSource::newForSource( $template = id(new PonderQuestionTransaction());
PhabricatorContentSource::SOURCE_WEB, $xactions = array();
array(
'ip' => $request->getRemoteAddr(),
));
$question->setContentSource($content_source);
id(new PonderQuestionEditor()) $xactions[] = id(clone $template)
->setQuestion($question) ->setTransactionType(PonderQuestionTransaction::TYPE_TITLE)
->setNewValue($v_title);
$xactions[] = id(clone $template)
->setTransactionType(PonderQuestionTransaction::TYPE_CONTENT)
->setNewValue($v_content);
$editor = id(new PonderQuestionEditor())
->setActor($user) ->setActor($user)
->save(); ->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true);
$editor->applyTransactions($question, $xactions);
return id(new AphrontRedirectResponse()) return id(new AphrontRedirectResponse())
->setURI('/Q'.$question->getID()); ->setURI('/Q'.$question->getID());
@ -60,13 +90,13 @@ final class PonderQuestionAskController extends PonderController {
id(new AphrontFormTextControl()) id(new AphrontFormTextControl())
->setLabel(pht('Question')) ->setLabel(pht('Question'))
->setName('title') ->setName('title')
->setValue($question->getTitle()) ->setValue($v_title)
->setError($e_title)) ->setError($e_title))
->appendChild( ->appendChild(
id(new PhabricatorRemarkupControl()) id(new PhabricatorRemarkupControl())
->setName('content') ->setName('content')
->setID('content') ->setID('content')
->setValue($question->getContent()) ->setValue($v_content)
->setLabel(pht('Description')) ->setLabel(pht('Description'))
->setUser($user)) ->setUser($user))
->appendChild( ->appendChild(
@ -92,9 +122,21 @@ final class PonderQuestionAskController extends PonderController {
)); ));
$crumbs = $this->buildApplicationCrumbs(); $crumbs = $this->buildApplicationCrumbs();
$id = $question->getID();
if ($id) {
$crumbs->addCrumb(
id(new PhabricatorCrumbView())
->setName("Q{$id}")
->setHref("/Q{$id}"));
$crumbs->addCrumb(
id(new PhabricatorCrumbView())
->setName(pht('Edit')));
} else {
$crumbs->addCrumb( $crumbs->addCrumb(
id(new PhabricatorCrumbView()) id(new PhabricatorCrumbView())
->setName(pht('Ask Question'))); ->setName(pht('Ask Question')));
}
return $this->buildApplicationPage( return $this->buildApplicationPage(
array( array(
@ -104,9 +146,9 @@ final class PonderQuestionAskController extends PonderController {
$preview, $preview,
), ),
array( array(
'title' => pht('Ask a Question'),
'device' => true, 'device' => true,
'dust' => true, 'dust' => true,
'title' => pht('Ask a Question'),
)); ));
} }

View file

@ -41,11 +41,7 @@ final class PonderQuestionViewController extends PonderController {
$this->loadHandles($object_phids); $this->loadHandles($object_phids);
$handles = $this->getLoadedHandles(); $handles = $this->getLoadedHandles();
$detail_panel = new PonderQuestionDetailView(); $question_xactions = $this->buildQuestionTransactions($question);
$detail_panel
->setQuestion($question)
->setUser($user)
->setHandles($handles);
$responses_panel = new PonderAnswerListView(); $responses_panel = new PonderAnswerListView();
$responses_panel $responses_panel
@ -79,7 +75,7 @@ final class PonderQuestionViewController extends PonderController {
$header, $header,
$actions, $actions,
$properties, $properties,
$detail_panel, $question_xactions,
$responses_panel, $responses_panel,
$answer_add_panel $answer_add_panel
), ),
@ -92,34 +88,48 @@ final class PonderQuestionViewController extends PonderController {
private function buildActionListView(PonderQuestion $question) { private function buildActionListView(PonderQuestion $question) {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $viewer = $request->getUser();
$action_list = id(new PhabricatorActionListView()) $id = $question->getID();
->setUser($user)
$can_edit = PhabricatorPolicyFilter::hasCapability(
$viewer,
$question,
PhabricatorPolicyCapability::CAN_EDIT);
$view = id(new PhabricatorActionListView())
->setUser($request->getUser())
->setObject($question) ->setObject($question)
->setObjectURI($request->getRequestURI()); ->setObjectURI($request->getRequestURI());
if ($user->getPhid() === $question->getAuthorPhid()) { $view->addAction(
id(new PhabricatorActionView())
->setIcon('edit')
->setName(pht('Edit Question'))
->setHref($this->getApplicationURI("/question/edit/{$id}/"))
->setDisabled(!$can_edit)
->setWorkflow(!$can_edit));
if ($question->getStatus() == PonderQuestionStatus::STATUS_OPEN) { if ($question->getStatus() == PonderQuestionStatus::STATUS_OPEN) {
$name = pht("Close Question"); $name = pht("Close Question");
$icon = "delete"; $icon = "delete";
$href = "close"; $href = "close";
} else { } else {
$name = pht("Open Question"); $name = pht("Reopen Question");
$icon = "enable"; $icon = "enable";
$href = "open"; $href = "open";
} }
$action_list->addAction(
$view->addAction(
id(new PhabricatorActionView()) id(new PhabricatorActionView())
->setName($name) ->setName($name)
->setIcon($icon) ->setIcon($icon)
->setRenderAsForm(true) ->setRenderAsForm($can_edit)
->setHref( ->setWorkflow(!$can_edit)
$this->getApplicationURI( ->setDisabled(!$can_edit)
"/question/{$href}/{$this->questionID}/"))); ->setHref($this->getApplicationURI("/question/{$href}/{$id}/")));
}
return $action_list; return $view;
} }
private function buildPropertyListView( private function buildPropertyListView(
@ -142,6 +152,45 @@ final class PonderQuestionViewController extends PonderController {
pht('Created'), pht('Created'),
phabricator_datetime($question->getDateCreated(), $viewer)); phabricator_datetime($question->getDateCreated(), $viewer));
$view->invokeWillRenderEvent();
$view->addTextContent(
PhabricatorMarkupEngine::renderOneObject(
$question,
$question->getMarkupField(),
$viewer));
return $view; return $view;
} }
private function buildQuestionTransactions(PonderQuestion $question) {
$viewer = $this->getRequest()->getUser();
$xactions = id(new PonderQuestionTransactionQuery())
->setViewer($viewer)
->withObjectPHIDs(array($question->getPHID()))
->execute();
$engine = id(new PhabricatorMarkupEngine())
->setViewer($viewer);
foreach ($xactions as $xaction) {
if ($xaction->getComment()) {
$engine->addObject(
$xaction->getComment(),
PhabricatorApplicationTransactionComment::MARKUP_FIELD_COMMENT);
}
}
$engine->process();
$timeline = id(new PhabricatorApplicationTransactionView())
->setUser($viewer)
->setTransactions($xactions)
->setMarkupEngine($engine);
// TODO: Add comment form.
return $timeline;
}
} }

View file

@ -1,53 +1,76 @@
<?php <?php
final class PonderQuestionEditor extends PhabricatorEditor { final class PonderQuestionEditor
extends PhabricatorApplicationTransactionEditor {
private $question; public function getTransactionTypes() {
private $shouldEmail = true; $types = parent::getTransactionTypes();
public function setQuestion(PonderQuestion $question) { $types[] = PhabricatorTransactions::TYPE_COMMENT;
$this->question = $question; $types[] = PonderQuestionTransaction::TYPE_TITLE;
return $this; $types[] = PonderQuestionTransaction::TYPE_CONTENT;
return $types;
} }
protected function getCustomTransactionOldValue(
PhabricatorLiskDAO $object,
PhabricatorApplicationTransaction $xaction) {
public function setShouldEmail($se) { switch ($xaction->getTransactionType()) {
$this->shouldEmail = $se; case PonderQuestionTransaction::TYPE_TITLE:
return $this; return $object->getTitle();
} case PonderQuestionTransaction::TYPE_CONTENT:
return $object->getContent();
public function save() {
$actor = $this->requireActor();
if (!$this->question) {
throw new Exception("Must set question before saving it");
}
$question = $this->question;
$question->save();
$question->attachRelated();
id(new PhabricatorSearchIndexer())
->indexDocumentByPHID($question->getPHID());
// subscribe author and @mentions
$subeditor = id(new PhabricatorSubscriptionsEditor())
->setObject($question)
->setActor($actor);
$subeditor->subscribeExplicit(array($question->getAuthorPHID()));
$content = $question->getContent();
$at_mention_phids = PhabricatorMarkupEngine::extractPHIDsFromMentions(
array($content));
$subeditor->subscribeImplicit($at_mention_phids);
$subeditor->save();
if ($this->shouldEmail && $at_mention_phids) {
id(new PonderMentionMail(
$question,
$question,
$actor))
->setToPHIDs($at_mention_phids)
->send();
} }
} }
protected function getCustomTransactionNewValue(
PhabricatorLiskDAO $object,
PhabricatorApplicationTransaction $xaction) {
switch ($xaction->getTransactionType()) {
case PonderQuestionTransaction::TYPE_TITLE:
case PonderQuestionTransaction::TYPE_CONTENT:
return $xaction->getNewValue();
}
}
protected function applyCustomInternalTransaction(
PhabricatorLiskDAO $object,
PhabricatorApplicationTransaction $xaction) {
switch ($xaction->getTransactionType()) {
case PonderQuestionTransaction::TYPE_TITLE:
$object->setTitle($xaction->getNewValue());
break;
case PonderQuestionTransaction::TYPE_CONTENT:
$object->setContent($xaction->getNewValue());
break;
}
}
protected function applyCustomExternalTransaction(
PhabricatorLiskDAO $object,
PhabricatorApplicationTransaction $xaction) {
return;
}
protected function mergeTransactions(
PhabricatorApplicationTransaction $u,
PhabricatorApplicationTransaction $v) {
$type = $u->getTransactionType();
switch ($type) {
case PonderQuestionTransaction::TYPE_TITLE:
case PonderQuestionTransaction::TYPE_CONTENT:
return $v;
}
return parent::mergeTransactions($u, $v);
}
// TODO: Feed support
// TODO: Mail support
// TODO: Add/remove answers
} }

View file

@ -0,0 +1,10 @@
<?php
final class PonderQuestionTransactionQuery
extends PhabricatorApplicationTransactionQuery {
protected function getTemplateApplicationTransaction() {
return new PonderQuestionTransaction();
}
}

View file

@ -3,6 +3,9 @@
final class PonderQuestionTransaction final class PonderQuestionTransaction
extends PhabricatorApplicationTransaction { extends PhabricatorApplicationTransaction {
const TYPE_TITLE = 'ponder.question:question';
const TYPE_CONTENT = 'ponder.question:content';
public function getApplicationName() { public function getApplicationName() {
return 'ponder'; return 'ponder';
} }