Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ChatController
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 index
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * The Chat Controller displays the chat page.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-01-19
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Frontend;
21
22use phpMyFAQ\Chat;
23use phpMyFAQ\Core\Exception;
24use phpMyFAQ\Session\Token;
25use phpMyFAQ\Translation;
26use phpMyFAQ\User\UserSession;
27use Symfony\Component\HttpFoundation\RedirectResponse;
28use Symfony\Component\HttpFoundation\Request;
29use Symfony\Component\HttpFoundation\Response;
30use Symfony\Component\Routing\Attribute\Route;
31
32final class ChatController extends AbstractFrontController
33{
34    public function __construct(
35        private readonly UserSession $userSession,
36    ) {
37        parent::__construct();
38    }
39
40    /**
41     * Displays the user's chat/messages page.
42     *
43     * @throws Exception
44     * @throws \Exception
45     */
46    #[Route(path: '/user/chat', name: 'public.user.chat', methods: ['GET'])]
47    public function index(Request $request): Response
48    {
49        if (!$this->currentUser->isLoggedIn()) {
50            return new RedirectResponse($this->configuration->getDefaultUrl());
51        }
52
53        $this->userSession->setCurrentUser($this->currentUser);
54        $this->userSession->userTracking('chat', 0);
55
56        $chat = new Chat($this->configuration);
57
58        $conversations = $chat->getConversationList($this->currentUser->getUserId());
59        $unreadCount = $chat->getUnreadCount($this->currentUser->getUserId());
60
61        return $this->render('chat.twig', [
62            ...$this->getHeader($request),
63            'title' => sprintf('%s - %s', Translation::getString(key: 'msgChat'), $this->configuration->getTitle()),
64            'conversations' => $conversations,
65            'unreadCount' => $unreadCount,
66            'currentUserId' => $this->currentUser->getUserId(),
67            'csrfTokenSendMessage' => Token::getInstance($this->session)->getTokenString('send-chat-message'),
68            'csrfTokenMarkRead' => Token::getInstance($this->session)->getTokenString('mark-chat-read'),
69        ]);
70    }
71}