1<?php
2
3declare(strict_types=1);
4/**
5 * @copyright Copyright (c) 2020 Daniel Rudolf <nextcloud.com@daniel-rudolf.de>
6 *
7 * @author Daniel Rudolf <nextcloud.com@daniel-rudolf.de>
8 *
9 * @license GNU AGPL version 3 or any later version
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as
13 * published by the Free Software Foundation, either version 3 of the
14 * License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26namespace OCA\Talk\Command\Room;
27
28use InvalidArgumentException;
29use OC\Core\Command\Base;
30use OCA\Talk\Exceptions\RoomNotFoundException;
31use OCA\Talk\Room;
32use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
33use Symfony\Component\Console\Input\InputArgument;
34use Symfony\Component\Console\Input\InputInterface;
35use Symfony\Component\Console\Output\OutputInterface;
36
37class Promote extends Base {
38	use TRoomCommand;
39
40	protected function configure(): void {
41		$this
42			->setName('talk:room:promote')
43			->setDescription('Promotes participants of a room to moderators')
44			->addArgument(
45				'token',
46				InputArgument::REQUIRED,
47				'Token of the room in which users should be promoted'
48			)->addArgument(
49				'participant',
50				InputArgument::REQUIRED | InputArgument::IS_ARRAY,
51				'Promotes the given participants of the room to moderators'
52			);
53	}
54
55	protected function execute(InputInterface $input, OutputInterface $output): int {
56		$token = $input->getArgument('token');
57		$users = $input->getArgument('participant');
58
59		try {
60			$room = $this->manager->getRoomByToken($token);
61		} catch (RoomNotFoundException $e) {
62			$output->writeln('<error>Room not found.</error>');
63			return 1;
64		}
65
66		if (!in_array($room->getType(), [Room::TYPE_GROUP, Room::TYPE_PUBLIC], true)) {
67			$output->writeln('<error>Room is no group call.</error>');
68			return 1;
69		}
70
71		try {
72			$this->addRoomModerators($room, $users);
73		} catch (InvalidArgumentException $e) {
74			$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
75			return 1;
76		}
77
78		$output->writeln('<info>Participants successfully promoted to moderators.</info>');
79		return 0;
80	}
81
82	public function completeArgumentValues($argumentName, CompletionContext $context) {
83		switch ($argumentName) {
84			case 'token':
85				return $this->completeTokenValues($context);
86
87			case 'participant':
88				return $this->completeParticipantValues($context);
89		}
90
91		return parent::completeArgumentValues($argumentName, $context);
92	}
93}
94