Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / doorkeeper / DiffusionDoorkeeperCommitFeedStoryPublisher.php
blob8f1362860ddf568168a14719dadac2aea4d3fdeb
1 <?php
3 final class DiffusionDoorkeeperCommitFeedStoryPublisher
4 extends DoorkeeperFeedStoryPublisher {
6 private $auditRequests;
7 private $activePHIDs;
8 private $passivePHIDs;
10 private function getAuditRequests() {
11 return $this->auditRequests;
14 public function canPublishStory(PhabricatorFeedStory $story, $object) {
15 return
16 ($story instanceof PhabricatorApplicationTransactionFeedStory) &&
17 ($object instanceof PhabricatorRepositoryCommit);
20 public function isStoryAboutObjectCreation($object) {
21 // TODO: Although creation stories exist, they currently don't have a
22 // primary object PHID set, so they'll never make it here because they
23 // won't pass `canPublishStory()`.
24 return false;
27 public function isStoryAboutObjectClosure($object) {
28 // TODO: This isn't quite accurate, but pretty close: check if this story
29 // is a close (which clearly is about object closure) or is an "Accept" and
30 // the commit is fully audited (which is almost certainly a closure).
31 // After ApplicationTransactions, we could annotate feed stories more
32 // explicitly.
34 $story = $this->getFeedStory();
35 $xaction = $story->getPrimaryTransaction();
36 switch ($xaction->getTransactionType()) {
37 case PhabricatorAuditActionConstants::ACTION:
38 switch ($xaction->getNewValue()) {
39 case PhabricatorAuditActionConstants::CLOSE:
40 return true;
41 case PhabricatorAuditActionConstants::ACCEPT:
42 if ($object->isAuditStatusAudited()) {
43 return true;
45 break;
49 return false;
52 public function willPublishStory($commit) {
53 $requests = id(new DiffusionCommitQuery())
54 ->setViewer($this->getViewer())
55 ->withPHIDs(array($commit->getPHID()))
56 ->needAuditRequests(true)
57 ->executeOne()
58 ->getAudits();
60 // TODO: This is messy and should be generalized, but we don't have a good
61 // query for it yet. Since we run in the daemons, just do the easiest thing
62 // we can for the moment. Figure out who all of the "active" (need to
63 // audit) and "passive" (no action necessary) users are.
65 $auditor_phids = mpull($requests, 'getAuditorPHID');
66 $objects = id(new PhabricatorObjectQuery())
67 ->setViewer($this->getViewer())
68 ->withPHIDs($auditor_phids)
69 ->execute();
71 $active = array();
72 $passive = array();
74 foreach ($requests as $request) {
75 $status = $request->getAuditStatus();
77 $object = idx($objects, $request->getAuditorPHID());
78 if (!$object) {
79 continue;
82 $request_phids = array();
83 if ($object instanceof PhabricatorUser) {
84 $request_phids = array($object->getPHID());
85 } else if ($object instanceof PhabricatorOwnersPackage) {
86 $request_phids = PhabricatorOwnersOwner::loadAffiliatedUserPHIDs(
87 array($object->getID()));
88 } else if ($object instanceof PhabricatorProject) {
89 $project = id(new PhabricatorProjectQuery())
90 ->setViewer($this->getViewer())
91 ->withIDs(array($object->getID()))
92 ->needMembers(true)
93 ->executeOne();
94 $request_phids = $project->getMemberPHIDs();
95 } else {
96 // Dunno what this is.
97 $request_phids = array();
100 switch ($status) {
101 case PhabricatorAuditRequestStatus::AUDIT_REQUIRED:
102 case PhabricatorAuditRequestStatus::AUDIT_REQUESTED:
103 case PhabricatorAuditRequestStatus::CONCERNED:
104 $active += array_fuse($request_phids);
105 break;
106 default:
107 $passive += array_fuse($request_phids);
108 break;
113 // Remove "Active" users from the "Passive" list.
114 $passive = array_diff_key($passive, $active);
116 $this->activePHIDs = $active;
117 $this->passivePHIDs = $passive;
118 $this->auditRequests = $requests;
120 return $commit;
123 public function getOwnerPHID($object) {
124 return $object->getAuthorPHID();
127 public function getActiveUserPHIDs($object) {
128 return $this->activePHIDs;
131 public function getPassiveUserPHIDs($object) {
132 return $this->passivePHIDs;
135 public function getCCUserPHIDs($object) {
136 return PhabricatorSubscribersQuery::loadSubscribersForPHID(
137 $object->getPHID());
140 public function getObjectTitle($object) {
141 $prefix = $this->getTitlePrefix($object);
143 $repository = $object->getRepository();
144 $name = $repository->formatCommitName($object->getCommitIdentifier());
146 $title = $object->getSummary();
148 return ltrim("{$prefix} {$name}: {$title}");
151 public function getObjectURI($object) {
152 $repository = $object->getRepository();
153 $name = $repository->formatCommitName($object->getCommitIdentifier());
154 return PhabricatorEnv::getProductionURI('/'.$name);
157 public function getObjectDescription($object) {
158 $data = $object->loadCommitData();
159 if ($data) {
160 return $data->getCommitMessage();
162 return null;
165 public function isObjectClosed($object) {
166 return $object->getAuditStatusObject()->getIsClosed();
169 public function getResponsibilityTitle($object) {
170 $prefix = $this->getTitlePrefix($object);
171 return pht('%s Audit', $prefix);
174 private function getTitlePrefix(PhabricatorRepositoryCommit $commit) {
175 return pht('[Diffusion]');