diff --git a/src/applications/differential/controller/DifferentialInlineCommentEditController.php b/src/applications/differential/controller/DifferentialInlineCommentEditController.php index deba6af01b..9e1f28670f 100644 --- a/src/applications/differential/controller/DifferentialInlineCommentEditController.php +++ b/src/applications/differential/controller/DifferentialInlineCommentEditController.php @@ -1,191 +1,206 @@ getRequest()->getURIData('id'); } private function loadRevision() { $viewer = $this->getViewer(); $revision_id = $this->getRevisionID(); $revision = id(new DifferentialRevisionQuery()) ->setViewer($viewer) ->withIDs(array($revision_id)) ->executeOne(); if (!$revision) { throw new Exception(pht('Invalid revision ID "%s".', $revision_id)); } return $revision; } protected function createComment() { // Verify revision and changeset correspond to actual objects. $changeset_id = $this->getChangesetID(); $revision = $this->loadRevision(); if (!id(new DifferentialChangeset())->load($changeset_id)) { throw new Exception(pht('Invalid changeset ID!')); } return id(new DifferentialInlineComment()) ->setRevision($revision) ->setChangesetID($changeset_id); } protected function loadComment($id) { return id(new DifferentialInlineCommentQuery()) ->setViewer($this->getViewer()) ->withIDs(array($id)) ->withDeletedDrafts(true) ->needHidden(true) ->executeOne(); } protected function loadCommentByPHID($phid) { return id(new DifferentialInlineCommentQuery()) ->setViewer($this->getViewer()) ->withPHIDs(array($phid)) ->withDeletedDrafts(true) ->needHidden(true) ->executeOne(); } protected function loadCommentForEdit($id) { $request = $this->getRequest(); $user = $request->getUser(); $inline = $this->loadComment($id); if (!$this->canEditInlineComment($user, $inline)) { throw new Exception(pht('That comment is not editable!')); } return $inline; } protected function loadCommentForDone($id) { $request = $this->getRequest(); $viewer = $request->getUser(); $inline = $this->loadComment($id); if (!$inline) { throw new Exception(pht('Unable to load inline "%d".', $id)); } $changeset = id(new DifferentialChangesetQuery()) ->setViewer($viewer) ->withIDs(array($inline->getChangesetID())) ->executeOne(); if (!$changeset) { throw new Exception(pht('Unable to load changeset.')); } $diff = id(new DifferentialDiffQuery()) ->setViewer($viewer) ->withIDs(array($changeset->getDiffID())) ->executeOne(); if (!$diff) { throw new Exception(pht('Unable to load diff.')); } $revision = id(new DifferentialRevisionQuery()) ->setViewer($viewer) ->withIDs(array($diff->getRevisionID())) ->executeOne(); if (!$revision) { throw new Exception(pht('Unable to load revision.')); } if ($revision->getAuthorPHID() !== $viewer->getPHID()) { throw new Exception(pht('You are not the revision owner.')); } return $inline; } private function canEditInlineComment( PhabricatorUser $user, DifferentialInlineComment $inline) { // Only the author may edit a comment. if ($inline->getAuthorPHID() != $user->getPHID()) { return false; } // Saved comments may not be edited, for now, although the schema now // supports it. if (!$inline->isDraft()) { return false; } // Inline must be attached to the active revision. if ($inline->getRevisionID() != $this->getRevisionID()) { return false; } return true; } protected function deleteComment(PhabricatorInlineCommentInterface $inline) { $inline->openTransaction(); + + $inline->setIsDeleted(1)->save(); DifferentialDraft::deleteHasDraft( $inline->getAuthorPHID(), $inline->getRevisionPHID(), $inline->getPHID()); - $inline->delete(); + + $inline->saveTransaction(); + } + + protected function undeleteComment( + PhabricatorInlineCommentInterface $inline) { + $inline->openTransaction(); + + $inline->setIsDeleted(0)->save(); + DifferentialDraft::markHasDraft( + $inline->getAuthorPHID(), + $inline->getRevisionPHID(), + $inline->getPHID()); + $inline->saveTransaction(); } protected function saveComment(PhabricatorInlineCommentInterface $inline) { $inline->openTransaction(); $inline->save(); DifferentialDraft::markHasDraft( $inline->getAuthorPHID(), $inline->getRevisionPHID(), $inline->getPHID()); $inline->saveTransaction(); } protected function loadObjectOwnerPHID( PhabricatorInlineCommentInterface $inline) { return $this->loadRevision()->getAuthorPHID(); } protected function hideComments(array $ids) { $viewer = $this->getViewer(); $table = new DifferentialHiddenComment(); $conn_w = $table->establishConnection('w'); $sql = array(); foreach ($ids as $id) { $sql[] = qsprintf( $conn_w, '(%s, %d)', $viewer->getPHID(), $id); } queryfx( $conn_w, 'INSERT IGNORE INTO %T (userPHID, commentID) VALUES %Q', $table->getTableName(), implode(', ', $sql)); } protected function showComments(array $ids) { $viewer = $this->getViewer(); $table = new DifferentialHiddenComment(); $conn_w = $table->establishConnection('w'); queryfx( $conn_w, 'DELETE FROM %T WHERE userPHID = %s AND commentID IN (%Ld)', $table->getTableName(), $viewer->getPHID(), $ids); } } diff --git a/src/applications/diffusion/controller/DiffusionInlineCommentController.php b/src/applications/diffusion/controller/DiffusionInlineCommentController.php index 09f5f06ad5..8af9a1cd65 100644 --- a/src/applications/diffusion/controller/DiffusionInlineCommentController.php +++ b/src/applications/diffusion/controller/DiffusionInlineCommentController.php @@ -1,124 +1,129 @@ getRequest()->getURIData('phid'); } private function loadCommit() { $viewer = $this->getViewer(); $commit_phid = $this->getCommitPHID(); $commit = id(new DiffusionCommitQuery()) ->setViewer($viewer) ->withPHIDs(array($commit_phid)) ->executeOne(); if (!$commit) { throw new Exception(pht('Invalid commit PHID "%s"!', $commit_phid)); } return $commit; } protected function createComment() { $commit = $this->loadCommit(); // TODO: Write a real PathQuery object? $path_id = $this->getChangesetID(); $path = queryfx_one( id(new PhabricatorRepository())->establishConnection('r'), 'SELECT path FROM %T WHERE id = %d', PhabricatorRepository::TABLE_PATH, $path_id); if (!$path) { throw new Exception(pht('Invalid path ID!')); } return id(new PhabricatorAuditInlineComment()) ->setCommitPHID($commit->getPHID()) ->setPathID($path_id); } protected function loadComment($id) { return PhabricatorAuditInlineComment::loadID($id); } protected function loadCommentByPHID($phid) { return PhabricatorAuditInlineComment::loadPHID($phid); } protected function loadCommentForEdit($id) { $request = $this->getRequest(); $user = $request->getUser(); $inline = $this->loadComment($id); if (!$this->canEditInlineComment($user, $inline)) { throw new Exception(pht('That comment is not editable!')); } return $inline; } protected function loadCommentForDone($id) { $request = $this->getRequest(); $viewer = $request->getUser(); $inline = $this->loadComment($id); if (!$inline) { throw new Exception(pht('Failed to load comment "%d".', $id)); } $commit = id(new DiffusionCommitQuery()) ->setViewer($viewer) ->withPHIDs(array($inline->getCommitPHID())) ->executeOne(); if (!$commit) { throw new Exception(pht('Failed to load commit.')); } if ((!$commit->getAuthorPHID()) || ($commit->getAuthorPHID() != $viewer->getPHID())) { throw new Exception(pht('You can not mark this comment as complete.')); } return $inline; } private function canEditInlineComment( PhabricatorUser $user, PhabricatorAuditInlineComment $inline) { // Only the author may edit a comment. if ($inline->getAuthorPHID() != $user->getPHID()) { return false; } // Saved comments may not be edited. if ($inline->getAuditCommentID()) { return false; } // Inline must be attached to the active revision. if ($inline->getCommitPHID() != $this->getCommitPHID()) { return false; } return true; } protected function deleteComment(PhabricatorInlineCommentInterface $inline) { - return $inline->delete(); + $inline->setIsDeleted(1)->save(); + } + + protected function undeleteComment( + PhabricatorInlineCommentInterface $inline) { + $inline->setIsDeleted(0)->save(); } protected function saveComment(PhabricatorInlineCommentInterface $inline) { return $inline->save(); } protected function loadObjectOwnerPHID( PhabricatorInlineCommentInterface $inline) { return $this->loadCommit()->getAuthorPHID(); } } diff --git a/src/infrastructure/diff/PhabricatorInlineCommentController.php b/src/infrastructure/diff/PhabricatorInlineCommentController.php index 343319b3d8..fd847b5aca 100644 --- a/src/infrastructure/diff/PhabricatorInlineCommentController.php +++ b/src/infrastructure/diff/PhabricatorInlineCommentController.php @@ -1,382 +1,389 @@ commentID; } public function getOperation() { return $this->operation; } public function getCommentText() { return $this->commentText; } public function getLineLength() { return $this->lineLength; } public function getLineNumber() { return $this->lineNumber; } public function getIsOnRight() { return $this->isOnRight; } public function getChangesetID() { return $this->changesetID; } public function getIsNewFile() { return $this->isNewFile; } public function setRenderer($renderer) { $this->renderer = $renderer; return $this; } public function getRenderer() { return $this->renderer; } public function setReplyToCommentPHID($phid) { $this->replyToCommentPHID = $phid; return $this; } public function getReplyToCommentPHID() { return $this->replyToCommentPHID; } public function processRequest() { $request = $this->getRequest(); $user = $request->getUser(); $this->readRequestParameters(); $op = $this->getOperation(); switch ($op) { case 'hide': case 'show': if (!$request->validateCSRF()) { return new Aphront404Response(); } $ids = $request->getStrList('ids'); if ($ids) { if ($op == 'hide') { $this->hideComments($ids); } else { $this->showComments($ids); } } return id(new AphrontAjaxResponse())->setContent(array()); case 'done': if (!$request->validateCSRF()) { return new Aphront404Response(); } $inline = $this->loadCommentForDone($this->getCommentID()); $is_draft_state = false; switch ($inline->getFixedState()) { case PhabricatorInlineCommentInterface::STATE_DRAFT: $next_state = PhabricatorInlineCommentInterface::STATE_UNDONE; break; case PhabricatorInlineCommentInterface::STATE_UNDRAFT: $next_state = PhabricatorInlineCommentInterface::STATE_DONE; break; case PhabricatorInlineCommentInterface::STATE_DONE: $next_state = PhabricatorInlineCommentInterface::STATE_UNDRAFT; $is_draft_state = true; break; default: case PhabricatorInlineCommentInterface::STATE_UNDONE: $next_state = PhabricatorInlineCommentInterface::STATE_DRAFT; $is_draft_state = true; break; } $inline->setFixedState($next_state)->save(); return id(new AphrontAjaxResponse()) ->setContent( array( 'draftState' => $is_draft_state, )); case 'delete': case 'undelete': case 'refdelete': if (!$request->validateCSRF()) { return new Aphront404Response(); } // NOTE: For normal deletes, we just process the delete immediately // and show an "Undo" action. For deletes by reference from the // preview ("refdelete"), we prompt first (because the "Undo" may // not draw, or may not be easy to locate). if ($op == 'refdelete') { if (!$request->isFormPost()) { return $this->newDialog() ->setTitle(pht('Really delete comment?')) ->addHiddenInput('id', $this->getCommentID()) ->addHiddenInput('op', $op) ->appendParagraph(pht('Delete this inline comment?')) ->addCancelButton('#') ->addSubmitButton(pht('Delete')); } } $is_delete = ($op == 'delete' || $op == 'refdelete'); $inline = $this->loadCommentForEdit($this->getCommentID()); - $inline->setIsDeleted((int)$is_delete)->save(); + + if ($is_delete) { + $this->deleteComment($inline); + } else { + $this->undeleteComment($inline); + } return $this->buildEmptyResponse(); case 'edit': $inline = $this->loadCommentForEdit($this->getCommentID()); $text = $this->getCommentText(); if ($request->isFormPost()) { if (strlen($text)) { $inline->setContent($text); $this->saveComment($inline); return $this->buildRenderedCommentResponse( $inline, $this->getIsOnRight()); } else { $this->deleteComment($inline); return $this->buildEmptyResponse(); } } $edit_dialog = $this->buildEditDialog(); $edit_dialog->setTitle(pht('Edit Inline Comment')); $edit_dialog->addHiddenInput('id', $this->getCommentID()); $edit_dialog->addHiddenInput('op', 'edit'); $edit_dialog->appendChild( $this->renderTextArea( nonempty($text, $inline->getContent()))); $view = $this->buildScaffoldForView($edit_dialog); return id(new AphrontAjaxResponse()) ->setContent($view->render()); case 'create': $text = $this->getCommentText(); if (!$request->isFormPost() || !strlen($text)) { return $this->buildEmptyResponse(); } $inline = $this->createComment() ->setChangesetID($this->getChangesetID()) ->setAuthorPHID($user->getPHID()) ->setLineNumber($this->getLineNumber()) ->setLineLength($this->getLineLength()) ->setIsNewFile($this->getIsNewFile()) ->setContent($text); if ($this->getReplyToCommentPHID()) { $inline->setReplyToCommentPHID($this->getReplyToCommentPHID()); } $this->saveComment($inline); return $this->buildRenderedCommentResponse( $inline, $this->getIsOnRight()); case 'reply': default: $edit_dialog = $this->buildEditDialog(); if ($this->getOperation() == 'reply') { $edit_dialog->setTitle(pht('Reply to Inline Comment')); } else { $edit_dialog->setTitle(pht('New Inline Comment')); } // NOTE: We read the values from the client (the display values), not // the values from the database (the original values) when replying. // In particular, when replying to a ghost comment which was moved // across diffs and then moved backward to the most recent visible // line, we want to reply on the display line (which exists), not on // the comment's original line (which may not exist in this changeset). $is_new = $this->getIsNewFile(); $number = $this->getLineNumber(); $length = $this->getLineLength(); $edit_dialog->addHiddenInput('op', 'create'); $edit_dialog->addHiddenInput('is_new', $is_new); $edit_dialog->addHiddenInput('number', $number); $edit_dialog->addHiddenInput('length', $length); $text_area = $this->renderTextArea($this->getCommentText()); $edit_dialog->appendChild($text_area); $view = $this->buildScaffoldForView($edit_dialog); return id(new AphrontAjaxResponse()) ->setContent($view->render()); } } private function readRequestParameters() { $request = $this->getRequest(); // NOTE: This isn't necessarily a DifferentialChangeset ID, just an // application identifier for the changeset. In Diffusion, it's a Path ID. $this->changesetID = $request->getInt('changesetID'); $this->isNewFile = (int)$request->getBool('is_new'); $this->isOnRight = $request->getBool('on_right'); $this->lineNumber = $request->getInt('number'); $this->lineLength = $request->getInt('length'); $this->commentText = $request->getStr('text'); $this->commentID = $request->getInt('id'); $this->operation = $request->getStr('op'); $this->renderer = $request->getStr('renderer'); $this->replyToCommentPHID = $request->getStr('replyToCommentPHID'); if ($this->getReplyToCommentPHID()) { $reply_phid = $this->getReplyToCommentPHID(); $reply_comment = $this->loadCommentByPHID($reply_phid); if (!$reply_comment) { throw new Exception( pht('Failed to load comment "%s".', $reply_phid)); } // NOTE: It's fine to reply to a comment from a different changeset, so // the reply comment may not appear on the same changeset that the new // comment appears on. This is expected in the case of ghost comments. // We currently put the new comment on the visible changeset, not the // original comment's changeset. $this->isNewFile = $reply_comment->getIsNewFile(); } } private function buildEditDialog() { $request = $this->getRequest(); $user = $request->getUser(); $edit_dialog = id(new PHUIDiffInlineCommentEditView()) ->setUser($user) ->setSubmitURI($request->getRequestURI()) ->setIsOnRight($this->getIsOnRight()) ->setIsNewFile($this->getIsNewFile()) ->setNumber($this->getLineNumber()) ->setLength($this->getLineLength()) ->setRenderer($this->getRenderer()) ->setReplyToCommentPHID($this->getReplyToCommentPHID()) ->setChangesetID($this->getChangesetID()); return $edit_dialog; } private function buildEmptyResponse() { return id(new AphrontAjaxResponse()) ->setContent( array( 'markup' => '', )); } private function buildRenderedCommentResponse( PhabricatorInlineCommentInterface $inline, $on_right) { $request = $this->getRequest(); $user = $request->getUser(); $engine = new PhabricatorMarkupEngine(); $engine->setViewer($user); $engine->addObject( $inline, PhabricatorInlineCommentInterface::MARKUP_FIELD_BODY); $engine->process(); $phids = array($user->getPHID()); $handles = $this->loadViewerHandles($phids); $object_owner_phid = $this->loadObjectOwnerPHID($inline); $view = id(new PHUIDiffInlineCommentDetailView()) ->setUser($user) ->setInlineComment($inline) ->setIsOnRight($on_right) ->setMarkupEngine($engine) ->setHandles($handles) ->setEditable(true) ->setCanMarkDone(false) ->setObjectOwnerPHID($object_owner_phid); $view = $this->buildScaffoldForView($view); return id(new AphrontAjaxResponse()) ->setContent( array( 'inlineCommentID' => $inline->getID(), 'markup' => $view->render(), )); } private function renderTextArea($text) { return id(new PhabricatorRemarkupControl()) ->setUser($this->getRequest()->getUser()) ->setSigil('differential-inline-comment-edit-textarea') ->setName('text') ->setValue($text) ->setDisableFullScreen(true); } private function buildScaffoldForView(PHUIDiffInlineCommentView $view) { $renderer = DifferentialChangesetHTMLRenderer::getHTMLRendererByKey( $this->getRenderer()); $view = $renderer->getRowScaffoldForInline($view); return id(new PHUIDiffInlineCommentTableScaffold()) ->addRowScaffold($view); } }