1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +01:00

Make adding an answer to a Ponder question a QuestionTransaction operation

Summary: Ref T3578. This is currently handled in a weird way in the Answer transaction. Instead, make it a Question transaction so, e.g., viewing Question transaction history shows who added answers and when.

Test Plan: Added answers to questions in Ponder.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T3578

Differential Revision: https://secure.phabricator.com/D6603
This commit is contained in:
epriestley 2013-07-28 15:59:53 -07:00
parent e6967ed2ec
commit 8fa5944768
5 changed files with 117 additions and 9 deletions

View file

@ -14,6 +14,7 @@ final class PonderAnswerSaveController extends PonderController {
$question = id(new PonderQuestionQuery())
->setViewer($viewer)
->withIDs(array($question_id))
->needAnswers(true)
->executeOne();
if (!$question) {
return new Aphront404Response();
@ -39,21 +40,30 @@ final class PonderAnswerSaveController extends PonderController {
'ip' => $request->getRemoteAddr(),
));
$res = new PonderAnswer();
$res
->setContent($answer)
$res = id(new PonderAnswer())
->setAuthorPHID($viewer->getPHID())
->setQuestionID($question->getID())
->setContent($answer)
->setVoteCount(0)
->setQuestionID($question_id)
->setContentSource($content_source);
id(new PonderAnswerEditor())
$xactions = array();
$xactions[] = id(new PonderQuestionTransaction())
->setTransactionType(PonderQuestionTransaction::TYPE_ANSWERS)
->setNewValue(
array(
'+' => array(
array('answer' => $res),
),
));
$editor = id(new PonderQuestionEditor())
->setActor($viewer)
->setQuestion($question)
->setAnswer($res)
->saveAnswer();
->setContentSourceFromRequest($request);
$editor->applyTransactions($question, $xactions);
return id(new AphrontRedirectResponse())->setURI(
id(new PhutilURI('/Q'. $question->getID())));
id(new PhutilURI('/Q'.$question->getID())));
}
}

View file

@ -3,15 +3,52 @@
final class PonderQuestionEditor
extends PhabricatorApplicationTransactionEditor {
protected function shouldApplyInitialEffects(
PhabricatorLiskDAO $object,
array $xactions) {
foreach ($xactions as $xaction) {
switch ($xaction->getTransactionType()) {
case PonderQuestionTransaction::TYPE_ANSWERS:
return true;
}
}
return false;
}
protected function applyInitialEffects(
PhabricatorLiskDAO $object,
array $xactions) {
foreach ($xactions as $xaction) {
switch ($xaction->getTransactionType()) {
case PonderQuestionTransaction::TYPE_ANSWERS:
$new_value = $xaction->getNewValue();
$new = idx($new_value, '+', array());
foreach ($new as $new_answer) {
$answer = idx($new_answer, 'answer');
if (!$answer) {
continue;
}
$answer->save();
}
break;
}
}
}
public function getTransactionTypes() {
$types = parent::getTransactionTypes();
$types[] = PhabricatorTransactions::TYPE_COMMENT;
$types[] = PonderQuestionTransaction::TYPE_TITLE;
$types[] = PonderQuestionTransaction::TYPE_CONTENT;
$types[] = PonderQuestionTransaction::TYPE_ANSWERS;
return $types;
}
protected function getCustomTransactionOldValue(
PhabricatorLiskDAO $object,
PhabricatorApplicationTransaction $xaction) {
@ -21,6 +58,8 @@ final class PonderQuestionEditor
return $object->getTitle();
case PonderQuestionTransaction::TYPE_CONTENT:
return $object->getContent();
case PonderQuestionTransaction::TYPE_ANSWERS:
return mpull($object->getAnswers(), 'getPHID');
}
}
@ -32,6 +71,22 @@ final class PonderQuestionEditor
case PonderQuestionTransaction::TYPE_TITLE:
case PonderQuestionTransaction::TYPE_CONTENT:
return $xaction->getNewValue();
case PonderQuestionTransaction::TYPE_ANSWERS:
$raw_new_value = $xaction->getNewValue();
$new_value = array();
foreach ($raw_new_value as $key => $answers) {
$phids = array();
foreach ($answers as $answer) {
$obj = idx($answer, 'answer');
if (!$answer) {
continue;
}
$phids[] = $obj->getPHID();
}
$new_value[$key] = $phids;
}
$xaction->setNewValue($new_value);
return $this->getPHIDTransactionNewValue($xaction);
}
}
@ -46,6 +101,19 @@ final class PonderQuestionEditor
case PonderQuestionTransaction::TYPE_CONTENT:
$object->setContent($xaction->getNewValue());
break;
case PonderQuestionTransaction::TYPE_ANSWERS:
$old = $xaction->getOldValue();
$new = $xaction->getNewValue();
$add = array_diff_key($new, $old);
$rem = array_diff_key($old, $new);
$count = $object->getAnswerCount();
$count += count($add);
$count -= count($rem);
$object->setAnswerCount($count);
break;
}
}

View file

@ -17,6 +17,8 @@ final class PonderQuestionQuery
const STATUS_OPEN = 'status-open';
const STATUS_CLOSED = 'status-closed';
private $needAnswers;
public function withIDs(array $ids) {
$this->ids = $ids;
return $this;
@ -42,6 +44,11 @@ final class PonderQuestionQuery
return $this;
}
public function needAnswers($need_answers) {
$this->needAnswers = $need_answers;
return $this;
}
public function setOrder($order) {
$this->order = $order;
return $this;
@ -124,6 +131,22 @@ final class PonderQuestionQuery
return $question->loadAllFromArray($data);
}
public function willFilterPage(array $questions) {
if ($this->needAnswers) {
$answers = id(new PonderAnswerQuery())
->setViewer($this->getViewer())
->withQuestionIDs(mpull($questions, 'getID'))
->execute();
$answers = mgroup($answers, 'getQuestionID');
foreach ($questions as $question) {
$question->attachAnswers(idx($answers, $question->getID(), array()));
}
}
return $questions;
}
private function buildJoinsClause(AphrontDatabaseConnection $conn_r) {
$joins = array();

View file

@ -114,6 +114,12 @@ final class PonderQuestion extends PonderDAO
return $this->comments;
}
public function attachAnswers(array $answers) {
assert_instances_of($answers, 'PonderAnswer');
$this->answers = $answers;
return $this;
}
public function getAnswers() {
return $this->answers;
}

View file

@ -5,6 +5,7 @@ final class PonderQuestionTransaction
const TYPE_TITLE = 'ponder.question:question';
const TYPE_CONTENT = 'ponder.question:content';
const TYPE_ANSWERS = 'ponder.question:answer';
public function getApplicationName() {
return 'ponder';