diff --git a/src/applications/ponder/controller/PonderVoteSaveController.php b/src/applications/ponder/controller/PonderVoteSaveController.php index 5becd6fc56..24ef3c4c55 100644 --- a/src/applications/ponder/controller/PonderVoteSaveController.php +++ b/src/applications/ponder/controller/PonderVoteSaveController.php @@ -1,41 +1,44 @@ kind = $data['kind']; } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $newvote = $request->getInt("vote"); $phid = $request->getStr("phid"); if (1 < $newvote || $newvote < -1) { return new Aphront400Response(); } $target = null; if ($this->kind == "question") { $target = PonderQuestionQuery::loadSingleByPHID($user, $phid); } else if ($this->kind == "answer") { - $target = PonderAnswerQuery::loadSingleByPHID($user, $phid); + $target = id(new PonderAnswerQuery()) + ->setViewer($user) + ->withPHID($phid) + ->executeOne(); } if (!$target) { return new Aphront404Response(); } $editor = id(new PonderVoteEditor()) ->setVotable($target) ->setActor($user) ->setVote($newvote) ->saveVote(); return id(new AphrontAjaxResponse())->setContent("."); } } diff --git a/src/applications/ponder/query/PonderAnswerQuery.php b/src/applications/ponder/query/PonderAnswerQuery.php index e0ba550600..608c3c62e5 100644 --- a/src/applications/ponder/query/PonderAnswerQuery.php +++ b/src/applications/ponder/query/PonderAnswerQuery.php @@ -1,112 +1,95 @@ viewer = $viewer; + return $this; + } + + public function getViewer() { + return $this->viewer; + } + + public function executeOne() { + return head($this->execute()); + } + public function withID($qid) { $this->id = $qid; return $this; } public function withPHID($phid) { $this->phid = $phid; return $this; } public function withAuthorPHID($phid) { $this->authorPHID = $phid; return $this; } public function orderByNewest($usethis) { $this->orderNewest = $usethis; return $this; } - public static function loadByAuthor($viewer, $author_phid, $offset, $count) { - if (!$viewer) { - throw new Exception("Must set viewer when calling loadByAuthor"); - } - - return id(new PonderAnswerQuery()) - ->withAuthorPHID($author_phid) - ->setOffset($offset) - ->setLimit($count) - ->orderByNewest(true) - ->execute(); - } - - public static function loadSingle($viewer, $id) { - if (!$viewer) { - throw new Exception("Must set viewer when calling loadSingle"); - } - return idx(id(new PonderAnswerQuery()) - ->withID($id) - ->execute(), $id); - } - - public static function loadSingleByPHID($viewer, $phid) { - if (!$viewer) { - throw new Exception("Must set viewer when calling loadSingle"); - } - - return array_shift(id(new PonderAnswerQuery()) - ->withPHID($phid) - ->execute()); - } - private function buildWhereClause($conn_r) { $where = array(); if ($this->id) { $where[] = qsprintf($conn_r, '(id = %d)', $this->id); } if ($this->phid) { $where[] = qsprintf($conn_r, '(phid = %s)', $this->phid); } if ($this->authorPHID) { $where[] = qsprintf($conn_r, '(authorPHID = %s)', $this->authorPHID); } return $this->formatWhereClause($where); } private function buildOrderByClause($conn_r) { $order = array(); if ($this->orderNewest) { $order[] = qsprintf($conn_r, 'id DESC'); } if (count($order) == 0) { $order[] = qsprintf($conn_r, 'id ASC'); } return ($order ? 'ORDER BY ' . implode(', ', $order) : ''); } public function execute() { $answer = new PonderAnswer(); $conn_r = $answer->establishConnection('r'); $select = qsprintf( $conn_r, 'SELECT r.* FROM %T r', $answer->getTableName()); $where = $this->buildWhereClause($conn_r); $order_by = $this->buildOrderByClause($conn_r); $limit = $this->buildLimitClause($conn_r); return $answer->loadAllFromArray( queryfx_all( $conn_r, '%Q %Q %Q %Q', $select, $where, $order_by, $limit)); } } diff --git a/src/applications/ponder/storage/PonderAnswer.php b/src/applications/ponder/storage/PonderAnswer.php index 11490b2dae..fd4b0ad988 100644 --- a/src/applications/ponder/storage/PonderAnswer.php +++ b/src/applications/ponder/storage/PonderAnswer.php @@ -1,121 +1,109 @@ question = $question; return $this; } public function getQuestion() { return $this->question; } public function setUserVote($vote) { $this->vote = $vote['data']; if (!$this->vote) { $this->vote = PonderVote::VOTE_NONE; } return $this; } public function getUserVote() { return $this->vote; } public function setComments($comments) { $this->comments = $comments; return $this; } public function getComments() { return $this->comments; } public function getConfiguration() { return array( self::CONFIG_AUX_PHID => true, ) + parent::getConfiguration(); } - public function setTitle($title) { - $this->title = $title; - if (!$this->getID()) { - $this->originalTitle = $title; - } - return $this; - } - public function generatePHID() { return PhabricatorPHID::generateNewPHID( PhabricatorPHIDConstants::PHID_TYPE_ANSW); } public function setContentSource(PhabricatorContentSource $content_source) { $this->contentSource = $content_source->serialize(); return $this; } public function getContentSource() { return PhabricatorContentSource::newFromSerialized($this->contentSource); } - public function getAnswers() { - return $this->loadRelatives(new PonderAnswer(), "questionID"); - } - public function getMarkupField() { return self::MARKUP_FIELD_CONTENT; } // Markup interface public function getMarkupFieldKey($field) { $hash = PhabricatorHash::digest($this->getMarkupText($field)); $id = $this->getID(); return "ponder:A{$id}:{$field}:{$hash}"; } public function getMarkupText($field) { return $this->getContent(); } public function newMarkupEngine($field) { return PhabricatorMarkupEngine::newPonderMarkupEngine(); } public function didMarkupText( $field, $output, PhutilMarkupEngine $engine) { return $output; } public function shouldUseMarkupCache($field) { return (bool)$this->getID(); } // votable interface public function getUserVoteEdgeType() { return PhabricatorEdgeConfig::TYPE_VOTING_USER_HAS_ANSWER; } public function getVotablePHID() { return $this->getPHID(); } }