diff --git a/assets/css/style.css b/assets/css/style.css index ce3daa5..eb3bfa7 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -1048,9 +1048,6 @@ textarea:focus, Chat / dialogs ========================================================================== */ .chat-panel { - border: 1px solid var(--color-border); - border-radius: var(--radius-md); - overflow: hidden; display: flex; flex-direction: column; } @@ -1059,27 +1056,29 @@ textarea:focus, display: flex; align-items: center; gap: 12px; - padding: 14px 18px; - background: var(--color-surface-muted); + padding: 0 0 16px; border-bottom: 1px solid var(--color-border); } -.chat-panel-header .user-avatar { - width: 38px; - height: 38px; - font-size: 1em; +.chat-panel-header .chat-panel-avatar { + width: 46px; + height: 46px; + font-size: 1.15em; + font-weight: 700; background: var(--gradient-brand); color: #fff; + flex-shrink: 0; } .chat-panel-header-meta { display: flex; flex-direction: column; gap: 2px; + min-width: 0; } .chat-panel-header-meta strong { - font-size: 0.98em; + font-size: 1.1em; } .chat-panel-header .pill { @@ -1089,7 +1088,7 @@ textarea:focus, .messageBox { display: flex; flex-direction: column; - padding: 22px 24px 6px; + padding: 20px 0 6px; max-height: 56vh; overflow-y: auto; } @@ -1115,6 +1114,28 @@ textarea:focus, border-color: transparent; } +.messageBox .message.grouped { + padding-top: 12px; + margin-bottom: 4px; +} + +.message-day-separator { + display: flex; + align-items: center; + justify-content: center; + margin: 6px 0 18px; +} + +.message-day-separator span { + background: var(--color-surface-muted); + border: 1px solid var(--color-border); + color: var(--color-text-muted); + font-size: 0.78em; + font-weight: 600; + padding: 4px 12px; + border-radius: 999px; +} + .messageBox .message a { color: inherit; text-decoration: none; @@ -1175,13 +1196,16 @@ a.fileLink { .chat-compose { border-top: 1px solid var(--color-border); - background: var(--color-surface-muted); - padding: 16px 24px; + padding: 16px 0 0; } .chat-compose textarea { margin-bottom: 10px; - height: 80px; + height: 44px; + max-height: 200px; + min-height: 44px; + resize: none; + overflow-y: auto; } .chat-compose-actions { @@ -1194,6 +1218,110 @@ a.fileLink { margin-left: auto; } +.chat-compose-actions .button:disabled { + opacity: 0.6; + cursor: default; + transform: none; + box-shadow: var(--shadow-sm); +} + +/* ========================================================================== + Dialogs list (messenger-style) + ========================================================================== */ +.dialog-list { + display: flex; + flex-direction: column; + gap: 4px; +} + +.dialog-row { + display: flex; + align-items: center; + gap: 14px; + padding: 14px 16px; + border-radius: var(--radius-md); + text-decoration: none; + color: inherit; + transition: background var(--transition); +} + +.dialog-row:hover { + background: var(--color-surface-muted); +} + +.dialog-row.has-unread .dialog-row-preview, +.dialog-row.has-unread strong { + color: var(--color-text); + font-weight: 700; +} + +.avatar-wrap { + position: relative; + display: inline-flex; + flex-shrink: 0; +} + +.avatar-wrap .user-avatar { + background: var(--gradient-brand); +} + +.avatar-status-dot { + position: absolute; + right: -1px; + bottom: -1px; + width: 11px; + height: 11px; + border-radius: 50%; + background: #cbd2e0; + border: 2px solid var(--color-surface); +} + +.avatar-status-dot.is-online { + background: #22c55e; +} + +.dialog-row-main { + flex: 1; + min-width: 0; +} + +.dialog-row-top { + display: flex; + align-items: baseline; + justify-content: space-between; + gap: 10px; +} + +.dialog-row-top strong { + font-size: 0.98em; + color: var(--color-text-muted); + font-weight: 600; +} + +.dialog-row-time { + flex-shrink: 0; + font-size: 0.78em; + color: var(--color-text-faint); +} + +.dialog-row-preview { + margin-top: 2px; + color: var(--color-text-faint); + font-size: 0.9em; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.dialog-row-you { + color: var(--color-text-muted); + font-weight: 600; +} + +.dialog-row-empty { + font-style: italic; +} + /* ========================================================================== File input ========================================================================== */ diff --git a/core/lib/Core/App.class.php b/core/lib/Core/App.class.php index 41bfd9c..05984fd 100644 --- a/core/lib/Core/App.class.php +++ b/core/lib/Core/App.class.php @@ -121,16 +121,22 @@ foreach ($dialogs as $key => $dialog) { $dialogs[$key]['companionOnline'] = User::isOnline((int)$dialog['companionId']); $dialogs[$key]['messagesCount'] = $USER->getDialogObject()->getDialogMessagesCount((int)$dialog['id']); + $dialogs[$key]['lastMessage'] = $USER->getDialogObject()->getLastMessage((int)$dialog['id']); + $dialogs[$key]['companionAvatar'] = self::getAvatarPath($dialog['companionData']['image_id'] ?? null); } } - self::render('dialogs.twig', ['dialogs' => $dialogs]); + self::render('dialogs.twig', [ + 'dialogs' => $dialogs, + 'userId' => User::isAuthorized() ? $USER->getId() : null, + ]); } public static function dialog(int $id) { /** @var User $USER */ global $USER; - $companionId = $USER->getDialogObject()->getDialogCompanionId($id); + $companionId = $USER->getDialogObject()->getDialogCompanionId($id); + $companionObject = new User($companionId); self::render( 'dialog.twig', [ @@ -138,19 +144,52 @@ 'messages' => $USER->getMessages($id, true), 'userId' => $USER->getId(), 'companionId' => $companionId, - 'companionName' => (new User($companionId))->getName(), + 'companionName' => $companionObject->getName(), + 'companionAvatar' => self::getAvatarPath($companionObject->getAllUserData(true)['image_id'] ?? null), 'companionOnline' => User::isOnline($companionId), 'messagesCount' => $USER->getDialogObject()->getDialogMessagesCount($id), ] ); } + /** + * Получение пути к файлу аватара по идентификатору изображения + * + * @param int|null $imageId Идентификатор файла + * + * @return string|null + */ + private static function getAvatarPath(?int $imageId): ?string + { + if (empty($imageId)) { + return null; + } + try { + $image = (new File((int)$imageId))->getAllProps(); + } catch (CoreException $e) { + return null; + } + return $image['path'] ?? null; + } + public static function sendMessage(int $userId) { /** @var User $USER */ global $USER; + + $isAjax = ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') === 'XMLHttpRequest'; + $message = trim((string)($_REQUEST['message'] ?? '')); + + if (empty($_FILES['file']['tmp_name']) && $message === '') { + if ($isAjax) { + self::outputJson(['success' => false, 'error' => 'Сообщение пустое']); + } + header('Location: /dialog/' . (int)$_REQUEST['dialogId']); + return; + } + if (empty($_FILES['file']['tmp_name'])) { - $USER->getDialogObject()->sendMessage($userId, $_REQUEST['message']); + $USER->getDialogObject()->sendMessage($userId, $message); } else { $fileObject = new File(); $fileObject->saveFile($_FILES['file']['tmp_name'], $_FILES['file']['name'], true); @@ -158,7 +197,66 @@ $USER->getDialogObject()->sendFile($userId, $fileObject->getId(), in_array($_FILES['file']['type'], ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], true)); } - header('Location: /dialog/' . (int)$_REQUEST['dialogId']); + $dialogId = (int)$_REQUEST['dialogId']; + + if ($isAjax) { + self::outputDialogJson($dialogId); + } + + header('Location: /dialog/' . $dialogId); + } + + public static function dialogMessages(int $id) + { + self::outputDialogJson($id); + } + + /** + * Отдаёт JSON со свежим списком сообщений диалога (для AJAX-отправки и опроса новых сообщений) + * + * @param int $dialogId Идентификатор диалога + * + * @return void + */ + private static function outputDialogJson(int $dialogId): void + { + /** @var User $USER */ + global $USER, $twig; + + if (empty($USER)) { + self::outputJson(['success' => false, 'error' => 'Не авторизован']); + } + + $companionId = $USER->getDialogObject()->getDialogCompanionId($dialogId); + if ($companionId === null) { + self::outputJson(['success' => false, 'error' => 'Диалог не найден']); + } + + $html = $twig->render('_messageList.twig', [ + 'messages' => $USER->getMessages($dialogId, true), + 'userId' => $USER->getId(), + ]); + + self::outputJson([ + 'success' => true, + 'html' => $html, + 'messagesCount' => $USER->getDialogObject()->getDialogMessagesCount($dialogId), + 'companionOnline' => User::isOnline($companionId), + ]); + } + + /** + * Отдаёт массив данных в виде JSON и завершает работу скрипта + * + * @param array $data Данные + * + * @return void + */ + private static function outputJson(array $data): void + { + header('Content-Type: application/json; charset=UTF-8'); + echo json_encode($data); + die(); } public static function users() diff --git a/core/lib/Core/Models/Dialog.class.php b/core/lib/Core/Models/Dialog.class.php index 7656812..2ac04d3 100644 --- a/core/lib/Core/Models/Dialog.class.php +++ b/core/lib/Core/Models/Dialog.class.php @@ -242,5 +242,19 @@ return $DB->getCount(self::TABLE_MESSAGES, ['dialog_id' => $dialogId]); } + /** + * Получение последнего сообщения диалога (для превью в списке диалогов) + * + * @param int $dialogId Идентификатор диалога + * + * @return array|null + */ + public function getLastMessage(int $dialogId): ?array + { + /** @var $DB DataBase Объект базы данных */ + $DB = DataBase::getInstance(); + $res = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '" ORDER BY id DESC LIMIT 1'); + return $res ? $res[0] : null; + } } diff --git a/core/routes.php b/core/routes.php index 8c0829b..94f747e 100644 --- a/core/routes.php +++ b/core/routes.php @@ -37,6 +37,7 @@ Router::route('/dialogs', '\Core\App::dialogs'); Router::route('/dialog/(\d+)', '\Core\App::dialog'); + Router::route('/dialog/(\d+)/messages', '\Core\App::dialogMessages'); Router::route('/users', '\Core\App::users'); Router::route('/users/(\d+)', '\Core\App::userProfile'); diff --git a/templates/_messageList.twig b/templates/_messageList.twig new file mode 100644 index 0000000..1da49c3 --- /dev/null +++ b/templates/_messageList.twig @@ -0,0 +1,46 @@ +{% import '_icons.twig' as icons %} +{% if messages is empty %} +
+ {{ icons.chat() }} + Сообщений пока нет — начните переписку +
+{% else %} + {% for message in messages %} + {% set prevMessage = not loop.first ? messages[loop.index0 - 1] : null %} + {% set isNewDay = prevMessage is null or (message.date|date('Y-m-d')) != (prevMessage.date|date('Y-m-d')) %} + {% set isGrouped = prevMessage is not null and prevMessage.user_from == message.user_from and not isNewDay %} + + {% if isNewDay %} +
{{ message.date|date('d.m.Y') }}
+ {% endif %} + +
+ {% if not isGrouped %} +
{{ message.user_from_data.name }}
+ {% endif %} +
+ {% if message.type == 'text' %} + {{ message.text }} + {% elseif message.type == 'image' %} + {% if message.file.error == false %} + + {{ message.file.name }} + + {% else %} + Некорректное изображение + {% endif %} + {% elseif message.type == 'file' %} + {% if message.file.error == false %} + {{ icons.paperclip() }} {{ message.file.name }} + {% else %} + Некорректный файл + {% endif %} + {% endif %} +
+
{{ message.date|date('H:i') }}
+ {% if message.user_from == userId %} +
 
+ {% endif %} +
+ {% endfor %} +{% endif %} diff --git a/templates/dialog.twig b/templates/dialog.twig index 86e3c4d..3ae4471 100644 --- a/templates/dialog.twig +++ b/templates/dialog.twig @@ -3,81 +3,47 @@ {% block title %}Диалог с {{ companionName }}{% endblock %} {% block main %} {% if isAuthorized %} -
-
{{ (companionName|default('?'))|slice(0, 1)|upper }}
-
-

{{ companionName }}

-

Диалог №{{ dialog_id }}

-
-
+ {{ icons.arrowLeft() }} Все диалоги -
+
- {{ (companionName|default('?'))|slice(0, 1)|upper }} + + {% if companionAvatar %} + {{ companionName }} + {% else %} + {{ (companionName|default('?'))|slice(0, 1)|upper }} + {% endif %} +
{{ companionName }} - + - {{ companionOnline ? 'Онлайн' : 'Не в сети' }} + {{ companionOnline ? 'Онлайн' : 'Не в сети' }}
- {{ icons.chat() }} {{ messagesCount }} сообщений + {{ icons.chat() }} {{ messagesCount }} сообщений
- {% if messages is empty %} -
-
- {{ icons.chat() }} - Сообщений пока нет — начните переписку -
-
- {% else %} -
- {% for message in messages %} -
- -
- {% if message.type == 'text' %} - {{ message.text }} - {% elseif message.type == 'image' %} - {% if message.file.error == false %} - - {{ message.file.name }} - - {% else %} - Некорректное изображение - {% endif %} - {% elseif message.type == 'file' %} - {% if message.file.error == false %} - {{ icons.paperclip() }} {{ message.file.name }} - {% else %} - Некорректный файл - {% endif %} - {% endif %} -
-
{{ message.date }}
- {% if message.user_from == userId %} -
 
- {% endif %} -
- {% endfor %} -
- {% endif %} +
+ {% include '_messageList.twig' with {messages: messages, userId: userId} %} +
-
+ - +
- +
+ + {% else %}
{{ icons.warning() }}Вы не авторизованы
{% endif %} diff --git a/templates/dialogs.twig b/templates/dialogs.twig index 7bcfed9..7d58f99 100644 --- a/templates/dialogs.twig +++ b/templates/dialogs.twig @@ -23,48 +23,46 @@ Диалогов пока нет
{% else %} -
- - - - - - - - - - - - {% for dialog in dialogs %} - - - - - - - - {% endfor %} - -
СобеседникСтатусСообщенийНепрочитанных
-
- {{ dialog.companionData.nameForDisplay|slice(0, 1)|upper }} -
- {{ dialog.companionData.name }} - {{ dialog.companionData.login }} -
-
-
- - - {{ dialog.companionOnline ? 'Онлайн' : 'Не в сети' }} - - {{ dialog.messagesCount }} - {% if dialog.unviewedCount > 0 %} - {{ dialog.unviewedCount }} + {{ icons.chat() }} Открыть
+ + + +
+
+ {{ dialog.companionData.name }} + {% if dialog.lastMessage %} + {{ dialog.lastMessage.date|date('d.m.Y H:i') }} + {% endif %} +
+
+ {% if dialog.lastMessage %} + {% if dialog.lastMessage.user_from == userId %}Вы:{% endif %} + {% if dialog.lastMessage.type == 'text' %} + {{ dialog.lastMessage.text|slice(0, 80) }}{% if dialog.lastMessage.text|length > 80 %}…{% endif %} + {% elseif dialog.lastMessage.type == 'image' %} + {{ icons.paperclip() }} Изображение + {% elseif dialog.lastMessage.type == 'file' %} + {{ icons.paperclip() }} Файл + {% endif %} + {% else %} + Сообщений пока нет + {% endif %} +
+
+ {% if dialog.unviewedCount > 0 %} + {{ dialog.unviewedCount }} + {% endif %} + + {% endfor %}
{% endif %}