diff options
author | Daniel Rudolf <github.com@daniel-rudolf.de> | 2020-05-08 13:56:39 +0300 |
---|---|---|
committer | Daniel Rudolf <github.com@daniel-rudolf.de> | 2020-05-08 13:57:15 +0300 |
commit | 3cacb89e4f2bf329dc3396b75dfe0cb57815a35f (patch) | |
tree | 4ad39faf7f5df89b91f65c4b55e04eff34ea65c8 | |
parent | b2664ba7a9e9d7267c86ae951adfdd9f857a4f63 (diff) |
Add --group and --circle options to talk:room:* commands
Signed-off-by: Daniel Rudolf <github.com@daniel-rudolf.de>
-rw-r--r-- | lib/Command/Room/Add.php | 16 | ||||
-rw-r--r-- | lib/Command/Room/Create.php | 20 | ||||
-rw-r--r-- | lib/Command/Room/TRoomCommand.php | 91 |
3 files changed, 119 insertions, 8 deletions
diff --git a/lib/Command/Room/Add.php b/lib/Command/Room/Add.php index e9b1c8276..b1d145d2d 100644 --- a/lib/Command/Room/Add.php +++ b/lib/Command/Room/Add.php @@ -62,14 +62,26 @@ class Add extends Base { 'Token of the room to add users to' )->addArgument( 'user', - InputArgument::REQUIRED | InputArgument::IS_ARRAY, + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Invites the given users to the room' + )->addOption( + 'group', + null, + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Invites all members of the given groups to the room' + )->addOption( + 'circle', + null, + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Invites all members of the given circles to the room' ); } protected function execute(InputInterface $input, OutputInterface $output): ?int { $token = $input->getArgument('token'); $users = $input->getArgument('user'); + $groups = $input->getOption('group'); + $circles = $input->getOption('circle'); try { $room = $this->manager->getRoomByToken($token); @@ -85,6 +97,8 @@ class Add extends Base { try { $this->addRoomParticipants($room, $users); + $this->addRoomParticipantsByGroup($room, $groups); + $this->addRoomParticipantsByCircle($room, $circles); } catch (Exception $e) { $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); return 1; diff --git a/lib/Command/Room/Create.php b/lib/Command/Room/Create.php index 5b6ecd6e0..dc20fd182 100644 --- a/lib/Command/Room/Create.php +++ b/lib/Command/Room/Create.php @@ -60,9 +60,19 @@ class Create extends Base { 'The name of the room to create' )->addArgument( 'user', - InputArgument::REQUIRED | InputArgument::IS_ARRAY, + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Invites the given users to the room to create' )->addOption( + 'group', + null, + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Invites all members of the given group to the room to create' + )->addOption( + 'circle', + null, + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + 'Invites all members of the given circle to the room to create' + )->addOption( 'public', null, InputOption::VALUE_NONE, @@ -88,10 +98,12 @@ class Create extends Base { protected function execute(InputInterface $input, OutputInterface $output): ?int { $name = $input->getArgument('name'); $users = $input->getArgument('user'); - $moderators = $input->getOption('moderator'); + $groups = $input->getOption('group'); + $circles = $input->getOption('circle'); $public = $input->getOption('public'); $readonly = $input->getOption('readonly'); - $password = (string) $input->getOption('password'); + $password = $input->getOption('password'); + $moderators = $input->getOption('moderator'); $name = trim($name); if (!$this->validateRoomName($name)) { @@ -106,6 +118,8 @@ class Create extends Base { $this->setRoomPassword($room, $password); $this->addRoomParticipants($room, $users); + $this->addRoomParticipantsByGroup($room, $groups); + $this->addRoomParticipantsByCircle($room, $circles); $this->addRoomModerators($room, $moderators); } catch (Exception $e) { $room->deleteRoom(); diff --git a/lib/Command/Room/TRoomCommand.php b/lib/Command/Room/TRoomCommand.php index b7aa1361e..a99ef1547 100644 --- a/lib/Command/Room/TRoomCommand.php +++ b/lib/Command/Room/TRoomCommand.php @@ -25,9 +25,12 @@ declare(strict_types=1); namespace OCA\Talk\Command\Room; use InvalidArgumentException; +use OCA\Circles\Api\v1\Circles; +use OCA\Circles\Model\Member; use OCA\Talk\Exceptions\ParticipantNotFoundException; use OCA\Talk\Participant; use OCA\Talk\Room; +use OCP\IUser; trait TRoomCommand { @@ -122,11 +125,82 @@ trait TRoomCommand /** * @param Room $room + * @param string[] $groupIds + * + * @throws InvalidArgumentException + */ + protected function addRoomParticipantsByGroup(Room $room, array $groupIds): void { + if (!$groupIds) { + return; + } + + $groupManager = \OC::$server->getGroupManager(); + + $users = []; + foreach ($groupIds as $groupId) { + $group = $groupManager->get($groupId); + if ($group === null) { + throw new InvalidArgumentException(sprintf("Group '%s' not found.", $groupId)); + } + + $groupUsers = array_map(function (IUser $user) { + return $user->getUID(); + }, $group->getUsers()); + + $users = array_merge($users, array_values($groupUsers)); + } + + $this->addRoomParticipants($room, $users); + } + + /** + * @param Room $room + * @param string[] $circleIds + * + * @throws InvalidArgumentException + */ + protected function addRoomParticipantsByCircle(Room $room, array $circleIds): void { + if (!$circleIds) { + return; + } + + if (!\OC::$server->getAppManager()->isEnabledForUser('circles')) { + throw new InvalidArgumentException("App 'circles' is not enabled."); + } + + $users = []; + foreach ($circleIds as $circleId) { + try { + $circle = Circles::detailsCircle($circleId); + } catch (\Exception $e) { + throw new InvalidArgumentException(sprintf("Circle '%s' not found.", $circleId)); + } + + $circleUsers = array_filter($circle->getMembers(), function (Member $member) { + if (($member->getType() !== Member::TYPE_USER) || ($member->getUserId() === '')) { + return false; + } + + return in_array($member->getStatus(), [Member::STATUS_INVITED, Member::STATUS_MEMBER], true); + }); + + $users = array_merge($users, $circleUsers); + } + + $this->addRoomParticipants($room, $users); + } + + /** + * @param Room $room * @param string[] $userIds * * @throws InvalidArgumentException */ protected function addRoomParticipants(Room $room, array $userIds): void { + if (!$userIds) { + return; + } + $userManager = \OC::$server->getUserManager(); $participants = []; @@ -136,14 +210,23 @@ trait TRoomCommand throw new InvalidArgumentException(sprintf("User '%s' not found.", $userId)); } + if (isset($participants[$user->getUID()])) { + // nothing to do, user is going to be a participant already + continue; + } + try { $room->getParticipant($user->getUID()); - // nothing to do, user already is a participant + + // nothing to do, user is a participant already + continue; } catch (ParticipantNotFoundException $e) { - $participants[] = [ - 'userId' => $user->getUID(), - ]; + // we expect the user not to be a participant yet } + + $participants[$user->getUID()] = [ + 'userId' => $user->getUID(), + ]; } \call_user_func_array([$room, 'addUsers'], $participants); |