Skip to content

Commit 860f32e

Browse files
committed
Summer cleanup of the federation app
- Use IEventDispatcher instead of deprecated symfony dispatcher - Use LoggerInterface where possible - Use php 7.4 properties - Add type hinting where possible - Move federation hooks to a seperate listener Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent b282fe1 commit 860f32e

22 files changed

+316
-480
lines changed

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
'OCA\\DAV\\Listener\\CardListener' => $baseDir . '/../lib/Listener/CardListener.php',
247247
'OCA\\DAV\\Listener\\ClearPhotoCacheListener' => $baseDir . '/../lib/Listener/ClearPhotoCacheListener.php',
248248
'OCA\\DAV\\Listener\\SubscriptionListener' => $baseDir . '/../lib/Listener/SubscriptionListener.php',
249+
'OCA\\DAV\\Listener\\TrustedServerRemovedListener' => $baseDir . '/../lib/Listener/TrustedServerRemovedListener.php',
249250
'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndex.php',
250251
'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php',
251252
'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => $baseDir . '/../lib/Migration/BuildSocialSearchIndex.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ class ComposerStaticInitDAV
261261
'OCA\\DAV\\Listener\\CardListener' => __DIR__ . '/..' . '/../lib/Listener/CardListener.php',
262262
'OCA\\DAV\\Listener\\ClearPhotoCacheListener' => __DIR__ . '/..' . '/../lib/Listener/ClearPhotoCacheListener.php',
263263
'OCA\\DAV\\Listener\\SubscriptionListener' => __DIR__ . '/..' . '/../lib/Listener/SubscriptionListener.php',
264+
'OCA\\DAV\\Listener\\TrustedServerRemovedListener' => __DIR__ . '/..' . '/../lib/Listener/TrustedServerRemovedListener.php',
264265
'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndex.php',
265266
'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php',
266267
'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildSocialSearchIndex.php',

apps/dav/lib/AppInfo/Application.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
use OCA\DAV\Events\CardUpdatedEvent;
7272
use OCA\DAV\Events\SubscriptionCreatedEvent;
7373
use OCA\DAV\Events\SubscriptionDeletedEvent;
74+
use OCP\Federation\Events\TrustedServerRemovedEvent;
7475
use OCA\DAV\HookManager;
7576
use OCA\DAV\Listener\ActivityUpdaterListener;
7677
use OCA\DAV\Listener\AddressbookListener;
@@ -83,6 +84,7 @@
8384
use OCA\DAV\Listener\CardListener;
8485
use OCA\DAV\Listener\ClearPhotoCacheListener;
8586
use OCA\DAV\Listener\SubscriptionListener;
87+
use OCA\DAV\Listener\TrustedServerRemovedListener;
8688
use OCA\DAV\Search\ContactsSearchProvider;
8789
use OCA\DAV\Search\EventsSearchProvider;
8890
use OCA\DAV\Search\TasksSearchProvider;
@@ -182,6 +184,7 @@ public function register(IRegistrationContext $context): void {
182184
$context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class);
183185
$context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class);
184186
$context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class);
187+
$context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class);
185188

186189
$context->registerNotifierService(Notifier::class);
187190

@@ -235,18 +238,6 @@ public function registerHooks(HookManager $hm,
235238
// Here we should recalculate if reminders should be sent to new or old sharees
236239
});
237240

