Correctif Message

This commit is contained in:
noureddina 2024-12-20 11:37:55 +01:00
parent 6deb9b30cd
commit 1eb8418cff
3 changed files with 44 additions and 13 deletions

View File

@ -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,

View File

@ -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;
}
}

View File

@ -17,11 +17,17 @@
<!-- Liste des messages reçus -->
<div class="max-w-4xl mx-auto">
{% for message in messages %}
{% if message.receiver == app.user or message.sender == app.user %}
<div class="bg-white p-6 rounded-lg shadow mb-4">
<div class="flex justify-between items-center">
<div>
<h2 class="text-xl font-semibold">De : {{ message.sender.nickname }}</h2>
<h2 class="text-xl font-semibold">
De :
{% if message.sender == app.user %}
{{ message.receiver.nickname }}
{% else %}
{{ message.sender.nickname }}
{% endif %}
</h2>
<p class="text-gray-600">{{ message.sendingDate|date('d/m/Y H:i') }}</p>
</div>
<a href="{{ path('app_message_conversation', { id: message.id }) }}" class="bg-teal-500 text-white px-4 py-2 rounded hover:bg-teal-600">
@ -30,7 +36,6 @@
</div>
<p class="mt-4 text-gray-700">{{ message.content }}</p>
</div>
{% endif %}
{% else %}
<p class="text-center text-gray-500">Vous n'avez reçu aucun message.</p>
{% endfor %}