-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dav): expose Nextcloud groups to Contact's system addressbook co…
…ntacts And move event listeners related to SyncService to proper ones Note: Changes to user system addressbook cards are now always done in a QueuedJob Closes #38432 Signed-off-by: Thomas Citharel <[email protected]>
- Loading branch information
Showing
11 changed files
with
498 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
apps/dav/lib/BackgroundJob/SyncSystemAddressBookAfterUsersChange.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2023, Citharel Thomas <[email protected]> | ||
* | ||
* @author Citharel Thomas <[email protected]> | ||
* | ||
* @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\DAV\BackgroundJob; | ||
|
||
use OCA\DAV\CardDAV\SyncService; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\QueuedJob; | ||
use OCP\IUser; | ||
|
||
class SyncSystemAddressBookAfterUsersChange extends QueuedJob { | ||
private SyncService $syncService; | ||
|
||
public function __construct(SyncService $syncService, ITimeFactory $time) { | ||
parent::__construct($time); | ||
$this->syncService = $syncService; | ||
} | ||
|
||
/** | ||
* @param IUser[] $argument | ||
* @return void | ||
*/ | ||
public function run($argument): void { | ||
foreach ($argument as $user) { | ||
$this->syncService->updateUser($user); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2023 Thomas Citharel <[email protected]> | ||
* | ||
* @author Thomas Citharel <[email protected]> | ||
* | ||
* @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\DAV\Listener; | ||
|
||
use OCA\DAV\AppInfo\Application; | ||
use OCA\DAV\BackgroundJob\SyncSystemAddressBookAfterUsersChange; | ||
use OCP\BackgroundJob\IJobList; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Group\Events\BeforeGroupChangedEvent; | ||
use OCP\Group\Events\BeforeGroupDeletedEvent; | ||
use OCP\IConfig; | ||
|
||
/** | ||
* @template-implements IEventListener<BeforeGroupChangedEvent|BeforeGroupDeletedEvent> | ||
*/ | ||
class GroupChangeListener implements IEventListener { | ||
public function __construct(private IJobList $jobList, private IConfig $config) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!$this->canSyncBecauseOfGroupChange($event)) { | ||
// Not what we subscribed to | ||
return; | ||
} | ||
/** @var BeforeGroupChangedEvent|BeforeGroupDeletedEvent $event */ | ||
$this->jobList->add(SyncSystemAddressBookAfterUsersChange::class, $event->getGroup()->getUsers()); | ||
} | ||
|
||
private function canSyncBecauseOfGroupChange(Event $event): bool { | ||
return ($event instanceof BeforeGroupChangedEvent || $event instanceof BeforeGroupDeletedEvent) && $this->config->getAppValue(Application::APP_ID, 'system_addressbook_expose_groups', 'no') === 'yes'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2023 Thomas Citharel <[email protected]> | ||
* | ||
* @author Thomas Citharel <[email protected]> | ||
* | ||
* @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\DAV\Listener; | ||
|
||
use OCA\DAV\AppInfo\Application; | ||
use OCA\DAV\BackgroundJob\SyncSystemAddressBookAfterUsersChange; | ||
use OCP\Accounts\UserUpdatedEvent; | ||
use OCP\BackgroundJob\IJobList; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Group\Events\UserAddedEvent; | ||
use OCP\Group\Events\UserRemovedEvent; | ||
use OCP\IConfig; | ||
|
||
/** | ||
* @template-implements IEventListener<UserUpdatedEvent|UserAddedEvent|UserRemovedEvent> | ||
*/ | ||
class UserChangeListener implements IEventListener { | ||
public function __construct(private IJobList $jobList, private IConfig $config) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!($event instanceof UserUpdatedEvent || $event instanceof UserAddedEvent || $event instanceof UserRemovedEvent)) { | ||
// Not what we subscribed to | ||
return; | ||
} | ||
|
||
if ($this->canSyncBecauseOfGroupMembershipChange($event)) { | ||
/** @var UserUpdatedEvent|UserAddedEvent|UserRemovedEvent $event */ | ||
$this->jobList->add(SyncSystemAddressBookAfterUsersChange::class, [$event->getUser()]); | ||
} | ||
} | ||
|
||
private function canSyncBecauseOfGroupMembershipChange(Event $event): bool { | ||
return $event instanceof UserUpdatedEvent || $this->config->getAppValue(Application::APP_ID, 'system_addressbook_expose_groups', 'no') === 'yes'; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
apps/dav/tests/unit/BackgroundJob/SyncSystemAddressBookAfterUserChangeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright 2018, Georg Ehrke <[email protected]> | ||
* | ||
* @author Christoph Wurst <[email protected]> | ||
* @author Georg Ehrke <[email protected]> | ||
* @author Joas Schilling <[email protected]> | ||
* @author Morris Jobke <[email protected]> | ||
* @author Roeland Jago Douma <[email protected]> | ||
* | ||
* @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\DAV\Tests\unit\BackgroundJob; | ||
|
||
use OCA\DAV\BackgroundJob\SyncSystemAddressBookAfterUsersChange; | ||
use OCA\DAV\CardDAV\SyncService; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\IUser; | ||
use Test\TestCase; | ||
|
||
class SyncSystemAddressBookAfterUserChangeTest extends TestCase { | ||
private SyncService|MockObject $syncService; | ||
|
||
private SyncSystemAddressBookAfterUsersChange $backgroundJob; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->syncService = $this->createMock(SyncService::class); | ||
$timeFactory = $this->createMock(ITimeFactory::class); | ||
|
||
$this->backgroundJob = new SyncSystemAddressBookAfterUsersChange( | ||
$this->syncService, $timeFactory); | ||
} | ||
|
||
public function testRun(): void { | ||
$user1 = $this->createMock(IUser::class); | ||
$user2 = $this->createMock(IUser::class); | ||
|
||
$this->syncService->expects($this->exactly(2))->method('updateUser')->withConsecutive([$user1], [$user2]); | ||
|
||
$this->backgroundJob->run([$user1, $user2]); | ||
} | ||
} |
Oops, something went wrong.