63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace OCA\UPschooling\Service;
|
|
|
|
use Aryess\PhpMatrixSdk\Room;
|
|
use OCA\UPschooling\Db\ChatMapper;
|
|
use OCA\UPschooling\Db\MatrixChat;
|
|
use OCA\UPschooling\Db\MatrixTicket;
|
|
use OCA\UPschooling\Db\TicketMapper;
|
|
use OCA\UPschooling\Exceptions\RoomNotFoundException;
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class ChatService
|
|
{
|
|
|
|
/** @var ChatMapper */
|
|
private $chatMapper;
|
|
|
|
/** @var MatrixService */
|
|
private $matrixService;
|
|
|
|
/** @var LoggerInterface */
|
|
private $logger;
|
|
|
|
public function __construct(ChatMapper $chatMapper, MatrixService $matrixService, LoggerInterface $logger)
|
|
{
|
|
$this->chatMapper = $chatMapper;
|
|
$this->matrixService = $matrixService;
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
/**
|
|
* @param $ticket MatrixTicket Support ticket
|
|
* @throws RoomNotFoundException
|
|
*/
|
|
public function getOrCreateChatRoom(MatrixTicket $ticket): MatrixChat
|
|
{
|
|
try {
|
|
return $this->chatMapper->findCurrent($ticket->getId());
|
|
} catch (DoesNotExistException $e) {
|
|
if ($ticket->getMatrixHelperUser() == null) {
|
|
$this->logger->debug("No helper assigned to ticket " . $ticket->getId() . ". Not creating chat room");
|
|
throw new RoomNotFoundException();
|
|
}
|
|
$this->logger->debug("Chat room for ticket " . $ticket->getId() . " does not exist. Creating room..");
|
|
$roomId = $this->matrixService->createRoom();
|
|
$startDate = new \DateTime('now', new \DateTimeZone("utc"));
|
|
$matrixChat = new MatrixChat();
|
|
$matrixChat->setTicketId($ticket->getId());
|
|
$matrixChat->setMatrixRoomId($roomId);
|
|
$matrixChat->setMatrixHelperUser($ticket->getMatrixHelperUser());
|
|
$matrixChat->setDateStart($this->chatMapper->convertDateTimeTz($startDate));
|
|
$matrixChat->setVersion(1);
|
|
|
|
$this->matrixService->inviteUser($roomId, $ticket->getMatrixHelperUser());
|
|
$this->matrixService->inviteUser($roomId, $ticket->getMatrixAssistedUser());
|
|
|
|
return $this->chatMapper->insert($matrixChat);
|
|
}
|
|
}
|
|
}
|