Skip to content

Commit 00b098c

Browse files
CarlSchwanbackportbot[bot]
authored andcommitted
refactor: Use GlobalScale/IConfig everywhere
refactor: Use GlobalScale/IConfig everywhere Signed-off-by: Carl Schwan <carl@carlschwan.eu> [skip ci]
1 parent f4f9e66 commit 00b098c

5 files changed

Lines changed: 66 additions & 47 deletions

File tree

apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCP\AppFramework\Utility\ITimeFactory;
1414
use OCP\BackgroundJob\IJobList;
1515
use OCP\BackgroundJob\Job;
16+
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
1617
use OCP\Http\Client\IClientService;
1718
use OCP\IConfig;
1819
use OCP\IUser;
@@ -38,6 +39,7 @@ public function __construct(
3839
private IUserManager $userManager,
3940
private IAccountManager $accountManager,
4041
private Signer $signer,
42+
private GlobalScaleConfig $globalScaleConfig,
4143
) {
4244
parent::__construct($time);
4345

@@ -85,7 +87,7 @@ public function start(IJobList $jobList): void {
8587
protected function shouldRemoveBackgroundJob(): bool {
8688
// TODO: Remove global scale condition once lookup server is used for non-global scale federation
8789
// return $this->config->getAppValue('files_sharing', 'lookupServerUploadEnabled', 'no') !== 'yes'
88-
return !$this->config->getSystemValueBool('gs.enabled', false)
90+
return !$this->globalScaleConfig->isGlobalScaleEnabled()
8991
|| $this->config->getSystemValueBool('has_internet_connection', true) === false
9092
|| $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') === ''
9193
|| $this->retries >= 5;

apps/lookup_server_connector/lib/UpdateLookupServer.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
use OCA\LookupServerConnector\BackgroundJobs\RetryJob;
1212
use OCP\BackgroundJob\IJobList;
13+
use OCP\Config\IUserConfig;
14+
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
1315
use OCP\IConfig;
1416
use OCP\IUser;
1517

@@ -19,26 +21,21 @@
1921
* @package OCA\LookupServerConnector
2022
*/
2123
class UpdateLookupServer {
22-
/**
23-
* @param IJobList $jobList
24-
* @param IConfig $config
25-
*/
2624
public function __construct(
27-
private IJobList $jobList,
28-
private IConfig $config,
25+
private readonly IJobList $jobList,
26+
private readonly IConfig $config,
27+
private readonly IUserConfig $userConfig,
28+
private readonly GlobalScaleConfig $globalScaleConfig,
2929
) {
3030
}
3131

32-
/**
33-
* @param IUser $user
34-
*/
3532
public function userUpdated(IUser $user): void {
3633
if (!$this->shouldUpdateLookupServer()) {
3734
return;
3835
}
3936

4037
// Reset retry counter
41-
$this->config->deleteUserValue(
38+
$this->userConfig->deleteUserConfig(
4239
$user->getUID(),
4340
'lookup_server_connector',
4441
'update_retries'
@@ -47,17 +44,15 @@ public function userUpdated(IUser $user): void {
4744
}
4845

4946
/**
50-
* check if we should update the lookup server, we only do it if
47+
* Check if we should update the lookup server, we only do it if
5148
*
5249
* + we have an internet connection
5350
* + the lookup server update was not disabled by the admin
5451
* + we have a valid lookup server URL
55-
*
56-
* @return bool
5752
*/
5853
private function shouldUpdateLookupServer(): bool {
5954
// TODO: Consider reenable for non-global-scale setups by checking "'files_sharing', 'lookupServerUploadEnabled'" instead of "gs.enabled"
60-
return $this->config->getSystemValueBool('gs.enabled', false)
55+
return $this->globalScaleConfig->isGlobalScaleEnabled()
6156
&& $this->config->getSystemValueBool('has_internet_connection', true)
6257
&& $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com') !== '';
6358
}

apps/settings/lib/BackgroundJobs/VerifyUserData.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\AppFramework\Utility\ITimeFactory;
1515
use OCP\BackgroundJob\IJobList;
1616
use OCP\BackgroundJob\Job;
17+
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
1718
use OCP\Http\Client\IClientService;
1819
use OCP\IConfig;
1920
use OCP\IUserManager;
@@ -37,6 +38,7 @@ public function __construct(
3738
private LoggerInterface $logger,
3839
ITimeFactory $timeFactory,
3940
private IConfig $config,
41+
private GlobalScaleConfig $globalScaleConfig,
4042
) {
4143
parent::__construct($timeFactory);
4244

@@ -123,7 +125,7 @@ protected function verifyWebsite(array $argument) {
123125

124126
protected function verifyViaLookupServer(array $argument, string $dataType): bool {
125127
// TODO: Consider to enable for non-global-scale setups by checking 'files_sharing', 'lookupServerUploadEnabled'
126-
if (!$this->config->getSystemValueBool('gs.enabled', false)
128+
if (!$this->globalScaleConfig->isGlobalScaleEnabled()
127129
|| empty($this->lookupServerUrl)
128130
|| $this->config->getSystemValue('has_internet_connection', true) === false
129131
) {

lib/private/Collaboration/Collaborators/LookupPlugin.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use OCP\Collaboration\Collaborators\ISearchResult;
1212
use OCP\Collaboration\Collaborators\SearchResultType;
1313
use OCP\Federation\ICloudIdManager;
14+
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
1415
use OCP\Http\Client\IClientService;
1516
use OCP\IConfig;
1617
use OCP\IUserSession;
@@ -22,20 +23,21 @@ class LookupPlugin implements ISearchPlugin {
2223
private string $currentUserRemote;
2324

2425
public function __construct(
25-
private IConfig $config,
26-
private IClientService $clientService,
26+
private readonly IConfig $config,
27+
private readonly IClientService $clientService,
2728
IUserSession $userSession,
28-
private ICloudIdManager $cloudIdManager,
29-
private LoggerInterface $logger,
30-
private ?TrustedServers $trustedServers,
29+
private readonly ICloudIdManager $cloudIdManager,
30+
private readonly LoggerInterface $logger,
31+
private readonly ?TrustedServers $trustedServers,
32+
private readonly GlobalScaleConfig $globalScaleConfig,
3133
) {
3234
$currentUserCloudId = $userSession->getUser()->getCloudId();
3335
$this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote();
3436
}
3537

3638
#[\Override]
3739
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
38-
$isGlobalScaleEnabled = $this->config->getSystemValueBool('gs.enabled', false);
40+
$isGlobalScaleEnabled = $this->globalScaleConfig->isGlobalScaleEnabled();
3941
$isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'no') === 'yes';
4042
$hasInternetConnection = $this->config->getSystemValueBool('has_internet_connection', true);
4143

tests/lib/Collaboration/Collaborators/LookupPluginTest.php

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
57
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -13,6 +15,7 @@
1315
use OCP\Collaboration\Collaborators\SearchResultType;
1416
use OCP\Federation\ICloudId;
1517
use OCP\Federation\ICloudIdManager;
18+
use OCP\GlobalScale\IConfig as GlobalScaleConfig;
1619
use OCP\Http\Client\IClient;
1720
use OCP\Http\Client\IClientService;
1821
use OCP\Http\Client\IResponse;
@@ -25,18 +28,13 @@
2528
use Test\TestCase;
2629

2730
class LookupPluginTest extends TestCase {
28-
/** @var IConfig|MockObject */
29-
protected $config;
30-
/** @var IClientService|MockObject */
31-
protected $clientService;
32-
/** @var IUserSession|MockObject */
33-
protected $userSession;
34-
/** @var ICloudIdManager|MockObject */
35-
protected $cloudIdManager;
36-
/** @var LookupPlugin */
37-
protected $plugin;
38-
/** @var LoggerInterface|MockObject */
39-
protected $logger;
31+
protected IConfig&MockObject $config;
32+
protected GlobalScaleConfig&MockObject $globalScaleConfig;
33+
protected IClientService&MockObject $clientService;
34+
protected IUserSession&MockObject $userSession;
35+
protected ICloudIdManager&MockObject $cloudIdManager;
36+
protected LookupPlugin $plugin;
37+
protected LoggerInterface&MockObject $logger;
4038

4139
#[\Override]
4240
protected function setUp(): void {
@@ -45,6 +43,7 @@ protected function setUp(): void {
4543
$this->userSession = $this->createMock(IUserSession::class);
4644
$this->cloudIdManager = $this->createMock(ICloudIdManager::class);
4745
$this->config = $this->createMock(IConfig::class);
46+
$this->globalScaleConfig = $this->createMock(GlobalScaleConfig::class);
4847
$this->logger = $this->createMock(LoggerInterface::class);
4948
$this->clientService = $this->createMock(IClientService::class);
5049
$cloudId = $this->createMock(ICloudId::class);
@@ -72,7 +71,8 @@ protected function setUp(): void {
7271
$this->userSession,
7372
$this->cloudIdManager,
7473
$this->logger,
75-
null
74+
null,
75+
$this->globalScaleConfig,
7676
);
7777
}
7878

@@ -81,13 +81,16 @@ public function testSearchNoLookupServerURI(): void {
8181
->method('getAppValue')
8282
->with('files_sharing', 'lookupServerEnabled', 'no')
8383
->willReturn('yes');
84-
$this->config->expects($this->exactly(2))
84+
$this->config->expects($this->once())
8585
->method('getSystemValueBool')
8686
->willReturnMap([
87-
['gs.enabled', false, true],
8887
['has_internet_connection', true, true],
8988
]);
9089

90+
$this->globalScaleConfig->expects($this->once())
91+
->method('isGlobalScaleEnabled')
92+
->willReturn(true);
93+
9194
$this->config->expects($this->once())
9295
->method('getSystemValueString')
9396
->with('lookup_server', 'https://lookup.nextcloud.com')
@@ -107,13 +110,16 @@ public function testSearchNoInternet(): void {
107110
->method('getAppValue')
108111
->with('files_sharing', 'lookupServerEnabled', 'no')
109112
->willReturn('yes');
110-
$this->config->expects($this->exactly(2))
113+
$this->config->expects($this->exactly(1))
111114
->method('getSystemValueBool')
112115
->willReturnMap([
113-
['gs.enabled', false, false],
114116
['has_internet_connection', true, false],
115117
]);
116118

119+
$this->globalScaleConfig->expects($this->exactly(1))
120+
->method('isGlobalScaleEnabled')
121+
->willReturn(false);
122+
117123
$this->clientService->expects($this->never())
118124
->method('newClient');
119125

@@ -140,13 +146,16 @@ public function testSearch(array $searchParams): void {
140146
->method('getAppValue')
141147
->with('files_sharing', 'lookupServerEnabled', 'no')
142148
->willReturn('yes');
143-
$this->config->expects($this->exactly(2))
149+
$this->config->expects($this->once())
144150
->method('getSystemValueBool')
145151
->willReturnMap([
146-
['gs.enabled', false, true],
147152
['has_internet_connection', true, true],
148153
]);
149154

155+
$this->globalScaleConfig->expects($this->once())
156+
->method('isGlobalScaleEnabled')
157+
->willReturn(true);
158+
150159
$this->config->expects($this->once())
151160
->method('getSystemValueString')
152161
->with('lookup_server', 'https://lookup.nextcloud.com')
@@ -202,12 +211,15 @@ public function testSearchEnableDisableLookupServer(array $searchParams, $GSEnab
202211
->method('addResultSet')
203212
->with($type, $searchParams['expectedResult'], []);
204213

205-
$this->config->expects($this->exactly(2))
214+
$this->config->expects($this->once())
206215
->method('getSystemValueBool')
207216
->willReturnMap([
208-
['gs.enabled', false, $GSEnabled],
209217
['has_internet_connection', true, true],
210218
]);
219+
220+
$this->globalScaleConfig->expects($this->once())
221+
->method('isGlobalScaleEnabled')
222+
->willReturn($GSEnabled);
211223
$this->config->expects($this->once())
212224
->method('getSystemValueString')
213225
->with('lookup_server', 'https://lookup.nextcloud.com')
@@ -232,12 +244,15 @@ public function testSearchEnableDisableLookupServer(array $searchParams, $GSEnab
232244
->willReturn($client);
233245
} else {
234246
$searchResult->expects($this->never())->method('addResultSet');
235-
$this->config->expects($this->exactly(2))
247+
$this->config->expects($this->once())
236248
->method('getSystemValueBool')
237249
->willReturnMap([
238-
['gs.enabled', false, $GSEnabled],
239250
['has_internet_connection', true, true],
240251
]);
252+
253+
$this->globalScaleConfig->expects($this->once())
254+
->method('isGlobalScaleEnabled')
255+
->willReturn($GSEnabled);
241256
}
242257
$moreResults = $this->plugin->search(
243258
$searchParams['search'],
@@ -251,13 +266,16 @@ public function testSearchEnableDisableLookupServer(array $searchParams, $GSEnab
251266

252267

253268
public function testSearchGSDisabled(): void {
254-
$this->config->expects($this->atLeastOnce())
269+
$this->config->expects($this->once())
255270
->method('getSystemValueBool')
256271
->willReturnMap([
257272
['has_internet_connection', true, true],
258-
['gs.enabled', false, false],
259273
]);
260274

275+
$this->globalScaleConfig->expects($this->once())
276+
->method('isGlobalScaleEnabled')
277+
->willReturn(false);
278+
261279
/** @var ISearchResult|MockObject $searchResult */
262280
$searchResult = $this->createMock(ISearchResult::class);
263281
$searchResult->expects($this->never())

0 commit comments

Comments
 (0)