src/Controller/ApiMessageController.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use FOS\RestBundle\Controller\AbstractFOSRestController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use FOS\RestBundle\Controller\Annotations\View;
  6. use FOS\RestBundle\Controller\Annotations\Post;
  7. use App\Entity\Chat;
  8. use App\Entity\Message;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use App\Entity\User;
  11. use App\Form\MessageType;
  12. use Symfony\Component\HttpKernel\Exception\HttpException;
  13. class ApiMessageController extends AbstractFOSRestController
  14. {
  15.     /**
  16.      * @View(serializerGroups={"default", "public", "message", "chat"})
  17.      * @Route("/api/chats", name="api_chats")
  18.      */
  19.     public function getCurrentChats(Request $request) {
  20.         $this->denyAccessUnlessGranted('ROLE_USER'null'Unable to access this page!');
  21.         $user $this->getUser();
  22.         if (!$user->hasAValidSubscription()) {
  23.             throw new HttpException(402'You need to pay to access to this feature.');
  24.         }
  25.         $em $this->getDoctrine()->getManager();
  26.         $repo $em->getRepository(Chat::class);
  27.         $start 0;
  28.         $limit 10;
  29.         if ($request->query->has('page') && is_numeric($request->query->get('page')))
  30.         {
  31.             $start $request->query->get('page');
  32.         }
  33.         $chats $repo->findChats($user->getId(), $start$limit);
  34.         $count $repo->getChatsNumber($user->getId());
  35.         return array ('items' => $chats'total' => $count);
  36.     }
  37.     /**
  38.      * @View(serializerGroups={"default", "public", "message"})
  39.      * @Post()
  40.      * @Route("/api/message/send/{friend}", name="api_message_send")
  41.      */
  42.     public function sendMessage(Request $requestUser $friend)
  43.     {
  44.         $this->denyAccessUnlessGranted('ROLE_USER'null'Unable to access this page!');
  45.         $user $this->getUser();
  46.         if (!$user->hasAValidSubscription()) {
  47.             throw new HttpException(402'You need to pay to access to this feature.');
  48.         }
  49.         if ($friend->getId() === $user->getId()) {
  50.             throw new HttpException(403,'You cannot chat with yourself!');
  51.         }
  52.         $em $this->getDoctrine()->getManager();
  53.         $repo $em->getRepository(Chat::class);
  54.         $message = new Message();
  55.         $form $this->createForm(MessageType::class, $message);
  56.         $form->submit(($request->request->get($form->getName())));
  57.         if ($form->isValid()) {
  58.             $chat $repo->findExistingChat($user->getId(), $friend->getId());
  59.             if ($chat === null) {
  60.                 $chat = new Chat();
  61.                 $chat->setBlock(false);
  62.                 $chat->setUserInitChat($user);
  63.                 $chat->setUserRespondChat($friend);
  64.                 $chat->setDateFirstMessage(new \DateTime('now'));
  65.                 $em->persist($chat);
  66.             }
  67.             $message->setDateCreate(new \DateTime('now'));
  68.             $message->setRedactor($user);
  69.             $message->setChat($chat);
  70.             $message->setModerated(false);
  71.             $chat->addMessage($message);
  72.             if ($chat->getUserInitChat() == $user) {
  73.                 $chat->setLastMessageInitiator($message);
  74.             } else {
  75.                 $chat->setLastMessageResponder($message);
  76.             }
  77.             $em->persist($message);
  78.             $em->persist($chat);
  79.             $em->flush();
  80.             return ['ok'=>true'message' => $message];
  81.         }
  82.         throw new HttpException(403,'Invalid message!');
  83.     }
  84.     /**
  85.      * @View(serializerGroups={"default", "public", "chat"})
  86.      * @Route("/api/messages/{friend}", name="api_messages")
  87.      */
  88.     public function getMessages(Request $requestUser $friend)
  89.     {
  90.         $this->denyAccessUnlessGranted('ROLE_USER'null'Unable to access this page!');
  91.         $user $this->getUser();
  92.         if (!$user->hasAValidSubscription()) {
  93.             throw new HttpException(402'You need to pay to access to this feature.');
  94.         }
  95.         if ($friend->getId() === $user->getId()) {
  96.             throw new HttpException(403,'You cannot chat with yourself!');
  97.         }
  98.         $em $this->getDoctrine()->getManager();
  99.         $repo $em->getRepository(Chat::class);
  100.         $chat $repo->findExistingChat($user->getId(), $friend->getId());
  101.         if ($chat === null) {
  102.             return array('items' => [], 'total' => 0'chat' => null);
  103.         }
  104.         $repom $em->getRepository(Message::class);
  105.         $start 0;
  106.         $limit 10;
  107.         if ($request->query->has('page') && is_numeric($request->query->get('page')))
  108.         {
  109.             $start $request->query->get('page');
  110.         }
  111.         $messages $repom->findBy(array('chat' => $chat->getId()), array('date_create' => 'desc'), $limit$start);
  112.         $count $repom->getMessageNumber($chat->getId());
  113.         $maxdate $chat->getUserInitChat()->getId() === $user->getId() ? $chat->getDateLastReadInitiator() : $chat->getDateLastReadResponder();
  114.         for($i 0$i count($messages); $i++) {
  115.             if ($maxdate === null || $maxdate <= $messages[$i]->getDateCreate()) {
  116.                 $maxdate $messages[$i]->getDateCreate();
  117.             }
  118.         }
  119.         if ($chat->getUserInitChat()->getId() === $user->getId()) {
  120.             $chat->setDateLastReadInitiator($maxdate);
  121.         } else {
  122.             $chat->setDateLastReadResponder($maxdate);
  123.         }
  124.         $em->persist($chat);
  125.         $em->flush();
  126.         return array('items' => $messages'total' => (($count/$limit) + 1), 'chat' => $chat);
  127.     }
  128.     /**
  129.      * @View(serializerGroups={"default", "public", "chat"})
  130.      * @Route("/api/message/writing/{friend}", name="api_messages_writing")
  131.      */
  132.     public function isCurrentlyWriting (Request $requestUser $friend) {
  133.         $this->denyAccessUnlessGranted('ROLE_USER'null'Unable to access this page!');
  134.         $user $this->getUser();
  135.         if (!$user->hasAValidSubscription()) {
  136.             throw new HttpException(402'You need to pay to access to this feature.');
  137.         }
  138.         if ($friend->getId() === $user->getId()) {
  139.             throw new HttpException(403,'You cannot chat with yourself!');
  140.         }
  141.         $em $this->getDoctrine()->getManager();
  142.         $repo $em->getRepository(Chat::class);
  143.         $chat $repo->findExistingChat($user->getId(), $friend->getId());
  144.         if ($chat === null) {
  145.             return array('ok' => true);
  146.         }
  147.         if ($chat->getUserInitChat()->getId() === $user->getId()) {
  148.             $chat->setDateLastWritingInitiator(new \DateTime('now'));
  149.         } else {
  150.             $chat->setDateLastWritingResponder(new \DateTime('now'));
  151.         }
  152.         $em->persist($chat);
  153.         $em->flush();
  154.         return array('ok' => true);
  155.     }
  156. }