<?php
namespace App\Controller;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\Routing\Annotation\Route;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\Annotations\Post;
use App\Entity\Chat;
use App\Entity\Message;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\User;
use App\Form\MessageType;
use Symfony\Component\HttpKernel\Exception\HttpException;
class ApiMessageController extends AbstractFOSRestController
{
/**
* @View(serializerGroups={"default", "public", "message", "chat"})
* @Route("/api/chats", name="api_chats")
*/
public function getCurrentChats(Request $request) {
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$user = $this->getUser();
if (!$user->hasAValidSubscription()) {
throw new HttpException(402, 'You need to pay to access to this feature.');
}
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository(Chat::class);
$start = 0;
$limit = 10;
if ($request->query->has('page') && is_numeric($request->query->get('page')))
{
$start = $request->query->get('page');
}
$chats = $repo->findChats($user->getId(), $start, $limit);
$count = $repo->getChatsNumber($user->getId());
return array ('items' => $chats, 'total' => $count);
}
/**
* @View(serializerGroups={"default", "public", "message"})
* @Post()
* @Route("/api/message/send/{friend}", name="api_message_send")
*/
public function sendMessage(Request $request, User $friend)
{
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$user = $this->getUser();
if (!$user->hasAValidSubscription()) {
throw new HttpException(402, 'You need to pay to access to this feature.');
}
if ($friend->getId() === $user->getId()) {
throw new HttpException(403,'You cannot chat with yourself!');
}
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository(Chat::class);
$message = new Message();
$form = $this->createForm(MessageType::class, $message);
$form->submit(($request->request->get($form->getName())));
if ($form->isValid()) {
$chat = $repo->findExistingChat($user->getId(), $friend->getId());
if ($chat === null) {
$chat = new Chat();
$chat->setBlock(false);
$chat->setUserInitChat($user);
$chat->setUserRespondChat($friend);
$chat->setDateFirstMessage(new \DateTime('now'));
$em->persist($chat);
}
$message->setDateCreate(new \DateTime('now'));
$message->setRedactor($user);
$message->setChat($chat);
$message->setModerated(false);
$chat->addMessage($message);
if ($chat->getUserInitChat() == $user) {
$chat->setLastMessageInitiator($message);
} else {
$chat->setLastMessageResponder($message);
}
$em->persist($message);
$em->persist($chat);
$em->flush();
return ['ok'=>true, 'message' => $message];
}
throw new HttpException(403,'Invalid message!');
}
/**
* @View(serializerGroups={"default", "public", "chat"})
* @Route("/api/messages/{friend}", name="api_messages")
*/
public function getMessages(Request $request, User $friend)
{
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$user = $this->getUser();
if (!$user->hasAValidSubscription()) {
throw new HttpException(402, 'You need to pay to access to this feature.');
}
if ($friend->getId() === $user->getId()) {
throw new HttpException(403,'You cannot chat with yourself!');
}
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository(Chat::class);
$chat = $repo->findExistingChat($user->getId(), $friend->getId());
if ($chat === null) {
return array('items' => [], 'total' => 0, 'chat' => null);
}
$repom = $em->getRepository(Message::class);
$start = 0;
$limit = 10;
if ($request->query->has('page') && is_numeric($request->query->get('page')))
{
$start = $request->query->get('page');
}
$messages = $repom->findBy(array('chat' => $chat->getId()), array('date_create' => 'desc'), $limit, $start);
$count = $repom->getMessageNumber($chat->getId());
$maxdate = $chat->getUserInitChat()->getId() === $user->getId() ? $chat->getDateLastReadInitiator() : $chat->getDateLastReadResponder();
for($i = 0; $i < count($messages); $i++) {
if ($maxdate === null || $maxdate <= $messages[$i]->getDateCreate()) {
$maxdate = $messages[$i]->getDateCreate();
}
}
if ($chat->getUserInitChat()->getId() === $user->getId()) {
$chat->setDateLastReadInitiator($maxdate);
} else {
$chat->setDateLastReadResponder($maxdate);
}
$em->persist($chat);
$em->flush();
return array('items' => $messages, 'total' => (($count/$limit) + 1), 'chat' => $chat);
}
/**
* @View(serializerGroups={"default", "public", "chat"})
* @Route("/api/message/writing/{friend}", name="api_messages_writing")
*/
public function isCurrentlyWriting (Request $request, User $friend) {
$this->denyAccessUnlessGranted('ROLE_USER', null, 'Unable to access this page!');
$user = $this->getUser();
if (!$user->hasAValidSubscription()) {
throw new HttpException(402, 'You need to pay to access to this feature.');
}
if ($friend->getId() === $user->getId()) {
throw new HttpException(403,'You cannot chat with yourself!');
}
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository(Chat::class);
$chat = $repo->findExistingChat($user->getId(), $friend->getId());
if ($chat === null) {
return array('ok' => true);
}
if ($chat->getUserInitChat()->getId() === $user->getId()) {
$chat->setDateLastWritingInitiator(new \DateTime('now'));
} else {
$chat->setDateLastWritingResponder(new \DateTime('now'));
}
$em->persist($chat);
$em->flush();
return array('ok' => true);
}
}