-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathConsumer.php
More file actions
126 lines (107 loc) · 4.22 KB
/
Consumer.php
File metadata and controls
126 lines (107 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Activity;
use OCP\Activity\ActivitySettings;
use OCP\Activity\IBulkConsumer;
use OCP\Activity\IConsumer;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\Activity\ISetting;
use OCP\Config\IUserConfig;
use OCP\Config\ValueType;
use OCP\DB\Exception;
use OCP\IConfig;
class Consumer implements IConsumer, IBulkConsumer {
public function __construct(
protected Data $data,
protected IManager $manager,
protected UserSettings $userSettings,
protected NotificationGenerator $notificationGenerator,
protected IUserConfig $userConfig,
protected IConfig $config,
) {
}
/**
* Send an event to the notifications of a user
*
* @param IEvent $event
*
* @return void
*/
#[\Override]
public function receive(IEvent $event): void {
$selfAction = $event->getAffectedUser() === $event->getAuthor();
$notificationSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'notification', $event->getType());
$emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType());
$emailSetting = ($emailSetting) ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false;
$activityId = $this->data->send($event);
if (!$selfAction && $notificationSetting && $activityId) {
$this->notificationGenerator->sendNotificationForEvent($event, $activityId);
}
// Add activity to mail queue and user is not the author
if ($emailSetting !== false && !$selfAction) {
$latestSend = $event->getTimestamp() + $emailSetting;
$this->data->storeMail($event, $latestSend);
}
}
/**
* Send an event to the notifications of a bulk of users
*
* @param IEvent $event
* @param array $affectedUserIds
* @param ISetting $setting
* @return void
* @throws Exception
*/
#[\Override]
public function bulkReceive(IEvent $event, array $affectedUserIds, ISetting $setting): void {
if (empty($affectedUserIds)) {
return;
}
$activityIds = $this->data->bulkSend($event, $affectedUserIds);
if (empty($activityIds)) {
return;
}
$canChangeMail = $setting->canChangeMail();
$canChangePush = $setting instanceof ActivitySettings && $setting->canChangeNotification() === true;
$defaultPushEnabled = $setting instanceof ActivitySettings && $setting->isDefaultEnabledNotification();
$userPushSettings = null;
if ($canChangePush === true) {
$userPushSettings = $this->userConfig->getValuesByUsers('activity', 'notify_notification_' . $event->getType(), ValueType::BOOL, $affectedUserIds);
}
$userEmailSettings = $batchTimeSettings = null;
$emailEnabled = $this->config->getAppValue('activity', 'enable_email', 'yes') !== 'no';
if ($emailEnabled && ($canChangeMail === true || $setting->isDefaultEnabledMail() === true)) {
$userEmailSettings = $this->userConfig->getValuesByUsers('activity', 'notify_email_' . $event->getType(), ValueType::BOOL, $affectedUserIds);
$batchTimeSettings = $this->userConfig->getValuesByUsers('activity', 'notify_setting_batchtime', ValueType::INT, $affectedUserIds);
}
$shouldFlush = $this->notificationGenerator->deferNotifications();
foreach ($activityIds as $activityId => $affectedUser) {
if ($event->getAuthor() === $affectedUser) {
continue;
}
$event->setAffectedUser($affectedUser);
if ($canChangePush === true) {
$notificationSetting = isset($userPushSettings[$affectedUser]) ? (bool)$userPushSettings[$affectedUser] : $defaultPushEnabled;
} else {
$notificationSetting = $defaultPushEnabled;
}
$emailSetting = $userEmailSettings[$affectedUser] ?? false;
$emailSetting = ($emailSetting) ? ($batchTimeSettings[$affectedUser] ?? false) : false;
if ($notificationSetting === true) {
$this->notificationGenerator->sendNotificationForEvent($event, $activityId, $notificationSetting);
}
if (isset($emailSetting) && $emailSetting !== false) {
$latestSend = (int)($event->getTimestamp() + $emailSetting);
$this->data->storeMail($event, $latestSend);
}
}
if ($shouldFlush === true) {
$this->notificationGenerator->flushNotifications();
}
}
}