From 67e9742a8fbbca34c9c74e75600f92761d6b0456 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 25 Oct 2023 11:19:41 +0200 Subject: [PATCH] feat(setup): Add a setup message when the rate limit was reached Signed-off-by: Joas Schilling --- lib/AppInfo/Application.php | 3 + lib/Push.php | 12 +++- .../SetupWarningOnRateLimitReached.php | 72 +++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 lib/Settings/SetupWarningOnRateLimitReached.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 4a7faed0f..21a46a439 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -33,6 +33,7 @@ use OCA\Notifications\Listener\UserCreatedListener; use OCA\Notifications\Listener\UserDeletedListener; use OCA\Notifications\Notifier\AdminNotifications; +use OCA\Notifications\Settings\SetupWarningOnRateLimitReached; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; @@ -57,6 +58,8 @@ public function register(IRegistrationContext $context): void { return $c->getServer()->get(IProvider::class); }); + $context->registerSetupCheck(SetupWarningOnRateLimitReached::class); + $context->registerNotifierService(AdminNotifications::class); $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); diff --git a/lib/Push.php b/lib/Push.php index ff12ccbaf..b34984b45 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -32,7 +32,9 @@ use OC\Authentication\Token\IProvider; use OC\Security\IdentityProof\Key; use OC\Security\IdentityProof\Manager; +use OCA\Notifications\AppInfo\Application; use OCP\AppFramework\Http; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Http\Client\IClientService; use OCP\ICache; @@ -118,7 +120,8 @@ class Push { 'twofactor_nextcloud_notification' => true, ]; - public function __construct(IDBConnection $connection, + public function __construct( + IDBConnection $connection, INotificationManager $notificationManager, IConfig $config, IProvider $tokenProvider, @@ -127,7 +130,9 @@ public function __construct(IDBConnection $connection, ICacheFactory $cacheFactory, IUserStatusManager $userStatusManager, IFactory $l10nFactory, - LoggerInterface $log) { + protected ITimeFactory $timeFactory, + LoggerInterface $log, + ) { $this->db = $connection; $this->notificationManager = $notificationManager; $this->config = $config; @@ -540,6 +545,9 @@ protected function sendNotificationsToProxies(): void { $this->printInfo('Push notification sent successfully'); } } elseif ($status !== Http::STATUS_OK) { + if ($status === Http::STATUS_TOO_MANY_REQUESTS) { + $this->config->setAppValue(Application::APP_ID, 'rate_limit_reached', (string) $this->timeFactory->getTime()); + } $error = $body && $bodyData === null ? $body : 'no reason given'; $this->printInfo('Could not send notification to push server [' . $proxyServer . ']: ' . $error); $this->log->warning('Could not send notification to push server [{url}]: {error}', [ diff --git a/lib/Settings/SetupWarningOnRateLimitReached.php b/lib/Settings/SetupWarningOnRateLimitReached.php new file mode 100644 index 000000000..5e2a12daa --- /dev/null +++ b/lib/Settings/SetupWarningOnRateLimitReached.php @@ -0,0 +1,72 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Notifications\Settings; + +use OCA\Notifications\AppInfo\Application; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IConfig; +use OCP\IL10N; +use OCP\SetupCheck\ISetupCheck; +use OCP\SetupCheck\SetupResult; + +class SetupWarningOnRateLimitReached implements ISetupCheck { + public function __construct( + private IConfig $config, + private ITimeFactory $timeFactory, + private IL10N $l, + ) { + } + + /** + * @inheritDoc + */ + public function getCategory(): string { + return 'notifications'; + } + + /** + * @inheritDoc + */ + public function getName(): string { + return $this->l->t('Push notifications - Fair use policy'); + } + + /** + * @inheritDoc + */ + public function run(): SetupResult { + $lastReached = (int) $this->config->getAppValue(Application::APP_ID, 'rate_limit_reached', '0'); + if ($lastReached < ($this->timeFactory->getTime() - 7*24*3600)) { + return SetupResult::success(); + } + + return SetupResult::error( + $this->l->t('Nextcloud GmbH sponsors a free push notification gateway for private users. To ensure good service, the gateway limits the number of push notifications per server and the limit was reached for this server. For enterprise users, a more scalable gateway is available.'), + 'https://nextcloud.com/fairusepolicy' + ); + } +}