Skip to content

Commit

Permalink
Chat improvements (show message count + show last message date) (rele…
Browse files Browse the repository at this point in the history
…ated to #464)
  • Loading branch information
frostieDE committed Oct 3, 2024
1 parent 76af073 commit 8509102
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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 = [ ];
Expand All @@ -77,6 +75,7 @@ public function index(Sorter $sorter): Response {
'chats' => $chats,
'messagesCount' => $messagesCount,
'unreadCount' => $unreadCount,
'lastMessageDates' => $lastMessageDates,
'attachmentsCount' => $attachmentsCount
]);
}
Expand Down
43 changes: 43 additions & 0 deletions src/Repository/ChatMessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions src/Repository/ChatMessageRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Entity\Chat;
use App\Entity\ChatMessage;
use App\Entity\User;
use DateTime;

interface ChatMessageRepositoryInterface {

Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions templates/chat/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
{% endif %}
</span>

{% set lastMessageDate = lastMessageDates[chat.id] %}
{% if lastMessageDate is not null %}
<span class="badge text-bg-secondary ms-2">
<i class="fas fa-clock"></i> {{ lastMessageDate|format_datetime }}
</span>
{% endif %}

<span class="badge text-bg-secondary ms-2">
<i class="fa-regular fa-comments"></i> {{ messagesCount[chat.id] }}
</span>
Expand Down

0 comments on commit 8509102

Please sign in to comment.