From d63ac46d8d0ed01ca998e852a5dc8fdf2f2e1ef2 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 15 Mar 2018 12:25:11 +0100 Subject: [PATCH] Embed personal note in link share email This instead of replacing the whole body --- core/ajax/share.php | 33 +++++++------ core/templates/altmail.php | 5 ++ core/templates/mail.php | 6 +++ .../Share/Filters/MailNotificationFilter.php | 14 ++++++ lib/private/Share/MailNotifications.php | 27 ++++++---- tests/lib/Share/MailNotificationsTest.php | 49 +++++++++++++++++++ 6 files changed, 108 insertions(+), 26 deletions(-) diff --git a/core/ajax/share.php b/core/ajax/share.php index 879468b621ec..9ed6dcba30b3 100644 --- a/core/ajax/share.php +++ b/core/ajax/share.php @@ -161,13 +161,19 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { break; case 'email': + $emailBody = null; + + if (isset($_POST['emailBody'])) { + $emailBody = trim((string)$_POST['emailBody']); + } // read and filter post variables $filter = new MailNotificationFilter([ 'link' => $_POST['link'], 'file' => $_POST['file'], - 'toAddress' => $_POST['toaddress'], - 'expiration' => $_POST['expiration'] + 'toAddress' => $_POST['toAddress'], + 'expiration' => $_POST['expiration'], + 'personalNote' => $emailBody ]); // read post variables @@ -175,11 +181,6 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { $file = (string)$_POST['file']; $toAddress = (string)$_POST['toAddress']; $options = array(); - $emailBody = null; - - if (isset($_POST['emailBody'])) { - $emailBody = trim((string)$_POST['emailBody']); - } if (isset($_POST['bccSelf']) && $_POST['bccSelf'] === 'true') { $options['bcc'] = \OC::$server->getUserSession()->getUser()->getEMailAddress(); @@ -207,16 +208,16 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { } } - $subject = (string)$l10n->t('%s shared »%s« with you', [$sendingUser->getDisplayName(), $filter->getFile()]); - if ($emailBody === null || $emailBody === '') { - list($htmlBody, $textBody) = $mailNotification->createMailBody($file, $link, $expiration); - } else { - $htmlBody = null; - $textBody = strip_tags($emailBody); + if ($emailBody !== null || $emailBody !== '') { + $emailBody = strip_tags($emailBody); } - - $result = $mailNotification->sendLinkShareMailFromBody($toAddress, $subject, $htmlBody, $textBody, $options); - + $result = $mailNotification->sendLinkShareMail( + $filter->getToAddress(), + $filter->getFile(), + $filter->getLink(), + $filter->getExpirationDate(), + $filter->getPersonalNote() + ); if(empty($result)) { // Get the token from the link $linkParts = explode('/', $link); diff --git a/core/templates/altmail.php b/core/templates/altmail.php index cf5285a868b6..da60443c6a4c 100644 --- a/core/templates/altmail.php +++ b/core/templates/altmail.php @@ -4,6 +4,11 @@ print_unescaped($l->t("The share will expire on %s.", [$_['expiration']])); print_unescaped("\n\n"); } +if (isset($_['personal_note'])) { + // TRANSLATORS personal note in share notification email + print_unescaped($l->t("Personal note from the sender: %s.", [$_['personal_note']])); + print_unescaped("\n\n"); +} // TRANSLATORS term at the end of a mail p($l->t("Cheers!")); ?> diff --git a/core/templates/mail.php b/core/templates/mail.php index b05eceebb805..2b1b668e218d 100644 --- a/core/templates/mail.php +++ b/core/templates/mail.php @@ -17,6 +17,12 @@ p($l->t("The share will expire on %s.", [$_['expiration']])); print_unescaped('

'); } + +if (isset($_['personal_note'])) { + // TRANSLATORS personal note in share notification email + p($l->t("Personal note from the sender: %s.", [$_['personal_note']])); + print_unescaped('

'); +} // TRANSLATORS term at the end of a mail p($l->t('Cheers!')); ?> diff --git a/lib/private/Share/Filters/MailNotificationFilter.php b/lib/private/Share/Filters/MailNotificationFilter.php index 756184a2dc3b..edf37898bf53 100644 --- a/lib/private/Share/Filters/MailNotificationFilter.php +++ b/lib/private/Share/Filters/MailNotificationFilter.php @@ -69,6 +69,13 @@ public function __construct($data = []) { ['name' => 'StripTags'], ], ], + 'personalNote' => [ + 'required' => false, + 'filters' => [ + ['name' => 'StringTrim'], + ['name' => 'StripTags'], + ], + ], 'expirationDate' => [ 'required' => true, 'filters' => [ @@ -108,4 +115,11 @@ public function getToAddress() { public function getExpirationDate() { return $this->inputFilter->getValue('expirationDate'); } + + /** + * @return string The filtered personal note + */ + public function getPersonalNote() { + return $this->inputFilter->getValue('personalNote'); + } } diff --git a/lib/private/Share/MailNotifications.php b/lib/private/Share/MailNotifications.php index b605c25b1fdc..8f964c17a501 100644 --- a/lib/private/Share/MailNotifications.php +++ b/lib/private/Share/MailNotifications.php @@ -138,7 +138,7 @@ public function sendInternalShareMail($recipientList, $itemSource, $itemType) { ['fileId' => $items[0]['item_source']] ); - list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal'); + list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, null, 'internal'); // send it out now try { @@ -169,9 +169,9 @@ public function sendInternalShareMail($recipientList, $itemSource, $itemType) { } - public function sendLinkShareMail($recipient, $filename, $link, $expiration) { + public function sendLinkShareMail($recipient, $filename, $link, $expiration, $personalNote = null) { $subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]); - list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration); + list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, $personalNote); return $this->sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $textBody); } @@ -230,24 +230,31 @@ public function sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $text * @param string $filename the shared file * @param string $link link to the shared file * @param int $expiration expiration date (timestamp) + * @param string $personalNote optional personal note * @param string $prefix prefix of mail template files * @return array an array of the html mail body and the plain text mail body */ - public function createMailBody($filename, $link, $expiration, $prefix = '') { + public function createMailBody($filename, $link, $expiration, $personalNote = null, $prefix = '') { $formattedDate = $expiration ? $this->l->l('date', $expiration) : null; $html = new \OC_Template('core', $prefix . 'mail', ''); - $html->assign ('link', $link); - $html->assign ('user_displayname', $this->senderDisplayName); - $html->assign ('filename', $filename); + $html->assign('link', $link); + $html->assign('user_displayname', $this->senderDisplayName); + $html->assign('filename', $filename); $html->assign('expiration', $formattedDate); + if ($personalNote !== null && $personalNote !== '') { + $html->assign('personal_note', $personalNote); + } $htmlMail = $html->fetchPage(); $plainText = new \OC_Template('core', $prefix . 'altmail', ''); - $plainText->assign ('link', $link); - $plainText->assign ('user_displayname', $this->senderDisplayName); - $plainText->assign ('filename', $filename); + $plainText->assign('link', $link); + $plainText->assign('user_displayname', $this->senderDisplayName); + $plainText->assign('filename', $filename); $plainText->assign('expiration', $formattedDate); + if ($personalNote !== null && $personalNote !== '') { + $plainText->assign('personal_note', $personalNote); + } $plainTextMail = $plainText->fetchPage(); return [$htmlMail, $plainTextMail]; diff --git a/tests/lib/Share/MailNotificationsTest.php b/tests/lib/Share/MailNotificationsTest.php index e4efb15dc7c0..ad6dc5efa4bd 100644 --- a/tests/lib/Share/MailNotificationsTest.php +++ b/tests/lib/Share/MailNotificationsTest.php @@ -131,6 +131,55 @@ public function testSendLinkShareMailWithoutReplyTo() { $this->assertSame([], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600)); } + public function testSendLinkShareMailPersonalNote() { + $message = $this->getMockBuilder('\OC\Mail\Message') + ->disableOriginalConstructor()->getMock(); + + $message + ->expects($this->once()) + ->method('setSubject') + ->with('TestUser shared »MyFile« with you'); + $message + ->expects($this->once()) + ->method('setTo') + ->with(['lukas@owncloud.com']); + $message + ->expects($this->once()) + ->method('setHtmlBody') + ->with($this->stringContains('personal note')); + $message + ->expects($this->once()) + ->method('setPlainBody') + ->with($this->stringContains('personal note')); + + $message + ->expects($this->once()) + ->method('setFrom') + ->with([Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']); + + $this->mailer + ->expects($this->once()) + ->method('createMessage') + ->will($this->returnValue($message)); + + $this->mailer + ->expects($this->once()) + ->method('send') + ->with($message) + ->will($this->returnValue([])); + + $mailNotifications = new MailNotifications( + $this->user, + $this->l10n, + $this->mailer, + $this->logger, + $this->defaults, + $this->urlGenerator + ); + + $this->assertSame([], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600, 'personal note')); + } + public function dataSendLinkShareMailWithReplyTo() { return [ ['lukas@owncloud.com', ['lukas@owncloud.com']],