Skip to content

Commit

Permalink
Migrate fair use of free push service check to new SetupCheck API
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Jan 25, 2024
1 parent 7c79521 commit 67541f2
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 42 deletions.
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
'OCA\\Settings\\SetupChecks\\DatabasePendingBigIntConversions' => $baseDir . '/../lib/SetupChecks/DatabasePendingBigIntConversions.php',
'OCA\\Settings\\SetupChecks\\DefaultPhoneRegionSet' => $baseDir . '/../lib/SetupChecks/DefaultPhoneRegionSet.php',
'OCA\\Settings\\SetupChecks\\EmailTestSuccessful' => $baseDir . '/../lib/SetupChecks/EmailTestSuccessful.php',
'OCA\\Settings\\SetupChecks\\FairUseOfFreePushService' => $baseDir . '/../lib/SetupChecks/FairUseOfFreePushService.php',
'OCA\\Settings\\SetupChecks\\FileLocking' => $baseDir . '/../lib/SetupChecks/FileLocking.php',
'OCA\\Settings\\SetupChecks\\ForwardedForHeaders' => $baseDir . '/../lib/SetupChecks/ForwardedForHeaders.php',
'OCA\\Settings\\SetupChecks\\InternetConnectivity' => $baseDir . '/../lib/SetupChecks/InternetConnectivity.php',
Expand Down
1 change: 1 addition & 0 deletions apps/settings/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ComposerStaticInitSettings
'OCA\\Settings\\SetupChecks\\DatabasePendingBigIntConversions' => __DIR__ . '/..' . '/../lib/SetupChecks/DatabasePendingBigIntConversions.php',
'OCA\\Settings\\SetupChecks\\DefaultPhoneRegionSet' => __DIR__ . '/..' . '/../lib/SetupChecks/DefaultPhoneRegionSet.php',
'OCA\\Settings\\SetupChecks\\EmailTestSuccessful' => __DIR__ . '/..' . '/../lib/SetupChecks/EmailTestSuccessful.php',
'OCA\\Settings\\SetupChecks\\FairUseOfFreePushService' => __DIR__ . '/..' . '/../lib/SetupChecks/FairUseOfFreePushService.php',
'OCA\\Settings\\SetupChecks\\FileLocking' => __DIR__ . '/..' . '/../lib/SetupChecks/FileLocking.php',
'OCA\\Settings\\SetupChecks\\ForwardedForHeaders' => __DIR__ . '/..' . '/../lib/SetupChecks/ForwardedForHeaders.php',
'OCA\\Settings\\SetupChecks\\InternetConnectivity' => __DIR__ . '/..' . '/../lib/SetupChecks/InternetConnectivity.php',
Expand Down
4 changes: 4 additions & 0 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use OCA\Settings\SetupChecks\DatabasePendingBigIntConversions;
use OCA\Settings\SetupChecks\DefaultPhoneRegionSet;
use OCA\Settings\SetupChecks\EmailTestSuccessful;
use OCA\Settings\SetupChecks\FairUseOfFreePushService;
use OCA\Settings\SetupChecks\FileLocking;
use OCA\Settings\SetupChecks\ForwardedForHeaders;
use OCA\Settings\SetupChecks\InternetConnectivity;
Expand Down Expand Up @@ -209,6 +210,9 @@ public function register(IRegistrationContext $context): void {
$context->registerSetupCheck(SystemIs64bit::class);
$context->registerSetupCheck(TempSpaceAvailable::class);
$context->registerSetupCheck(TransactionIsolation::class);
if (!\OCP\Server::get(\OCP\Support\Subscription\IRegistry::class)->delegateHasValidSubscription()) {
$context->registerSetupCheck(FairUseOfFreePushService::class);
}

$context->registerUserMigrator(AccountMigrator::class);
}
Expand Down
19 changes: 0 additions & 19 deletions apps/settings/lib/Controller/CheckSetupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Notification\IManager;
use OCP\SetupCheck\ISetupCheckManager;
use Psr\Log\LoggerInterface;

