~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to phabricator/src/applications/herald/adapter/HeraldAdapter.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-06-13 10:52:10 UTC
  • mfrom: (0.30.1) (0.29.1) (0.17.4) (2.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20150613105210-5uirr7tvnk0n6e6y
Tags: 0~git20150613-1
* New snapshot release (closes: #787805)
* fixed typo in logrotate script (closes: #787645)

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
  const FIELD_TASK_STATUS            = 'taskstatus';
45
45
  const FIELD_PUSHER_IS_COMMITTER    = 'pusher-is-committer';
46
46
  const FIELD_PATH                   = 'path';
 
47
  const FIELD_SPACE = 'space';
47
48
 
48
49
  const CONDITION_CONTAINS        = 'contains';
49
50
  const CONDITION_NOT_CONTAINS    = '!contains';
101
102
  const VALUE_TASK_STATUS     = 'taskstatus';
102
103
  const VALUE_LEGAL_DOCUMENTS   = 'legaldocuments';
103
104
  const VALUE_APPLICATION_EMAIL = 'applicationemail';
 
105
  const VALUE_SPACE = 'space';
104
106
 
105
107
  private $contentSource;
106
108
  private $isNewObject;
110
112
  private $queuedTransactions = array();
111
113
  private $emailPHIDs = array();
112
114
  private $forcedEmailPHIDs = array();
 
115
  private $unsubscribedPHIDs;
113
116
 
114
117
  public function getEmailPHIDs() {
115
118
    return array_values($this->emailPHIDs);
196
199
        return true;
197
200
      case self::FIELD_IS_NEW_OBJECT:
198
201
        return $this->getIsNewObject();
 
202
      case self::FIELD_CC:
 
203
        $object = $this->getObject();
 
204
 
 
205
        if (!($object instanceof PhabricatorSubscribableInterface)) {
 
206
          throw new Exception(
 
207
            pht(
 
208
              'Adapter object (of class "%s") does not implement interface '.
 
209
              '"%s", so the subscribers field value can not be determined.',
 
210
              get_class($object),
 
211
              'PhabricatorSubscribableInterface'));
 
212
        }
 
213
 
 
214
        $phid = $object->getPHID();
 
215
        return PhabricatorSubscribersQuery::loadSubscribersForPHID($phid);
199
216
      case self::FIELD_APPLICATION_EMAIL:
200
217
        $value = array();
201
218
        // while there is only one match by implementation, we do set
204
221
          $value[] = $this->getApplicationEmail()->getPHID();
205
222
        }
206
223
        return $value;
 
224
      case self::FIELD_SPACE:
 
225
        $object = $this->getObject();
 
226
 
 
227
        if (!($object instanceof PhabricatorSpacesInterface)) {
 
228
          throw new Exception(
 
229
            pht(
 
230
              'Adapter object (of class "%s") does not implement interface '.
 
231
              '"%s", so the Space field value can not be determined.',
 
232
              get_class($object),
 
233
              'PhabricatorSpacesInterface'));
 
234
        }
 
235
 
 
236
        return PhabricatorSpacesNamespaceQuery::getObjectSpacePHID($object);
207
237
      default:
208
238
        if ($this->isHeraldCustomKey($field_name)) {
209
239
          return $this->getCustomFieldValue($field_name);
385
415
      self::FIELD_TASK_STATUS => pht('Task status'),
386
416
      self::FIELD_PUSHER_IS_COMMITTER => pht('Pusher same as committer'),
387
417
      self::FIELD_PATH => pht('Path'),
 
418
      self::FIELD_SPACE => pht('Space'),
388
419
    ) + $this->getCustomFieldNameMap();
389
420
  }
390
421
 
438
469
      case self::FIELD_PUSHER:
439
470
      case self::FIELD_TASK_PRIORITY:
440
471
      case self::FIELD_TASK_STATUS:
 
472
      case self::FIELD_SPACE:
441
473
        return array(
442
474
          self::CONDITION_IS_ANY,
443
475
          self::CONDITION_IS_NOT_ANY,
942
974
            return self::VALUE_TASK_PRIORITY;
943
975
          case self::FIELD_TASK_STATUS:
944
976
            return self::VALUE_TASK_STATUS;
 
977
          case self::FIELD_SPACE:
 
978
            return self::VALUE_SPACE;
945
979
          default:
946
980
            return self::VALUE_USER;
947
981
        }
1541
1575
      case self::ACTION_ADD_PROJECTS:
1542
1576
      case self::ACTION_REMOVE_PROJECTS:
1543
1577
        return $this->applyProjectsEffect($effect);
 
1578
      case self::ACTION_ADD_CC:
 
1579
      case self::ACTION_REMOVE_CC:
 
1580
        return $this->applySubscribersEffect($effect);
1544
1581
      case self::ACTION_FLAG:
1545
1582
        return $this->applyFlagEffect($effect);
1546
1583
      case self::ACTION_EMAIL:
1547
1584
        return $this->applyEmailEffect($effect);
 
1585
      case self::ACTION_NOTHING:
 
1586
        return $this->applyNothingEffect($effect);
1548
1587
      default:
1549
1588
        break;
1550
1589
    }
1563
1602
    return $result;
1564
1603
  }
