Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / subscriptions / editor / PhabricatorSubscriptionsEditor.php
blob3ee4b5f6e88f3023d2d4c2eb1a4cb0554fa65976
1 <?php
3 final class PhabricatorSubscriptionsEditor extends PhabricatorEditor {
5 private $object;
7 private $explicitSubscribePHIDs = array();
8 private $implicitSubscribePHIDs = array();
9 private $unsubscribePHIDs = array();
11 public function setObject(PhabricatorSubscribableInterface $object) {
12 $this->object = $object;
13 return $this;
16 /**
17 * Add explicit subscribers. These subscribers have explicitly subscribed
18 * (or been subscribed) to the object, and will be added even if they
19 * had previously unsubscribed.
21 * @param list<phid> List of PHIDs to explicitly subscribe.
22 * @return this
24 public function subscribeExplicit(array $phids) {
25 $this->explicitSubscribePHIDs += array_fill_keys($phids, true);
26 return $this;
30 /**
31 * Add implicit subscribers. These subscribers have taken some action which
32 * implicitly subscribes them (e.g., adding a comment) but it will be
33 * suppressed if they've previously unsubscribed from the object.
35 * @param list<phid> List of PHIDs to implicitly subscribe.
36 * @return this
38 public function subscribeImplicit(array $phids) {
39 $this->implicitSubscribePHIDs += array_fill_keys($phids, true);
40 return $this;
44 /**
45 * Unsubscribe PHIDs and mark them as unsubscribed, so implicit subscriptions
46 * will not resubscribe them.
48 * @param list<phid> List of PHIDs to unsubscribe.
49 * @return this
51 public function unsubscribe(array $phids) {
52 $this->unsubscribePHIDs += array_fill_keys($phids, true);
53 return $this;
57 public function save() {
58 if (!$this->object) {
59 throw new PhutilInvalidStateException('setObject');
61 $actor = $this->requireActor();
63 $src = $this->object->getPHID();
65 if ($this->implicitSubscribePHIDs) {
66 $unsub = PhabricatorEdgeQuery::loadDestinationPHIDs(
67 $src,
68 PhabricatorObjectHasUnsubscriberEdgeType::EDGECONST);
69 $unsub = array_fill_keys($unsub, true);
70 $this->implicitSubscribePHIDs = array_diff_key(
71 $this->implicitSubscribePHIDs,
72 $unsub);
75 $add = $this->implicitSubscribePHIDs + $this->explicitSubscribePHIDs;
76 $del = $this->unsubscribePHIDs;
78 // If a PHID is marked for both subscription and unsubscription, treat
79 // unsubscription as the stronger action.
80 $add = array_diff_key($add, $del);
82 if ($add || $del) {
83 $u_type = PhabricatorObjectHasUnsubscriberEdgeType::EDGECONST;
84 $s_type = PhabricatorObjectHasSubscriberEdgeType::EDGECONST;
86 $editor = new PhabricatorEdgeEditor();
88 foreach ($add as $phid => $ignored) {
89 $editor->removeEdge($src, $u_type, $phid);
90 $editor->addEdge($src, $s_type, $phid);
93 foreach ($del as $phid => $ignored) {
94 $editor->removeEdge($src, $s_type, $phid);
95 $editor->addEdge($src, $u_type, $phid);
98 $editor->save();