diff --git a/src/Controller/ChatController.php b/src/Controller/ChatController.php index edcd6f8e..142ea564 100644 --- a/src/Controller/ChatController.php +++ b/src/Controller/ChatController.php @@ -11,9 +11,8 @@ use App\Filesystem\ChatFilesystem; use App\Filesystem\FileNotFoundException; use App\Form\ChatMessageType; -use App\Form\ChatUserRecipientType; -use App\Form\Model\NewChat; use App\Form\ChatType; +use App\Form\ChatUserRecipientType; use App\Repository\ChatMessageAttachmentRepositoryInterface; use App\Repository\ChatMessageRepositoryInterface; use App\Repository\ChatRepositoryInterface; @@ -22,14 +21,11 @@ use App\Security\Voter\ChatMessageVoter; use App\Security\Voter\ChatVoter; use App\Settings\ChatSettings; -use App\Sorting\ChatStrategy; -use App\Sorting\SortDirection; use App\Sorting\Sorter; use SchulIT\CommonBundle\Form\ConfirmType; use SchulIT\CommonBundle\Utils\RefererHelper; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Security\Http\Attribute\IsGranted; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; @@ -43,7 +39,7 @@ public function __construct(RefererHelper private readonly ChatRepositoryInterface $chatRepository, private readonly ChatMessageRepositoryInterface $chatMessageRepository, private readonly ChatMessageAttachmentRepositoryInterface $attachmentRepository, - private readonly ChatSettings $chatSettings) { + private readonly ChatSettings $chatSettings) { parent::__construct($redirectHelper); } @@ -63,9 +59,11 @@ public function index(Sorter $sorter): Response { $unreadCount[$chat->getId()] = $this->chatMessageRepository->countUnreadMessages($user, $chat); } + $lastMessageDates = [ ]; $messagesCount = [ ]; foreach($chats as $chat) { - $messagesCount[$chat->getId()] = 0 /*$chat->getMessages()->count()*/; + $messagesCount[$chat->getId()] = $this->chatMessageRepository->countByChat($chat); + $lastMessageDates[$chat->getId()] = $this->chatMessageRepository->findLastMessageDate($chat); } $attachmentsCount = [ ]; @@ -77,6 +75,7 @@ public function index(Sorter $sorter): Response { 'chats' => $chats, 'messagesCount' => $messagesCount, 'unreadCount' => $unreadCount, + 'lastMessageDates' => $lastMessageDates, 'attachmentsCount' => $attachmentsCount ]); } diff --git a/src/Repository/ChatMessageRepository.php b/src/Repository/ChatMessageRepository.php index 58a5ac86..456b596b 100644 --- a/src/Repository/ChatMessageRepository.php +++ b/src/Repository/ChatMessageRepository.php @@ -5,6 +5,7 @@ use App\Entity\Chat; use App\Entity\ChatMessage; use App\Entity\User; +use DateTime; use Doctrine\DBAL\Exception as DbalException; use PHPUnit\Exception; @@ -28,6 +29,48 @@ public function findByChatAndRange(Chat $chat, int $numberOfMessages, ?ChatMessa return $qb->getQuery()->getResult(); } + public function findLastMessageDate(Chat $chat): ?DateTime { + $dateString = $this->em->createQueryBuilder() + ->select('m.updatedAt') + ->from(ChatMessage::class, 'm') + ->where('m.chat = :chat') + ->setParameter('chat', $chat->getId()) + ->setMaxResults(1) + ->orderBy('m.createdAt', 'DESC') + ->getQuery() + ->getSingleScalarResult(); + + if($dateString !== null) { + return new DateTime($dateString); + } + + $dateString = $this->em->createQueryBuilder() + ->select('m.createdAt') + ->from(ChatMessage::class, 'm') + ->where('m.chat = :chat') + ->setParameter('chat', $chat->getId()) + ->setMaxResults(1) + ->orderBy('m.createdAt', 'DESC') + ->getQuery() + ->getSingleScalarResult(); + + if($dateString !== null) { + return new DateTime($dateString); + } + + return null; + } + + public function countByChat(Chat $chat): int { + return $this->em->createQueryBuilder() + ->select('COUNT(m.id)') + ->from(ChatMessage::class, 'm') + ->where('m.chat = :chat') + ->setParameter('chat', $chat->getId()) + ->getQuery() + ->getSingleScalarResult(); + } + public function countUnreadMessages(User $user, Chat|null $chat = null): int { $qbOwnMessages = $this->em->createQueryBuilder() ->select('mInner.id') diff --git a/src/Repository/ChatMessageRepositoryInterface.php b/src/Repository/ChatMessageRepositoryInterface.php index 33a37d04..c70317a3 100644 --- a/src/Repository/ChatMessageRepositoryInterface.php +++ b/src/Repository/ChatMessageRepositoryInterface.php @@ -5,6 +5,7 @@ use App\Entity\Chat; use App\Entity\ChatMessage; use App\Entity\User; +use DateTime; interface ChatMessageRepositoryInterface { @@ -18,6 +19,10 @@ public function findByChatAndRange(Chat $chat, int $numberOfMessages, ChatMessag public function countUnreadMessages(User $user, Chat|null $chat = null): int; + public function countByChat(Chat $chat): int; + + public function findLastMessageDate(Chat $chat): ?DateTime; + public function persist(ChatMessage $message): void; public function remove(ChatMessage $message): void; diff --git a/templates/chat/index.html.twig b/templates/chat/index.html.twig index 0b0637b0..2f6a423b 100644 --- a/templates/chat/index.html.twig +++ b/templates/chat/index.html.twig @@ -37,6 +37,13 @@ {% endif %} + {% set lastMessageDate = lastMessageDates[chat.id] %} + {% if lastMessageDate is not null %} + + {{ lastMessageDate|format_datetime }} + + {% endif %} + {{ messagesCount[chat.id] }}