Skip to content

Commit 464b9fb

Browse files
authored
Merge pull request #21551 from nextcloud/backport/21535/stable18
[stable18] Fix language in share notes email for users
2 parents 41a33b0 + 70cf8bd commit 464b9fb

File tree

3 files changed

+65
-40
lines changed

3 files changed

+65
-40
lines changed

lib/private/Share20/DefaultShareProvider.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@
4343
use OCP\Files\Folder;
4444
use OCP\Files\IRootFolder;
4545
use OCP\Files\Node;
46+
use OCP\IConfig;
4647
use OCP\IDBConnection;
4748
use OCP\IGroup;
4849
use OCP\IGroupManager;
49-
use OCP\IL10N;
5050
use OCP\IURLGenerator;
5151
use OCP\IUser;
5252
use OCP\IUserManager;
53+
use OCP\L10N\IFactory;
5354
use OCP\Mail\IMailer;
5455
use OCP\Share\Exceptions\ShareNotFound;
5556
use OCP\Share\IShare;
@@ -84,41 +85,34 @@ class DefaultShareProvider implements IShareProvider {
8485
/** @var Defaults */
8586
private $defaults;
8687

87-
/** @var IL10N */
88-
private $l;
88+
/** @var IFactory */
89+
private $l10nFactory;
8990

9091
/** @var IURLGenerator */
9192
private $urlGenerator;
9293

93-
/**
94-
* DefaultShareProvider constructor.
95-
*
96-
* @param IDBConnection $connection
97-
* @param IUserManager $userManager
98-
* @param IGroupManager $groupManager
99-
* @param IRootFolder $rootFolder
100-
* @param IMailer $mailer ;
101-
* @param Defaults $defaults
102-
* @param IL10N $l
103-
* @param IURLGenerator $urlGenerator
104-
*/
94+
/** @var IConfig */
95+
private $config;
96+
10597
public function __construct(
10698
IDBConnection $connection,
10799
IUserManager $userManager,
108100
IGroupManager $groupManager,
109101
IRootFolder $rootFolder,
110102
IMailer $mailer,
111103
Defaults $defaults,
112-
IL10N $l,
113-
IURLGenerator $urlGenerator) {
104+
IFactory $l10nFactory,
105+
IURLGenerator $urlGenerator,
106+
IConfig $config) {
114107
$this->dbConn = $connection;
115108
$this->userManager = $userManager;
116109
$this->groupManager = $groupManager;
117110
$this->rootFolder = $rootFolder;
118111
$this->mailer = $mailer;
119112
$this->defaults = $defaults;
120-
$this->l = $l;
113+
$this->l10nFactory = $l10nFactory;
121114
$this->urlGenerator = $urlGenerator;
115+
$this->config = $config;
122116
}
123117

124118
/**
@@ -1413,46 +1407,59 @@ private function propagateNote(IShare $share) {
14131407
*/
14141408
private function sendNote(array $recipients, IShare $share) {
14151409

1416-
$toList = [];
1410+
$toListByLanguage = [];
14171411

14181412
foreach ($recipients as $recipient) {
14191413
/** @var IUser $recipient */
14201414
$email = $recipient->getEMailAddress();
14211415
if ($email) {
1422-
$toList[$email] = $recipient->getDisplayName();
1416+
$language = $this->config->getSystemValue('force_language', false);
1417+
$language = \is_string($language) ? $language : $this->config->getUserValue($recipient->getUID(), 'core', 'lang', null);
1418+
$language = $language ?? $this->config->getSystemValue('default_language', 'en');
1419+
1420+
if (!isset($toListByLanguage[$language])) {
1421+
$toListByLanguage[$language] = [];
1422+
}
1423+
$toListByLanguage[$language][$email] = $recipient->getDisplayName();
14231424
}
14241425
}
14251426

1426-
if (!empty($toList)) {
1427+
if (empty($toListByLanguage)) {
1428+
return;
1429+
}
1430+
1431+
foreach ($toListByLanguage as $l10n => $toList) {
14271432

14281433
$filename = $share->getNode()->getName();
14291434
$initiator = $share->getSharedBy();
14301435
$note = $share->getNote();
14311436

1437+
$l = $this->l10nFactory->get('lib', $l10n);
1438+
14321439
$initiatorUser = $this->userManager->get($initiator);
14331440
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
14341441
$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
1435-
$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1436-
$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
1442+
$plainHeading = $l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1443+
$htmlHeading = $l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
14371444
$message = $this->mailer->createMessage();
14381445

14391446
$emailTemplate = $this->mailer->createEMailTemplate('defaultShareProvider.sendNote');
14401447

1441-
$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
1448+
$emailTemplate->setSubject($l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
14421449
$emailTemplate->addHeader();
14431450
$emailTemplate->addHeading($htmlHeading, $plainHeading);
14441451
$emailTemplate->addBodyText(htmlspecialchars($note), $note);
14451452

14461453
$link = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
14471454
$emailTemplate->addBodyButton(
1448-
$this->l->t('Open »%s«', [$filename]),
1455+
$l->t('Open »%s«', [$filename]),
14491456
$link
14501457
);
14511458

14521459

14531460
// The "From" contains the sharers name
14541461
$instanceName = $this->defaults->getName();
1455-
$senderName = $this->l->t(
1462+
$senderName = $l->t(
14561463
'%1$s via %2$s',
14571464
[
14581465
$initiatorDisplayName,

lib/private/Share20/ProviderFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ protected function defaultShareProvider() {
8989
$this->serverContainer->getLazyRootFolder(),
9090
$this->serverContainer->getMailer(),
9191
$this->serverContainer->query(Defaults::class),
92-
$this->serverContainer->getL10N('sharing'),
93-
$this->serverContainer->getURLGenerator()
92+
$this->serverContainer->getL10NFactory(),
93+
$this->serverContainer->getURLGenerator(),
94+
$this->serverContainer->getConfig()
9495
);
9596
}
9697

tests/lib/Share20/DefaultShareProviderTest.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@
2828
use OCP\Files\File;
2929
use OCP\Files\Folder;
3030
use OCP\Files\IRootFolder;
31+
use OCP\IConfig;
3132
use OCP\IDBConnection;
3233
use OCP\IGroup;
3334
use OCP\IGroupManager;
3435
use OCP\IL10N;
3536
use OCP\IURLGenerator;
3637
use OCP\IUser;
3738
use OCP\IUserManager;
39+
use OCP\L10N\IFactory;
3840
use OCP\Mail\IMailer;
3941
use OCP\Share\IShare;
42+
use PHPUnit\Framework\MockObject\MockObject;
4043

4144
/**
4245
* Class DefaultShareProviderTest
@@ -64,6 +67,9 @@ class DefaultShareProviderTest extends \Test\TestCase {
6467
/** @var \PHPUnit_Framework_MockObject_MockObject|IMailer */
6568
protected $mailer;
6669

70+
/** @var IFactory|MockObject */
71+
protected $l10nFactory;
72+
6773
/** @var \PHPUnit_Framework_MockObject_MockObject|IL10N */
6874
protected $l10n;
6975

@@ -73,15 +79,20 @@ class DefaultShareProviderTest extends \Test\TestCase {
7379
/** @var \PHPUnit_Framework_MockObject_MockObject|IURLGenerator */
7480
protected $urlGenerator;
7581

82+
/** @var IConfig|MockObject */
83+
protected $config;
84+
7685
protected function setUp(): void {
7786
$this->dbConn = \OC::$server->getDatabaseConnection();
7887
$this->userManager = $this->createMock(IUserManager::class);
7988
$this->groupManager = $this->createMock(IGroupManager::class);
8089
$this->rootFolder = $this->createMock(IRootFolder::class);
8190
$this->mailer = $this->createMock(IMailer::class);
91+
$this->l10nFactory = $this->createMock(IFactory::class);
8292
$this->l10n = $this->createMock(IL10N::class);
8393
$this->defaults = $this->getMockBuilder(Defaults::class)->disableOriginalConstructor()->getMock();
8494
$this->urlGenerator = $this->createMock(IURLGenerator::class);
95+
$this->config = $this->createMock(IConfig::class);
8596

8697
$this->userManager->expects($this->any())->method('userExists')->willReturn(true);
8798

@@ -95,8 +106,9 @@ protected function setUp(): void {
95106
$this->rootFolder,
96107
$this->mailer,
97108
$this->defaults,
98-
$this->l10n,
99-
$this->urlGenerator
109+
$this->l10nFactory,
110+
$this->urlGenerator,
111+
$this->config
100112
);
101113
}
102114

@@ -432,8 +444,9 @@ public function testDeleteSingleShare() {
432444
$this->rootFolder,
433445
$this->mailer,
434446
$this->defaults,
435-
$this->l10n,
436-
$this->urlGenerator
447+
$this->l10nFactory,
448+
$this->urlGenerator,
449+
$this->config
437450
])
438451
->setMethods(['getShareById'])
439452
->getMock();
@@ -526,8 +539,9 @@ public function testDeleteGroupShareWithUserGroupShares() {
526539
$this->rootFolder,
527540
$this->mailer,
528541
$this->defaults,
529-
$this->l10n,
530-
$this->urlGenerator
542+
$this->l10nFactory,
543+
$this->urlGenerator,
544+
$this->config
531545
])
532546
->setMethods(['getShareById'])
533547
->getMock();
@@ -2470,8 +2484,9 @@ public function testGetSharesInFolder() {
24702484
$rootFolder,
24712485
$this->mailer,
24722486
$this->defaults,
2473-
$this->l10n,
2474-
$this->urlGenerator
2487+
$this->l10nFactory,
2488+
$this->urlGenerator,
2489+
$this->config
24752490
);
24762491

24772492
$password = md5(time());
@@ -2567,8 +2582,9 @@ public function testGetAccessListNoCurrentAccessRequired() {
25672582
$rootFolder,
25682583
$this->mailer,
25692584
$this->defaults,
2570-
$this->l10n,
2571-
$this->urlGenerator
2585+
$this->l10nFactory,
2586+
$this->urlGenerator,
2587+
$this->config
25722588
);
25732589

25742590
$u1 = $userManager->createUser('testShare1', 'test');
@@ -2662,8 +2678,9 @@ public function testGetAccessListCurrentAccessRequired() {
26622678
$rootFolder,
26632679
$this->mailer,
26642680
$this->defaults,
2665-
$this->l10n,
2666-
$this->urlGenerator
2681+
$this->l10nFactory,
2682+
$this->urlGenerator,
2683+
$this->config
26672684
);
26682685

26692686
$u1 = $userManager->createUser('testShare1', 'test');

0 commit comments

Comments
 (0)