@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
1<?php
2
3final class DiffusionInlineCommentController
4 extends PhabricatorInlineCommentController {
5
6 protected function newInlineCommentQuery() {
7 return new DiffusionDiffInlineCommentQuery();
8 }
9
10 protected function newContainerObject() {
11 return $this->loadCommit();
12 }
13
14 private function getCommitPHID() {
15 return $this->getRequest()->getURIData('phid');
16 }
17
18 private function loadCommit() {
19 $viewer = $this->getViewer();
20 $commit_phid = $this->getCommitPHID();
21
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));
28 }
29
30 return $commit;
31 }
32
33 protected function createComment() {
34 $commit = $this->loadCommit();
35
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!'));
45 }
46
47 return id(new PhabricatorAuditInlineComment())
48 ->setCommitPHID($commit->getPHID())
49 ->setPathID($path_id);
50 }
51
52 protected function loadCommentForDone($id) {
53 $viewer = $this->getViewer();
54
55 $inline = $this->loadCommentByID($id);
56 if (!$inline) {
57 throw new Exception(pht('Failed to load comment "%d".', $id));
58 }
59
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.'));
66 }
67
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();
73
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.'));
83 }
84
85 return $inline;
86 }
87
88 protected function canEditInlineComment(
89 PhabricatorUser $viewer,
90 PhabricatorAuditInlineComment $inline) {
91
92 // Only the author may edit a comment.
93 if ($inline->getAuthorPHID() != $viewer->getPHID()) {
94 return false;
95 }
96
97 // Saved comments may not be edited.
98 if ($inline->getTransactionPHID()) {
99 return false;
100 }
101
102 // Inline must be attached to the active revision.
103 if ($inline->getCommitPHID() != $this->getCommitPHID()) {
104 return false;
105 }
106
107 return true;
108 }
109
110 protected function loadObjectOwnerPHID(
111 PhabricatorInlineComment $inline) {
112 return $this->loadCommit()->getAuthorPHID();
113 }
114
115
116}