1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-30 01:10:58 +01:00

Remove email from handles

Summary:
Since user emails aren't in the user table, we had to do extra data fetching
for handles, and the emails are only used in MetaMTA, so we move the email
code into MetaMTA and remove it from handles.

Test Plan: send test emails

Reviewers: jungejason, vrana, epriestley

Reviewed By: epriestley

CC: aran, Koolvin

Differential Revision: https://secure.phabricator.com/D2494
This commit is contained in:
Nick Harper 2012-05-17 21:46:45 -07:00
parent 8bbc724210
commit c2a9a8079f
6 changed files with 91 additions and 60 deletions

View file

@ -301,14 +301,14 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
$value = array($value); $value = array($value);
} }
foreach (array_filter($value) as $phid) { foreach (array_filter($value) as $phid) {
$phids[] = $phid; $type = phid_get_type($phid);
$phids[$phid] = $type;
} }
break; break;
} }
} }
$handles = id(new PhabricatorObjectHandleData($phids)) $this->loadEmailAndNameDataFromPHIDs($phids);
->loadHandles();
$exclude = array(); $exclude = array();
@ -335,14 +335,13 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
} }
if (!PhabricatorEnv::getEnvConfig('metamta.can-send-as-user')) { if (!PhabricatorEnv::getEnvConfig('metamta.can-send-as-user')) {
$handle = $handles[$from];
if (empty($params['reply-to'])) { if (empty($params['reply-to'])) {
$params['reply-to'] = $handle->getEmail(); $params['reply-to'] = $phids[$from]['email'];
$params['reply-to-name'] = $handle->getFullName(); $params['reply-to-name'] = $phids[$from]['name'];
} }
$mailer->setFrom( $mailer->setFrom(
$default, $default,
$handle->getFullName()); $phids[$from]['name']);
unset($params['from']); unset($params['from']);
} }
} }
@ -361,30 +360,24 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
switch ($key) { switch ($key) {
case 'from': case 'from':
$mailer->setFrom($handles[$value]->getEmail()); $mailer->setFrom($phids[$from]['email']);
break; break;
case 'reply-to': case 'reply-to':
$mailer->addReplyTo($value, $reply_to_name); $mailer->addReplyTo($value, $reply_to_name);
break; break;
case 'to': case 'to':
$emails = $this->getDeliverableEmailsFromHandles( $to_emails = $this->filterSendable($value, $phids, $exclude);
$value, if ($to_emails) {
$handles, $add_to = array_merge($add_to, $to_emails);
$exclude);
if ($emails) {
$add_to = array_merge($add_to, $emails);
} }
break; break;
case 'raw-to': case 'raw-to':
$add_to = array_merge($add_to, $value); $add_to = array_merge($add_to, $value);
break; break;
case 'cc': case 'cc':
$emails = $this->getDeliverableEmailsFromHandles( $cc_emails = $this->filterSendable($value, $phids, $exclude);
$value, if ($cc_emails) {
$handles, $add_cc = $cc_emails;
$exclude);
if ($emails) {
$add_cc = $emails;
} }
break; break;
case 'headers': case 'headers':
@ -659,27 +652,81 @@ final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
return base64_encode($base); return base64_encode($base);
} }
private function getDeliverableEmailsFromHandles( private function loadEmailAndNameDataFromPHIDs(array &$phids) {
array $phids, $users = array();
array $handles, $mlsts = array();
array $exclude) { // first iteration - group by types to do data fetches
assert_instances_of($handles, 'PhabricatorObjectHandle'); foreach ($phids as $phid => $type) {
switch ($type) {
case PhabricatorPHIDConstants::PHID_TYPE_USER:
$users[] = $phid;
break;
case PhabricatorPHIDConstants::PHID_TYPE_MLST:
$mlsts[] = $phid;
break;
}
}
$user_emails = array();
if ($users) {
$user_emails = id(new PhabricatorUserEmail())->loadAllWhere(
'userPHID IN (%Ls) AND isPrimary = 1', $users);
$users = id(new PhabricatorUser())->loadAllWhere(
'phid IN (%Ls)', $users);
$user_emails = mpull($user_emails, null, 'getUserPHID');
$users = mpull($users, null, 'getPHID');
}
if ($mlsts) {
$mlsts = id(new PhabricatorMetaMTAMailingList())->loadAllWhere(
'phid IN (%Ls)', $mlsts);
$mlsts = mpull($mlsts, null, 'getPHID');
}
$emails = array(); // second iteration - create entries for each phid
foreach ($phids as $phid) { $default = PhabricatorEnv::getEnvConfig('metamta.default-address');
if ($handles[$phid]->isDisabled()) { foreach ($phids as $phid => &$value) {
continue; $name = '';
} $email = $default;
if (!$handles[$phid]->isComplete()) { $is_mailable = false;
continue; switch ($value) {
case PhabricatorPHIDConstants::PHID_TYPE_USER:
$user = $users[$phid];
if ($user) {
$name = $user->getFullName();
$is_mailable = !$user->getIsDisabled()
&& !$user->getIsSystemAgent();
}
$email = $user_emails[$phid] ?
$user_emails[$phid]->getAddress() :
$default;
break;
case PhabricatorPHIDConstants::PHID_TYPE_MLST:
$mlst = $mlsts[$phid];
if ($mlst) {
$name = $mlst->getName();
$email = $mlst->getEmail();
$is_mailable = true;
}
break;
} }
$value = array(
'name' => $name,
'email' => $email,
'mailable' => $is_mailable,
);
}
}
private function filterSendable($value, $phids, $exclude) {
$result = array();
foreach ($value as $phid) {
if (isset($exclude[$phid])) { if (isset($exclude[$phid])) {
continue; continue;
} }
$emails[$phid] = $handles[$phid]->getEmail(); if (isset($phids[$phid]) && $phids[$phid]['mailable']) {
$result[$phid] = $phids[$phid]['email'];
}
} }
return $result;
return $emails;
} }
public static function shouldMultiplexAllMail() { public static function shouldMultiplexAllMail() {

View file

@ -7,9 +7,12 @@
phutil_require_module('phabricator', 'applications/metamta/storage/base'); phutil_require_module('phabricator', 'applications/metamta/storage/base');
phutil_require_module('phabricator', 'applications/metamta/storage/mailinglist');
phutil_require_module('phabricator', 'applications/people/storage/email');
phutil_require_module('phabricator', 'applications/people/storage/preferences'); phutil_require_module('phabricator', 'applications/people/storage/preferences');
phutil_require_module('phabricator', 'applications/people/storage/user'); phutil_require_module('phabricator', 'applications/people/storage/user');
phutil_require_module('phabricator', 'applications/phid/handle/data'); phutil_require_module('phabricator', 'applications/phid/constants');
phutil_require_module('phabricator', 'applications/phid/utils');
phutil_require_module('phabricator', 'infrastructure/daemon/workers/storage/task'); phutil_require_module('phabricator', 'infrastructure/daemon/workers/storage/task');
phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'infrastructure/env');

View file

@ -581,6 +581,10 @@ EOBODY;
return self::getDefaultProfileImageURI(); return self::getDefaultProfileImageURI();
} }
public function getFullName() {
return $this->getUsername().' ('.$this->getRealName().')';
}
public static function loadOneWithEmailAddress($address) { public static function loadOneWithEmailAddress($address) {
$email = id(new PhabricatorUserEmail())->loadOneWhere( $email = id(new PhabricatorUserEmail())->loadOneWhere(
'address = %s', 'address = %s',

View file

@ -88,15 +88,6 @@ final class PhabricatorObjectHandle {
return $this->type; return $this->type;
} }
public function setEmail($email) {
$this->email = $email;
return $this;
}
public function getEmail() {
return $this->email;
}
public function setImageURI($uri) { public function setImageURI($uri) {
$this->imageURI = $uri; $this->imageURI = $uri;
return $this; return $this;

View file

@ -149,16 +149,6 @@ final class PhabricatorObjectHandleData {
$images = mpull($images, 'getBestURI', 'getPHID'); $images = mpull($images, 'getBestURI', 'getPHID');
} }
// TODO: This probably should not be part of Handles anymore, only
// MetaMTA actually uses it.
$emails = id(new PhabricatorUserEmail())->loadAllWhere(
'userPHID IN (%Ls) AND isPrimary = 1',
$phids);
$emails = mpull($emails, 'getAddress', 'getUserPHID');
$statuses = id(new PhabricatorUserStatus())->loadCurrentStatuses(
$phids);
foreach ($phids as $phid) { foreach ($phids as $phid) {
$handle = new PhabricatorObjectHandle(); $handle = new PhabricatorObjectHandle();
$handle->setPHID($phid); $handle->setPHID($phid);
@ -169,7 +159,6 @@ final class PhabricatorObjectHandleData {
$user = $users[$phid]; $user = $users[$phid];
$handle->setName($user->getUsername()); $handle->setName($user->getUsername());
$handle->setURI('/p/'.$user->getUsername().'/'); $handle->setURI('/p/'.$user->getUsername().'/');
$handle->setEmail(idx($emails, $phid));
$handle->setFullName( $handle->setFullName(
$user->getUsername().' ('.$user->getRealName().')'); $user->getUsername().' ('.$user->getRealName().')');
$handle->setAlternateID($user->getID()); $handle->setAlternateID($user->getID());
@ -177,8 +166,7 @@ final class PhabricatorObjectHandleData {
if (isset($statuses[$phid])) { if (isset($statuses[$phid])) {
$handle->setStatus($statuses[$phid]->getTextStatus()); $handle->setStatus($statuses[$phid]->getTextStatus());
} }
$handle->setDisabled($user->getIsDisabled() || $handle->setDisabled($user->getIsDisabled());
$user->getIsSystemAgent());
$img_uri = idx($images, $user->getProfileImagePHID()); $img_uri = idx($images, $user->getProfileImagePHID());
if ($img_uri) { if ($img_uri) {
@ -208,7 +196,6 @@ final class PhabricatorObjectHandleData {
$handle->setName('Unknown Mailing List'); $handle->setName('Unknown Mailing List');
} else { } else {
$list = $lists[$phid]; $list = $lists[$phid];
$handle->setEmail($list->getEmail());
$handle->setName($list->getName()); $handle->setName($list->getName());
$handle->setURI($list->getURI()); $handle->setURI($list->getURI());
$handle->setFullName($list->getName()); $handle->setFullName($list->getName());

View file

@ -11,7 +11,6 @@ phutil_require_module('arcanist', 'differential/constants/revisionstatus');
phutil_require_module('phabricator', 'applications/files/storage/file'); phutil_require_module('phabricator', 'applications/files/storage/file');
phutil_require_module('phabricator', 'applications/maniphest/constants/owner'); phutil_require_module('phabricator', 'applications/maniphest/constants/owner');
phutil_require_module('phabricator', 'applications/maniphest/constants/status'); phutil_require_module('phabricator', 'applications/maniphest/constants/status');
phutil_require_module('phabricator', 'applications/people/storage/email');
phutil_require_module('phabricator', 'applications/people/storage/user'); phutil_require_module('phabricator', 'applications/people/storage/user');
phutil_require_module('phabricator', 'applications/people/storage/userstatus'); phutil_require_module('phabricator', 'applications/people/storage/userstatus');
phutil_require_module('phabricator', 'applications/phid/constants'); phutil_require_module('phabricator', 'applications/phid/constants');