Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(bots): Add API to list, enable and disable bots in a conversation
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Aug 7, 2023
commit 4ee530a1901f81233c38c6c8881bf0feefe8a33a
12 changes: 12 additions & 0 deletions appinfo/routes/routesBotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@
'token' => '[a-z0-9]{4,30}',
];

$requirementsWithBotId = [
'apiVersion' => 'v1',
'token' => '[a-z0-9]{4,30}',
'botId' => '[0-9]+',
];

return [
'ocs' => [
/** @see \OCA\Talk\Controller\BotController::sendMessage() */
['name' => 'Bot#sendMessage', 'url' => '/api/{apiVersion}/bot/{token}/message', 'verb' => 'POST', 'requirements' => $requirements],
/** @see \OCA\Talk\Controller\BotController::listBots() */
['name' => 'Bot#listBots', 'url' => '/api/{apiVersion}/bot/{token}', 'verb' => 'GET', 'requirements' => $requirements],
/** @see \OCA\Talk\Controller\BotController::enableBot() */
['name' => 'Bot#enableBot', 'url' => '/api/{apiVersion}/bot/{token}/{botId}', 'verb' => 'POST', 'requirements' => $requirementsWithBotId],
/** @see \OCA\Talk\Controller\BotController::disableBot() */
['name' => 'Bot#disableBot', 'url' => '/api/{apiVersion}/bot/{token}/{botId}', 'verb' => 'DELETE', 'requirements' => $requirementsWithBotId],
],
];
98 changes: 95 additions & 3 deletions lib/Controller/BotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@
use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Exceptions\UnauthorizedException;
use OCA\Talk\Manager;
use OCA\Talk\Middleware\Attribute\RequireLoggedInModeratorParticipant;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\Bot;
use OCA\Talk\Model\BotConversation;
use OCA\Talk\Model\BotConversationMapper;
use OCA\Talk\Model\BotServerMapper;
use OCA\Talk\Service\BotService;
use OCA\Talk\Service\ChecksumVerificationService;
use OCA\Talk\Service\ParticipantService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -52,9 +58,11 @@ public function __construct(
protected ParticipantService $participantService,
protected ITimeFactory $timeFactory,
protected ChecksumVerificationService $checksumVerificationService,
protected BotService $botService,
protected Manager $manager,
protected LoggerInterface $logger,
protected BotConversationMapper $botConversationMapper,
protected BotServerMapper $botServerMapper,
protected BotService $botService,
protected Manager $manager,
protected LoggerInterface $logger,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -139,4 +147,88 @@ public function sendMessage(string $token, string $message, string $referenceId

return new DataResponse([], Http::STATUS_CREATED);
}

#[NoAdminRequired]
#[RequireLoggedInModeratorParticipant]
public function listBots(): DataResponse {
$alreadyInstalled = array_map(static function (BotConversation $bot): int {
return $bot->getBotId();
}, $this->botConversationMapper->findForToken($this->room->getToken()));

$bots = $this->botServerMapper->getAllBots();
foreach ($bots as $bot) {
$state = in_array($bot->getId(), $alreadyInstalled, true) ? Bot::STATE_ENABLED : Bot::STATE_DISABLED;

if ($bot->getState() === Bot::STATE_NO_SETUP) {
if ($state === Bot::STATE_DISABLED) {
continue;
}
$state = Bot::STATE_NO_SETUP;
}

$data[] = [
'id' => $bot->getId(),
'name' => $bot->getName(),
'description' => $bot->getDescription(),
'state' => $state ,
];
}

return new DataResponse($data);
}

#[NoAdminRequired]
#[RequireLoggedInModeratorParticipant]
public function enableBot(int $botId): DataResponse {
try {
$bot = $this->botServerMapper->findById($botId);
} catch (DoesNotExistException) {
return new DataResponse([
'error' => 'bot',
], Http::STATUS_BAD_REQUEST);
}

if ($bot->getState() !== Bot::STATE_ENABLED) {
return new DataResponse([
'error' => 'bot',
], Http::STATUS_BAD_REQUEST);
}

$alreadyInstalled = array_map(static function (BotConversation $bot): int {
return $bot->getBotId();
}, $this->botConversationMapper->findForToken($this->room->getToken()));

if (in_array($botId, $alreadyInstalled)) {
return new DataResponse([], Http::STATUS_OK);
}

$conversationBot = new BotConversation();
$conversationBot->setBotId($botId);
$conversationBot->setToken($this->room->getToken());
$conversationBot->setState(Bot::STATE_ENABLED);

$this->botConversationMapper->insert($conversationBot);
return new DataResponse([], Http::STATUS_CREATED);
}

#[NoAdminRequired]
#[RequireLoggedInModeratorParticipant]
public function disableBot(int $botId): DataResponse {
try {
$bot = $this->botServerMapper->findById($botId);
} catch (DoesNotExistException) {
return new DataResponse([
'error' => 'bot',
], Http::STATUS_BAD_REQUEST);
}

if ($bot->getState() !== Bot::STATE_ENABLED) {
return new DataResponse([
'error' => 'bot',
], Http::STATUS_BAD_REQUEST);
}

$this->botConversationMapper->deleteByBotIdAndTokens($botId, [$this->room->getToken()]);
return new DataResponse([], Http::STATUS_OK);
}
}