Skip to content

Commit 40fe9db

Browse files
Merge pull request #2709 from nextcloud/backport/2673/stable33
[stable33] feat: make amount of items sent on email configurable via occ
2 parents ba5f1c9 + 688008c commit 40fe9db

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

lib/MailQueueHandler.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
class MailQueueHandler {
3737
public const CLI_EMAIL_BATCH_SIZE = 500;
3838
public const WEB_EMAIL_BATCH_SIZE = 25;
39-
/** Number of entries we want to list in the email */
40-
public const ENTRY_LIMIT = 200;
39+
public const MAIL_MAX_ITEMS_DEFAULT = 200;
40+
public const MAIL_MAX_ITEMS_CAP = 1000;
4141

4242
protected array $languages;
4343
protected string $senderAddress;
@@ -191,7 +191,7 @@ private function fetchAffectedUsers(IQueryBuilder $query): array {
191191
*
192192
* @return array [data of the first max. 200 entries, total number of entries]
193193
*/
194-
protected function getItemsForUser(string $affectedUser, int $maxTime, int $maxNumItems = self::ENTRY_LIMIT): array {
194+
protected function getItemsForUser(string $affectedUser, int $maxTime, int $maxNumItems = self::MAIL_MAX_ITEMS_DEFAULT): array {
195195
$query = $this->connection->getQueryBuilder();
196196
$query->select('*')
197197
->from('activity_mq')
@@ -282,7 +282,7 @@ protected function sendEmailToUser(string $userName, string $email, string $lang
282282
return true;
283283
}
284284

285-
[$mailData, $skippedCount] = $this->getItemsForUser($userName, $maxTime);
285+
[$mailData, $skippedCount] = $this->getItemsForUser($userName, $maxTime, $this->getMailMaxItems());
286286

287287
$l = $this->getLanguage($lang);
288288
$this->activityManager->setCurrentUserId($userName);
@@ -416,4 +416,15 @@ protected function deleteSentItems(array $affectedUsers, int $maxTime): void {
416416
->andWhere($query->expr()->in('amq_affecteduser', $query->createNamedParameter($affectedUsers, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR));
417417
$query->executeStatement();
418418
}
419+
420+
private function getMailMaxItems(): int {
421+
$maxItems = $this->appConfig->getValueInt('activity', 'mail_max_items', self::MAIL_MAX_ITEMS_DEFAULT);
422+
if ($maxItems < 1) {
423+
return 1;
424+
}
425+
if ($maxItems > self::MAIL_MAX_ITEMS_CAP) {
426+
return self::MAIL_MAX_ITEMS_CAP;
427+
}
428+
return $maxItems;
429+
}
419430
}

tests/MailQueueHandlerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,26 @@ public function testSendEmailsSkipsWhenAdminEmailDisabled(): void {
403403
}
404404
}
405405

406+
public function testGetMailMaxItemsReturnsCapWhenValueExceedsCap(): void {
407+
$this->appConfig->method('getValueInt')
408+
->with('activity', 'mail_max_items', $this->mailQueueHandler::MAIL_MAX_ITEMS_DEFAULT)
409+
->willReturn(9999);
410+
411+
$result = self::invokePrivate($this->mailQueueHandler, 'getMailMaxItems', []);
412+
413+
$this->assertSame(MailQueueHandler::MAIL_MAX_ITEMS_CAP, $result);
414+
}
415+
416+
public function testGetMailMaxItemsReturnsOneWhenValueIsBelowOne(): void {
417+
$this->appConfig->method('getValueInt')
418+
->with('activity', 'mail_max_items', $this->mailQueueHandler::MAIL_MAX_ITEMS_DEFAULT)
419+
->willReturn(0);
420+
421+
$result = self::invokePrivate($this->mailQueueHandler, 'getMailMaxItems', []);
422+
423+
$this->assertSame(1, $result);
424+
}
425+
406426
/**
407427
* @param array $users
408428
* @param int $maxTime

0 commit comments

Comments
 (0)