Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / diffusion / controller / DiffusionInlineCommentController.php
blobb8ef5478270c984d9c027ff69049e0c49a3ca0c8
1 <?php
3 final class DiffusionInlineCommentController
4 extends PhabricatorInlineCommentController {
6 protected function newInlineCommentQuery() {
7 return new DiffusionDiffInlineCommentQuery();
10 protected function newContainerObject() {
11 return $this->loadCommit();
14 private function getCommitPHID() {
15 return $this->getRequest()->getURIData('phid');
18 private function loadCommit() {
19 $viewer = $this->getViewer();
20 $commit_phid = $this->getCommitPHID();
22 $commit = id(new DiffusionCommitQuery())
23 ->setViewer($viewer)
24 ->withPHIDs(array($commit_phid))
25 ->executeOne();
26 if (!$commit) {
27 throw new Exception(pht('Invalid commit PHID "%s"!', $commit_phid));
30 return $commit;
33 protected function createComment() {
34 $commit = $this->loadCommit();
36 // TODO: Write a real PathQuery object?
37 $path_id = $this->getChangesetID();
38 $path = queryfx_one(
39 id(new PhabricatorRepository())->establishConnection('r'),
40 'SELECT path FROM %T WHERE id = %d',
41 PhabricatorRepository::TABLE_PATH,
42 $path_id);
43 if (!$path) {
44 throw new Exception(pht('Invalid path ID!'));
47 return id(new PhabricatorAuditInlineComment())
48 ->setCommitPHID($commit->getPHID())
49 ->setPathID($path_id);
52 protected function loadCommentForDone($id) {
53 $viewer = $this->getViewer();
55 $inline = $this->loadCommentByID($id);
56 if (!$inline) {
57 throw new Exception(pht('Failed to load comment "%d".', $id));
60 $commit = id(new DiffusionCommitQuery())
61 ->setViewer($viewer)
62 ->withPHIDs(array($inline->getCommitPHID()))
63 ->executeOne();
64 if (!$commit) {
65 throw new Exception(pht('Failed to load commit.'));
68 $owner_phid = $commit->getAuthorPHID();
69 $viewer_phid = $viewer->getPHID();
70 $viewer_is_owner = ($owner_phid && ($owner_phid == $viewer_phid));
71 $viewer_is_author = ($viewer_phid == $inline->getAuthorPHID());
72 $is_draft = $inline->isDraft();
74 if ($viewer_is_owner) {
75 // You can mark inlines on your own commits as "Done".
76 } else if ($viewer_is_author && $is_draft) {
77 // You can mark your own unsubmitted inlines as "Done".
78 } else {
79 throw new Exception(
80 pht(
81 'You can not mark this comment as complete: you did not author '.
82 'the commit and the comment is not a draft you wrote.'));
85 return $inline;
88 protected function canEditInlineComment(
89 PhabricatorUser $viewer,
90 PhabricatorAuditInlineComment $inline) {
92 // Only the author may edit a comment.
93 if ($inline->getAuthorPHID() != $viewer->getPHID()) {
94 return false;
97 // Saved comments may not be edited.
98 if ($inline->getTransactionPHID()) {
99 return false;
102 // Inline must be attached to the active revision.
103 if ($inline->getCommitPHID() != $this->getCommitPHID()) {
104 return false;
107 return true;
110 protected function loadObjectOwnerPHID(
111 PhabricatorInlineComment $inline) {
112 return $this->loadCommit()->getAuthorPHID();