Nextcloud-App/lib/Controller/TicketApiController.php
Ben 940c7bed6e
Start implementing matrix chat integration
Implement auto-login for element-web
2022-04-30 14:52:25 +02:00

109 lines
2.6 KiB
PHP

<?php
namespace OCA\UPschooling\Controller;
use OCA\UPschooling\AppInfo\Application;
use OCA\UPschooling\Db\TicketMapper;
use OCA\UPschooling\Service\MatrixService;
use OCA\UPschooling\Service\TicketService;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use Punic\Data;
class TicketApiController extends ApiController
{
/** @var LoggerInterface */
private $logger;
/** @var TicketService */
private $ticketService;
/** @var MatrixService */
private $matrixService;
/** @var TicketMapper */
private $ticketMapper;
/** @var string */
private $userId;
use Errors;
public function __construct(
IRequest $request,
LoggerInterface $logger,
TicketService $ticketService,
TicketMapper $ticketMapper,
MatrixService $matrixService,
string $userId
) {
parent::__construct(Application::APP_ID, $request);
$this->logger = $logger;
$this->ticketService = $ticketService;
$this->ticketMapper = $ticketMapper;
$this->matrixService = $matrixService;
$this->userId = $userId;
}
/**
* @NoAdminRequired
*/
public function index(): DataResponse
{
return new DataResponse($this->ticketService->findAll($this->userId));
}
/**
* @NoAdminRequired
*/
public function show(int $id): DataResponse
{
return $this->handleNotFound(function () use ($id) {
return $this->ticketService->find($id, $this->userId);
});
}
/**
* @NoAdminRequired
*/
public function fetchChat(int $id): DataResponse
{
return $this->handleNotFound(function () use ($id) {
$matrixUser = $this->ticketService->getOrCreateUser($this->userId);
$ticket = $this->ticketMapper->findForUser($id, $matrixUser);
$this->logger->debug("fetchChat found matrix data for room " . $ticket->getMatrixRoom());
return array(
'matrixRoom' => $ticket->getMatrixRoom(), // FIXME: wrong room, create one for helper and user
'matrixAccessToken' => $matrixUser->getMatrixToken(),
'matrixServerUrl' => $this->matrixService->getServerUrl(),
);
});
}
/**
* @NoAdminRequired
*/
public function create(string $title, string $content): DataResponse
{
return new DataResponse($this->ticketService->create($title, $content, $this->userId));
}
/**
* @NoAdminRequired
*/
public function update(int $id): DataResponse
{
return new DataResponse($this->ticketService->assign($id, $this->userId));
}
public function destroy(int $id): DataResponse
{
return $this->handleNotFound(function () use ($id) {
return $this->ticketService->delete($id, $this->userId);
});
}
}