Skip to content

Commit f0d02e1

Browse files
miaulalalabackportbot[bot]
authored andcommitted
fix(bulkReceive): honour admin email toggle and ISetting notification defaults
fix(bulkReceive): honour admin email toggle and ISetting notification defaults The admin Enable notification emails toggle was not checked in bulkReceive(), so emails could be queued even when the toggle was disabled. Also, ISetting-based settings that do not implement ActivitySettings had canChangeNotification() called on them, which caused a fatal error; the default push-enabled state was also ignored. Injects IAppConfig and uses getValueString() to check the toggle, consistent with the approach used elsewhere. ISetting instances that are not ActivitySettings now fall back to the setting default via isDefaultEnabledNotification() without calling canChangeNotification(). Signed-off-by: Anna Larch <anna@nextcloud.com> [skip ci]
1 parent c6b3562 commit f0d02e1

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

lib/Consumer.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OCP\Config\IUserConfig;
1717
use OCP\Config\ValueType;
1818
use OCP\DB\Exception;
19+
use OCP\IAppConfig;
1920

2021
class Consumer implements IConsumer, IBulkConsumer {
2122

@@ -25,6 +26,7 @@ public function __construct(
2526
protected UserSettings $userSettings,
2627
protected NotificationGenerator $notificationGenerator,
2728
protected IUserConfig $userConfig,
29+
protected IAppConfig $appConfig,
2830
) {
2931
}
3032

@@ -78,13 +80,16 @@ public function bulkReceive(IEvent $event, array $affectedUserIds, ISetting $set
7880

7981
$canChangeMail = $setting->canChangeMail();
8082
$canChangePush = $setting instanceof ActivitySettings && $setting->canChangeNotification() === true;
83+
$defaultPushEnabled = $setting instanceof ActivitySettings && $setting->isDefaultEnabledNotification();
8184

82-
$userPushSettings = $userEmailSettings = $batchTimeSettings = null;
85+
$userPushSettings = null;
8386
if ($canChangePush === true) {
8487
$userPushSettings = $this->userConfig->getValuesByUsers('activity', 'notify_notification_' . $event->getType(), ValueType::BOOL, $affectedUserIds);
8588
}
8689

87-
if ($canChangeMail === true || $setting->isDefaultEnabledMail() === true) {
90+
$userEmailSettings = $batchTimeSettings = null;
91+
$emailEnabled = $this->appConfig->getValueString('activity', 'enable_email', 'yes') !== 'no';
92+
if ($emailEnabled && ($canChangeMail === true || $setting->isDefaultEnabledMail() === true)) {
8893
$userEmailSettings = $this->userConfig->getValuesByUsers('activity', 'notify_email_' . $event->getType(), ValueType::BOOL, $affectedUserIds);
8994
$batchTimeSettings = $this->userConfig->getValuesByUsers('activity', 'notify_setting_batchtime', ValueType::INT, $affectedUserIds);
9095
}
@@ -95,14 +100,17 @@ public function bulkReceive(IEvent $event, array $affectedUserIds, ISetting $set
95100
continue;
96101
}
97102
$event->setAffectedUser($affectedUser);
98-
$notificationSetting = $userPushSettings[$affectedUser] ?? null;
99-
if ($notificationSetting !== null) {
100-
$notificationSetting = (bool)$notificationSetting;
103+
104+
if ($canChangePush === true) {
105+
$notificationSetting = isset($userPushSettings[$affectedUser]) ? (bool)$userPushSettings[$affectedUser] : $defaultPushEnabled;
106+
} else {
107+
$notificationSetting = $defaultPushEnabled;
101108
}
109+
102110
$emailSetting = $userEmailSettings[$affectedUser] ?? false;
103111
$emailSetting = ($emailSetting) ? ($batchTimeSettings[$affectedUser] ?? false) : false;
104112

105-
if ($notificationSetting !== false) {
113+
if ($notificationSetting === true) {
106114
$this->notificationGenerator->sendNotificationForEvent($event, $activityId, $notificationSetting);
107115
}
108116

tests/ConsumerTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\Activity\IEvent;
3333
use OCP\Activity\IManager;
3434
use OCP\Config\IUserConfig;
35+
use OCP\IAppConfig;
3536
use OCP\IDBConnection;
3637
use OCP\IL10N;
3738
use OCP\L10N\IFactory;
@@ -52,6 +53,7 @@ class ConsumerTest extends TestCase {
5253
protected NotificationGenerator&MockObject $notificationGenerator;
5354
protected UserSettings $userSettings;
5455
private IUserConfig&MockObject $userConfig;
56+
private IAppConfig&MockObject $appConfig;
5557
private IEvent $event;
5658
private Consumer $consumer;
5759

@@ -69,7 +71,10 @@ protected function setUp(): void {
6971
$this->notificationGenerator = $this->createMock(NotificationGenerator::class);
7072
$this->l10nFactory = $this->createMock(IFactory::class);
7173
$this->userConfig = $this->createMock(IUserConfig::class);
72-
74+
$this->appConfig = $this->createMock(IAppConfig::class);
75+
$this->appConfig->method('getValueString')
76+
->with('activity', 'enable_email', 'yes')
77+
->willReturn('yes');
7378
$this->data->method('send')
7479
->willReturn(1);
7580
$this->l10nFactory
@@ -94,6 +99,7 @@ protected function setUp(): void {
9499
$this->userSettings,
95100
$this->notificationGenerator,
96101
$this->userConfig,
102+
$this->appConfig,
97103
);
98104

99105
$this->event = Server::get(IManager::class)->generateEvent();
@@ -142,6 +148,7 @@ public function testReceiveStream(string $type, string $author, string $affected
142148
$this->userSettings,
143149
$this->notificationGenerator,
144150
$this->userConfig,
151+
$this->appConfig,
145152
);
146153
$event = Server::get(IManager::class)->generateEvent();
147154
$event->setApp('test')

0 commit comments

Comments
 (0)