127 lines
3.1 KiB
PHP
127 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace OCA\UPschooling\Controller;
|
|
|
|
use OCA\UPschooling\AppInfo\Application;
|
|
use OCA\UPschooling\Db\TicketMapper;
|
|
use OCA\UPschooling\Exceptions\RoomNotFoundException;
|
|
use OCA\UPschooling\Service\ChatService;
|
|
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;
|
|
|
|
class TicketApiController extends ApiController
|
|
{
|
|
|
|
/** @var LoggerInterface */
|
|
private $logger;
|
|
|
|
/** @var TicketService */
|
|
private $ticketService;
|
|
|
|
/** @var MatrixService */
|
|
private $matrixService;
|
|
|
|
/** @var ChatService */
|
|
private $chatService;
|
|
|
|
/** @var TicketMapper */
|
|
private $ticketMapper;
|
|
|
|
private ?string $userId;
|
|
|
|
use Errors;
|
|
|
|
public function __construct(
|
|
IRequest $request,
|
|
LoggerInterface $logger,
|
|
TicketService $ticketService,
|
|
TicketMapper $ticketMapper,
|
|
MatrixService $matrixService,
|
|
ChatService $chatService,
|
|
?string $userId
|
|
) {
|
|
parent::__construct(Application::APP_ID, $request);
|
|
$this->logger = $logger;
|
|
$this->ticketService = $ticketService;
|
|
$this->ticketMapper = $ticketMapper;
|
|
$this->matrixService = $matrixService;
|
|
$this->chatService = $chatService;
|
|
$this->userId = $userId;
|
|
}
|
|
|
|
/**
|
|
* @CORS
|
|
* @NoCSRFRequired
|
|
* @NoAdminRequired
|
|
*/
|
|
public function index(): DataResponse
|
|
{
|
|
return new DataResponse($this->ticketService->findAll($this->userId));
|
|
}
|
|
|
|
/**
|
|
* @CORS
|
|
* @NoCSRFRequired
|
|
* @NoAdminRequired
|
|
*/
|
|
public function show(int $id): DataResponse
|
|
{
|
|
return $this->handleNotFound(function () use ($id) {
|
|
return $this->ticketService->find($id, $this->userId);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @CORS
|
|
* @NoCSRFRequired
|
|
* @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 ticket " . print_r($ticket->jsonSerialize(), true));
|
|
// returns 404 RoomNotFoundException if no room could/should be created
|
|
$chatRoom = $this->chatService->getOrCreateChatRoom($ticket);
|
|
$this->logger->debug("fetchChat got chat room " . print_r($chatRoom->jsonSerialize(), true));
|
|
return array(
|
|
'matrixChatRoom' => $chatRoom->getMatrixRoomId(),
|
|
'matrixAccessToken' => $matrixUser->getMatrixToken(),
|
|
'matrixServerUrl' => $this->matrixService->getServerUrl(),
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @CORS
|
|
* @NoCSRFRequired
|
|
* @NoAdminRequired
|
|
*/
|
|
public function create(string $title, string $content): DataResponse
|
|
{
|
|
return new DataResponse($this->ticketService->create($title, $content, $this->userId));
|
|
}
|
|
|
|
/**
|
|
* @CORS
|
|
* @NoCSRFRequired
|
|
* @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);
|
|
});
|
|
}
|
|
}
|