<?php
namespace App\Controller\API\React;
use App\Controller\API\Common\CommonChatController;
use App\Entity\Chat;
use App\Entity\ChatMessage;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\User;
use OpenApi\Annotations as OA;
final class ChatController extends CommonChatController
{
/**
* Получить список чатов пользователя.
*
* @Route("/api/react/user/chats", name="api_react_get_user_chats", methods={"GET"})
* @OA\Get( tags={"User Chats"} )
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Response(response="200", description="Chat list",
* @OA\JsonContent(
* type="array", @OA\Items(
* type="object",
* @OA\Property(property="uuid", type="string", example="hash-string"),
* @OA\Property(property="name", type="string", example="System notifications | Chat about order num 100"),
* @OA\Property(property="type", type="string", example="system|admin|user|vendor"),
* @OA\Property(property="avatar", type="string", example="admin.jpg"),
* @OA\Property(property="createdAt", type="string", example="2023-01-01"),
* @OA\Property(property="lastMessageDate", type="string", example="2023-01-01"),
* @OA\Property(property="lastMessageText", type="string", example="Some text"),
* @OA\Property(property="unreadMessages", type="integer", example="10"),
* @OA\Property(property="status", type="string", example="active | deleted"),
* )
* )
* )
*
* @return Response
*/
public function list(): Response
{
return parent::list();
}
/**
* Получить количество новых сообщений.
*
* @Route("/api/react/user/chats/unread_messages", name="api_react_get_user_chats_unread_messages", methods={"GET"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Get( tags={"User Chats"} )
* @OA\Response(response="200", description="Chat unread messages",
* @OA\JsonContent(
* @OA\Property(property="unread_messages", type="integer", example="10"),
* )
* )
* @return Response
*/
public function unreadMessages(): Response
{
return parent::unreadMessages();
}
/**
* Получить конкретный чат.
*
* @Route("/api/react/user/chat/{uuid}", name="api_react_get_user_chat", methods={"GET"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Get( tags={"User Chats"} )
* @OA\Response(response="200", description="Сообщения чата",
* @OA\JsonContent(
* @OA\Property(property="uuid", type="string", example="hash-string"),
* @OA\Property(property="name", type="string", example="System notifications | Chat about order num 100"),
* @OA\Property(property="status", type="string", example="active | deleted"),
* @OA\Property(property="isReadonly", type="boolean"),
* @OA\Property(property="created_at", type="string", example="2023-01-01"),
* @OA\Property(property="type", type="string", example="admin"),
* @OA\Property(property="messages", type="array", @OA\Items(
* type="object",
* @OA\Property(property="uuid", type="string", example="hash-string"),
* @OA\Property(property="status", type="string", example="sent | delivered | read"),
* @OA\Property(property="sender", type="string", example="user | admin | vendor"),
* @OA\Property(property="content", type="string", example="Chat message"),
* @OA\Property(property="attach", type="string", example="http://url-to-attach-file"),
* @OA\Property(property="created_at", type="string", example="2023-01-01"),
* ))
* )
* )
* @return Response
*/
public function view(Chat $chat): Response
{
return parent::view($chat);
}
/**
* Опубликовать сообщение в чат.
*
* @Route("/api/react/user/chat/{uuid}/message", name="api_react_user_chat_message", methods={"POST"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Post(
* tags={"User Chats"},
* @OA\RequestBody(
* @OA\MediaType(mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(property="message", type="string", example="Message text"),
* @OA\Property(property="attach", type="file"),
* )
* )
* )
* )
*
* @OA\Response(
* response=200,
* description="Сообщение отправлено",
* @OA\JsonContent(
* @OA\Property(property="status", type="string", example="success"),
* )
* )
* @OA\Response(
* response=400,
* description="Ошибка - неверные данные"
* )
* @return Response
*/
public function newMessage(Chat $chat,
Request $request): Response
{
return parent::newMessage($chat, $request);
}
/**
* @Route("/api/react/user/chat/message/{uuid}/attach/download", name="api_react_user_chat_message_attach_download", methods={"GET"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Get( tags={"Vendor Chats"} )
*
* @OA\Response(
* response=200,
* description="Attach body" )
*/
public function downloadAttach(ChatMessage $message): Response
{
try {
return parent::downloadAttach($message);
} catch (\Exception $e) {
return new JsonResponse(['message' => $e->getMessage()], 500);
}
}
/**
* Отредактировать сообщение чата.
*
* @Route("/api/react/user/chat_message/{uuid}", name="api_react_user_chat", methods={"PUT"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Put(
* tags={"User Chats"},
* @OA\RequestBody(
* @OA\MediaType(mediaType="application/json",
* @OA\Schema(
* @OA\Property(property="message", type="string", example="Message text"),
* @OA\Property(property="attach", type="string", example="Not implemented yet"),
* )
* )
* )
* )
*
* @OA\Response(
* response=200,
* description="Сообщение обновлено",
* @OA\JsonContent(
* @OA\Property(property="status", type="string", example="success"),
* )
* )
* @OA\Response(
* response=400,
* description="Ошибка - неверные данные"
* )
* @return Response
*/
public function update(ChatMessage $chatMessage,
Request $request): Response
{
/** @var User $user */
$user = $this->getUser();
if ($chatMessage->getChat()->getUser() !== $user) {
return new JsonResponse(['message' => 'wrong owner'], 400);
}
$data = json_decode($request->getContent(), false);
$message = isset($data->message) ? trim($data->message) : null;
// todo: attach
if (empty($message)) {
return new JsonResponse(['message' => 'Empty message'], 400);
}
$chatMessage->setContent($message);
$this->em->persist($chatMessage);
$this->em->flush();
return new JsonResponse([
'message' => 'message updated',
], 200);
}
/**
* Обновить статус сообщения в чате.
*
* @Route("/api/react/user/chat_message/{uuid}/status", name="api_react_user_chat_message_status", methods={"POST"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Post(
* tags={"User Chats"},
* @OA\RequestBody(
* @OA\MediaType(mediaType="application/json",
* @OA\Schema(
* @OA\Property(property="status", type="string", example="sent|delivered|read"),
* )
* )
* )
* )
*
* @OA\Response( response=200, description="Статус обновлён",
* @OA\JsonContent(
* @OA\Property(property="status", type="string", example="success"),
* )
* )
* @OA\Response( response=400, description="Ошибка - неверные данные" )
* @return Response
*/
public function messageStatus(ChatMessage $chatMessage,
Request $request): Response
{
return parent::messageStatus($chatMessage, $request);
}
/**
* Обновить статус всех сообщений в чате пользователя.
*
* @Route("/api/react/user/chat/{uuid}/messsges/status", name="api_react_user_chat_all_messages_status", methods={"POST"})
* @OA\Parameter(name="Authorization", in="header", required=true, description="JWT access-token")
* @OA\Post(
* tags={"User Chats"},
* @OA\RequestBody(
* @OA\MediaType(mediaType="application/json",
* @OA\Schema(
* @OA\Property(property="status", type="string", example="delivered|read"),
* )
* )
* )
* )
*
* @OA\Response( response=200, description="Статус обновлён",
* @OA\JsonContent(
* @OA\Property(property="status", type="string", example="success"),
* )
* )
* @OA\Response( response=400, description="Ошибка - неверные данные" )
* @return Response
*/
public function chatMessagesStatus(Chat $chat,
Request $request): Response
{
return parent::chatMessagesStatus($chat, $request);
}
}