[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/metamta/query/ -> PhabricatorMetaMTAActorQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorMetaMTAActorQuery extends PhabricatorQuery {
   4  
   5    private $phids = array();
   6    private $viewer;
   7  
   8    public function setViewer(PhabricatorUser $viewer) {
   9      $this->viewer = $viewer;
  10      return $this;
  11    }
  12  
  13    public function getViewer() {
  14      return $this->viewer;
  15    }
  16  
  17    public function withPHIDs(array $phids) {
  18      $this->phids = $phids;
  19      return $this;
  20    }
  21  
  22    public function execute() {
  23      $phids = array_fuse($this->phids);
  24      $actors = array();
  25      $type_map = array();
  26      foreach ($phids as $phid) {
  27        $type_map[phid_get_type($phid)][] = $phid;
  28        $actors[$phid] = id(new PhabricatorMetaMTAActor())->setPHID($phid);
  29      }
  30  
  31      // TODO: Move this to PhabricatorPHIDType, or the objects, or some
  32      // interface.
  33  
  34      foreach ($type_map as $type => $phids) {
  35        switch ($type) {
  36          case PhabricatorPeopleUserPHIDType::TYPECONST:
  37            $this->loadUserActors($actors, $phids);
  38            break;
  39          case PhabricatorPeopleExternalPHIDType::TYPECONST:
  40            $this->loadExternalUserActors($actors, $phids);
  41            break;
  42          case PhabricatorMailingListListPHIDType::TYPECONST:
  43            $this->loadMailingListActors($actors, $phids);
  44            break;
  45          default:
  46            $this->loadUnknownActors($actors, $phids);
  47            break;
  48        }
  49      }
  50  
  51      return $actors;
  52    }
  53  
  54    private function loadUserActors(array $actors, array $phids) {
  55      assert_instances_of($actors, 'PhabricatorMetaMTAActor');
  56  
  57      $emails = id(new PhabricatorUserEmail())->loadAllWhere(
  58        'userPHID IN (%Ls) AND isPrimary = 1',
  59        $phids);
  60      $emails = mpull($emails, null, 'getUserPHID');
  61  
  62      $users = id(new PhabricatorPeopleQuery())
  63        ->setViewer($this->getViewer())
  64        ->withPHIDs($phids)
  65        ->execute();
  66      $users = mpull($users, null, 'getPHID');
  67  
  68      foreach ($phids as $phid) {
  69        $actor = $actors[$phid];
  70  
  71        $user = idx($users, $phid);
  72        if (!$user) {
  73          $actor->setUndeliverable(
  74            pht('Unable to load user record for this PHID.'));
  75        } else {
  76          $actor->setName($this->getUserName($user));
  77          if ($user->getIsDisabled()) {
  78            $actor->setUndeliverable(
  79              pht('This user is disabled; disabled users do not receive mail.'));
  80          }
  81          if ($user->getIsSystemAgent()) {
  82            $actor->setUndeliverable(
  83              pht('This user is a bot; bot accounts do not receive mail.'));
  84          }
  85  
  86          // NOTE: We do send email to unapproved users, and to unverified users,
  87          // because it would otherwise be impossible to get them to verify their
  88          // email addresses. Possibly we should white-list this kind of mail and
  89          // deny all other types of mail.
  90        }
  91  
  92        $email = idx($emails, $phid);
  93        if (!$email) {
  94          $actor->setUndeliverable(
  95            pht('Unable to load email record for this PHID.'));
  96        } else {
  97          $actor->setEmailAddress($email->getAddress());
  98        }
  99      }
 100    }
 101  
 102    private function loadExternalUserActors(array $actors, array $phids) {
 103      assert_instances_of($actors, 'PhabricatorMetaMTAActor');
 104  
 105      $xusers = id(new PhabricatorExternalAccountQuery())
 106        ->setViewer($this->getViewer())
 107        ->withPHIDs($phids)
 108        ->execute();
 109      $xusers = mpull($xusers, null, 'getPHID');
 110  
 111      foreach ($phids as $phid) {
 112        $actor = $actors[$phid];
 113  
 114        $xuser = idx($xusers, $phid);
 115        if (!$xuser) {
 116          $actor->setUndeliverable(
 117            pht('Unable to load external user record for this PHID.'));
 118          continue;
 119        }
 120  
 121        $actor->setName($xuser->getDisplayName());
 122  
 123        if ($xuser->getAccountType() != 'email') {
 124          $actor->setUndeliverable(
 125            pht(
 126              'Only external accounts of type "email" are deliverable; this '.
 127              'account has a different type.'));
 128          continue;
 129        }
 130  
 131        $actor->setEmailAddress($xuser->getAccountID());
 132      }
 133    }
 134  
 135    private function loadMailingListActors(array $actors, array $phids) {
 136      assert_instances_of($actors, 'PhabricatorMetaMTAActor');
 137  
 138      $lists = id(new PhabricatorMailingListQuery())
 139        ->setViewer($this->getViewer())
 140        ->withPHIDs($phids)
 141        ->execute();
 142      $lists = mpull($lists, null, 'getPHID');
 143  
 144      foreach ($phids as $phid) {
 145        $actor = $actors[$phid];
 146  
 147        $list = idx($lists, $phid);
 148        if (!$list) {
 149          $actor->setUndeliverable(
 150            pht(
 151              'Unable to load mailing list record for this PHID.'));
 152          continue;
 153        }
 154  
 155        $actor->setName($list->getName());
 156        $actor->setEmailAddress($list->getEmail());
 157      }
 158    }
 159  
 160    private function loadUnknownActors(array $actors, array $phids) {
 161      foreach ($phids as $phid) {
 162        $actor = $actors[$phid];
 163        $actor->setUndeliverable(pht('This PHID type is not mailable.'));
 164      }
 165    }
 166  
 167  
 168    /**
 169     * Small helper function to make sure we format the username properly as
 170     * specified by the `metamta.user-address-format` configuration value.
 171     */
 172    private function getUserName(PhabricatorUser $user) {
 173      $format = PhabricatorEnv::getEnvConfig('metamta.user-address-format');
 174  
 175      switch ($format) {
 176        case 'short':
 177          $name = $user->getUserName();
 178          break;
 179        case 'real':
 180          $name = strlen($user->getRealName()) ?
 181            $user->getRealName() : $user->getUserName();
 182          break;
 183        case 'full':
 184        default:
 185          $name = $user->getFullName();
 186          break;
 187      }
 188  
 189      return $name;
 190    }
 191  
 192  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1