diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-06-22 12:05:26 +0200 |
---|---|---|
committer | Carl Schwan <carl@carlschwan.eu> | 2022-06-22 16:11:42 +0200 |
commit | 812016d62614ac3669a3fdf51fa2e07170e08c08 (patch) | |
tree | 1d67fcb259fae084881e50741ea6d7a5beb3e648 /lib/private/User | |
parent | b282fe1e6f5587a6440d170df245ad5acb8dc976 (diff) | |
download | nextcloud-server-812016d62614ac3669a3fdf51fa2e07170e08c08.tar.gz nextcloud-server-812016d62614ac3669a3fdf51fa2e07170e08c08.zip |
Cleanup avatar related code
- Move event listener to new event handling
- Add typing almost everywhere
- Fix inconsistent interface parameter
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'lib/private/User')
-rw-r--r-- | lib/private/User/Listeners/UserChangedListener.php | 62 | ||||
-rw-r--r-- | lib/private/User/Listeners/UserDeletedListener.php | 63 |
2 files changed, 125 insertions, 0 deletions
diff --git a/lib/private/User/Listeners/UserChangedListener.php b/lib/private/User/Listeners/UserChangedListener.php new file mode 100644 index 00000000000..f3c7468460a --- /dev/null +++ b/lib/private/User/Listeners/UserChangedListener.php @@ -0,0 +1,62 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2022 Carl Schwan <carl@carlschwan.eu> + * + * @license AGPL-3.0-or-later + * + * 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 OC\User\Listeners; + +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\UserChangedEvent; +use OCP\Files\NotFoundException; +use OCP\IAvatarManager; + +/** + * @template-implements IEventListener<\OCP\User\Events\UserChangedEvent> + */ +class UserChangedListener implements IEventListener { + private IAvatarManager $avatarManager; + + public function __construct(IAvatarManager $avatarManager) { + $this->avatarManager = $avatarManager; + } + + public function handle(Event $event): void { + if (!($event instanceof UserChangedEvent)) { + return; + } + + $user = $event->getUser(); + $feature = $event->getFeature(); + $oldValue = $event->getOldValue(); + $value = $event->getValue(); + + // We only change the avatar on display name changes + if ($feature === 'displayName') { + try { + $avatar = $this->avatarManager->getAvatar($user->getUID()); + $avatar->userChanged($feature, $oldValue, $value); + } catch (NotFoundException $e) { + // no avatar to remove + } + } + } +} diff --git a/lib/private/User/Listeners/UserDeletedListener.php b/lib/private/User/Listeners/UserDeletedListener.php new file mode 100644 index 00000000000..a8ad56d97ab --- /dev/null +++ b/lib/private/User/Listeners/UserDeletedListener.php @@ -0,0 +1,63 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2022 Carl Schwan <carl@carlschwan.eu> + * + * @license AGPL-3.0-or-later + * + * 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 OC\User\Listeners; + +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\UserDeletedEvent; +use OCP\Files\NotFoundException; +use OCP\IAvatarManager; +use Psr\Log\LoggerInterface; + +/** + * @template-implements IEventListener<\OCP\User\Events\UserDeletedEvent> + */ +class UserDeletedListener implements IEventListener { + private IAvatarManager $avatarManager; + private LoggerInterface $logger; + + public function __construct(LoggerInterface $logger, IAvatarManager $avatarManager) { + $this->avatarManager = $avatarManager; + $this->logger = $logger; + } + + public function handle(Event $event): void { + if (!($event instanceof UserDeletedEvent)) { + return; + } + + $user = $event->getUser(); + + // Delete avatar on user deletion + try { + $avatar = $this->avatarManager->getAvatar($user->getUID()); + $avatar->remove(); + } catch (NotFoundException $e) { + // no avatar to remove + } catch (\Exception $e) { + // Ignore exceptions + $this->logger->info('Could not cleanup avatar of ' . $user->getUID()); + } + } +} |