Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/query/PhabricatorMetaMTAActorQuery.php
12256 views
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
default:
40
$this->loadUnknownActors($actors, $phids);
41
break;
42
}
43
}
44
45
return $actors;
46
}
47
48
private function loadUserActors(array $actors, array $phids) {
49
assert_instances_of($actors, 'PhabricatorMetaMTAActor');
50
51
$emails = id(new PhabricatorUserEmail())->loadAllWhere(
52
'userPHID IN (%Ls) AND isPrimary = 1',
53
$phids);
54
$emails = mpull($emails, null, 'getUserPHID');
55
56
$users = id(new PhabricatorPeopleQuery())
57
->setViewer($this->getViewer())
58
->withPHIDs($phids)
59
->needUserSettings(true)
60
->execute();
61
$users = mpull($users, null, 'getPHID');
62
63
foreach ($phids as $phid) {
64
$actor = $actors[$phid];
65
66
$user = idx($users, $phid);
67
if (!$user) {
68
$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_UNLOADABLE);
69
} else {
70
$actor->setName($this->getUserName($user));
71
if ($user->getIsDisabled()) {
72
$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_DISABLED);
73
}
74
if ($user->getIsSystemAgent()) {
75
$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_BOT);
76
}
77
78
// NOTE: We do send email to unapproved users, and to unverified users,
79
// because it would otherwise be impossible to get them to verify their
80
// email addresses. Possibly we should white-list this kind of mail and
81
// deny all other types of mail.
82
}
83
84
$email = idx($emails, $phid);
85
if (!$email) {
86
$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_NO_ADDRESS);
87
} else {
88
$actor->setEmailAddress($email->getAddress());
89
$actor->setIsVerified($email->getIsVerified());
90
}
91
}
92
}
93
94
private function loadUnknownActors(array $actors, array $phids) {
95
foreach ($phids as $phid) {
96
$actor = $actors[$phid];
97
$actor->setUndeliverable(PhabricatorMetaMTAActor::REASON_UNMAILABLE);
98
}
99
}
100
101
102
/**
103
* Small helper function to make sure we format the username properly as
104
* specified by the `metamta.user-address-format` configuration value.
105
*/
106
private function getUserName(PhabricatorUser $user) {
107
$format = PhabricatorEnv::getEnvConfig('metamta.user-address-format');
108
109
switch ($format) {
110
case 'short':
111
$name = $user->getUserName();
112
break;
113
case 'real':
114
$name = strlen($user->getRealName()) ?
115
$user->getRealName() : $user->getUserName();
116
break;
117
case 'full':
118
default:
119
$name = $user->getFullName();
120
break;
121
}
122
123
return $name;
124
}
125
126
}
127
128