src/Controller/API/React/ChatController.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller\API\React;
  3. use App\Controller\API\Common\CommonChatController;
  4. use App\Entity\Chat;
  5. use App\Entity\ChatMessage;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use App\Entity\User;
  11. use OpenApi\Annotations as OA;
  12. final class ChatController extends CommonChatController
  13. {
  14.     /**
  15.      * Получить список чатов пользователя.
  16.      *
  17.      * @Route("/api/react/user/chats", name="api_react_get_user_chats",  methods={"GET"})
  18.      * @OA\Get( tags={"User Chats"} )
  19.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  20.      * @OA\Response(response="200", description="Chat list",
  21.      *     @OA\JsonContent(
  22.      *         type="array", @OA\Items(
  23.      *             type="object",
  24.      *             @OA\Property(property="uuid", type="string", example="hash-string"),
  25.      *             @OA\Property(property="name", type="string", example="System notifications | Chat about order num 100"),
  26.      *             @OA\Property(property="type", type="string", example="system|admin|user|vendor"),
  27.      *             @OA\Property(property="avatar", type="string", example="admin.jpg"),
  28.      *             @OA\Property(property="createdAt", type="string", example="2023-01-01"),
  29.      *             @OA\Property(property="lastMessageDate", type="string", example="2023-01-01"),
  30.      *             @OA\Property(property="lastMessageText", type="string", example="Some text"),
  31.      *             @OA\Property(property="unreadMessages", type="integer", example="10"),
  32.      *             @OA\Property(property="status", type="string", example="active | deleted"),
  33.      *         )
  34.      *     )
  35.      * )
  36.      *
  37.      * @return Response
  38.      */
  39.     public function list(): Response
  40.     {
  41.         return parent::list();
  42.     }
  43.     /**
  44.      * Получить количество новых сообщений.
  45.      *
  46.      * @Route("/api/react/user/chats/unread_messages", name="api_react_get_user_chats_unread_messages", methods={"GET"})
  47.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  48.      * @OA\Get( tags={"User Chats"} )
  49.      * @OA\Response(response="200", description="Chat unread messages",
  50.      *     @OA\JsonContent(
  51.  *             @OA\Property(property="unread_messages", type="integer", example="10"),
  52.      *     )
  53.      * )
  54.      * @return Response
  55.      */
  56.     public function unreadMessages(): Response
  57.     {
  58.         return parent::unreadMessages();
  59.     }
  60.     /**
  61.      * Получить конкретный чат.
  62.      *
  63.      * @Route("/api/react/user/chat/{uuid}", name="api_react_get_user_chat",  methods={"GET"})
  64.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  65.      * @OA\Get( tags={"User Chats"} )
  66.      * @OA\Response(response="200", description="Сообщения чата",
  67.      *     @OA\JsonContent(
  68.      *            @OA\Property(property="uuid", type="string", example="hash-string"),
  69.      *            @OA\Property(property="name", type="string", example="System notifications | Chat about order num 100"),
  70.      *            @OA\Property(property="status", type="string", example="active | deleted"),
  71.      *            @OA\Property(property="isReadonly", type="boolean"),
  72.      *            @OA\Property(property="created_at", type="string", example="2023-01-01"),
  73.      *            @OA\Property(property="type", type="string", example="admin"),
  74.      *         @OA\Property(property="messages", type="array", @OA\Items(
  75.      *             type="object",
  76.      *             @OA\Property(property="uuid", type="string", example="hash-string"),
  77.      *             @OA\Property(property="status", type="string", example="sent | delivered | read"),
  78.      *             @OA\Property(property="sender", type="string", example="user | admin | vendor"),
  79.      *             @OA\Property(property="content", type="string", example="Chat message"),
  80.      *             @OA\Property(property="attach", type="string", example="http://url-to-attach-file"),
  81.      *             @OA\Property(property="created_at", type="string", example="2023-01-01"),
  82.      *         ))
  83.      *    )
  84.      * )
  85.      * @return Response
  86.      */
  87.     public function view(Chat $chat): Response
  88.     {
  89.         return parent::view($chat);
  90.     }
  91.     /**
  92.      * Опубликовать сообщение в чат.
  93.      *
  94.      * @Route("/api/react/user/chat/{uuid}/message", name="api_react_user_chat_message", methods={"POST"})
  95.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  96.      * @OA\Post(
  97.      *     tags={"User Chats"},
  98.      *     @OA\RequestBody(
  99.      *         @OA\MediaType(mediaType="multipart/form-data",
  100.      *           @OA\Schema(
  101.      *            @OA\Property(property="message", type="string", example="Message text"),
  102.      *            @OA\Property(property="attach", type="file"),
  103.      *           )
  104.      *         )
  105.      *     )
  106.      * )
  107.      *
  108.      * @OA\Response(
  109.      *     response=200,
  110.      *     description="Сообщение отправлено",
  111.      *    @OA\JsonContent(
  112.      *        @OA\Property(property="status", type="string", example="success"),
  113.      *    )
  114.      * )
  115.      * @OA\Response(
  116.      *     response=400,
  117.      *     description="Ошибка - неверные данные"
  118.      * )
  119.      * @return Response
  120.      */
  121.     public function newMessage(Chat $chat,
  122.                                Request $request): Response
  123.     {
  124.         return parent::newMessage($chat$request);
  125.     }
  126.     /**
  127.      * @Route("/api/react/user/chat/message/{uuid}/attach/download", name="api_react_user_chat_message_attach_download", methods={"GET"})
  128.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  129.      * @OA\Get( tags={"Vendor Chats"} )
  130.      *
  131.      * @OA\Response(
  132.      *     response=200,
  133.      *     description="Attach body" )
  134.      */
  135.     public function downloadAttach(ChatMessage $message): Response
  136.     {
  137.         try {
  138.             return parent::downloadAttach($message);
  139.         } catch (\Exception $e) {
  140.             return new JsonResponse(['message' => $e->getMessage()], 500);
  141.         }
  142.     }
  143.     /**
  144.      * Отредактировать сообщение чата.
  145.      *
  146.      * @Route("/api/react/user/chat_message/{uuid}", name="api_react_user_chat", methods={"PUT"})
  147.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  148.      * @OA\Put(
  149.      *     tags={"User Chats"},
  150.      *     @OA\RequestBody(
  151.      *         @OA\MediaType(mediaType="application/json",
  152.      *           @OA\Schema(
  153.      *            @OA\Property(property="message", type="string", example="Message text"),
  154.      *            @OA\Property(property="attach", type="string", example="Not implemented yet"),
  155.      *           )
  156.      *         )
  157.      *     )
  158.      * )
  159.      *
  160.      * @OA\Response(
  161.      *     response=200,
  162.      *     description="Сообщение обновлено",
  163.      *    @OA\JsonContent(
  164.      *        @OA\Property(property="status", type="string", example="success"),
  165.      *    )
  166.      * )
  167.      * @OA\Response(
  168.      *     response=400,
  169.      *     description="Ошибка - неверные данные"
  170.      * )
  171.      * @return Response
  172.      */
  173.     public function update(ChatMessage $chatMessage,
  174.                            Request $request): Response
  175.     {
  176.         /** @var User $user */
  177.         $user $this->getUser();
  178.         if ($chatMessage->getChat()->getUser() !== $user) {
  179.             return new JsonResponse(['message' => 'wrong owner'], 400);
  180.         }
  181.         $data json_decode($request->getContent(), false);
  182.         $message = isset($data->message) ? trim($data->message) : null;
  183.         // todo: attach
  184.         if (empty($message)) {
  185.             return new JsonResponse(['message' => 'Empty message'], 400);
  186.         }
  187.         $chatMessage->setContent($message);
  188.         $this->em->persist($chatMessage);
  189.         $this->em->flush();
  190.         return new JsonResponse([
  191.             'message' => 'message updated',
  192.         ], 200);
  193.     }
  194.     /**
  195.      * Обновить статус сообщения в чате.
  196.      *
  197.      * @Route("/api/react/user/chat_message/{uuid}/status", name="api_react_user_chat_message_status", methods={"POST"})
  198.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  199.      * @OA\Post(
  200.      *     tags={"User Chats"},
  201.      *     @OA\RequestBody(
  202.      *         @OA\MediaType(mediaType="application/json",
  203.      *           @OA\Schema(
  204.      *            @OA\Property(property="status", type="string", example="sent|delivered|read"),
  205.      *           )
  206.      *         )
  207.      *     )
  208.      * )
  209.      *
  210.      * @OA\Response( response=200, description="Статус обновлён",
  211.      *    @OA\JsonContent(
  212.      *        @OA\Property(property="status", type="string", example="success"),
  213.      *    )
  214.      * )
  215.      * @OA\Response( response=400, description="Ошибка - неверные данные" )
  216.      * @return Response
  217.      */
  218.     public function messageStatus(ChatMessage $chatMessage,
  219.                                   Request $request): Response
  220.     {
  221.         return parent::messageStatus($chatMessage$request);
  222.     }
  223.     /**
  224.      * Обновить статус всех сообщений в чате пользователя.
  225.      *
  226.      * @Route("/api/react/user/chat/{uuid}/messsges/status", name="api_react_user_chat_all_messages_status", methods={"POST"})
  227.      * @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
  228.      * @OA\Post(
  229.      *     tags={"User Chats"},
  230.      *     @OA\RequestBody(
  231.      *         @OA\MediaType(mediaType="application/json",
  232.      *             @OA\Schema(
  233.      *                 @OA\Property(property="status", type="string", example="delivered|read"),
  234.      *             )
  235.      *         )
  236.      *     )
  237.      * )
  238.      *
  239.      * @OA\Response( response=200, description="Статус обновлён",
  240.      *    @OA\JsonContent(
  241.      *        @OA\Property(property="status", type="string", example="success"),
  242.      *    )
  243.      * )
  244.      * @OA\Response( response=400, description="Ошибка - неверные данные" )
  245.      * @return Response
  246.      */
  247.     public function chatMessagesStatus(Chat $chat,
  248.                                        Request $request): Response
  249.     {
  250.         return parent::chatMessagesStatus($chat$request);
  251.     }
  252. }