From b71a55900aca11393bbdd09c01c3a1b6198dd623 Mon Sep 17 00:00:00 2001 From: Marek Sapota Date: Tue, 8 Nov 2011 15:15:44 -0800 Subject: [PATCH] Allow tweaking of Differential mail by using events Summary: Allow tweaking Differential mail before sending. Test Plan: Wrote a listener renaming Differential attachments and it worked without problems. Reviewers: epriestley Reviewed By: epriestley CC: aran, epriestley, mareksapota, davidreuss Differential Revision: 1091 --- src/__phutil_library_map__.php | 1 + .../mail/base/DifferentialMail.php | 28 +++++----- .../differential/mail/base/__init__.php | 3 + .../DifferentialReviewRequestMail.php | 8 +-- .../mail/reviewrequest/__init__.php | 1 + .../send/PhabricatorMetaMTASendController.php | 5 +- .../mail/PhabricatorMetaMTAAttachment.php | 56 +++++++++++++++++++ .../storage/mail/PhabricatorMetaMTAMail.php | 23 +++++--- .../metamta/storage/mail/__init__.php | 1 + src/docs/userguide/events.diviner | 9 ++- .../constant/type/PhabricatorEventType.php | 1 + 11 files changed, 105 insertions(+), 31 deletions(-) create mode 100644 src/applications/metamta/storage/mail/PhabricatorMetaMTAAttachment.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index e13f816c77..d47aa5cfe4 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -488,6 +488,7 @@ phutil_register_library_map(array( 'PhabricatorMailImplementationTestAdapter' => 'applications/metamta/adapter/test', 'PhabricatorMailReplyHandler' => 'applications/metamta/replyhandler/base', 'PhabricatorMarkupEngine' => 'applications/markup/engine', + 'PhabricatorMetaMTAAttachment' => 'applications/metamta/storage/mail', 'PhabricatorMetaMTAController' => 'applications/metamta/controller/base', 'PhabricatorMetaMTADAO' => 'applications/metamta/storage/base', 'PhabricatorMetaMTADaemon' => 'applications/metamta/daemon/mta', diff --git a/src/applications/differential/mail/base/DifferentialMail.php b/src/applications/differential/mail/base/DifferentialMail.php index c298f533c3..b02ee16377 100644 --- a/src/applications/differential/mail/base/DifferentialMail.php +++ b/src/applications/differential/mail/base/DifferentialMail.php @@ -90,13 +90,7 @@ abstract class DifferentialMail { ->setParentMessageID($this->parentMessageID) ->addHeader('Thread-Topic', $this->getRevision()->getTitle()); - foreach ($attachments as $attachment) { - $template->addAttachment( - $attachment['data'], - $attachment['filename'], - $attachment['mimetype'] - ); - } + $template->setAttachments($attachments); $template->setThreadID( $this->getThreadID(), @@ -120,6 +114,16 @@ abstract class DifferentialMail { $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); + $event = new PhabricatorEvent( + PhabricatorEventType::TYPE_DIFFERENTIAL_WILLSENDMAIL, + array( + 'mail' => $template, + ) + ); + PhabricatorEventEngine::dispatchEvent($event); + + $template = $event->getValue('mail'); + $mails = $reply_handler->multiplexMail( $template, array_select_keys($handles, $to_phids), @@ -177,14 +181,8 @@ EOTEXT; /** * You can override this method in a subclass and return array of attachments - * to be sent with the email. Each attachment is a dictionary with 'data', - * 'filename' and 'mimetype' keys. For example: - * - * array( - * 'data' => 'some text', - * 'filename' => 'example.txt', - * 'mimetype' => 'text/plain' - * ); + * to be sent with the email. Each attachment is an instance of + * PhabricatorMetaMTAAttachment. */ protected function buildAttachments() { return array(); diff --git a/src/applications/differential/mail/base/__init__.php b/src/applications/differential/mail/base/__init__.php index c69b84db4d..c92e1dfce6 100644 --- a/src/applications/differential/mail/base/__init__.php +++ b/src/applications/differential/mail/base/__init__.php @@ -9,6 +9,9 @@ phutil_require_module('phabricator', 'applications/metamta/storage/mail'); phutil_require_module('phabricator', 'applications/phid/handle/data'); phutil_require_module('phabricator', 'infrastructure/env'); +phutil_require_module('phabricator', 'infrastructure/events/constant/type'); +phutil_require_module('phabricator', 'infrastructure/events/engine'); +phutil_require_module('phabricator', 'infrastructure/events/event'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/differential/mail/reviewrequest/DifferentialReviewRequestMail.php b/src/applications/differential/mail/reviewrequest/DifferentialReviewRequestMail.php index 2a88dcec60..00288ad0be 100644 --- a/src/applications/differential/mail/reviewrequest/DifferentialReviewRequestMail.php +++ b/src/applications/differential/mail/reviewrequest/DifferentialReviewRequestMail.php @@ -104,10 +104,10 @@ abstract class DifferentialReviewRequestMail extends DifferentialMail { $bundle = ArcanistBundle::newFromChanges($changes); $unified_diff = $bundle->toUnifiedDiff(); - $attachments[] = array( - 'data' => $unified_diff, - 'filename' => $filename, - 'mimetype' => 'text/x-patch; charset=utf-8' + $attachments[] = new PhabricatorMetaMTAAttachment( + $unified_diff, + $filename, + 'text/x-patch; charset=utf-8' ); } return $attachments; diff --git a/src/applications/differential/mail/reviewrequest/__init__.php b/src/applications/differential/mail/reviewrequest/__init__.php index e47543ab06..0c4c47cb38 100644 --- a/src/applications/differential/mail/reviewrequest/__init__.php +++ b/src/applications/differential/mail/reviewrequest/__init__.php @@ -10,6 +10,7 @@ phutil_require_module('arcanist', 'parser/bundle'); phutil_require_module('arcanist', 'parser/diff/change'); phutil_require_module('phabricator', 'applications/differential/mail/base'); +phutil_require_module('phabricator', 'applications/metamta/storage/mail'); phutil_require_module('phabricator', 'applications/phid/handle/data'); phutil_require_module('phabricator', 'infrastructure/env'); diff --git a/src/applications/metamta/controller/send/PhabricatorMetaMTASendController.php b/src/applications/metamta/controller/send/PhabricatorMetaMTASendController.php index 1a6d482e21..420e2e36d0 100644 --- a/src/applications/metamta/controller/send/PhabricatorMetaMTASendController.php +++ b/src/applications/metamta/controller/send/PhabricatorMetaMTASendController.php @@ -34,10 +34,11 @@ class PhabricatorMetaMTASendController extends PhabricatorMetaMTAController { if ($files) { foreach ($files as $phid) { $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $phid); - $mail->addAttachment( + $mail->addAttachment(new PhabricatorMetaMTAAttachment( $file->loadFileData(), $file->getName(), - $file->getMimeType()); + $file->getMimeType() + )); } } diff --git a/src/applications/metamta/storage/mail/PhabricatorMetaMTAAttachment.php b/src/applications/metamta/storage/mail/PhabricatorMetaMTAAttachment.php new file mode 100644 index 0000000000..155e36ff2b --- /dev/null +++ b/src/applications/metamta/storage/mail/PhabricatorMetaMTAAttachment.php @@ -0,0 +1,56 @@ +setData($data); + $this->setFileName($filename); + $this->setMimeType($mimetype); + } + + public function getData() { + return $this->data; + } + + public function setData($data) { + $this->data = $data; + return $this; + } + + public function getFileName() { + return $this->filename; + } + + public function setFileName($filename) { + $this->filename = $filename; + return $this; + } + + public function getMimeType() { + return $this->mimetype; + } + + public function setMimeType($mimetype) { + $this->mimetype = $mimetype; + return $this; + } +} diff --git a/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php b/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php index 0ffd7017e1..386ac84d7f 100644 --- a/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php +++ b/src/applications/metamta/storage/mail/PhabricatorMetaMTAMail.php @@ -104,12 +104,17 @@ class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO { return $this; } - public function addAttachment($data, $filename, $mimetype) { - $this->parameters['attachments'][] = array( - 'data' => $data, - 'filename' => $filename, - 'mimetype' => $mimetype - ); + public function addAttachment(PhabricatorMetaMTAAttachment $attachment) { + $this->parameters['attachments'][] = $attachment; + return $this; + } + + public function getAttachments() { + return $this->getParam('attachments'); + } + + public function setAttachments(array $attachments) { + $this->setParam('attachments', $attachments); return $this; } @@ -312,9 +317,9 @@ class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO { case 'attachments': foreach ($value as $attachment) { $mailer->addAttachment( - $attachment['data'], - $attachment['filename'], - $attachment['mimetype'] + $attachment->getData(), + $attachment->getFileName(), + $attachment->getMimeType() ); } break; diff --git a/src/applications/metamta/storage/mail/__init__.php b/src/applications/metamta/storage/mail/__init__.php index 8e7330cba4..a61fe4f106 100644 --- a/src/applications/metamta/storage/mail/__init__.php +++ b/src/applications/metamta/storage/mail/__init__.php @@ -14,4 +14,5 @@ phutil_require_module('phutil', 'symbols'); phutil_require_module('phutil', 'utils'); +phutil_require_source('PhabricatorMetaMTAAttachment.php'); phutil_require_source('PhabricatorMetaMTAMail.php'); diff --git a/src/docs/userguide/events.diviner b/src/docs/userguide/events.diviner index 2507594abe..c9894c8a29 100644 --- a/src/docs/userguide/events.diviner +++ b/src/docs/userguide/events.diviner @@ -24,4 +24,11 @@ or alter the edit. Data available on this event: @{class:ManiphestTransaction}) being applied. - ##new## A boolean indicating if this task is being created. - ##mail## If this edit originates from email, the - @{class:PhabricatorMetaMTAReceivedMail} object. \ No newline at end of file + @{class:PhabricatorMetaMTAReceivedMail} object. + +== PhabricatorEventType::TYPE_DIFFERENTIAL_WILLSENDMAIL == + +This event is dispatche before Differential sends an email, and allows you to +edit the message that will be sent. Data available on this event: + + - ##mail## The {@class:PhabricatorMetaMTAMail} being edited. diff --git a/src/infrastructure/events/constant/type/PhabricatorEventType.php b/src/infrastructure/events/constant/type/PhabricatorEventType.php index 4d89a573fe..2822d4f382 100644 --- a/src/infrastructure/events/constant/type/PhabricatorEventType.php +++ b/src/infrastructure/events/constant/type/PhabricatorEventType.php @@ -20,5 +20,6 @@ final class PhabricatorEventType extends PhabricatorEventConstants { const TYPE_ALL = '*'; const TYPE_MANIPHEST_WILLEDITTASK = 'maniphest.willEditTask'; + const TYPE_DIFFERENTIAL_WILLSENDMAIL = 'differential.willSendMail'; }