1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-23 15:22: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',
'PonderPostBodyView' => 'applications/ponder/view/PonderPostBodyView.php',
'PonderQuestion' => 'applications/ponder/storage/PonderQuestion.php',
'PonderQuestionAskController' => 'applications/ponder/controller/PonderQuestionAskController.php',
'PonderQuestionDetailView' => 'applications/ponder/view/PonderQuestionDetailView.php',
'PonderQuestionEditController' => 'applications/ponder/controller/PonderQuestionEditController.php',
'PonderQuestionEditor' => 'applications/ponder/editor/PonderQuestionEditor.php',
'PonderQuestionListController' => 'applications/ponder/controller/PonderQuestionListController.php',
'PonderQuestionMailReceiver' => 'applications/ponder/mail/PonderQuestionMailReceiver.php',
@ -1907,6 +1907,7 @@ phutil_register_library_map(array(
'PonderQuestionStatusController' => 'applications/ponder/controller/PonderQuestionStatusController.php',
'PonderQuestionTransaction' => 'applications/ponder/storage/PonderQuestionTransaction.php',
'PonderQuestionTransactionComment' => 'applications/ponder/storage/PonderQuestionTransactionComment.php',
'PonderQuestionTransactionQuery' => 'applications/ponder/query/PonderQuestionTransactionQuery.php',
'PonderQuestionViewController' => 'applications/ponder/controller/PonderQuestionViewController.php',
'PonderRemarkupRule' => 'applications/ponder/remarkup/PonderRemarkupRule.php',
'PonderReplyHandler' => 'applications/ponder/mail/PonderReplyHandler.php',
@ -4025,9 +4026,9 @@ phutil_register_library_map(array(
4 => 'PhabricatorPolicyInterface',
5 => 'PhabricatorTokenReceiverInterface',
),
'PonderQuestionAskController' => 'PonderController',
'PonderQuestionDetailView' => 'AphrontView',
'PonderQuestionEditor' => 'PhabricatorEditor',
'PonderQuestionEditController' => 'PonderController',
'PonderQuestionEditor' => 'PhabricatorApplicationTransactionEditor',
'PonderQuestionListController' =>
array(
0 => 'PonderController',
@ -4041,6 +4042,7 @@ phutil_register_library_map(array(
'PonderQuestionStatusController' => 'PonderController',
'PonderQuestionTransaction' => 'PhabricatorApplicationTransaction',
'PonderQuestionTransactionComment' => 'PhabricatorApplicationTransactionComment',
'PonderQuestionTransactionQuery' => 'PhabricatorApplicationTransactionQuery',
'PonderQuestionViewController' => 'PonderController',
'PonderRemarkupRule' => 'PhabricatorRemarkupRuleObject',
'PonderReplyHandler' => 'PhabricatorMailReplyHandler',

View file

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

View file

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

View file

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

View file

@ -41,11 +41,7 @@ final class PonderQuestionViewController extends PonderController {
$this->loadHandles($object_phids);
$handles = $this->getLoadedHandles();
$detail_panel = new PonderQuestionDetailView();
$detail_panel
->setQuestion($question)
->setUser($user)
->setHandles($handles);
$question_xactions = $this->buildQuestionTransactions($question);
$responses_panel = new PonderAnswerListView();
$responses_panel
@ -79,7 +75,7 @@ final class PonderQuestionViewController extends PonderController {
$header,
$actions,
$properties,
$detail_panel,
$question_xactions,
$responses_panel,
$answer_add_panel
),
@ -92,34 +88,48 @@ final class PonderQuestionViewController extends PonderController {
private function buildActionListView(PonderQuestion $question) {
$request = $this->getRequest();
$user = $request->getUser();
$viewer = $request->getUser();
$action_list = id(new PhabricatorActionListView())
->setUser($user)
$id = $question->getID();
$can_edit = PhabricatorPolicyFilter::hasCapability(
$viewer,
$question,
PhabricatorPolicyCapability::CAN_EDIT);
$view = id(new PhabricatorActionListView())
->setUser($request->getUser())
->setObject($question)
->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) {
$name = pht("Close Question");
$icon = "delete";
$href = "close";
} else {
$name = pht("Open Question");
$name = pht("Reopen Question");
$icon = "enable";
$href = "open";
}
$action_list->addAction(
$view->addAction(
id(new PhabricatorActionView())
->setName($name)
->setIcon($icon)
->setRenderAsForm(true)
->setHref(
$this->getApplicationURI(
"/question/{$href}/{$this->questionID}/")));
}
->setRenderAsForm($can_edit)
->setWorkflow(!$can_edit)
->setDisabled(!$can_edit)
->setHref($this->getApplicationURI("/question/{$href}/{$id}/")));
return $action_list;
return $view;
}
private function buildPropertyListView(
@ -142,6 +152,45 @@ final class PonderQuestionViewController extends PonderController {
pht('Created'),
phabricator_datetime($question->getDateCreated(), $viewer));
$view->invokeWillRenderEvent();
$view->addTextContent(
PhabricatorMarkupEngine::renderOneObject(
$question,
$question->getMarkupField(),
$viewer));
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
final class PonderQuestionEditor extends PhabricatorEditor {
final class PonderQuestionEditor
extends PhabricatorApplicationTransactionEditor {
private $question;
private $shouldEmail = true;
public function getTransactionTypes() {
$types = parent::getTransactionTypes();
public function setQuestion(PonderQuestion $question) {
$this->question = $question;
return $this;
$types[] = PhabricatorTransactions::TYPE_COMMENT;
$types[] = PonderQuestionTransaction::TYPE_TITLE;
$types[] = PonderQuestionTransaction::TYPE_CONTENT;
return $types;
}
protected function getCustomTransactionOldValue(
PhabricatorLiskDAO $object,
PhabricatorApplicationTransaction $xaction) {
public function setShouldEmail($se) {
$this->shouldEmail = $se;
return $this;
}
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();
switch ($xaction->getTransactionType()) {
case PonderQuestionTransaction::TYPE_TITLE:
return $object->getTitle();
case PonderQuestionTransaction::TYPE_CONTENT:
return $object->getContent();
}
}
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
extends PhabricatorApplicationTransaction {
const TYPE_TITLE = 'ponder.question:question';
const TYPE_CONTENT = 'ponder.question:content';
public function getApplicationName() {
return 'ponder';
}