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..b1cc1311e --- /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' + ); + } +} diff --git a/tests/Unit/PushTest.php b/tests/Unit/PushTest.php index ddd3bcbb9..b0390ff38 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -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; @@ -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; @@ -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') @@ -114,6 +118,7 @@ protected function getPush(array $methods = []) { $this->cacheFactory, $this->userStatusManager, $this->l10nFactory, + $this->timeFactory, $this->logger, ]) ->setMethods($methods)