/
GhostLogic
/
SudakOnline
Обзор
Документация
Войти
/
GhostLogic
/
SudakOnline
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
api/src/Controllers/ChatController.php
52 строки
2 KB
GhostLogic
Загрузить файлы в «»
28 апр 2026, 18:11
Верифицирован
28 апр 2026, 18:11
3c10f40
Код
Авторство
О чём код?
<?php namespace SudakOnline\Controllers; use SudakOnline\Services\ChatService; class ChatController { private ChatService $chatService; public function __construct() { $this->chatService = new ChatService(); } // GET /chat/messages?last_id=0&limit=50 public function getMessages() { $lastId = isset($_GET['last_id']) ? (int)$_GET['last_id'] : 0; $limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 50; $userId = $GLOBALS['currentUser']['id'] ?? null; if (!$userId) { echo json_encode(['success' => false, 'error' => 'Не авторизован']); return; } $messages = $this->chatService->getMessages($userId, $lastId, $limit); echo json_encode(['success' => true, 'messages' => $messages]); } // POST /chat/send public function sendMessage() { $userId = $GLOBALS['currentUser']['id'] ?? null; if (!$userId) { echo json_encode(['success' => false, 'error' => 'Не авторизован']); return; } $input = json_decode(file_get_contents('php://input'), true); $text = trim($input['text'] ?? ''); if (mb_strlen($text) == 0 || mb_strlen($text) > 1000) { echo json_encode(['success' => false, 'error' => 'Некорректный текст (1-1000 символов)']); return; } $result = $this->chatService->sendMessage($userId, $text); echo json_encode($result); } // GET /chat/online – количество активных сессий за последние 5 минут public function getOnlineCount() { $count = $this->chatService->getOnlineCount(); echo json_encode(['success' => true, 'online' => $count]); } }