Skip to content

Commit

Permalink
Allow different language in public link share email
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Jun 6, 2018
1 parent cebbb4e commit 115cac1
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 23 deletions.
2 changes: 2 additions & 0 deletions core/ajax/share.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
\OC::$server->getUserSession()->getUser(),
\OC::$server->getL10N('lib'),
\OC::$server->getMailer(),
\OC::$server->getConfig(),
\OC::$server->getLogger(),
$defaults,
\OC::$server->getURLGenerator(),
Expand Down Expand Up @@ -190,6 +191,7 @@ function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
$sendingUser,
$l10n,
\OC::$server->getMailer(),
\OC::$server->getConfig(),
\OC::$server->getLogger(),
$defaults,
\OC::$server->getURLGenerator(),
Expand Down
74 changes: 54 additions & 20 deletions lib/private/Share/MailNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace OC\Share;

use DateTime;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
Expand Down Expand Up @@ -58,6 +59,8 @@ class MailNotifications {
private $l;
/** @var IMailer */
private $mailer;
/** @var IConfig */
private $config;
/** @var Defaults */
private $defaults;
/** @var ILogger */
Expand All @@ -71,20 +74,23 @@ class MailNotifications {
* @param IUser $user
* @param IL10N $l10n
* @param IMailer $mailer
* @param IConfig $config
* @param ILogger $logger
* @param Defaults $defaults
* @param IURLGenerator $urlGenerator
*/
public function __construct(IUser $user,
IL10N $l10n,
IMailer $mailer,
IConfig $config,
ILogger $logger,
Defaults $defaults,
IURLGenerator $urlGenerator,
EventDispatcher $eventDispatcher) {
$this->l = $l10n;
$this->user = $user;
$this->mailer = $mailer;
$this->config = $config;
$this->logger = $logger;
$this->defaults = $defaults;
$this->urlGenerator = $urlGenerator;
Expand Down Expand Up @@ -167,13 +173,7 @@ public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
$message->setTo([$to => $recipientDisplayName]);
$message->setHtmlBody($htmlBody);
$message->setPlainBody($textBody);
$message->setFrom([
Util::getDefaultEmailAddress('sharing-noreply') =>
(string)$this->l->t('%s via %s', [
$this->senderDisplayName,
$this->defaults->getName()
]),
]);
$message->setFrom($this->getFrom($this->l));
if ($this->replyTo !== null) {
$message->setReplyTo([$this->replyTo]);
}
Expand All @@ -189,8 +189,18 @@ public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
}

public function sendLinkShareMail($recipient, $filename, $link, $expiration, $personalNote = null, $options = []) {
$subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, $personalNote);
$notificationLang = $this->config->getAppValue(
'core',
'shareapi_public_notification_lang',
null
);
if ($notificationLang !== null) {
$l10n = \OC::$server->getL10N('lib', $notificationLang);
} else {
$l10n = $this->l;
}
$subject = (string)$l10n->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, $personalNote, '', $l10n);

/**
* The event consumer of share.sendmail would have following data
Expand All @@ -203,6 +213,7 @@ public function sendLinkShareMail($recipient, $filename, $link, $expiration, $pe
$bccRecipients = isset($options['bcc']) ? $options['bcc'] : '';
$event = new GenericEvent(null, ['link' => $link, 'to' => $recipient, 'cc' => $ccRecipients, 'bcc' => $bccRecipients]);
$this->eventDispatcher->dispatch('share.sendmail', $event);
$options['l10n'] = $l10n;
return $this->sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $textBody, $options);
}

Expand All @@ -223,6 +234,7 @@ public function sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $text
}
$ccRecipients = (isset($options['cc']) && $options['cc'] !== '') ? $this->_mailStringToArray($options['cc']) : null;
$bccRecipients = (isset($options['bcc']) && $options['bcc'] !== '') ? $this->_mailStringToArray($options['bcc']) : null;
$l10n = (isset($options['l10n'])) ? $options['l10n'] : $this->l;

try {
$message = $this->mailer->createMessage();
Expand All @@ -238,13 +250,7 @@ public function sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $text
$message->setCc($ccRecipients);
}
$message->setPlainBody($textBody);
$message->setFrom([
Util::getDefaultEmailAddress('sharing-noreply') =>
(string)$this->l->t('%s via %s', [
$this->senderDisplayName,
$this->defaults->getName()
]),
]);
$message->setFrom($this->getFrom($l10n));
if ($this->replyTo !== null) {
$message->setReplyTo([$this->replyTo]);
}
Expand All @@ -264,12 +270,21 @@ public function sendLinkShareMailFromBody($recipient, $subject, $htmlBody, $text
* @param int $expiration expiration date (timestamp)
* @param string $personalNote optional personal note
* @param string $prefix prefix of mail template files
* @param IL10N|null $overrideL10n
*
* @return array an array of the html mail body and the plain text mail body
*/
public function createMailBody($filename, $link, $expiration, $personalNote = null, $prefix = '') {
$formattedDate = $expiration ? $this->l->l('date', $expiration) : null;
public function createMailBody($filename,
$link,
$expiration,
$personalNote = null,
$prefix = '',
$overrideL10n = null
) {
$l10n = $overrideL10n === null ? $this->l : $overrideL10n;
$formattedDate = $expiration ? $l10n->l('date', $expiration) : null;

$html = new \OC_Template('core', $prefix . 'mail', '');
$html = new \OC_Template('core', $prefix . 'mail', '', true, $l10n);
$html->assign('link', $link);
$html->assign('user_displayname', $this->senderDisplayName);
$html->assign('filename', $filename);
Expand All @@ -279,7 +294,7 @@ public function createMailBody($filename, $link, $expiration, $personalNote = nu
}
$htmlMail = $html->fetchPage();

$plainText = new \OC_Template('core', $prefix . 'altmail', '');
$plainText = new \OC_Template('core', $prefix . 'altmail', '', true, $l10n);
$plainText->assign('link', $link);
$plainText->assign('user_displayname', $this->senderDisplayName);
$plainText->assign('filename', $filename);
Expand All @@ -301,4 +316,23 @@ public function createMailBody($filename, $link, $expiration, $personalNote = nu
protected function getItemSharedWithUser($itemSource, $itemType, $recipient) {
return Share::getItemSharedWithUser($itemType, $itemSource, $recipient->getUID());
}

/**
* Get default sender details
*
* @param IL10N $l10n
*
* @return string[]
*/
protected function getFrom($l10n) {
return [
Util::getDefaultEmailAddress('sharing-noreply') => (string) $l10n->t(
'%s via %s',
[
$this->senderDisplayName,
$this->defaults->getName()
]
)
];
}
}
11 changes: 8 additions & 3 deletions lib/private/legacy/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/

use OC\TemplateLayout;
use OCP\IL10N;
use OCP\Theme\ITheme;

require_once __DIR__.'/template/functions.php';
Expand Down Expand Up @@ -76,13 +77,17 @@ class OC_Template extends \OC\Template\Base {
* now, $renderAs can be set to "guest", "user" or
* "admin".
* @param bool $registerCall = true
* @param IL10N|null $l10n
*/
public function __construct($app, $name, $renderAs = "", $registerCall = true) {
public function __construct($app, $name, $renderAs = "", $registerCall = true, $l10n = null) {
self::initTemplateEngine($renderAs);
$requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';

$parts = \explode('/', $app); // fix translation when app is something like core/lostpassword
$l10n = \OC::$server->getL10N($parts[0]);
// fix translation when app is something like core/lostpassword
$parts = \explode('/', $app);

$languageCode = $l10n === null ? null : $l10n->getLanguageCode();
$l10n = \OC::$server->getL10N($parts[0], $languageCode);

$theme = OC_Util::getTheme();
$template = $this->findTemplate($theme, $app, $name);
Expand Down
49 changes: 49 additions & 0 deletions tests/lib/Share/MailNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use OC\Share\MailNotifications;
use OCP\Defaults;
use OCP\IConfig;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
Expand All @@ -45,6 +46,8 @@ class MailNotificationsTest extends TestCase {
private $mailer;
/** @var ILogger */
private $logger;
/** @var IConfig */
private $config;
/** @var Defaults | \PHPUnit_Framework_MockObject_MockObject */
private $defaults;
/** @var IUser | \PHPUnit_Framework_MockObject_MockObject */
Expand All @@ -62,6 +65,8 @@ public function setUp() {
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder('\OCP\ILogger')
->disableOriginalConstructor()->getMock();
$this->config = $this->getMockBuilder(IConfig::class)
->disableOriginalConstructor()->getMock();
$this->defaults = $this->getMockBuilder('\OCP\Defaults')
->disableOriginalConstructor()->getMock();
$this->user = $this->getMockBuilder('\OCP\IUser')
Expand Down Expand Up @@ -127,6 +132,7 @@ public function testSendLinkShareMailWithoutReplyTo() {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand Down Expand Up @@ -177,6 +183,7 @@ public function testSendLinkShareMailWithRecipientAndOptions() {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand Down Expand Up @@ -243,6 +250,7 @@ public function testSendLinkShareMailPersonalNote() {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand Down Expand Up @@ -321,6 +329,7 @@ public function testSendLinkShareMailWithReplyTo($to, array $expectedTo) {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand All @@ -336,6 +345,7 @@ public function testSendLinkShareMailException() {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand All @@ -355,6 +365,7 @@ public function testSendInternalShareMail() {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand Down Expand Up @@ -403,6 +414,7 @@ public function testSendInternalShareMailException() {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand Down Expand Up @@ -450,6 +462,7 @@ public function testGetItemSharedWithUser() {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand Down Expand Up @@ -495,6 +508,7 @@ public function testSendInternalShareMailNoMail($emptiness) {
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
Expand Down Expand Up @@ -528,6 +542,41 @@ public function testSendInternalShareMailNoMail($emptiness) {
$this->assertSame(['No mail 1', 'No mail 2'], $result);
}

public function testPublicLinkNotificationIsTranslated() {
$mailNotifications = new MailNotifications(
$this->user,
$this->l10n,
$this->mailer,
$this->config,
$this->logger,
$this->defaults,
$this->urlGenerator,
$this->eventDispatcher
);
$this->config->expects($this->once())
->method('getAppValue')
->with('core', 'shareapi_public_notification_lang', null)
->willReturn('ru');
$message = $this->getMockBuilder(\OC\Mail\Message::class)
->disableOriginalConstructor()->getMock();
$message
->expects($this->once())
->method('setFrom')
->with([Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser через UnitTestCloud']);

$this->mailer
->expects($this->once())
->method('createMessage')
->will($this->returnValue($message));

$this->l10n->expects($this->never())
->method('t');

$mailNotifications->sendLinkShareMail(
'justin@piper.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600
);
}

/**
* @param string $subject
*/
Expand Down

0 comments on commit 115cac1

Please sign in to comment.