Skip to content

Commit

Permalink
feat(setup): Add a setup message when the rate limit was reached
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Oct 25, 2023
1 parent 2f45b03 commit 67e9742
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
12 changes: 10 additions & 2 deletions lib/Push.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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}', [
Expand Down
72 changes: 72 additions & 0 deletions lib/Settings/SetupWarningOnRateLimitReached.php
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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'
);
}
}

0 comments on commit 67e9742

Please sign in to comment.