Path: blob/master/src/applications/metamta/query/PhabricatorMetaMTAActorQuery.php
12256 views
<?php12final class PhabricatorMetaMTAActorQuery extends PhabricatorQuery {34private $phids = array();5private $viewer;67public function setViewer(PhabricatorUser $viewer) {8$this->viewer = $viewer;9return $this;10}1112public function getViewer() {13return $this->viewer;14}1516public function withPHIDs(array $phids) {17$this->phids = $phids;18return $this;19}2021public function execute() {22$phids = array_fuse($this->phids);23$actors = array();24$type_map = array();25foreach ($phids as $phid) {26$type_map[phid_get_type($phid)][] = $phid;27$actors[$phid] = id(new PhabricatorMetaMTAActor())->setPHID($phid);28}2930// TODO: Move this to PhabricatorPHIDType, or the objects, or some31// interface.3233foreach ($type_map as $type => $phids) {34switch ($type) {35case PhabricatorPeopleUserPHIDType::TYPECONST:36$this->loadUserActors($actors, $phids);37break;38default:39$this->loadUnknownActors($actors, $phids);40break;41}42}4344return $actors;45}4647private function loadUserActors(array $actors, array $phids) {48assert_instances_of($actors, 'PhabricatorMetaMTAActor');4950$emails = id(new PhabricatorUserEmail())->loadAllWhere(51'userPHID IN (%Ls) AND isPrimary = 1',52$phids);53$emails = mpull($emails, null, 'getUserPHID');5455$users = id(new PhabricatorPeopleQuery())56->setViewer($this->getViewer())57->withPHIDs($phids)58->needUserSettings(true)59->execute();60$users = mpull($users, null, 'getPHID');6162foreach ($phids as $phid) {63$actor = $actors[$phid];6465$user = idx($users, $phid);66if (!$user) {67$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_UNLOADABLE);68} else {69$actor->setName($this->getUserName($user));70if ($user->getIsDisabled()) {71$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_DISABLED);72}73if ($user->getIsSystemAgent()) {74$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_BOT);75}7677// NOTE: We do send email to unapproved users, and to unverified users,78// because it would otherwise be impossible to get them to verify their79// email addresses. Possibly we should white-list this kind of mail and80// deny all other types of mail.81}8283$email = idx($emails, $phid);84if (!$email) {85$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_NO_ADDRESS);86} else {87$actor->setEmailAddress($email->getAddress());88$actor->setIsVerified($email->getIsVerified());89}90}91}9293private function loadUnknownActors(array $actors, array $phids) {94foreach ($phids as $phid) {95$actor = $actors[$phid];96$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_UNMAILABLE);97}98}99100101/**102* Small helper function to make sure we format the username properly as103* specified by the `metamta.user-address-format` configuration value.104*/105private function getUserName(PhabricatorUser $user) {106$format = PhabricatorEnv::getEnvConfig('metamta.user-address-format');107108switch ($format) {109case 'short':110$name = $user->getUserName();111break;112case 'real':113$name = strlen($user->getRealName()) ?114$user->getRealName() : $user->getUserName();115break;116case 'full':117default:118$name = $user->getFullName();119break;120}121122return $name;123}124125}126127128