1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-21 13:00:56 +01:00

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
This commit is contained in:
Marek Sapota 2011-11-08 15:15:44 -08:00
parent 802dcd4cfb
commit b71a55900a
11 changed files with 105 additions and 31 deletions

View file

@ -488,6 +488,7 @@ phutil_register_library_map(array(
'PhabricatorMailImplementationTestAdapter' => 'applications/metamta/adapter/test', 'PhabricatorMailImplementationTestAdapter' => 'applications/metamta/adapter/test',
'PhabricatorMailReplyHandler' => 'applications/metamta/replyhandler/base', 'PhabricatorMailReplyHandler' => 'applications/metamta/replyhandler/base',
'PhabricatorMarkupEngine' => 'applications/markup/engine', 'PhabricatorMarkupEngine' => 'applications/markup/engine',
'PhabricatorMetaMTAAttachment' => 'applications/metamta/storage/mail',
'PhabricatorMetaMTAController' => 'applications/metamta/controller/base', 'PhabricatorMetaMTAController' => 'applications/metamta/controller/base',
'PhabricatorMetaMTADAO' => 'applications/metamta/storage/base', 'PhabricatorMetaMTADAO' => 'applications/metamta/storage/base',
'PhabricatorMetaMTADaemon' => 'applications/metamta/daemon/mta', 'PhabricatorMetaMTADaemon' => 'applications/metamta/daemon/mta',

View file

@ -90,13 +90,7 @@ abstract class DifferentialMail {
->setParentMessageID($this->parentMessageID) ->setParentMessageID($this->parentMessageID)
->addHeader('Thread-Topic', $this->getRevision()->getTitle()); ->addHeader('Thread-Topic', $this->getRevision()->getTitle());
foreach ($attachments as $attachment) { $template->setAttachments($attachments);
$template->addAttachment(
$attachment['data'],
$attachment['filename'],
$attachment['mimetype']
);
}
$template->setThreadID( $template->setThreadID(
$this->getThreadID(), $this->getThreadID(),
@ -120,6 +114,16 @@ abstract class DifferentialMail {
$handles = id(new PhabricatorObjectHandleData($phids))->loadHandles(); $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( $mails = $reply_handler->multiplexMail(
$template, $template,
array_select_keys($handles, $to_phids), array_select_keys($handles, $to_phids),
@ -177,14 +181,8 @@ EOTEXT;
/** /**
* You can override this method in a subclass and return array of attachments * 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', * to be sent with the email. Each attachment is an instance of
* 'filename' and 'mimetype' keys. For example: * PhabricatorMetaMTAAttachment.
*
* array(
* 'data' => 'some text',
* 'filename' => 'example.txt',
* 'mimetype' => 'text/plain'
* );
*/ */
protected function buildAttachments() { protected function buildAttachments() {
return array(); return array();

View file

@ -9,6 +9,9 @@
phutil_require_module('phabricator', 'applications/metamta/storage/mail'); phutil_require_module('phabricator', 'applications/metamta/storage/mail');
phutil_require_module('phabricator', 'applications/phid/handle/data'); phutil_require_module('phabricator', 'applications/phid/handle/data');
phutil_require_module('phabricator', 'infrastructure/env'); 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'); phutil_require_module('phutil', 'utils');

View file

@ -104,10 +104,10 @@ abstract class DifferentialReviewRequestMail extends DifferentialMail {
$bundle = ArcanistBundle::newFromChanges($changes); $bundle = ArcanistBundle::newFromChanges($changes);
$unified_diff = $bundle->toUnifiedDiff(); $unified_diff = $bundle->toUnifiedDiff();
$attachments[] = array( $attachments[] = new PhabricatorMetaMTAAttachment(
'data' => $unified_diff, $unified_diff,
'filename' => $filename, $filename,
'mimetype' => 'text/x-patch; charset=utf-8' 'text/x-patch; charset=utf-8'
); );
} }
return $attachments; return $attachments;

View file

@ -10,6 +10,7 @@ phutil_require_module('arcanist', 'parser/bundle');
phutil_require_module('arcanist', 'parser/diff/change'); phutil_require_module('arcanist', 'parser/diff/change');
phutil_require_module('phabricator', 'applications/differential/mail/base'); 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', 'applications/phid/handle/data');
phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'infrastructure/env');

View file

@ -34,10 +34,11 @@ class PhabricatorMetaMTASendController extends PhabricatorMetaMTAController {
if ($files) { if ($files) {
foreach ($files as $phid) { foreach ($files as $phid) {
$file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $phid); $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $phid);
$mail->addAttachment( $mail->addAttachment(new PhabricatorMetaMTAAttachment(
$file->loadFileData(), $file->loadFileData(),
$file->getName(), $file->getName(),
$file->getMimeType()); $file->getMimeType()
));
} }
} }

View file

@ -0,0 +1,56 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class PhabricatorMetaMTAAttachment {
protected $data;
protected $filename;
protected $mimetype;
public function __construct($data, $filename, $mimetype) {
$this->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;
}
}

View file

@ -104,12 +104,17 @@ class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
return $this; return $this;
} }
public function addAttachment($data, $filename, $mimetype) { public function addAttachment(PhabricatorMetaMTAAttachment $attachment) {
$this->parameters['attachments'][] = array( $this->parameters['attachments'][] = $attachment;
'data' => $data, return $this;
'filename' => $filename, }
'mimetype' => $mimetype
); public function getAttachments() {
return $this->getParam('attachments');
}
public function setAttachments(array $attachments) {
$this->setParam('attachments', $attachments);
return $this; return $this;
} }
@ -312,9 +317,9 @@ class PhabricatorMetaMTAMail extends PhabricatorMetaMTADAO {
case 'attachments': case 'attachments':
foreach ($value as $attachment) { foreach ($value as $attachment) {
$mailer->addAttachment( $mailer->addAttachment(
$attachment['data'], $attachment->getData(),
$attachment['filename'], $attachment->getFileName(),
$attachment['mimetype'] $attachment->getMimeType()
); );
} }
break; break;

View file

@ -14,4 +14,5 @@ phutil_require_module('phutil', 'symbols');
phutil_require_module('phutil', 'utils'); phutil_require_module('phutil', 'utils');
phutil_require_source('PhabricatorMetaMTAAttachment.php');
phutil_require_source('PhabricatorMetaMTAMail.php'); phutil_require_source('PhabricatorMetaMTAMail.php');

View file

@ -24,4 +24,11 @@ or alter the edit. Data available on this event:
@{class:ManiphestTransaction}) being applied. @{class:ManiphestTransaction}) being applied.
- ##new## A boolean indicating if this task is being created. - ##new## A boolean indicating if this task is being created.
- ##mail## If this edit originates from email, the - ##mail## If this edit originates from email, the
@{class:PhabricatorMetaMTAReceivedMail} object. @{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.

View file

@ -20,5 +20,6 @@ final class PhabricatorEventType extends PhabricatorEventConstants {
const TYPE_ALL = '*'; const TYPE_ALL = '*';
const TYPE_MANIPHEST_WILLEDITTASK = 'maniphest.willEditTask'; const TYPE_MANIPHEST_WILLEDITTASK = 'maniphest.willEditTask';
const TYPE_DIFFERENTIAL_WILLSENDMAIL = 'differential.willSendMail';
} }