Skip to content

Commit c5c3d04

Browse files
authored
Merge pull request #21535 from nextcloud/bugfix/noid/backportable-wrong-language-in-share-note-email
Fix language in share notes email for users
2 parents a29359a + 95652d4 commit c5c3d04

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,12 +43,13 @@
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\IGroupManager;
48-
use OCP\IL10N;
4949
use OCP\IURLGenerator;
5050
use OCP\IUser;
5151
use OCP\IUserManager;
52+
use OCP\L10N\IFactory;
5253
use OCP\Mail\IMailer;
5354
use OCP\Share\Exceptions\ShareNotFound;
5455
use OCP\Share\IShare;
@@ -82,41 +83,34 @@ class DefaultShareProvider implements IShareProvider {
8283
/** @var Defaults */
8384
private $defaults;
8485

85-
/** @var IL10N */
86-
private $l;
86+
/** @var IFactory */
87+
private $l10nFactory;
8788

8889
/** @var IURLGenerator */
8990
private $urlGenerator;
9091

91-
/**
92-
* DefaultShareProvider constructor.
93-
*
94-
* @param IDBConnection $connection
95-
* @param IUserManager $userManager
96-
* @param IGroupManager $groupManager
97-
* @param IRootFolder $rootFolder
98-
* @param IMailer $mailer ;
99-
* @param Defaults $defaults
100-
* @param IL10N $l
101-
* @param IURLGenerator $urlGenerator
102-
*/
92+
/** @var IConfig */
93+
private $config;
94+
10395
public function __construct(
10496
IDBConnection $connection,
10597
IUserManager $userManager,
10698
IGroupManager $groupManager,
10799
IRootFolder $rootFolder,
108100
IMailer $mailer,
109101
Defaults $defaults,
110-
IL10N $l,
111-
IURLGenerator $urlGenerator) {
102+
IFactory $l10nFactory,
103+
IURLGenerator $urlGenerator,
104+
IConfig $config) {
112105
$this->dbConn = $connection;
113106
$this->userManager = $userManager;
114107
$this->groupManager = $groupManager;
115108
$this->rootFolder = $rootFolder;
116109
$this->mailer = $mailer;
117110
$this->defaults = $defaults;
118-
$this->l = $l;
111+
$this->l10nFactory = $l10nFactory;
119112
$this->urlGenerator = $urlGenerator;
113+
$this->config = $config;
120114
}
121115

122116
/**
@@ -1401,45 +1395,58 @@ private function propagateNote(IShare $share) {
14011395
* @throws \OCP\Files\NotFoundException
14021396
*/
14031397
private function sendNote(array $recipients, IShare $share) {
1404-
$toList = [];
1398+
$toListByLanguage = [];
14051399

14061400
foreach ($recipients as $recipient) {
14071401
/** @var IUser $recipient */
14081402
$email = $recipient->getEMailAddress();
14091403
if ($email) {
1410-
$toList[$email] = $recipient->getDisplayName();
1404+
$language = $this->config->getSystemValue('force_language', false);
1405+
$language = \is_string($language) ? $language : $this->config->getUserValue($recipient->getUID(), 'core', 'lang', null);
1406+
$language = $language ?? $this->config->getSystemValue('default_language', 'en');
1407+
1408+
if (!isset($toListByLanguage[$language])) {
1409+
$toListByLanguage[$language] = [];
1410+
}
1411+
$toListByLanguage[$language][$email] = $recipient->getDisplayName();
14111412
}
14121413
}
14131414

1414-
if (!empty($toList)) {
1415+
if (empty($toListByLanguage)) {
1416+
return;
1417+
}
1418+
1419+
foreach ($toListByLanguage as $l10n => $toList) {
14151420
$filename = $share->getNode()->getName();
14161421
$initiator = $share->getSharedBy();
14171422
$note = $share->getNote();
14181423

1424+
$l = $this->l10nFactory->get('lib', $l10n);
1425+
14191426
$initiatorUser = $this->userManager->get($initiator);
14201427
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
14211428
$initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
1422-
$plainHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1423-
$htmlHeading = $this->l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
1429+
$plainHeading = $l->t('%1$s shared »%2$s« with you and wants to add:', [$initiatorDisplayName, $filename]);
1430+
$htmlHeading = $l->t('%1$s shared »%2$s« with you and wants to add', [$initiatorDisplayName, $filename]);
14241431
$message = $this->mailer->createMessage();
14251432

14261433
$emailTemplate = $this->mailer->createEMailTemplate('defaultShareProvider.sendNote');
14271434

1428-
$emailTemplate->setSubject($this->l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
1435+
$emailTemplate->setSubject($l->t('»%s« added a note to a file shared with you', [$initiatorDisplayName]));
14291436
$emailTemplate->addHeader();
14301437
$emailTemplate->addHeading($htmlHeading, $plainHeading);
14311438
$emailTemplate->addBodyText(htmlspecialchars($note), $note);
14321439

14331440
$link = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]);
14341441
$emailTemplate->addBodyButton(
1435-
$this->l->t('Open »%s«', [$filename]),
1442+
$l->t('Open »%s«', [$filename]),
14361443
$link
14371444
);
14381445

14391446

14401447
// The "From" contains the sharers name
14411448
$instanceName = $this->defaults->getName();
1442-
$senderName = $this->l->t(
1449+
$senderName = $l->t(
14431450
'%1$s via %2$s',
14441451
[
14451452
$initiatorDisplayName,

lib/private/Share20/ProviderFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ protected function defaultShareProvider() {
8787
$this->serverContainer->getLazyRootFolder(),
8888
$this->serverContainer->getMailer(),
8989
$this->serverContainer->query(Defaults::class),
90-
$this->serverContainer->getL10N('sharing'),
91-
$this->serverContainer->getURLGenerator()
90+
$this->serverContainer->getL10NFactory(),
91+
$this->serverContainer->getURLGenerator(),
92+
$this->serverContainer->getConfig()
9293
);
9394
}
9495

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

@@ -454,8 +466,9 @@ public function testDeleteSingleShare() {
454466
$this->rootFolder,
455467
$this->mailer,
456468
$this->defaults,
457-
$this->l10n,
458-
$this->urlGenerator
469+
$this->l10nFactory,
470+
$this->urlGenerator,
471+
$this->config
459472
])
460473
->setMethods(['getShareById'])
461474
->getMock();
@@ -548,8 +561,9 @@ public function testDeleteGroupShareWithUserGroupShares() {
548561
$this->rootFolder,
549562
$this->mailer,
550563
$this->defaults,
551-
$this->l10n,
552-
$this->urlGenerator
564+
$this->l10nFactory,
565+
$this->urlGenerator,
566+
$this->config
553567
])
554568
->setMethods(['getShareById'])
555569
->getMock();
@@ -2474,8 +2488,9 @@ public function testGetSharesInFolder() {
24742488
$rootFolder,
24752489
$this->mailer,
24762490
$this->defaults,
2477-
$this->l10n,
2478-
$this->urlGenerator
2491+
$this->l10nFactory,
2492+
$this->urlGenerator,
2493+
$this->config
24792494
);
24802495

24812496
$password = md5(time());
@@ -2571,8 +2586,9 @@ public function testGetAccessListNoCurrentAccessRequired() {
25712586
$rootFolder,
25722587
$this->mailer,
25732588
$this->defaults,
2574-
$this->l10n,
2575-
$this->urlGenerator
2589+
$this->l10nFactory,
2590+
$this->urlGenerator,
2591+
$this->config
25762592
);
25772593

25782594
$u1 = $userManager->createUser('testShare1', 'test');
@@ -2666,8 +2682,9 @@ public function testGetAccessListCurrentAccessRequired() {
26662682
$rootFolder,
26672683
$this->mailer,
26682684
$this->defaults,
2669-
$this->l10n,
2670-
$this->urlGenerator
2685+
$this->l10nFactory,
2686+
$this->urlGenerator,
2687+
$this->config
26712688
);
26722689

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

0 commit comments

Comments
 (0)