238-
$dispatcher->addListener('OCP\Federation\TrustedServerEvent::remove',
239-
function (GenericEvent $event) {
240-
/** @var CardDavBackend $cardDavBackend */
241-
$cardDavBackend = \OC::$server->query(CardDavBackend::class);
242-
$addressBookUri = $event->getSubject();
243-
$addressBook = $cardDavBackend->getAddressBooksByUri('principals/system/system', $addressBookUri);
244-
if (!is_null($addressBook)) {
245-
$cardDavBackend->deleteAddressBook($addressBook['id']);
246-
}
247-
}
248-
);
249-
250241
$eventHandler = function () use ($container, $serverContainer): void {
251242
try {
252243
/** @var UpdateCalendarResourcesRoomsBackgroundJob $job */

apps/dav/lib/CardDAV/SyncService.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,13 @@ public function __construct(CardDavBackend $backend,
7373
}
7474

7575
/**
76-
* @param string $url
77-
* @param string $userName
78-
* @param string $addressBookUrl
79-
* @param string $sharedSecret
80-
* @param string $syncToken
81-
* @param int $targetBookId
82-
* @param string $targetPrincipal
8376
* @param array $targetProperties
8477
* @return string
8578
* @throws \Exception
8679
*/
87-
public function syncRemoteAddressBook($url, $userName, $addressBookUrl, $sharedSecret, $syncToken, $targetBookId, $targetPrincipal, $targetProperties) {
80+
public function syncRemoteAddressBook(string $url, string $userName, string $addressBookUrl, string $sharedSecret, string $syncToken, string $targetBookId, string $targetPrincipal, array $targetProperties) {
8881
// 1. create addressbook
89-
$book = $this->ensureSystemAddressBookExists($targetPrincipal, (string)$targetBookId, $targetProperties);
82+
$book = $this->ensureSystemAddressBookExists($targetPrincipal, $targetBookId, $targetProperties);
9083
$addressBookId = $book['id'];
9184

9285
// 2. query changes
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2022 Carl Schwan <carl@carlschwan.eu>
7+
*
8+
* @author Carl Schwan <carl@carlschwan.eu>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
namespace OCA\DAV\Listener;
27+
28+
use OCA\DAV\CardDAV\CardDavBackend;
29+
use OCP\EventDispatcher\Event;
30+
use OCP\EventDispatcher\IEventListener;
31+
use OCP\Federation\Events\TrustedServerRemovedEvent;
32+
33+
class TrustedServerRemovedListener implements IEventListener {
34+
private CardDavBackend $cardDavBackend;
35+
36+
public function __construct(CardDavBackend $cardDavBackend) {
37+
$this->cardDavBackend = $cardDavBackend;
38+
}
39+
40+
public function handle(Event $event): void {
41+
if (!$event instanceof TrustedServerRemovedEvent) {
42+
return;
43+
}
44+
$addressBookUri = $event->getUrlHash();
45+
$addressBook = $this->cardDavBackend->getAddressBooksByUri('principals/system/system', $addressBookUri);
46+
if (!is_null($addressBook)) {
47+
$this->cardDavBackend->deleteAddressBook($addressBook['id']);
48+
}
49+
}
50+
}

apps/federation/lib/BackgroundJob/GetSharedSecret.php

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,59 +42,34 @@
4242
use OCP\ILogger;
4343
use OCP\IURLGenerator;
4444
use OCP\OCS\IDiscoveryService;
45+
use Psr\Log\LoggerInterface;
4546

4647
/**
4748
* Class GetSharedSecret
4849
*
49-
* request shared secret from remote Nextcloud
50+
* Request shared secret from remote Nextcloud
5051
*
5152
* @package OCA\Federation\Backgroundjob
5253
*/
5354
class GetSharedSecret extends Job {
55+
private IClient $httpClient;
56+
private IJobList $jobList;
57+
private IURLGenerator $urlGenerator;
58+
private TrustedServers $trustedServers;
59+
private IDiscoveryService $ocsDiscoveryService;
60+
private LoggerInterface $logger;
61+
protected bool $retainJob = false;
62+
private string $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret';
63+
64+
/** 30 day = 2592000sec */
65+
private int $maxLifespan = 2592000;
5466

55-
/** @var IClient */
56-
private $httpClient;
57-
58-
/** @var IJobList */
59-
private $jobList;
60-
61-
/** @var IURLGenerator */
62-
private $urlGenerator;
63-
64-
/** @var TrustedServers */
65-
private $trustedServers;
66-
67-
/** @var IDiscoveryService */
68-
private $ocsDiscoveryService;
69-
70-
/** @var ILogger */
71-
private $logger;
72-
73-
/** @var bool */
74-
protected $retainJob = false;
75-
76-
private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret';
77-
78-
/** @var int 30 day = 2592000sec */
79-
private $maxLifespan = 2592000;
80-
81-
/**
82-
* RequestSharedSecret constructor.
83-
*
84-
* @param IClientService $httpClientService
85-
* @param IURLGenerator $urlGenerator
86-
* @param IJobList $jobList
87-
* @param TrustedServers $trustedServers
88-
* @param ILogger $logger
89-
* @param IDiscoveryService $ocsDiscoveryService
90-
* @param ITimeFactory $timeFactory
91-
*/
9267
public function __construct(
9368
IClientService $httpClientService,
9469
IURLGenerator $urlGenerator,
9570
IJobList $jobList,
9671
TrustedServers $trustedServers,
97-
ILogger $logger,
72+
LoggerInterface $logger,
9873
IDiscoveryService $ocsDiscoveryService,
9974
ITimeFactory $timeFactory
10075
) {
@@ -128,7 +103,7 @@ public function execute(IJobList $jobList, ILogger $logger = null) {
128103
}
129104

130105
/**
131-
* call execute() method of parent
106+
* Call execute() method of parent
132107
*
133108
* @param IJobList $jobList
134109
* @param ILogger $logger
@@ -185,14 +160,16 @@ protected function run($argument) {
185160
}
186161
} catch (RequestException $e) {
187162
$status = -1; // There is no status code if we could not connect
188-
$this->logger->logException($e, [
189-
'message' => 'Could not connect to ' . $target,
190-
'level' => ILogger::INFO,
163+
$this->logger->info('Could not connect to ' . $target, [
164+
'exception' => $e,
191165
'app' => 'federation',
192166
]);
193167
} catch (\Throwable $e) {
194168
$status = Http::STATUS_INTERNAL_SERVER_ERROR;
195-
$this->logger->logException($e, ['app' => 'federation']);
169+
$this->logger->error($e->getMessage(), [
170+
'app' => 'federation',
171+
'exception' => $e,
172+
]);
196173
}
197174

198175
// if we received a unexpected response we try again later
@@ -226,7 +203,7 @@ protected function run($argument) {
226203
*
227204
* @param array $argument
228205
*/
229-
protected function reAddJob(array $argument) {
206+
protected function reAddJob(array $argument): void {
230207
$url = $argument['url'];
231208
$created = isset($argument['created']) ? (int)$argument['created'] : $this->time->getTime();
232209
$token = $argument['token'];

apps/federation/lib/Command/SyncFederationAddressBooks.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@
2828
use Symfony\Component\Console\Helper\ProgressBar;
2929
use Symfony\Component\Console\Input\InputInterface;
3030
use Symfony\Component\Console\Output\OutputInterface;
31+
use OCA\Federation\SyncFederationAddressBooks as SyncService;
3132

3233
class SyncFederationAddressBooks extends Command {
34+
private SyncService $syncService;
3335

34-
/** @var \OCA\Federation\SyncFederationAddressBooks */
35-
private $syncService;
36-
37-
/**
38-
* @param \OCA\Federation\SyncFederationAddressBooks $syncService
39-
*/
40-
public function __construct(\OCA\Federation\SyncFederationAddressBooks $syncService) {
36+
public function __construct(SyncService $syncService) {
4137
parent::__construct();
4238

4339
$this->syncService = $syncService;
@@ -49,11 +45,6 @@ protected function configure() {
4945
->setDescription('Synchronizes addressbooks of all federated clouds');
5046
}
5147

52-
/**
53-
* @param InputInterface $input
54-
* @param OutputInterface $output
55-
* @return int
56-
*/
5748
protected function execute(InputInterface $input, OutputInterface $output): int {
5849
$progress = new ProgressBar($output);
5950
$progress->start();

0 commit comments

Comments
 (0)