@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 183 lines 4.9 kB view raw
1<?php 2 3final class DifferentialInlineCommentEditController 4 extends PhabricatorInlineCommentController { 5 6 protected function newInlineCommentQuery() { 7 return new DifferentialDiffInlineCommentQuery(); 8 } 9 10 protected function newContainerObject() { 11 return $this->loadRevision(); 12 } 13 14 private function getRevisionID() { 15 return $this->getRequest()->getURIData('id'); 16 } 17 18 private function loadRevision() { 19 $viewer = $this->getViewer(); 20 $revision_id = $this->getRevisionID(); 21 22 $revision = id(new DifferentialRevisionQuery()) 23 ->setViewer($viewer) 24 ->withIDs(array($revision_id)) 25 ->executeOne(); 26 if (!$revision) { 27 throw new Exception(pht('Invalid revision ID "%s".', $revision_id)); 28 } 29 30 return $revision; 31 } 32 33 protected function createComment() { 34 // Verify revision and changeset correspond to actual objects, and are 35 // connected to one another. 36 $changeset_id = $this->getChangesetID(); 37 $viewer = $this->getViewer(); 38 39 $revision = $this->loadRevision(); 40 41 $changeset = id(new DifferentialChangesetQuery()) 42 ->setViewer($viewer) 43 ->withIDs(array($changeset_id)) 44 ->executeOne(); 45 if (!$changeset) { 46 throw new Exception( 47 pht( 48 'Invalid changeset ID "%s"!', 49 $changeset_id)); 50 } 51 52 $diff = $changeset->getDiff(); 53 if ($diff->getRevisionID() != $revision->getID()) { 54 throw new Exception( 55 pht( 56 'Changeset ID "%s" is part of diff ID "%s", but that diff '. 57 'is attached to revision "%s", not revision "%s".', 58 $changeset_id, 59 $diff->getID(), 60 $diff->getRevisionID(), 61 $revision->getID())); 62 } 63 64 return id(new DifferentialInlineComment()) 65 ->setRevision($revision) 66 ->setChangesetID($changeset_id); 67 } 68 69 protected function loadCommentForDone($id) { 70 $viewer = $this->getViewer(); 71 72 $inline = $this->loadCommentByID($id); 73 if (!$inline) { 74 throw new Exception(pht('Unable to load inline "%d".', $id)); 75 } 76 77 $changeset = id(new DifferentialChangesetQuery()) 78 ->setViewer($viewer) 79 ->withIDs(array($inline->getChangesetID())) 80 ->executeOne(); 81 if (!$changeset) { 82 throw new Exception(pht('Unable to load changeset.')); 83 } 84 85 $diff = id(new DifferentialDiffQuery()) 86 ->setViewer($viewer) 87 ->withIDs(array($changeset->getDiffID())) 88 ->executeOne(); 89 if (!$diff) { 90 throw new Exception(pht('Unable to load diff.')); 91 } 92 93 $revision = id(new DifferentialRevisionQuery()) 94 ->setViewer($viewer) 95 ->withIDs(array($diff->getRevisionID())) 96 ->executeOne(); 97 if (!$revision) { 98 throw new Exception(pht('Unable to load revision.')); 99 } 100 101 $viewer_phid = $viewer->getPHID(); 102 $is_owner = ($viewer_phid == $revision->getAuthorPHID()); 103 $is_author = ($viewer_phid == $inline->getAuthorPHID()); 104 $is_draft = ($inline->isDraft()); 105 106 if ($is_owner) { 107 // You own the revision, so you can mark the comment as "Done". 108 } else if ($is_author && $is_draft) { 109 // You made this comment and it's still a draft, so you can mark 110 // it as "Done". 111 } else { 112 throw new Exception( 113 pht( 114 'You are not the revision owner, and this is not a draft comment '. 115 'you authored.')); 116 } 117 118 return $inline; 119 } 120 121 protected function canEditInlineComment( 122 PhabricatorUser $viewer, 123 DifferentialInlineComment $inline) { 124 125 // Only the author may edit a comment. 126 if ($inline->getAuthorPHID() != $viewer->getPHID()) { 127 return false; 128 } 129 130 // Saved comments may not be edited, for now, although the schema now 131 // supports it. 132 if (!$inline->isDraft()) { 133 return false; 134 } 135 136 // Inline must be attached to the active revision. 137 if ($inline->getRevisionID() != $this->getRevisionID()) { 138 return false; 139 } 140 141 return true; 142 } 143 144 protected function loadObjectOwnerPHID( 145 PhabricatorInlineComment $inline) { 146 return $this->loadRevision()->getAuthorPHID(); 147 } 148 149 protected function hideComments(array $ids) { 150 $viewer = $this->getViewer(); 151 $table = new DifferentialHiddenComment(); 152 $conn_w = $table->establishConnection('w'); 153 154 $sql = array(); 155 foreach ($ids as $id) { 156 $sql[] = qsprintf( 157 $conn_w, 158 '(%s, %d)', 159 $viewer->getPHID(), 160 $id); 161 } 162 163 queryfx( 164 $conn_w, 165 'INSERT IGNORE INTO %T (userPHID, commentID) VALUES %LQ', 166 $table->getTableName(), 167 $sql); 168 } 169 170 protected function showComments(array $ids) { 171 $viewer = $this->getViewer(); 172 $table = new DifferentialHiddenComment(); 173 $conn_w = $table->establishConnection('w'); 174 175 queryfx( 176 $conn_w, 177 'DELETE FROM %T WHERE userPHID = %s AND commentID IN (%Ld)', 178 $table->getTableName(), 179 $viewer->getPHID(), 180 $ids); 181 } 182 183}