diff --git a/lib/MailQueueHandler.php b/lib/MailQueueHandler.php index ff2fddc17..ee68dba79 100644 --- a/lib/MailQueueHandler.php +++ b/lib/MailQueueHandler.php @@ -36,8 +36,8 @@ class MailQueueHandler { public const CLI_EMAIL_BATCH_SIZE = 500; public const WEB_EMAIL_BATCH_SIZE = 25; - /** Number of entries we want to list in the email */ - public const ENTRY_LIMIT = 200; + public const MAIL_MAX_ITEMS_DEFAULT = 200; + public const MAIL_MAX_ITEMS_CAP = 1000; protected array $languages; protected string $senderAddress; @@ -191,7 +191,7 @@ private function fetchAffectedUsers(IQueryBuilder $query): array { * * @return array [data of the first max. 200 entries, total number of entries] */ - protected function getItemsForUser(string $affectedUser, int $maxTime, int $maxNumItems = self::ENTRY_LIMIT): array { + protected function getItemsForUser(string $affectedUser, int $maxTime, int $maxNumItems = self::MAIL_MAX_ITEMS_DEFAULT): array { $query = $this->connection->getQueryBuilder(); $query->select('*') ->from('activity_mq') @@ -282,7 +282,7 @@ protected function sendEmailToUser(string $userName, string $email, string $lang return true; } - [$mailData, $skippedCount] = $this->getItemsForUser($userName, $maxTime); + [$mailData, $skippedCount] = $this->getItemsForUser($userName, $maxTime, $this->getMailMaxItems()); $l = $this->getLanguage($lang); $this->activityManager->setCurrentUserId($userName); @@ -416,4 +416,15 @@ protected function deleteSentItems(array $affectedUsers, int $maxTime): void { ->andWhere($query->expr()->in('amq_affecteduser', $query->createNamedParameter($affectedUsers, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR)); $query->executeStatement(); } + + private function getMailMaxItems(): int { + $maxItems = $this->appConfig->getValueInt('activity', 'mail_max_items', self::MAIL_MAX_ITEMS_DEFAULT); + if ($maxItems < 1) { + return 1; + } + if ($maxItems > self::MAIL_MAX_ITEMS_CAP) { + return self::MAIL_MAX_ITEMS_CAP; + } + return $maxItems; + } } diff --git a/tests/MailQueueHandlerTest.php b/tests/MailQueueHandlerTest.php index a86ebdca8..437d8c0bb 100644 --- a/tests/MailQueueHandlerTest.php +++ b/tests/MailQueueHandlerTest.php @@ -403,6 +403,26 @@ public function testSendEmailsSkipsWhenAdminEmailDisabled(): void { } } + public function testGetMailMaxItemsReturnsCapWhenValueExceedsCap(): void { + $this->appConfig->method('getValueInt') + ->with('activity', 'mail_max_items', $this->mailQueueHandler::MAIL_MAX_ITEMS_DEFAULT) + ->willReturn(9999); + + $result = self::invokePrivate($this->mailQueueHandler, 'getMailMaxItems', []); + + $this->assertSame(MailQueueHandler::MAIL_MAX_ITEMS_CAP, $result); + } + + public function testGetMailMaxItemsReturnsOneWhenValueIsBelowOne(): void { + $this->appConfig->method('getValueInt') + ->with('activity', 'mail_max_items', $this->mailQueueHandler::MAIL_MAX_ITEMS_DEFAULT) + ->willReturn(0); + + $result = self::invokePrivate($this->mailQueueHandler, 'getMailMaxItems', []); + + $this->assertSame(1, $result); + } + /** * @param array $users * @param int $maxTime