-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(notifications): Rework the notification handling
- Don't generate notifications live on resetAllSignatures but use a cron job - Don't create notifications for users that signed any terms already - Don't create notifications when enabling the app, but only after terms have been added Signed-off-by: Joas Schilling <[email protected]>
- Loading branch information
1 parent
083f3b6
commit 03222a5
Showing
6 changed files
with
123 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\TermsOfService\BackgroundJobs; | ||
|
||
use OCA\TermsOfService\AppInfo\Application; | ||
use OCA\TermsOfService\Db\Mapper\SignatoryMapper; | ||
use OCA\TermsOfService\Db\Mapper\TermsMapper; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\QueuedJob; | ||
use OCP\IConfig; | ||
use OCP\IUser; | ||
use OCP\IUserManager; | ||
use OCP\Notification\IManager; | ||
use OCP\Notification\INotification; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class CreateNotifications extends QueuedJob { | ||
public const BATCH_SIZE = 1000; | ||
protected ?INotification $notification = null; | ||
protected int $currentBatch = 0; | ||
|
||
public function __construct( | ||
protected IUserManager $userManager, | ||
protected IManager $notificationsManager, | ||
protected TermsMapper $termsMapper, | ||
protected SignatoryMapper $signatoryMapper, | ||
protected IConfig $config, | ||
protected LoggerInterface $logger, | ||
ITimeFactory $time, | ||
) { | ||
parent::__construct($time); | ||
} | ||
|
||
protected function run($argument): void { | ||
if ($this->config->getAppValue(Application::APPNAME, 'sent_notifications', 'no') === 'yes') { | ||
$this->logger->debug('ToS Notifications have already been sent'); | ||
return; | ||
} | ||
|
||
$terms = $this->termsMapper->getTerms(); | ||
if (empty($terms)) { | ||
$this->logger->debug('No terms available to sign'); | ||
return; | ||
} | ||
|
||
$this->config->setAppValue(Application::APPNAME, 'sent_notifications', 'yes'); | ||
|
||
$this->notification = $this->notificationsManager->createNotification(); | ||
$this->notification->setApp('terms_of_service') | ||
->setSubject('accept_terms') | ||
->setObject('terms', '1'); | ||
|
||
// Mark all notifications as processed … | ||
$this->notificationsManager->markProcessed($this->notification); | ||
|
||
// … before generating new ones. | ||
$this->notification->setDateTime(new \DateTime()); | ||
|
||
$this->notificationsManager->defer(); | ||
$this->currentBatch = 0; | ||
$this->userManager->callForSeenUsers(\Closure::fromCallable([$this, 'callForSeenUsers'])); | ||
$this->notificationsManager->flush(); | ||
} | ||
|
||
public function callForSeenUsers(IUser $user): void { | ||
if ($this->signatoryMapper->hasSignedByUser($user)) { | ||
// User already signed in the meantime | ||
$this->logger->debug('User ' . $user->getUID() . ' already signed ToS'); | ||
return; | ||
} | ||
|
||
$this->notification->setUser($user->getUID()); | ||
$this->notificationsManager->notify($this->notification); | ||
|
||
// Make sure we don't create a too huge batch for the push notifications | ||
$this->currentBatch++; | ||
if ($this->currentBatch === self::BATCH_SIZE) { | ||
$this->notificationsManager->flush(); | ||
$this->notificationsManager->defer(); | ||
$this->currentBatch = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.