2015-07-18 08:07:31 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class PhabricatorSubscriptionsHeraldAction
|
|
|
|
extends HeraldAction {
|
|
|
|
|
|
|
|
const DO_PREVIOUSLY_UNSUBSCRIBED = 'do.previously-unsubscribed';
|
|
|
|
const DO_AUTOSUBSCRIBED = 'do.autosubscribed';
|
|
|
|
const DO_SUBSCRIBED = 'do.subscribed';
|
|
|
|
const DO_UNSUBSCRIBED = 'do.unsubscribed';
|
|
|
|
|
2015-07-24 08:19:47 -07:00
|
|
|
public function getActionGroupKey() {
|
|
|
|
return HeraldSupportActionGroup::ACTIONGROUPKEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function supportsObject($object) {
|
|
|
|
return ($object instanceof PhabricatorSubscribableInterface);
|
|
|
|
}
|
|
|
|
|
2015-07-18 08:07:31 -07:00
|
|
|
protected function applySubscribe(array $phids, $is_add) {
|
|
|
|
$adapter = $this->getAdapter();
|
|
|
|
|
2015-08-03 13:34:31 -07:00
|
|
|
$allowed_types = array(
|
|
|
|
PhabricatorPeopleUserPHIDType::TYPECONST,
|
|
|
|
PhabricatorProjectProjectPHIDType::TYPECONST,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Evaluating "No Effect" is a bit tricky for this rule type, so just
|
|
|
|
// do it manually below.
|
|
|
|
$current = array();
|
2015-07-18 08:07:31 -07:00
|
|
|
|
2015-08-03 13:34:31 -07:00
|
|
|
$targets = $this->loadStandardTargets($phids, $allowed_types, $current);
|
|
|
|
if (!$targets) {
|
2015-07-18 08:07:31 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:34:31 -07:00
|
|
|
$phids = array_fuse(array_keys($targets));
|
|
|
|
|
2015-07-18 08:07:31 -07:00
|
|
|
// The "Add Subscribers" rule only adds subscribers who haven't previously
|
|
|
|
// unsubscribed from the object explicitly. Filter these subscribers out
|
|
|
|
// before continuing.
|
|
|
|
if ($is_add) {
|
|
|
|
$unsubscribed = $adapter->loadEdgePHIDs(
|
|
|
|
PhabricatorObjectHasUnsubscriberEdgeType::EDGECONST);
|
|
|
|
|
|
|
|
foreach ($unsubscribed as $phid) {
|
2015-08-03 13:34:31 -07:00
|
|
|
if (isset($phids[$phid])) {
|
2015-07-18 08:07:31 -07:00
|
|
|
$unsubscribed[$phid] = $phid;
|
2015-08-03 13:34:31 -07:00
|
|
|
unset($phids[$phid]);
|
2015-07-18 08:07:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($unsubscribed) {
|
|
|
|
$this->logEffect(
|
|
|
|
self::DO_PREVIOUSLY_UNSUBSCRIBED,
|
|
|
|
array_values($unsubscribed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:34:31 -07:00
|
|
|
if (!$phids) {
|
2015-07-18 08:07:31 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$auto = array();
|
|
|
|
$object = $adapter->getObject();
|
2015-08-03 13:34:31 -07:00
|
|
|
foreach ($phids as $phid) {
|
2015-07-18 08:07:31 -07:00
|
|
|
if ($object->isAutomaticallySubscribed($phid)) {
|
|
|
|
$auto[$phid] = $phid;
|
2015-08-03 13:34:31 -07:00
|
|
|
unset($phids[$phid]);
|
2015-07-18 08:07:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($auto) {
|
|
|
|
$this->logEffect(self::DO_AUTOSUBSCRIBED, array_values($auto));
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:34:31 -07:00
|
|
|
if (!$phids) {
|
2015-07-18 08:07:31 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = $adapter->loadEdgePHIDs(
|
|
|
|
PhabricatorObjectHasSubscriberEdgeType::EDGECONST);
|
|
|
|
|
|
|
|
if ($is_add) {
|
|
|
|
$already = array();
|
2015-08-03 13:34:31 -07:00
|
|
|
foreach ($phids as $phid) {
|
2015-07-18 08:07:31 -07:00
|
|
|
if (isset($current[$phid])) {
|
|
|
|
$already[$phid] = $phid;
|
2015-08-03 13:34:31 -07:00
|
|
|
unset($phids[$phid]);
|
2015-07-18 08:07:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($already) {
|
2015-08-03 13:34:31 -07:00
|
|
|
$this->logEffect(self::DO_STANDARD_NO_EFFECT, $already);
|
2015-07-18 08:07:31 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$already = array();
|
2015-08-03 13:34:31 -07:00
|
|
|
foreach ($phids as $phid) {
|
2015-07-18 08:07:31 -07:00
|
|
|
if (empty($current[$phid])) {
|
|
|
|
$already[$phid] = $phid;
|
2015-08-03 13:34:31 -07:00
|
|
|
unset($phids[$phid]);
|
2015-07-18 08:07:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($already) {
|
2015-08-03 13:34:31 -07:00
|
|
|
$this->logEffect(self::DO_STANDARD_NO_EFFECT, $already);
|
2015-07-18 08:07:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:34:31 -07:00
|
|
|
if (!$phids) {
|
2015-07-18 08:07:31 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-03 13:34:31 -07:00
|
|
|
if ($is_add) {
|
|
|
|
$kind = '+';
|
|
|
|
} else {
|
|
|
|
$kind = '-';
|
|
|
|
}
|
|
|
|
|
2015-07-18 08:07:31 -07:00
|
|
|
$xaction = $adapter->newTransaction()
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)
|
|
|
|
->setNewValue(
|
|
|
|
array(
|
2015-08-03 13:34:31 -07:00
|
|
|
$kind => $phids,
|
2015-07-18 08:07:31 -07:00
|
|
|
));
|
|
|
|
|
|
|
|
$adapter->queueTransaction($xaction);
|
|
|
|
|
|
|
|
if ($is_add) {
|
2015-08-03 13:34:31 -07:00
|
|
|
$this->logEffect(self::DO_SUBSCRIBED, $phids);
|
2015-07-18 08:07:31 -07:00
|
|
|
} else {
|
2015-08-03 13:34:31 -07:00
|
|
|
$this->logEffect(self::DO_UNSUBSCRIBED, $phids);
|
2015-07-18 08:07:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getActionEffectMap() {
|
|
|
|
return array(
|
|
|
|
self::DO_PREVIOUSLY_UNSUBSCRIBED => array(
|
|
|
|
'icon' => 'fa-minus-circle',
|
|
|
|
'color' => 'grey',
|
|
|
|
'name' => pht('Previously Unsubscribed'),
|
|
|
|
),
|
|
|
|
self::DO_AUTOSUBSCRIBED => array(
|
|
|
|
'icon' => 'fa-envelope',
|
|
|
|
'color' => 'grey',
|
|
|
|
'name' => pht('Automatically Subscribed'),
|
|
|
|
),
|
|
|
|
self::DO_SUBSCRIBED => array(
|
|
|
|
'icon' => 'fa-envelope',
|
|
|
|
'color' => 'green',
|
|
|
|
'name' => pht('Added Subscribers'),
|
|
|
|
),
|
|
|
|
self::DO_UNSUBSCRIBED => array(
|
|
|
|
'icon' => 'fa-minus-circle',
|
|
|
|
'color' => 'green',
|
|
|
|
'name' => pht('Removed Subscribers'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-03 06:01:24 -07:00
|
|
|
protected function renderActionEffectDescription($type, $data) {
|
2015-07-18 08:07:31 -07:00
|
|
|
switch ($type) {
|
|
|
|
case self::DO_PREVIOUSLY_UNSUBSCRIBED:
|
|
|
|
return pht(
|
|
|
|
'Declined to resubscribe %s target(s) because they previously '.
|
|
|
|
'unsubscribed: %s.',
|
|
|
|
new PhutilNumber(count($data)),
|
|
|
|
$this->renderHandleList($data));
|
|
|
|
case self::DO_AUTOSUBSCRIBED:
|
|
|
|
return pht(
|
|
|
|
'%s automatically subscribed target(s) were not affected: %s.',
|
|
|
|
new PhutilNumber(count($data)),
|
|
|
|
$this->renderHandleList($data));
|
|
|
|
case self::DO_SUBSCRIBED:
|
|
|
|
return pht(
|
|
|
|
'Added %s subscriber(s): %s.',
|
|
|
|
new PhutilNumber(count($data)),
|
|
|
|
$this->renderHandleList($data));
|
|
|
|
case self::DO_UNSUBSCRIBED:
|
|
|
|
return pht(
|
|
|
|
'Removed %s subscriber(s): %s.',
|
|
|
|
new PhutilNumber(count($data)),
|
|
|
|
$this->renderHandleList($data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|