diff --git a/src/Controller/MessageController.php b/src/Controller/MessageController.php index af9633a..cca62c2 100644 --- a/src/Controller/MessageController.php +++ b/src/Controller/MessageController.php @@ -20,7 +20,7 @@ final class MessageController extends AbstractController public function index(MessageRepository $messageRepository): Response { $user = $this->getUser(); - $messages = $messageRepository->findByUser($user); + $messages = $messageRepository->findLatestMessagesByUser($user); return $this->render('message/index.html.twig', [ 'messages' => $messages, diff --git a/src/Repository/MessageRepository.php b/src/Repository/MessageRepository.php index fd3e6b3..19bf4a6 100644 --- a/src/Repository/MessageRepository.php +++ b/src/Repository/MessageRepository.php @@ -37,4 +37,30 @@ class MessageRepository extends ServiceEntityRepository ->getQuery() ->getResult(); } + + public function findLatestMessagesByUser(UserApp $user): array + { + $query = $this->createQueryBuilder('m') + ->where('m.sender = :user OR m.receiver = :user') + ->setParameter('user', $user) + ->orderBy('m.sendingDate', 'DESC') + ->getQuery() + ->getResult(); + + $lastMessages = []; + foreach ($query as $message) { + // Identifier l'autre utilisateur dans la conversation + $otherUser = $message->getSender() === $user ? $message->getReceiver() : $message->getSender(); + $key = $otherUser->getId(); + + // Conserver uniquement le dernier message avec cet utilisateur + if (!isset($lastMessages[$key])) { + $lastMessages[$key] = $message; + } + } + + return $lastMessages; + } + + } diff --git a/templates/message/index.html.twig b/templates/message/index.html.twig index 4c7da31..433924d 100644 --- a/templates/message/index.html.twig +++ b/templates/message/index.html.twig @@ -17,20 +17,25 @@
{{ message.sendingDate|date('d/m/Y H:i') }}
-{{ message.sendingDate|date('d/m/Y H:i') }}
{{ message.content }}
+ + Voir la Conversation +{{ message.content }}
+Vous n'avez reçu aucun message.
{% endfor %}