diff --git a/resources/sql/autopatches/20150622.metamta.1.phid-col.sql b/resources/sql/autopatches/20150622.metamta.1.phid-col.sql new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20150622.metamta.1.phid-col.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_metamta.metamta_mail + ADD phid VARBINARY(64) NOT NULL AFTER id; diff --git a/resources/sql/autopatches/20150622.metamta.2.phid-mig.php b/resources/sql/autopatches/20150622.metamta.2.phid-mig.php new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20150622.metamta.2.phid-mig.php @@ -0,0 +1,22 @@ +establishConnection('w'); + +echo pht('Assigning PHIDs to mails...')."\n"; +foreach (new LiskMigrationIterator($table) as $mail) { + $id = $mail->getID(); + + echo pht('Updating mail %d...', $id)."\n"; + if ($mail->getPHID()) { + continue; + } + + queryfx( + $conn_w, + 'UPDATE %T SET phid = %s WHERE id = %d', + $table->getTableName(), + $table->generatePHID(), + $id); +} +echo pht('Done.')."\n"; diff --git a/resources/sql/autopatches/20150622.metamta.3.phid-key.sql b/resources/sql/autopatches/20150622.metamta.3.phid-key.sql new file mode 100644 --- /dev/null +++ b/resources/sql/autopatches/20150622.metamta.3.phid-key.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_metamta.metamta_mail + ADD UNIQUE KEY `key_phid` (phid); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2107,6 +2107,8 @@ 'PhabricatorMetaMTAMail' => 'applications/metamta/storage/PhabricatorMetaMTAMail.php', 'PhabricatorMetaMTAMailBody' => 'applications/metamta/view/PhabricatorMetaMTAMailBody.php', 'PhabricatorMetaMTAMailBodyTestCase' => 'applications/metamta/view/__tests__/PhabricatorMetaMTAMailBodyTestCase.php', + 'PhabricatorMetaMTAMailPHIDType' => 'applications/metamta/phid/PhabricatorMetaMTAMailPHIDType.php', + 'PhabricatorMetaMTAMailQuery' => 'applications/metamta/query/PhabricatorMetaMTAMailQuery.php', 'PhabricatorMetaMTAMailSection' => 'applications/metamta/view/PhabricatorMetaMTAMailSection.php', 'PhabricatorMetaMTAMailTestCase' => 'applications/metamta/storage/__tests__/PhabricatorMetaMTAMailTestCase.php', 'PhabricatorMetaMTAMailableDatasource' => 'applications/metamta/typeahead/PhabricatorMetaMTAMailableDatasource.php', @@ -5742,9 +5744,14 @@ 'PhabricatorMetaMTAEmailBodyParser' => 'Phobject', 'PhabricatorMetaMTAEmailBodyParserTestCase' => 'PhabricatorTestCase', 'PhabricatorMetaMTAErrorMailAction' => 'PhabricatorSystemAction', - 'PhabricatorMetaMTAMail' => 'PhabricatorMetaMTADAO', + 'PhabricatorMetaMTAMail' => array( + 'PhabricatorMetaMTADAO', + 'PhabricatorPolicyInterface', + ), 'PhabricatorMetaMTAMailBody' => 'Phobject', 'PhabricatorMetaMTAMailBodyTestCase' => 'PhabricatorTestCase', + 'PhabricatorMetaMTAMailPHIDType' => 'PhabricatorPHIDType', + 'PhabricatorMetaMTAMailQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhabricatorMetaMTAMailSection' => 'Phobject', 'PhabricatorMetaMTAMailTestCase' => 'PhabricatorTestCase', 'PhabricatorMetaMTAMailableDatasource' => 'PhabricatorTypeaheadCompositeDatasource', diff --git a/src/applications/metamta/phid/PhabricatorMetaMTAMailPHIDType.php b/src/applications/metamta/phid/PhabricatorMetaMTAMailPHIDType.php new file mode 100644 --- /dev/null +++ b/src/applications/metamta/phid/PhabricatorMetaMTAMailPHIDType.php @@ -0,0 +1,43 @@ +withPHIDs($phids); + } + + public function loadHandles( + PhabricatorHandleQuery $query, + array $handles, + array $objects) { + + foreach ($handles as $phid => $handle) { + $mail = $objects[$phid]; + + $id = $mail->getID(); + $subject = $mail->getSubject(); + + $handle + ->setName($subject) + ->setFullName(pht('Mail %d: %s', $id, $subject)); + } + } +} diff --git a/src/applications/metamta/query/PhabricatorMetaMTAMailQuery.php b/src/applications/metamta/query/PhabricatorMetaMTAMailQuery.php new file mode 100644 --- /dev/null +++ b/src/applications/metamta/query/PhabricatorMetaMTAMailQuery.php @@ -0,0 +1,69 @@ +ids = $ids; + return $this; + } + + public function withPHIDs(array $phids) { + $this->phids = $phids; + return $this; + } + + protected function loadPage() { + $table = new PhabricatorMetaMTAMailQuery(); + $conn_r = $table->establishConnection('r'); + $viewer = $this->getViewer(); + + $data = queryfx_all( + $conn_r, + 'SELECT mail.* FROM %T mail %Q %Q %Q', + $table->getTableName(), + $this->buildWhereClause($conn_r), + $this->buildOrderClause($conn_r), + $this->buildLimitClause($conn_r)); + + return $table->loadAllFromArray($data); + } + + protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { + $where = array(); + + if ($this->ids !== null) { + $where[] = qsprintf( + $conn_r, + 'mail.id IN (%Ld)', + $this->ids); + } + + if ($this->phids !== null) { + $where[] = qsprintf( + $conn_r, + 'mail.phid IN (%Ls)', + $this->phids); + } + + $where[] = $this->buildPagingClause($conn_r); + + return $this->formatWhereClause($where); + } + + protected function getPrimaryTableAlias() { + return 'mail'; + } + + protected function getApplicationSearchObjectPHIDColumn() { + return 'mail.phid'; + } + + public function getQueryApplicationClass() { + return 'PhabricatorMetaMTAApplication'; + } + +} diff --git a/src/applications/metamta/storage/PhabricatorMetaMTAMail.php b/src/applications/metamta/storage/PhabricatorMetaMTAMail.php --- a/src/applications/metamta/storage/PhabricatorMetaMTAMail.php +++ b/src/applications/metamta/storage/PhabricatorMetaMTAMail.php @@ -3,7 +3,9 @@ /** * @task recipients Managing Recipients */ -final class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO { +final class PhabricatorMetaMTAMail + extends PhabricatorMetaMTADAO + implements PhabricatorPolicyInterface { const STATUS_QUEUE = 'queued'; const STATUS_SENT = 'sent'; @@ -29,6 +31,7 @@ protected function getConfiguration() { return array( + self::CONFIG_AUX_PHID => true, self::CONFIG_SERIALIZATION => array( 'parameters' => self::SERIALIZATION_JSON, ), @@ -54,6 +57,11 @@ ) + parent::getConfiguration(); } + public function generatePHID() { + return PhabricatorPHID::generateNewPHID( + PhabricatorMetaMTAMailPHIDType::TYPECONST); + } + protected function setParam($param, $value) { $this->parameters[$param] = $value; return $this; @@ -993,4 +1001,27 @@ } +/* -( PhabricatorPolicyInterface )----------------------------------------- */ + + + public function getCapabilities() { + return array( + PhabricatorPolicyCapability::CAN_VIEW, + ); + } + + public function getPolicy($capability) { + return PhabricatorPolicies::POLICY_NOONE; + } + + public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { + return in_array($viewer->getPHID(), $this->getAllActorPHIDs()); + } + + public function describeAutomaticCapability($capability) { + return pht( + 'The mail sender and message recipients can always see the mail.'); + } + + }