Skip to content

Commit 4bb1632

Browse files
committed
fixup! fix(bulkactivity): bulk query user settings
1 parent 8c75fe9 commit 4bb1632

File tree

2 files changed

+102
-27
lines changed

2 files changed

+102
-27
lines changed

lib/Consumer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function bulkReceive(IEvent $event, array $affectedUserIds, ISetting $set
8686

8787
if ($canChangeMail === true || $setting->isDefaultEnabledMail() === true) {
8888
$userEmailSettings = $this->userConfig->getValuesByUsers('activity', 'notify_email_ ' . $event->getType(), ValueType::BOOL, $affectedUserIds);
89-
$batchTimeSettings = $this->userConfig->getValuesByUsers('activity', 'setting_batchtime', ValueType::INT, $affectedUserIds);
89+
$batchTimeSettings = $this->userConfig->getValuesByUsers('activity', 'notify_setting_batchtime', ValueType::INT, $affectedUserIds);
9090
}
9191

9292
$shouldFlush = $this->notificationGenerator->deferNotifications();
@@ -96,7 +96,8 @@ public function bulkReceive(IEvent $event, array $affectedUserIds, ISetting $set
9696
}
9797
$event->setAffectedUser($affectedUser);
9898
$notificationSetting = $userPushSettings[$affectedUser] ?? false;
99-
$emailSetting = $userEmailSettings[$affectedUser] ?? $batchTimeSettings[$affectedUser] ?? false;
99+
$emailSetting = $userEmailSettings[$affectedUser] ?? false;
100+
$$batchTimeSettings[$affectedUser] ?? false;
100101

101102
if ($notificationSetting !== false) {
102103
$this->notificationGenerator->sendNotificationForEvent($event, $activityId, $notificationSetting);

tests/ConsumerTest.php

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
use OCA\Activity\Data;
2929
use OCA\Activity\NotificationGenerator;
3030
use OCA\Activity\UserSettings;
31+
use OCP\Activity\IEvent;
3132
use OCP\Activity\IManager;
33+
use OCP\Activity\ISetting;
3234
use OCP\Config\IUserConfig;
3335
use OCP\IDBConnection;
3436
use OCP\IL10N;
@@ -50,6 +52,8 @@ class ConsumerTest extends TestCase {
5052
protected NotificationGenerator&MockObject $notificationGenerator;
5153
protected UserSettings $userSettings;
5254
private IUserConfig&MockObject $userConfig;
55+
private IEvent $event;
56+
private Consumer $consumer;
5357

5458
protected function setUp(): void {
5559
parent::setUp();
@@ -83,6 +87,16 @@ protected function setUp(): void {
8387
['affectedUser', 'setting', 'batchtime', 10],
8488
['affectedUser2', 'setting', 'batchtime', 10],
8589
]);
90+
91+
$this->consumer = new Consumer(
92+
$this->data,
93+
$this->activityManager,
94+
$this->userSettings,
95+
$this->notificationGenerator,
96+
$this->userConfig,
97+
);
98+
99+
$this->event = Server::get(IManager::class)->generateEvent();
86100
}
87101

88102
protected function tearDown(): void {
@@ -123,9 +137,7 @@ public static function receiveData(): array {
123137

124138
#[DataProvider('receiveData')]
125139
public function testReceiveStream(string $type, string $author, string $affectedUser, string $subject): void {
126-
$consumer = new Consumer($this->data, $this->activityManager, $this->userSettings, $this->notificationGenerator);
127-
$event = Server::get(IManager::class)->generateEvent();
128-
$event->setApp('test')
140+
$this->event->setApp('test')
129141
->setType($type)
130142
->setAffectedUser($affectedUser)
131143
->setAuthor($author)
@@ -139,21 +151,13 @@ public function testReceiveStream(string $type, string $author, string $affected
139151
$this->data->expects($this->once())
140152
->method('send');
141153

142-
$consumer->receive($event);
154+
$this->consumer->receive($this->event);
143155
}
144156

145157
#[DataProvider('receiveData')]
146158
public function testReceiveEmail(string $type, string $author, string $affectedUser, string $subject, $expected): void {
147159
$time = time();
148-
$consumer = new Consumer(
149-
$this->data,
150-
$this->activityManager,
151-
$this->userSettings,
152-
$this->notificationGenerator,
153-
$this->userConfig,
154-
);
155-
$event = Server::get(IManager::class)->generateEvent();
156-
$event->setApp('test')
160+
$this->event->setApp('test')
157161
->setType($type)
158162
->setAffectedUser($affectedUser)
159163
->setAuthor($author)
@@ -169,23 +173,15 @@ public function testReceiveEmail(string $type, string $author, string $affectedU
169173
} else {
170174
$this->data->expects($this->once())
171175
->method('storeMail')
172-
->with($event, $time + 10);
176+
->with($this->event, $time + 10);
173177
}
174178

175-
$consumer->receive($event);
179+
$this->consumer->receive($this->event);
176180
}
177181

178182
#[DataProvider('receiveData')]
179183
public function testReceiveNotification(string $type, string $author, string $affectedUser, string $subject, $expected): void {
180-
$consumer = new Consumer(
181-
$this->data,
182-
$this->activityManager,
183-
$this->userSettings,
184-
$this->notificationGenerator,
185-
$this->userConfig,
186-
);
187-
$event = Server::get(IManager::class)->generateEvent();
188-
$event->setApp('test')
184+
$this->event->setApp('test')
189185
->setType($type)
190186
->setAffectedUser($affectedUser)
191187
->setAuthor($author)
@@ -204,6 +200,84 @@ public function testReceiveNotification(string $type, string $author, string $af
204200
->method('sendNotificationForEvent');
205201
}
206202

207-
$consumer->receive($event);
203+
$this->consumer->receive($this->event);
204+
}
205+
206+
public static function receiveBulkData(): array {
207+
/**
208+
* type
209+
* author
210+
* subject
211+
* affectedUsers
212+
* activityIds
213+
* ISettings canChangeEmail, canChangePush
214+
* IUserConfig notify_notification_type notify_email_type notify_setting_batchtime
215+
*
216+
*/
217+
218+
return [
219+
// Empty affected users
220+
['type', 'author','subject',
221+
[],
222+
[],
223+
[],
224+
[]
225+
],
226+
// Empty activity IDs
227+
['type', 'author','subject',
228+
['affectedUser', 'affectedUser1'],
229+
[],
230+
[],
231+
[]
232+
],
233+
['type', 'author','subject', ['affectedUser', 'affectedUser2', 'affectedUser3'], [true, true], [false, false, false]],
234+
['type2', 'author', 'subject', false, ['affectedUser', 'affectedUser2', 'affectedUser3']],
235+
['type', 'author','subject_self', 'affectedUser', ['affectedUser', 'affectedUser2', 'affectedUser3']],
236+
['type', 'author', 'subject_self', 'affectedUser2', ['affectedUser', 'affectedUser2', 'affectedUser3']],
237+
['type', 'author', 'subject2', 'affectedUser', ['affectedUser', 'affectedUser2', 'affectedUser3']],
238+
['type', 'author', 'subject2', 'affectedUser2', ['affectedUser', 'affectedUser2', 'affectedUser3']],
239+
['type', 'affectedUser', 'subject_self', 'affectedUser', ['affectedUser', 'affectedUser2', 'affectedUser3']],
240+
['type', 'affectedUser2', 'subject_self', false, ['affectedUser', 'affectedUser2', 'affectedUser3']],
241+
['type', 'affectedUser', 'subject2', 'affectedUser', ['affectedUser', 'affectedUser2', 'affectedUser3']],
242+
['type', 'affectedUser2','subject2', false, ['affectedUser', 'affectedUser2', 'affectedUser3']],
243+
];
244+
}
245+
246+
#[DataProvider('receiveBulkData')]
247+
public function testBulkReceiveNotification(string $type, string $author, string $subject, $expected, array $affectedUsers): void {
248+
$this->event->setApp('activity')
249+
->setType($type)
250+
->setAuthor($author)
251+
->setTimestamp(time())
252+
->setSubject($subject, ['subjectParam1', 'subjectParam2'])
253+
->setMessage('message', ['messageParam1', 'messageParam2'])
254+
->setObject('', 0, 'file')
255+
->setLink('link');
256+
$this->deleteTestActivities();
257+
258+
$settings = $this->createMock(ISetting::class);
259+
$settings->expects($this->any())
260+
->method('getValuesByUsers')
261+
->with($affectedUsers)
262+
->willReturn($affectedUsers);
263+
if (empty($affectedUsers)) {
264+
$this->data->expects($this->never())
265+
->method('bulkSend');
266+
$this->data->expects($this->never())
267+
->method('storeMail');
268+
$this->notificationGenerator->expects($this->never())
269+
->method('sendNotificationForEvent');
270+
}
271+
272+
if ($expected === false || $author === $affectedUser) {
273+
$this->notificationGenerator->expects($this->never())
274+
->method('sendNotificationForEvent');
275+
} else {
276+
$this->notificationGenerator->expects($this->once())
277+
->method('sendNotificationForEvent');
278+
}
279+
280+
$this->consumer->bulkReceive($this->event, $affectedUsers, );
208281
}
282+
209283
}

0 commit comments

Comments
 (0)