Skip to content

Commit 1838024

Browse files
cristianscheidmiaulalala
authored andcommitted
feat: make amount of items sent on email configurable via occ
Signed-off-by: Cristian Scheid <cristianscheid@gmail.com>
1 parent 81e974b commit 1838024

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
@@ -402,6 +402,26 @@ public function testSendEmailsSkipsWhenAdminEmailDisabled(): void {
402402
}
403403
}
404404

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

0 commit comments

Comments
 (0)