Skip to content

Commit

Permalink
fix: propagate group name changes
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Steinmetz <[email protected]>
  • Loading branch information
st3iny committed Jan 6, 2025
1 parent 30cd96e commit dde3a4e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Circles\Listeners\Files\PreparingShareSendMail as ListenerFilesPreparingShareSendMail;
use OCA\Circles\Listeners\Files\RemovingMember as ListenerFilesRemovingMember;
use OCA\Circles\Listeners\Files\ShareCreatedSendMail as ListenerFilesShareCreatedSendMail;
use OCA\Circles\Listeners\GroupChanged;
use OCA\Circles\Listeners\GroupCreated;
use OCA\Circles\Listeners\GroupDeleted;
use OCA\Circles\Listeners\GroupMemberAdded;
Expand All @@ -52,6 +53,7 @@
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Group\Events\GroupChangedEvent;
use OCP\Group\Events\GroupCreatedEvent;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\UserAddedEvent;
Expand Down Expand Up @@ -98,6 +100,7 @@ public function register(IRegistrationContext $context): void {

// Group Events
$context->registerEventListener(GroupCreatedEvent::class, GroupCreated::class);
$context->registerEventListener(GroupChangedEvent::class, GroupChanged::class);
$context->registerEventListener(GroupDeletedEvent::class, GroupDeleted::class);
$context->registerEventListener(UserAddedEvent::class, GroupMemberAdded::class);
$context->registerEventListener(UserRemovedEvent::class, GroupMemberRemoved::class);
Expand Down
55 changes: 55 additions & 0 deletions lib/Listeners/GroupChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Circles\Listeners;

use OCA\Circles\Exceptions\CircleNotFoundException;
use OCA\Circles\Service\CircleService;
use OCA\Circles\Service\FederatedUserService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Group\Events\GroupChangedEvent;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;

/** @template-implements IEventListener<GroupChangedEvent|Event> */
class GroupChanged implements IEventListener {
public function __construct(
private readonly IUserSession $userSession,
private readonly FederatedUserService $federatedUserService,
private readonly CircleService $circleService,
private readonly LoggerInterface $logger,
) {
}

public function handle(Event $event): void {
if (!($event instanceof GroupChangedEvent)) {
return;
}

if ($event->getFeature() !== 'displayName') {
return;
}

$user = $this->userSession->getUser();
$this->federatedUserService->setLocalCurrentUser($user);

$groupId = $event->getGroup()->getGID();
try {
$this->circleService->updateName("group:$groupId", $event->getValue());
} catch (CircleNotFoundException $e) {
// Silently ignore (there is no circle for the group yet)
} catch (\Exception $e) {
$this->logger->warning("Failed to update display name of circle of group $groupId: " . $e->getMessage(), [
'exception' => $e,
'groupId' => $groupId,
]);
}
}
}

0 comments on commit dde3a4e

Please sign in to comment.