-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathGenerateUserSettings.php
More file actions
71 lines (60 loc) · 2.13 KB
/
Copy pathGenerateUserSettings.php
File metadata and controls
71 lines (60 loc) · 2.13 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Notifications\BackgroundJob;
use OCA\Notifications\Model\Settings;
use OCA\Notifications\Model\SettingsMapper;
use OCP\AppFramework\Services\IAppConfig;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IUserManager;
class GenerateUserSettings extends TimedJob {
public function __construct(
ITimeFactory $time,
private readonly IDBConnection $connection,
private readonly IUserManager $userManager,
private readonly SettingsMapper $settingsMapper,
private readonly IAppConfig $appConfig,
) {
parent::__construct($time);
// run every day
$this->setInterval(24 * 60 * 60);
}
#[\Override]
protected function run($argument): void {
$update = $this->connection->getQueryBuilder();
$update->update('notifications_settings')
->set('next_send_time', $update->createNamedParameter(1))
->where($update->expr()->eq('next_send_time', $update->createNamedParameter(0)))
->andWhere($update->expr()->neq('batch_time', $update->createNamedParameter(Settings::EMAIL_SEND_OFF)));
$batchTime = $this->appConfig->getAppValueInt('setting_batchtime');
if ($batchTime === Settings::EMAIL_SEND_OFF) {
$update->andWhere($update->expr()->neq('batch_time', $update->createNamedParameter(Settings::EMAIL_SEND_DEFAULT)));
}
$update->executeStatement();
$query = $this->connection->getQueryBuilder();
$query->select('notification_id')
->from('notifications')
->orderBy('notification_id', 'DESC')
->setMaxResults(1);
$result = $query->executeQuery();
$maxId = (int)$result->fetchOne();
$result->closeCursor();
$this->userManager->callForSeenUsers(function (IUser $user) use ($maxId): void {
if (!$user->isEnabled()) {
return;
}
// Initializes the default settings
$settings = $this->settingsMapper->getSettingsByUser($user->getUID());
if ($settings->getLastSendId() === 0) {
$settings->setLastSendId($maxId);
$this->settingsMapper->update($settings);
}
});
}
}