diff options
author | Vitor Mattos <vitor@php.rio> | 2021-12-01 14:18:32 +0300 |
---|---|---|
committer | Vitor Mattos <vitor@php.rio> | 2021-12-02 18:27:26 +0300 |
commit | c9cf44e9309f6797e0b18bed48061f74328d01ab (patch) | |
tree | 350c6545723164278eda319651331c992728c4d3 /lib | |
parent | 1df244fde66aef14d1218596dcba569c4c7960b0 (diff) |
Add command to remove users from all rooms
Signed-off-by: Vitor Mattos <vitor@php.rio>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/User/Remove.php | 80 | ||||
-rw-r--r-- | lib/Listener/UserDeletedListener.php | 26 | ||||
-rw-r--r-- | lib/Manager.php | 19 |
3 files changed, 101 insertions, 24 deletions
diff --git a/lib/Command/User/Remove.php b/lib/Command/User/Remove.php new file mode 100644 index 000000000..f2852dcf0 --- /dev/null +++ b/lib/Command/User/Remove.php @@ -0,0 +1,80 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021, Vitor Mattos <vitor@php.rio> + * + * @author Vitor Mattos <vitor@php.rio> + * + * @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\Talk\Command\User; + +use OC\Core\Command\Base; +use OCA\Talk\Manager; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class Remove extends Base { + /** @var IUserManager */ + private $userManager; + /** @var Manager */ + private $manager; + + public function __construct(IUserManager $userManager, + Manager $manager) { + parent::__construct(); + $this->userManager = $userManager; + $this->manager = $manager; + } + + protected function configure(): void { + $this + ->setName('talk:user:remove') + ->setDescription('Remove a user from all their rooms') + ->addOption( + 'user', + null, + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Remove the given users from all rooms' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $userIds = $input->getOption('user'); + + $users = []; + foreach ($userIds as $userId) { + $user = $this->userManager->get($userId); + if (!$user) { + $output->writeln('<error>' . sprintf("User '%s' not found.", $userId) . '</error>'); + return 1; + } + $users[] = $user; + } + + foreach ($users as $user) { + $this->manager->removeUserFromAllRooms($user); + } + + $output->writeln('<info>Users successfully removed from all rooms.</info>'); + return 0; + } +} diff --git a/lib/Listener/UserDeletedListener.php b/lib/Listener/UserDeletedListener.php index c93a106bd..a96c695c4 100644 --- a/lib/Listener/UserDeletedListener.php +++ b/lib/Listener/UserDeletedListener.php @@ -24,8 +24,6 @@ declare(strict_types=1); namespace OCA\Talk\Listener; use OCA\Talk\Manager; -use OCA\Talk\Room; -use OCA\Talk\Service\ParticipantService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\User\Events\UserDeletedEvent; @@ -34,13 +32,9 @@ class UserDeletedListener implements IEventListener { /** @var Manager */ private $manager; - /** @var ParticipantService */ - private $participantService; - public function __construct(Manager $manager, - ParticipantService $participantService) { + public function __construct(Manager $manager) { $this->manager = $manager; - $this->participantService = $participantService; } public function handle(Event $event): void { @@ -50,22 +44,6 @@ class UserDeletedListener implements IEventListener { } $user = $event->getUser(); - - $rooms = $this->manager->getRoomsForUser($user->getUID()); - foreach ($rooms as $room) { - if ($this->participantService->getNumberOfUsers($room) === 1) { - $room->deleteRoom(); - } else { - $this->participantService->removeUser($room, $user, Room::PARTICIPANT_REMOVED); - } - } - - $leftRooms = $this->manager->getLeftOneToOneRoomsForUser($user->getUID()); - foreach ($leftRooms as $room) { - // We are changing the room type and name so a potential follow up - // user with the same user-id can not reopen the one-to-one conversation. - $room->setType(Room::TYPE_GROUP, true); - $room->setName($user->getDisplayName(), ''); - } + $this->manager->removeUserFromAllRooms($user); } } diff --git a/lib/Manager.php b/lib/Manager.php index 0422ab1fc..250773fb3 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -389,6 +389,25 @@ class Manager { return $rooms; } + public function removeUserFromAllRooms(IUser $user) { + $rooms = $this->getRoomsForUser($user->getUID()); + foreach ($rooms as $room) { + if ($this->participantService->getNumberOfUsers($room) === 1) { + $room->deleteRoom(); + } else { + $this->participantService->removeUser($room, $user, Room::PARTICIPANT_REMOVED); + } + } + + $leftRooms = $this->getLeftOneToOneRoomsForUser($user->getUID()); + foreach ($leftRooms as $room) { + // We are changing the room type and name so a potential follow up + // user with the same user-id can not reopen the one-to-one conversation. + $room->setType(Room::TYPE_GROUP, true); + $room->setName($user->getDisplayName(), ''); + } + } + /** * @param string $userId * @return string[] |