Skip to content

Commit

Permalink
Merge pull request #1699 from nextcloud/feat/noid/setup-check-for-rea…
Browse files Browse the repository at this point in the history
…ched-rate-limits

feat(setup): Add a setup message when the rate limit was reached
  • Loading branch information
nickvergessen committed Oct 26, 2023
2 parents 2f45b03 + efb79a0 commit acd6b9e
Show file tree
Hide file tree
Showing 4 changed files with 90 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'
);
}
}
5 changes: 5 additions & 0 deletions tests/Unit/PushTest.php
Expand Up @@ -30,6 +30,7 @@
use OC\Security\IdentityProof\Manager;
use OCA\Notifications\Push;
use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
Expand Down Expand Up @@ -74,6 +75,8 @@ class PushTest extends TestCase {
protected $userStatusManager;
/** @var IFactory|MockObject */
protected $l10nFactory;
/** @var ITimeFactory|MockObject */
protected $timeFactory;
/** @var LoggerInterface|MockObject */
protected $logger;

Expand All @@ -90,6 +93,7 @@ protected function setUp(): void {
$this->cache = $this->createMock(ICache::class);
$this->userStatusManager = $this->createMock(IUserStatusManager::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->cacheFactory->method('createDistributed')
Expand All @@ -114,6 +118,7 @@ protected function getPush(array $methods = []) {
$this->cacheFactory,
$this->userStatusManager,
$this->l10nFactory,
$this->timeFactory,
$this->logger,
])
->setMethods($methods)
Expand Down

0 comments on commit acd6b9e

Please sign in to comment.