From fe0ca0abf267d8a59f8fa13353c4ff6ea59bdb09 Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Wed, 28 Jan 2015 14:35:42 -0800 Subject: [PATCH] Application Emails - add datasource so we can have a typeahead Summary: Ref T5039. This will be necessary for Herald integration so users can make rules like "if app email is one of x, y, or z add projects foo, bar, and metallica." I think its best to do an actual typeahead here -- users select full email addresses -- rather than support prefix, suffix, etc stuff on the email address. I think the latter approach would yield lots of confusion, as well as prevent us from (more) easily providing diagnostic tools about what happened when and why. Test Plan: hacked a maniphest tokenizer to use this new datasource and it worked Reviewers: epriestley Reviewed By: epriestley Subscribers: Korvin, epriestley Maniphest Tasks: T5039 Differential Revision: https://secure.phabricator.com/D11546 --- src/__phutil_library_map__.php | 2 + ...habricatorMetaMTAApplicationEmailQuery.php | 13 ++++++ ...catorMetaMTAApplicationEmailDatasource.php | 43 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/applications/metamta/typeahead/PhabricatorMetaMTAApplicationEmailDatasource.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 2fac7ce13a..b65acebb9f 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -1953,6 +1953,7 @@ phutil_register_library_map(array( 'PhabricatorMetaMTAActorQuery' => 'applications/metamta/query/PhabricatorMetaMTAActorQuery.php', 'PhabricatorMetaMTAApplication' => 'applications/metamta/application/PhabricatorMetaMTAApplication.php', 'PhabricatorMetaMTAApplicationEmail' => 'applications/metamta/storage/PhabricatorMetaMTAApplicationEmail.php', + 'PhabricatorMetaMTAApplicationEmailDatasource' => 'applications/metamta/typeahead/PhabricatorMetaMTAApplicationEmailDatasource.php', 'PhabricatorMetaMTAApplicationEmailPHIDType' => 'applications/phid/PhabricatorMetaMTAApplicationEmailPHIDType.php', 'PhabricatorMetaMTAApplicationEmailQuery' => 'applications/metamta/query/PhabricatorMetaMTAApplicationEmailQuery.php', 'PhabricatorMetaMTAAttachment' => 'applications/metamta/storage/PhabricatorMetaMTAAttachment.php', @@ -5184,6 +5185,7 @@ phutil_register_library_map(array( 'PhabricatorMetaMTADAO', 'PhabricatorPolicyInterface', ), + 'PhabricatorMetaMTAApplicationEmailDatasource' => 'PhabricatorTypeaheadDatasource', 'PhabricatorMetaMTAApplicationEmailPHIDType' => 'PhabricatorPHIDType', 'PhabricatorMetaMTAApplicationEmailQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorMetaMTAConfigOptions' => 'PhabricatorApplicationConfigOptions', diff --git a/src/applications/metamta/query/PhabricatorMetaMTAApplicationEmailQuery.php b/src/applications/metamta/query/PhabricatorMetaMTAApplicationEmailQuery.php index 367e267dc1..3983e362cc 100644 --- a/src/applications/metamta/query/PhabricatorMetaMTAApplicationEmailQuery.php +++ b/src/applications/metamta/query/PhabricatorMetaMTAApplicationEmailQuery.php @@ -6,6 +6,7 @@ final class PhabricatorMetaMTAApplicationEmailQuery private $ids; private $phids; private $addresses; + private $addressPrefix; private $applicationPHIDs; public function withIDs(array $ids) { @@ -23,6 +24,11 @@ final class PhabricatorMetaMTAApplicationEmailQuery return $this; } + public function withAddressPrefix($prefix) { + $this->addressPrefix = $prefix; + return $this; + } + public function withApplicationPHIDs(array $phids) { $this->applicationPHIDs = $phids; return $this; @@ -75,6 +81,13 @@ final class PhabricatorMetaMTAApplicationEmailQuery $this->addresses); } + if ($this->addressPrefix !== null) { + $where[] = qsprintf( + $conn_r, + 'appemail.address LIKE %>', + $this->addressPrefix); + } + if ($this->applicationPHIDs !== null) { $where[] = qsprintf( $conn_r, diff --git a/src/applications/metamta/typeahead/PhabricatorMetaMTAApplicationEmailDatasource.php b/src/applications/metamta/typeahead/PhabricatorMetaMTAApplicationEmailDatasource.php new file mode 100644 index 0000000000..bbe5e0d8f6 --- /dev/null +++ b/src/applications/metamta/typeahead/PhabricatorMetaMTAApplicationEmailDatasource.php @@ -0,0 +1,43 @@ +getViewer(); + $raw_query = $this->getRawQuery(); + + $emails = id(new PhabricatorMetaMTAApplicationEmailQuery()) + ->setViewer($viewer) + ->withAddressPrefix($raw_query) + ->setLimit($this->getLimit()) + ->execute(); + + if ($emails) { + $handles = id(new PhabricatorHandleQuery()) + ->setViewer($viewer) + ->withPHIDs(mpull($emails, 'getPHID')) + ->execute(); + } else { + $handles = array(); + } + + $results = array(); + foreach ($handles as $handle) { + $results[] = id(new PhabricatorTypeaheadResult()) + ->setName($handle->getName()) + ->setPHID($handle->getPHID()); + } + + return $results; + } + +}