1565
1604
 
 
1605
  private function applyNothingEffect(HeraldEffect $effect) {
 
1606
    return new HeraldApplyTranscript(
 
1607
      $effect,
 
1608
      true,
 
1609
      pht('Did nothing.'));
 
1610
  }
1566
1611
 
1567
1612
  /**
1568
1613
   * @task apply
1593
1638
      pht('Added projects.'));
1594
1639
  }
1595
1640
 
 
1641
  /**
 
1642
   * @task apply
 
1643
   */
 
1644
  private function applySubscribersEffect(HeraldEffect $effect) {
 
1645
    if ($effect->getAction() == self::ACTION_ADD_CC) {
 
1646
      $kind = '+';
 
1647
      $is_add = true;
 
1648
    } else {
 
1649
      $kind = '-';
 
1650
      $is_add = false;
 
1651
    }
 
1652
 
 
1653
    $subscriber_phids = array_fuse($effect->getTarget());
 
1654
    if (!$subscriber_phids) {
 
1655
      return new HeraldApplyTranscript(
 
1656
        $effect,
 
1657
        false,
 
1658
        pht('This action lists no users or objects to affect.'));
 
1659
    }
 
1660
 
 
1661
    // The "Add Subscribers" rule only adds subscribers who haven't previously
 
1662
    // unsubscribed from the object explicitly. Filter these subscribers out
 
1663
    // before continuing.
 
1664
    $unsubscribed = array();
 
1665
    if ($is_add) {
 
1666
      if ($this->unsubscribedPHIDs === null) {
 
1667
        $this->unsubscribedPHIDs = PhabricatorEdgeQuery::loadDestinationPHIDs(
 
1668
          $this->getObject()->getPHID(),
 
1669
          PhabricatorObjectHasUnsubscriberEdgeType::EDGECONST);
 
1670
      }
 
1671
 
 
1672
      foreach ($this->unsubscribedPHIDs as $phid) {
 
1673
        if (isset($subscriber_phids[$phid])) {
 
1674
          $unsubscribed[$phid] = $phid;
 
1675
          unset($subscriber_phids[$phid]);
 
1676
        }
 
1677
      }
 
1678
    }
 
1679
 
 
1680
    if (!$subscriber_phids) {
 
1681
      return new HeraldApplyTranscript(
 
1682
        $effect,
 
1683
        false,
 
1684
        pht('All targets have previously unsubscribed explicitly.'));
 
1685
    }
 
1686
 
 
1687
    // Filter out PHIDs which aren't valid subscribers. Lower levels of the
 
1688
    // stack will fail loudly if we try to add subscribers with invalid PHIDs
 
1689
    // or unknown PHID types, so drop them here.
 
1690
    $invalid = array();
 
1691
    foreach ($subscriber_phids as $phid) {
 
1692
      $type = phid_get_type($phid);
 
1693
      switch ($type) {
 
1694
        case PhabricatorPeopleUserPHIDType::TYPECONST:
 
1695
        case PhabricatorProjectProjectPHIDType::TYPECONST:
 
1696
          break;
 
1697
        default:
 
1698
          $invalid[$phid] = $phid;
 
1699
          unset($subscriber_phids[$phid]);
 
1700
          break;
 
1701
      }
 
1702
    }
 
1703
 
 
1704
    if (!$subscriber_phids) {
 
1705
      return new HeraldApplyTranscript(
 
1706
        $effect,
 
1707
        false,
 
1708
        pht('All targets are invalid as subscribers.'));
 
1709
    }
 
1710
 
 
1711
    $xaction = $this->newTransaction()
 
1712
      ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)
 
1713
      ->setNewValue(
 
1714
        array(
 
1715
          $kind => $subscriber_phids,
 
1716
        ));
 
1717
 
 
1718
    $this->queueTransaction($xaction);
 
1719
 
 
1720
    // TODO: We could be more detailed about this, but doing it meaningfully
 
1721
    // probably requires substantial changes to how transactions are rendered
 
1722
    // first.
 
1723
    if ($is_add) {
 
1724
      $message = pht('Subscribed targets.');
 
1725
    } else {
 
1726
      $message = pht('Unsubscribed targets.');
 
1727
    }
 
1728
 
 
1729
    return new HeraldApplyTranscript($effect, true, $message);
 
1730
  }
 
1731
 
1596
1732
 
1597
1733
  /**
1598
1734
   * @task apply