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

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

+2
View File
@@ -33,6 +33,8 @@
}
date_default_timezone_set('Europe/Moscow');
define('START_MEMORY', memory_get_usage());
require_once __DIR__ . '/../vendor/autoload.php';
\Sentry\init(
[
+17
View File
@@ -36,6 +36,7 @@
*/
private static function getLayoutParams(): array
{
/** @var User $USER */
global $USER;
$finish_time = microtime(true);
@@ -45,10 +46,12 @@
}
$currentPage = explode('::', Registry::get('currentPage'))[1];
$memoryUse = memory_get_usage() - START_MEMORY;
return [
'salt' => md5(random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999) . random_int(1, 999999)),
'currentPage' => $currentPage,
'memoryUse' => SystemFunctions::convertBytes($memoryUse),
'executionTime' => $delta . ' cek.',
'isAuthorized' => User::isAuthorized(),
'isAdmin' => User::isAuthorized() && $USER->isAdmin(),
@@ -93,12 +96,14 @@
public static function dialogs()
{
/** @var User $USER */
global $USER;
self::render('dialogs.twig', ['dialogs' => User::isAuthorized() ? $USER->getDialogs() : []]);
}
public static function dialog(int $id)
{
/** @var User $USER */
global $USER;
self::render(
'dialog.twig',
@@ -114,6 +119,7 @@
public static function sendMessage(int $userId)
{
/** @var User $USER */
global $USER;
$USER->getDialogObject()->sendMessage($userId, $_REQUEST['message']);
header('Location: /dialog/' . (int)$_REQUEST['dialogId']);
@@ -168,4 +174,15 @@
$userData = (new User($id))->getAllUserData(true);
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;
}
/**
* Получение количества элементов
*
* @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));
switch ($i) {
case 0:
return $size .= ' Б';
return $size .= ' b';
case 1:
return $size .= ' КБ';
return $size .= ' Kb';
case 2:
return $size .= ' МБ';
return $size .= ' Mb';
case 3:
return $size .= ' ГБ';
return $size .= ' Gb';
case 4:
return $size .= 'ТБ';
return $size .= 'Tb';
}
return 'Неизвестно';
}
+33 -7
View File
@@ -33,7 +33,7 @@
* @return int|null
* @throws CoreException
*/
private static function getDialogId(int $userOne, int $userTwo): ?int
public static function getDialogId(int $userOne, int $userTwo): ?int
{
$DB = DB::getInstance();
$dialogId = $DB->query(
@@ -53,12 +53,23 @@
* @return void
* @throws CoreException
*/
private static function markDialogViewed(int $dialogId): void
private function markDialogViewed(int $dialogId): void
{
/** @var $DB DB Объект базы данных */
$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();
$messages = $DB->query('SELECT * FROM ' . self::TABLE_MESSAGES . ' WHERE `dialog_id`="' . $dialogId . '"');
if ($markDialogViewed) {
self::markDialogViewed($dialogId);
$this->markDialogViewed($dialogId);
}
if (empty($messages)) {
return [];
@@ -141,13 +152,28 @@
$DB = DB::getInstance();
$dialogId = self::getDialogId($this->user->getId(), $to);
if (empty($dialogId)) {
$dialogId = $DB->addItem(self::TABLE_DIALOGS, ['viewed'=> CODE_VALUE_N, 'send' => $this->user->getId(), 'receive' => $to]);
$dialogId = $this->createDialog($to);
} else {
$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) {
$dialogs[$key]['companionId'] = $this->getDialogObject()->getDialogCompanionId((int)$dialog['id']);
$dialogs[$key]['companionData'] = (new self((int)$dialogs[$key]['companionId']))->getAllUserData(true);
$dialogs[$key]['unviewedCount'] = $this->getDialogObject()->getDialogUnviewedMessagesCount((int)$dialog['id']);
}
return $dialogs;
+2
View File
@@ -41,6 +41,8 @@
Router::route('/users', '\Core\App::users');
Router::route('/users/(\d+)', '\Core\App::userProfile');
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/(\d+)', '\Core\App::test');