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

This commit is contained in:
rsgrinko committed 2023-08-03 00:21:20 +03:00
1 parent 6576be0c61
commit 95f0611022
12 files changed
+127 -53

No files matched your search

+29 -2
View File
@@ -175,10 +175,18 @@ body {
.main .messageBox { .main .messageBox {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
/*подумать
overflow-y: scroll;
overflow-x: hidden;
height: 300px;
*/
} }
.main .messageBox .message { .main .messageBox .message {
width: 300px; width: fit-content;
min-width: 300px;
max-width: 80%;
padding: 20px 10px; padding: 20px 10px;
position: relative; position: relative;
background: #eee; background: #eee;
@@ -194,6 +202,11 @@ body {
color: white; color: white;
} }
.main .messageBox .message a {
color: inherit;
text-decoration: none;
}
.main .messageBox .message.fromCompanion:before { .main .messageBox .message.fromCompanion:before {
content: ""; content: "";
border: solid transparent; border: solid transparent;
@@ -221,6 +234,7 @@ body {
font-weight: bold; font-weight: bold;
position: absolute; position: absolute;
top: 5px; top: 5px;
text-decoration: none;
} }
.main .messageBox .message .text { .main .messageBox .message .text {
@@ -244,4 +258,17 @@ body {
.main .header .mainMenu a { .main .header .mainMenu a {
color: white; color: white;
text-decoration: none; text-decoration: none;
} }
.deliveryStatus {
background: #cecece;
width: 10px;
height: 10px;
border-radius: 45px;
position: absolute;
right: 8px;
top: 8px;
}
.deliveryStatus.viewed {
background: #00f700;
}
+2
View File
@@ -33,6 +33,8 @@
} }
date_default_timezone_set('Europe/Moscow'); date_default_timezone_set('Europe/Moscow');
define('START_MEMORY', memory_get_usage());
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
\Sentry\init( \Sentry\init(
[ [
+17
View File
@@ -36,6 +36,7 @@
*/ */
private static function getLayoutParams(): array private static function getLayoutParams(): array
{ {
/** @var User $USER */
global $USER; global $USER;
$finish_time = microtime(true); $finish_time = microtime(true);
@@ -45,10 +46,12 @@
} }
$currentPage = explode('::', Registry::get('currentPage'))[1]; $currentPage = explode('::', Registry::get('currentPage'))[1];
$memoryUse = memory_get_usage() - START_MEMORY;
return [ return [
'salt' => md5(random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999)), 'salt' => md5(random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999)),
'currentPage' => $currentPage, 'currentPage' => $currentPage,
'memoryUse' => SystemFunctions::convertBytes($memoryUse),
'executionTime' => $delta . ' cek.', 'executionTime' => $delta . ' cek.',
'isAuthorized' => User::isAuthorized(), 'isAuthorized' => User::isAuthorized(),
'isAdmin' => User::isAuthorized() && $USER->isAdmin(), 'isAdmin' => User::isAuthorized() && $USER->isAdmin(),
@@ -93,12 +96,14 @@
public static function dialogs() public static function dialogs()
{ {
/** @var User $USER */
global $USER; global $USER;
self::render('dialogs.twig', ['dialogs' => User::isAuthorized() ? $USER->getDialogs() : []]); self::render('dialogs.twig', ['dialogs' => User::isAuthorized() ? $USER->getDialogs() : []]);
} }
public static function dialog(int $id) public static function dialog(int $id)
{ {
/** @var User $USER */
global $USER; global $USER;
self::render( self::render(
'dialog.twig', 'dialog.twig',
@@ -114,6 +119,7 @@
public static function sendMessage(int $userId) public static function sendMessage(int $userId)
{ {
/** @var User $USER */
global $USER; global $USER;
$USER->getDialogObject()->sendMessage($userId, $_REQUEST['message']); $USER->getDialogObject()->sendMessage($userId, $_REQUEST['message']);
header('Location: /dialog/' . (int)$_REQUEST['dialogId']); header('Location: /dialog/' . (int)$_REQUEST['dialogId']);
@@ -168,4 +174,15 @@
$userData = (new User($id))->getAllUserData(true); $userData = (new User($id))->getAllUserData(true);
self::render('userProfile.twig', ['userData' => $userData]); self::render('userProfile.twig', ['userData' => $userData]);
} }
public static function goToDialog(int $userId)
{
/** @var User $USER */
global $USER;
$dialogId = $USER->getDialogObject()->getDialogId($USER->getId(), $userId);
if (empty($dialogId)) {
$dialogId = $USER->getDialogObject()->createDialog($userId);
}
header('Location: /dialog/' . $dialogId);
}
} }
+23
View File
@@ -499,6 +499,29 @@
return null; return null;
} }
/**
* Получение количества элементов
*
* @param string $table Имя таблицы
* @param array|string $where Массив where
*
* @return int Количество элементов
*/
public function getCountItems(string $table, $where): int
{
self::$quantity++;
$count = 0;
try {
$result = $this->query('SELECT COUNT(*) as count FROM `' . $table . '` WHERE ' . $this->createWhere($where));
} catch (Throwable $e) {
$result = false;
}
if ($result) {
$count = (int)$result[0]['count'];
}
return $count;
}
/** /**
* Получить все данные из таблицы * Получить все данные из таблицы
@@ -71,15 +71,15 @@
$size = str_replace('.', ',', round($size, 2)); $size = str_replace('.', ',', round($size, 2));
switch ($i) { switch ($i) {
case 0: case 0:
return $size .= ' Б'; return $size .= ' b';
case 1: case 1:
return $size .= ' КБ'; return $size .= ' Kb';
case 2: case 2:
return $size .= ' МБ'; return $size .= ' Mb';
case 3: case 3:
return $size .= ' ГБ'; return $size .= ' Gb';
case 4: case 4:
return $size .= 'ТБ'; return $size .= 'Tb';
} }
return 'Неизвестно'; return 'Неизвестно';
} }
+33 -7
View File
@@ -33,7 +33,7 @@
* @return int|null * @return int|null
* @throws CoreException * @throws CoreException
*/ */
private static function getDialogId(int $userOne, int $userTwo): ?int public static function getDialogId(int $userOne, int $userTwo): ?int
{ {
$DB = DB::getInstance(); $DB = DB::getInstance();
$dialogId = $DB->query( $dialogId = $DB->query(
@@ -53,12 +53,23 @@
* @return void * @return void
* @throws CoreException * @throws CoreException
*/ */
private static function markDialogViewed(int $dialogId): void private function markDialogViewed(int $dialogId): void
{ {
/** @var $DB DB Объект базы данных */
$DB = DB::getInstance(); $DB = DB::getInstance();
$dialogId = $DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed' => CODE_VALUE_Y]); $dialogData = $DB->getItem(self::TABLE_DIALOGS, ['id' => $dialogId]);
if ($this->user->getId() === (int)$dialogData['receive']) {
$DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed' => CODE_VALUE_Y]);
$DB->update(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'user_to' => $this->user->getId(), 'viewed' => CODE_VALUE_N], ['viewed' => CODE_VALUE_Y]);
}
} }
public function createDialog(int $userId): ?int
{
/** @var $DB DB Объект базы данных */
$DB = DB::getInstance();
return $DB->addItem(self::TABLE_DIALOGS, ['viewed' => CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $userId]);
}
/** /**
* Получение диалогов * Получение диалогов
* *
@@ -119,7 +130,7 @@
$DB = DB::getInstance(); $DB = DB::getInstance();
$messages = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '"'); $messages = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '"');
if ($markDialogViewed) { if ($markDialogViewed) {
self::markDialogViewed($dialogId); $this->markDialogViewed($dialogId);
} }
if (empty($messages)) { if (empty($messages)) {
return []; return [];
@@ -141,13 +152,28 @@
$DB = DB::getInstance(); $DB = DB::getInstance();
$dialogId = self::getDialogId($this->user->getId(), $to); $dialogId = self::getDialogId($this->user->getId(), $to);
if (empty($dialogId)) { if (empty($dialogId)) {
$dialogId = $DB->addItem(self::TABLE_DIALOGS, ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]); $dialogId = $this->createDialog($to);
} else { } else {
$DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]); $DB->update(self::TABLE_DIALOGS, ['id' => $dialogId], ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]);
} }
$DB->addItem(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'user_from' => $this->user->getId(), 'user_to' => $to, 'text' => $message]); $result = $DB->addItem(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'user_from' => $this->user->getId(), 'user_to' => $to, 'text' => $message]);
return (int)$result > 0;
} }
/**
* Получение количества непрочитанных сообщений в диалоге
*
* @param int $dialogId Идентификатор диалога
*
* @return int Количество
*/
public function getDialogUnviewedMessagesCount(int $dialogId): int
{
/** @var $DB DB Объект базы данных */
$DB = DB::getInstance();
return $DB->getCountItems(self::TABLE_MESSAGES, ['dialog_id' => $dialogId, 'viewed' => CODE_VALUE_N, 'user_to' => $this->user->getId()]);
}
} }
+1
View File
@@ -904,6 +904,7 @@
foreach ($dialogs as $key => $dialog) { foreach ($dialogs as $key => $dialog) {
$dialogs[$key]['companionId'] = $this->getDialogObject()->getDialogCompanionId((int)$dialog['id']); $dialogs[$key]['companionId'] = $this->getDialogObject()->getDialogCompanionId((int)$dialog['id']);
$dialogs[$key]['companionData'] = (new self((int)$dialogs[$key]['companionId']))->getAllUserData(true); $dialogs[$key]['companionData'] = (new self((int)$dialogs[$key]['companionId']))->getAllUserData(true);
$dialogs[$key]['unviewedCount'] = $this->getDialogObject()->getDialogUnviewedMessagesCount((int)$dialog['id']);
} }
return $dialogs; return $dialogs;
+2
View File
@@ -41,6 +41,8 @@
Router::route('/users', '\Core\App::users'); Router::route('/users', '\Core\App::users');
Router::route('/users/(\d+)', '\Core\App::userProfile'); Router::route('/users/(\d+)', '\Core\App::userProfile');
Router::route('/users/(\d+)/sendMessage', '\Core\App::sendMessage'); Router::route('/users/(\d+)/sendMessage', '\Core\App::sendMessage');
Router::route('/users/(\d+)/dialog', '\Core\App::goToDialog');
Router::route('/test', '\Core\App::test', true); Router::route('/test', '\Core\App::test', true);
Router::route('/test/(\d+)', '\Core\App::test'); Router::route('/test/(\d+)', '\Core\App::test');
+4 -33
View File
@@ -4,48 +4,19 @@
{% if isAuthorized %} {% if isAuthorized %}
<h1>Диалог # {{ dialog_id }} с "{{ companionName }} ({{ companionId }})"</h1> <h1>Диалог # {{ dialog_id }} с "{{ companionName }} ({{ companionId }})"</h1>
<!--
<table class="table">
<thead>
<tr>
<td>ID</td>
<td>От</td>
<td>Кому</td>
<td>Дата</td>
<td>Сообщение</td>
</tr>
</thead>
<tbody>
{% for message in messages %}
<tr>
<td>{{ message.id }}</td>
<td>{{ message.user_from_data.name }} ({{ message.user_from_data.login }})</td>
<td>{{ message.user_to_data.name }} ({{ message.user_to_data.login }})</td>
<td>{{ message.date }}</td>
<td>{{ message.text }}</td>
</tr>
{% endfor %}
</tbody>
</table>
-->
<div class="messageBox"> <div class="messageBox">
{% for message in messages %} {% for message in messages %}
<div class="message {% if message.user_from == userId %}fromMe{% else %}fromCompanion{% endif %}"> <div class="message {% if message.user_from == userId %}fromMe{% else %}fromCompanion{% endif %}">
<div class="name">{{ message.user_from_data.name }}</div> <div class="name"><a href="/users/{{ message.user_from_data.id }}">{{ message.user_from_data.name }}</a></div>
<div class="text">{{ message.text }}</div> <div class="text">{{ message.text }}</div>
<div class="date">{{ message.date }}</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> </div>
{% endfor %} {% endfor %}
</div> </div>
<p> <p>
<form method="POST" action="/users/{{ companionId }}/sendMessage"> <form method="POST" action="/users/{{ companionId }}/sendMessage">
<input type="hidden" name="dialogId" value="{{ dialog_id }}"> <input type="hidden" name="dialogId" value="{{ dialog_id }}">
+2
View File
@@ -9,6 +9,7 @@
<tr> <tr>
<td>ID</td> <td>ID</td>
<td>Прочитано</td> <td>Прочитано</td>
<td>Непрочитанных</td>
<td>С кем диалог</td> <td>С кем диалог</td>
<td>Действия</td> <td>Действия</td>
</tr> </tr>
@@ -18,6 +19,7 @@
<tr> <tr>
<td>{{ dialog.id }}</td> <td>{{ dialog.id }}</td>
<td>{{ dialog.viewed }}</td> <td>{{ dialog.viewed }}</td>
<td>{{ dialog.unviewedCount }}</td>
<td>{{ dialog.companionData.nameForDisplay }}</td> <td>{{ dialog.companionData.nameForDisplay }}</td>
<td><a href="/dialog/{{ dialog.id }}">Открыть диалог</a></td> <td><a href="/dialog/{{ dialog.id }}">Открыть диалог</a></td>
</tr> </tr>
+1 -1
View File
@@ -41,7 +41,7 @@
</div> </div>
<div class="footer"> <div class="footer">
<a href="https://github.com/rsgrinko/delta_framework" target="_blank">DeltaFramework</a> by <a href="https://it-stories.ru/about" target="_blank">Roman S Grinko</a>, {{ currentYear }} | Generation time: {{ executionTime }} <a href="https://github.com/rsgrinko/delta_framework" target="_blank">DeltaFramework</a> by <a href="https://it-stories.ru/about" target="_blank">Roman S Grinko</a>, {{ currentYear }} | Generation time: {{ executionTime }} | Memory use: {{ memoryUse }}
</div> </div>
</div> </div>
</body> </body>
+8 -5
View File
@@ -5,11 +5,14 @@
{% block main %} {% block main %}
<h1>Профиль пользователя {{ userData.login }}</h1> <h1>Профиль пользователя {{ userData.login }}</h1>
<p> <p>
<span>ID: {{ userData.id }}</span><br> <span><b>ID:</b> {{ userData.id }}</span><br>
<span>Логин: {{ userData.login }}</span><br> <span><b>Логин:</b> {{ userData.login }}</span><br>
<span>Имя: {{ userData.name }}</span><br> <span><b>Имя:</b> {{ userData.name }}</span><br>
<span>E-Mail: {{ userData.email }}</span><br> <span><b>E-Mail:</b> {{ userData.email }}</span><br>
<span>Был активен: {{ userData.last_active }}</span><br> <span><b>Был активен:</b> {{ userData.last_active }}</span><br>
<form action="/users/{{ userData.id }}/dialog" method="POST">
<button class="button" type="submit">Написать</button>
</form>
</p> </p>
{% endblock %} {% endblock %}