Expand All @@ -72,8 +71,6 @@ class CheckSetupController extends Controller {
private $checker;
/** @var LoggerInterface */
private $logger;
/** @var IManager */
private $manager;
private ISetupCheckManager $setupCheckManager;

public function __construct($AppName,
Expand All @@ -83,7 +80,6 @@ public function __construct($AppName,
IL10N $l10n,
Checker $checker,
LoggerInterface $logger,
IManager $manager,
ISetupCheckManager $setupCheckManager,
) {
parent::__construct($AppName, $request);
Expand All @@ -92,7 +88,6 @@ public function __construct($AppName,
$this->l10n = $l10n;
$this->checker = $checker;
$this->logger = $logger;
$this->manager = $manager;
$this->setupCheckManager = $setupCheckManager;
}

Expand All @@ -105,19 +100,6 @@ public function setupCheckManager(): DataResponse {
return new DataResponse($this->setupCheckManager->runAll());
}

/**
* Check if is fair use of free push service
* @return bool
*/
private function isFairUseOfFreePushService(): bool {
$rateLimitReached = (int) $this->config->getAppValue('notifications', 'rate_limit_reached', '0');
if ($rateLimitReached >= (time() - 7 * 24 * 3600)) {
// Notifications app is showing a message already
return true;
}
return $this->manager->isFairUseOfFreePushService();
}

/**
* @NoCSRFRequired
* @return RedirectResponse
Expand Down Expand Up @@ -194,7 +176,6 @@ public function getFailedIntegrityCheckFiles(): DataDisplayResponse {
public function check() {
return new DataResponse(
[
'isFairUseOfFreePushService' => $this->isFairUseOfFreePushService(),
'reverseProxyDocs' => $this->urlGenerator->linkToDocs('admin-reverse-proxy'),
'reverseProxyGeneratedURL' => $this->urlGenerator->getAbsoluteURL('index.php'),
'generic' => $this->setupCheckManager->runAll(),
Expand Down
79 changes: 79 additions & 0 deletions apps/settings/lib/SetupChecks/FairUseOfFreePushService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.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\Settings\SetupChecks;

use OCP\IConfig;
use OCP\IL10N;
use OCP\Notification\IManager;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;

class FairUseOfFreePushService implements ISetupCheck {
public function __construct(
private IL10N $l10n,
private IConfig $config,
private IManager $notificationsManager,
) {
}

public function getName(): string {
return $this->l10n->t('Free push service');
}

public function getCategory(): string {
return 'system';
}

/**
* Check if is fair use of free push service
*/
private function isFairUseOfFreePushService(): bool {
$rateLimitReached = (int) $this->config->getAppValue('notifications', 'rate_limit_reached', '0');

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::getAppValue has been marked as deprecated
if ($rateLimitReached >= (time() - 7 * 24 * 3600)) {
// Notifications app is showing a message already
return true;
}
return $this->notificationsManager->isFairUseOfFreePushService();
}

public function run(): SetupResult {
if ($this->isFairUseOfFreePushService()) {
return SetupResult::success();
}

return SetupResult::error(
$this->l10n->t('This is the unsupported community build of Nextcloud. Given the size of this instance, performance, reliability and scalability cannot be guaranteed. Push notifications are limited to avoid overloading our free service. Learn more about the benefits of Nextcloud Enterprise at {link}.'),
descriptionParameters:[
'link' => [
'type' => 'highlight',
'id' => 'link',
'name' => 'https://nextcloud.com/enterprise',
'link' => 'https://nextcloud.com/enterprise',
],
],
);
}
}
6 changes: 0 additions & 6 deletions apps/settings/tests/Controller/CheckSetupControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\Notification\IManager;
use OCP\SetupCheck\ISetupCheckManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand All @@ -71,8 +70,6 @@ class CheckSetupControllerTest extends TestCase {
private $logger;
/** @var Checker|\PHPUnit\Framework\MockObject\MockObject */
private $checker;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
private $notificationManager;
/** @var ISetupCheckManager|MockObject */
private $setupCheckManager;

Expand All @@ -95,7 +92,6 @@ protected function setUp(): void {
$this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker')
->disableOriginalConstructor()->getMock();
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->notificationManager = $this->getMockBuilder(IManager::class)->getMock();
$this->setupCheckManager = $this->createMock(ISetupCheckManager::class);
$this->checkSetupController = $this->getMockBuilder(CheckSetupController::class)
->setConstructorArgs([
Expand All @@ -106,7 +102,6 @@ protected function setUp(): void {
$this->l10n,
$this->checker,
$this->logger,
$this->notificationManager,
$this->setupCheckManager,
])
->setMethods([
Expand Down Expand Up @@ -170,7 +165,6 @@ public function testCheck() {
[
'reverseProxyDocs' => 'reverse-proxy-doc-link',
'reverseProxyGeneratedURL' => 'https://server/index.php',
'isFairUseOfFreePushService' => false,
'generic' => [],
]
);
Expand Down
9 changes: 0 additions & 9 deletions core/js/setupchecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,6 @@
var afterCall = function(data, statusText, xhr) {
var messages = [];
if (xhr.status === 200 && data) {
if (!data.isFairUseOfFreePushService) {
messages.push({
msg: t('core', 'This is the unsupported community build of Nextcloud. Given the size of this instance, performance, reliability and scalability cannot be guaranteed. Push notifications are limited to avoid overloading our free service. Learn more about the benefits of Nextcloud Enterprise at {linkstart}https://nextcloud.com/enterprise{linkend}.')
.replace('{linkstart}', '<a target="_blank" rel="noreferrer noopener" class="external" href="https://nextcloud.com/enterprise">')
.replace('{linkend}', '</a>'),
type: OC.SetupChecks.MESSAGE_TYPE_ERROR
});
}

if (window.location.protocol === 'https:' && data.reverseProxyGeneratedURL.split('/')[0] !== 'https:') {
messages.push({
msg: t('core', 'You are accessing your instance over a secure connection, however your instance is generating insecure URLs. This most likely means that you are behind a reverse proxy and the overwrite config variables are not set correctly. Please read {linkstart}the documentation page about this ↗{linkend}.')
Expand Down
8 changes: 0 additions & 8 deletions core/js/tests/specs/setupchecksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json'
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyGeneratedURL: 'https://server',
generic: {
network: {
Expand Down Expand Up @@ -257,7 +256,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json'
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyGeneratedURL: 'https://server',
generic: {
network: {
Expand Down Expand Up @@ -291,7 +289,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json',
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyGeneratedURL: 'https://server',
generic: {
network: {
Expand Down Expand Up @@ -325,7 +322,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json',
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyDocs: 'https://docs.nextcloud.com/foo/bar.html',
reverseProxyGeneratedURL: 'https://server',
generic: {
Expand Down Expand Up @@ -390,7 +386,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json',
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyGeneratedURL: 'https://server',
generic: {
network: {
Expand Down Expand Up @@ -433,7 +428,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json',
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyDocs: 'https://docs.nextcloud.com/foo/bar.html',
reverseProxyGeneratedURL: 'http://server',
generic: {
Expand Down Expand Up @@ -466,7 +460,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json',
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyDocs: 'https://docs.nextcloud.com/foo/bar.html',
reverseProxyGeneratedURL: 'http://server',
generic: {
Expand Down Expand Up @@ -496,7 +489,6 @@ describe('OC.SetupChecks tests', function() {
'Content-Type': 'application/json',
},
JSON.stringify({
isFairUseOfFreePushService: true,
reverseProxyGeneratedURL: 'https://server',
generic: {
network: {
Expand Down

0 comments on commit 67541f2

Please sign in to comment.