переработка диалогов

This commit is contained in:
rsgrinko committed 2026-07-24 15:47:44 +03:00
1 parent 19da842559
commit 67314ce6e6
7 files changed
+366 -115

No files matched your search

+142 -14
View File
@@ -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
========================================================================== */
+103 -5
View File
@@ -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()
+14
View File
@@ -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;
}
}
+1
View File
@@ -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');
+46
View File
@@ -0,0 +1,46 @@
{% import '_icons.twig' as icons %}
{% if messages is empty %}
<div class="empty-state">
{{ icons.chat() }}
<span>Сообщений пока нет &mdash; начните переписку</span>
</div>
{% 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 %}
<div class="message-day-separator"><span>{{ message.date|date('d.m.Y') }}</span></div>
{% endif %}
<div class="message {% if message.user_from == userId %}fromMe{% else %}fromCompanion{% endif %}{% if isGrouped %} grouped{% endif %}">
{% if not isGrouped %}
<div class="name"><a href="/users/{{ message.user_from_data.id }}">{{ message.user_from_data.name }}</a></div>
{% endif %}
<div class="text">
{% if message.type == 'text' %}
{{ message.text }}
{% elseif message.type == 'image' %}
{% if message.file.error == false %}
<a href="{{ message.file.path }}" target="_blank">
<img class="dialogImage" src="{{ message.file.path }}" alt="{{ message.file.name }}">
</a>
{% else %}
<b>Некорректное изображение</b>
{% endif %}
{% elseif message.type == 'file' %}
{% if message.file.error == false %}
<a class="fileLink" href="{{ message.file.path }}" target="_blank">{{ icons.paperclip() }} {{ message.file.name }}</a>
{% else %}
<b>Некорректный файл</b>
{% endif %}
{% endif %}
</div>
<div class="date">{{ message.date|date('H:i') }}</div>
{% if message.user_from == userId %}
<div class="deliveryStatus {% if message.viewed == "Y" %}viewed{% endif %}">&nbsp;</div>
{% endif %}
</div>
{% endfor %}
{% endif %}
+22 -56
View File
@@ -3,81 +3,47 @@
{% block title %}Диалог с {{ companionName }}{% endblock %}
{% block main %}
{% if isAuthorized %}
<div class="page-head">
<div class="page-head-icon">{{ (companionName|default('?'))|slice(0, 1)|upper }}</div>
<div>
<h1>{{ companionName }}</h1>
<p class="page-subtitle">Диалог №{{ dialog_id }}</p>
</div>
</div>
<a class="back-link" href="/dialogs">{{ icons.arrowLeft() }} Все диалоги</a>
<div class="chat-panel">
<div class="chat-panel" id="chatPanel" data-dialog-id="{{ dialog_id }}" data-messages-count="{{ messagesCount }}">
<div class="chat-panel-header">
<span class="user-avatar">{{ (companionName|default('?'))|slice(0, 1)|upper }}</span>
<span class="user-avatar chat-panel-avatar">
{% if companionAvatar %}
<img src="{{ companionAvatar }}" alt="{{ companionName }}">
{% else %}
{{ (companionName|default('?'))|slice(0, 1)|upper }}
{% endif %}
</span>
<div class="chat-panel-header-meta">
<strong>{{ companionName }}</strong>
<span class="status {% if companionOnline %}is-online{% endif %}">
<span class="status {% if companionOnline %}is-online{% endif %}" id="companionStatus">
<span class="status-dot"></span>
{{ companionOnline ? 'Онлайн' : 'Не в сети' }}
<span id="companionStatusText">{{ companionOnline ? 'Онлайн' : 'Не в сети' }}</span>
</span>
</div>
<span class="pill">{{ icons.chat() }} {{ messagesCount }} сообщений</span>
<span class="pill">{{ icons.chat() }} <span id="messagesCountText">{{ messagesCount }}</span> сообщений</span>
</div>
{% if messages is empty %}
<div class="messageBox">
<div class="empty-state">
{{ icons.chat() }}
<span>Сообщений пока нет &mdash; начните переписку</span>
</div>
</div>
{% else %}
<div class="messageBox">
{% for message in messages %}
<div class="message {% if message.user_from == userId %}fromMe{% else %}fromCompanion{% endif %}">
<div class="name"><a href="/users/{{ message.user_from_data.id }}">{{ message.user_from_data.name }}</a></div>
<div class="text">
{% if message.type == 'text' %}
{{ message.text }}
{% elseif message.type == 'image' %}
{% if message.file.error == false %}
<a href="{{ message.file.path }}" target="_blank">
<img class="dialogImage" src="{{ message.file.path }}" alt="{{ message.file.name }}">
</a>
{% else %}
<b>Некорректное изображение</b>
{% endif %}
{% elseif message.type == 'file' %}
{% if message.file.error == false %}
<a class="fileLink" href="{{ message.file.path }}" target="_blank">{{ icons.paperclip() }} {{ message.file.name }}</a>
{% else %}
<b>Некорректный файл</b>
{% endif %}
{% endif %}
</div>
<div class="date">{{ message.date }}</div>
{% if message.user_from == userId %}
<div class="deliveryStatus {% if message.user_from == userId and message.viewed == "Y" %}viewed{% endif %}">&nbsp;</div>
{% endif %}
</div>
{% endfor %}
</div>
{% endif %}
<div class="messageBox" id="messageBox">
{% include '_messageList.twig' with {messages: messages, userId: userId} %}
</div>
<div class="chat-compose">
<form method="POST" action="/users/{{ companionId }}/sendMessage" enctype="multipart/form-data">
<form method="POST" action="/users/{{ companionId }}/sendMessage" enctype="multipart/form-data" id="chatComposeForm">
<input type="hidden" name="dialogId" value="{{ dialog_id }}">
<textarea name="message" placeholder="Введите сообщение..."></textarea>
<textarea name="message" id="chatMessageInput" placeholder="Введите сообщение... (Enter — отправить, Shift+Enter — новая строка)"></textarea>
<div class="chat-compose-actions">
<label class="input-file">
<input type="file" name="file">
<span>{{ icons.paperclip() }} Файл</span>
<input type="file" name="file" id="chatFileInput">
<span id="chatFileLabel">{{ icons.paperclip() }} Файл</span>
</label>
<button class="button" type="submit" value="sendMessage">{{ icons.send() }} Отправить</button>
<button class="button" type="submit" value="sendMessage" id="chatSendButton">{{ icons.send() }} Отправить</button>
</div>
</form>
</div>
</div>
<script src="/assets/js/chat.js?{{ salt }}"></script>
{% else %}
<div class="alert alert-error">{{ icons.warning() }}<span>Вы не авторизованы</span></div>
{% endif %}
+38 -40
View File
@@ -23,48 +23,46 @@
<span>Диалогов пока нет</span>
</div>
{% else %}
<div class="table-wrap">
<table class="table">
<thead>
<tr>
<th>Собеседник</th>
<th style="width: 120px;">Статус</th>
<th style="width: 110px;">Сообщений</th>
<th style="width: 130px;">Непрочитанных</th>
<th style="width: 150px;"></th>
</tr>
</thead>
<tbody>
{% for dialog in dialogs %}
<tr>
<td>
<div class="cell-user">
<span class="user-avatar">{{ dialog.companionData.nameForDisplay|slice(0, 1)|upper }}</span>
<div class="cell-user-meta">
<span>{{ dialog.companionData.name }}</span>
<span class="cell-user-sub">{{ dialog.companionData.login }}</span>
</div>
</div>
</td>
<td>
<span class="status {% if dialog.companionOnline %}is-online{% endif %}">
<span class="status-dot"></span>
{{ dialog.companionOnline ? 'Онлайн' : 'Не в сети' }}
</span>
</td>
<td>{{ dialog.messagesCount }}</td>
<td>
{% if dialog.unviewedCount > 0 %}
<span class="nav-badge">{{ dialog.unviewedCount }}</span>
<div class="dialog-list">
{% for dialog in dialogs %}
<a class="dialog-row {% if dialog.unviewedCount > 0 %}has-unread{% endif %}" href="/dialog/{{ dialog.id }}">
<span class="avatar-wrap">
<span class="user-avatar">
{% if dialog.companionAvatar %}
<img src="{{ dialog.companionAvatar }}" alt="{{ dialog.companionData.name }}">
{% else %}
&mdash;
{{ (dialog.companionData.name|default(dialog.companionData.login))|slice(0, 1)|upper }}
{% endif %}
</td>
<td><a class="row-action" href="/dialog/{{ dialog.id }}">{{ icons.chat() }} Открыть</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</span>
<span class="avatar-status-dot {% if dialog.companionOnline %}is-online{% endif %}"></span>
</span>
<div class="dialog-row-main">
<div class="dialog-row-top">
<strong>{{ dialog.companionData.name }}</strong>
{% if dialog.lastMessage %}
<span class="dialog-row-time">{{ dialog.lastMessage.date|date('d.m.Y H:i') }}</span>
{% endif %}
</div>
<div class="dialog-row-preview">
{% if dialog.lastMessage %}
{% if dialog.lastMessage.user_from == userId %}<span class="dialog-row-you">Вы:</span>{% endif %}
{% if dialog.lastMessage.type == 'text' %}
{{ dialog.lastMessage.text|slice(0, 80) }}{% if dialog.lastMessage.text|length > 80 %}&hellip;{% endif %}
{% elseif dialog.lastMessage.type == 'image' %}
{{ icons.paperclip() }} Изображение
{% elseif dialog.lastMessage.type == 'file' %}
{{ icons.paperclip() }} Файл
{% endif %}
{% else %}
<span class="dialog-row-empty">Сообщений пока нет</span>
{% endif %}
</div>
</div>
{% if dialog.unviewedCount > 0 %}
<span class="nav-badge">{{ dialog.unviewedCount }}</span>
{% endif %}
</a>
{% endfor %}
</div>
{% endif %}
</div>