Path: blob/master/src/applications/differential/controller/DifferentialInlineCommentEditController.php
12256 views
<?php12final class DifferentialInlineCommentEditController3extends PhabricatorInlineCommentController {45protected function newInlineCommentQuery() {6return new DifferentialDiffInlineCommentQuery();7}89protected function newContainerObject() {10return $this->loadRevision();11}1213private function getRevisionID() {14return $this->getRequest()->getURIData('id');15}1617private function loadRevision() {18$viewer = $this->getViewer();19$revision_id = $this->getRevisionID();2021$revision = id(new DifferentialRevisionQuery())22->setViewer($viewer)23->withIDs(array($revision_id))24->executeOne();25if (!$revision) {26throw new Exception(pht('Invalid revision ID "%s".', $revision_id));27}2829return $revision;30}3132protected function createComment() {33// Verify revision and changeset correspond to actual objects, and are34// connected to one another.35$changeset_id = $this->getChangesetID();36$viewer = $this->getViewer();3738$revision = $this->loadRevision();3940$changeset = id(new DifferentialChangesetQuery())41->setViewer($viewer)42->withIDs(array($changeset_id))43->executeOne();44if (!$changeset) {45throw new Exception(46pht(47'Invalid changeset ID "%s"!',48$changeset_id));49}5051$diff = $changeset->getDiff();52if ($diff->getRevisionID() != $revision->getID()) {53throw new Exception(54pht(55'Changeset ID "%s" is part of diff ID "%s", but that diff '.56'is attached to revision "%s", not revision "%s".',57$changeset_id,58$diff->getID(),59$diff->getRevisionID(),60$revision->getID()));61}6263return id(new DifferentialInlineComment())64->setRevision($revision)65->setChangesetID($changeset_id);66}6768protected function loadCommentForDone($id) {69$viewer = $this->getViewer();7071$inline = $this->loadCommentByID($id);72if (!$inline) {73throw new Exception(pht('Unable to load inline "%d".', $id));74}7576$changeset = id(new DifferentialChangesetQuery())77->setViewer($viewer)78->withIDs(array($inline->getChangesetID()))79->executeOne();80if (!$changeset) {81throw new Exception(pht('Unable to load changeset.'));82}8384$diff = id(new DifferentialDiffQuery())85->setViewer($viewer)86->withIDs(array($changeset->getDiffID()))87->executeOne();88if (!$diff) {89throw new Exception(pht('Unable to load diff.'));90}9192$revision = id(new DifferentialRevisionQuery())93->setViewer($viewer)94->withIDs(array($diff->getRevisionID()))95->executeOne();96if (!$revision) {97throw new Exception(pht('Unable to load revision.'));98}99100$viewer_phid = $viewer->getPHID();101$is_owner = ($viewer_phid == $revision->getAuthorPHID());102$is_author = ($viewer_phid == $inline->getAuthorPHID());103$is_draft = ($inline->isDraft());104105if ($is_owner) {106// You own the revision, so you can mark the comment as "Done".107} else if ($is_author && $is_draft) {108// You made this comment and it's still a draft, so you can mark109// it as "Done".110} else {111throw new Exception(112pht(113'You are not the revision owner, and this is not a draft comment '.114'you authored.'));115}116117return $inline;118}119120protected function canEditInlineComment(121PhabricatorUser $viewer,122DifferentialInlineComment $inline) {123124// Only the author may edit a comment.125if ($inline->getAuthorPHID() != $viewer->getPHID()) {126return false;127}128129// Saved comments may not be edited, for now, although the schema now130// supports it.131if (!$inline->isDraft()) {132return false;133}134135// Inline must be attached to the active revision.136if ($inline->getRevisionID() != $this->getRevisionID()) {137return false;138}139140return true;141}142143protected function loadObjectOwnerPHID(144PhabricatorInlineComment $inline) {145return $this->loadRevision()->getAuthorPHID();146}147148protected function hideComments(array $ids) {149$viewer = $this->getViewer();150$table = new DifferentialHiddenComment();151$conn_w = $table->establishConnection('w');152153$sql = array();154foreach ($ids as $id) {155$sql[] = qsprintf(156$conn_w,157'(%s, %d)',158$viewer->getPHID(),159$id);160}161162queryfx(163$conn_w,164'INSERT IGNORE INTO %T (userPHID, commentID) VALUES %LQ',165$table->getTableName(),166$sql);167}168169protected function showComments(array $ids) {170$viewer = $this->getViewer();171$table = new DifferentialHiddenComment();172$conn_w = $table->establishConnection('w');173174queryfx(175$conn_w,176'DELETE FROM %T WHERE userPHID = %s AND commentID IN (%Ld)',177$table->getTableName(),178$viewer->getPHID(),179$ids);180}181182}183184185