From 39df125d160d2051e69349a295ec37caa11df89f Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 16 Jan 2023 15:34:51 +0800 Subject: [PATCH] refactor(user_msg): bootstrap5 --- web/app/controllers/user_msg.php | 325 ++++++++++++++++++++----------- web/js/uoj.js | 4 +- 2 files changed, 208 insertions(+), 121 deletions(-) diff --git a/web/app/controllers/user_msg.php b/web/app/controllers/user_msg.php index 6441997..0478b44 100644 --- a/web/app/controllers/user_msg.php +++ b/web/app/controllers/user_msg.php @@ -1,100 +1,129 @@ strlen($_POST['message']) || strlen($_POST['message']) > 65535) { return 'fail'; } - $receiver = $_POST['receiver']; - $esc_message = DB::escape($_POST['message']); - $sender = Auth::id(); + $receiver = UOJRequest::user(UOJRequest::POST, 'receiver'); + if (!$receiver) { + return 'fail'; + } + $message = $_POST['message']; - if (!validateUsername($receiver) || !UOJUser::query($receiver)) { + if ($receiver['username'] === Auth::id()) { return 'fail'; } - DB::query("insert into user_msg (sender, receiver, message, send_time) values ('$sender', '$receiver', '$esc_message', now())"); + DB::insert([ + "insert into user_msg", + "(sender, receiver, message, send_time)", + "values", DB::tuple([Auth::id(), $receiver['username'], $message, DB::now()]) + ]); return "ok"; } function getConversations() { $username = Auth::id(); - $result = DB::query("select * from user_msg where sender = '$username' or receiver = '$username' order by send_time DESC"); - $ret = array(); - while ($msg = DB::fetch($result)) { + + $res = DB::selectAll([ + "select * from user_msg", + "where", DB::lor([ + "sender" => $username, + "receiver" => $username, + ]), + "order by send_time DESC" + ]); + $ret = []; + foreach ($res as $msg) { if ($msg['sender'] !== $username) { if (isset($ret[$msg['sender']])) { $ret[$msg['sender']][1] |= ($msg['read_time'] == null); continue; } - $ret[$msg['sender']] = array($msg['send_time'], ($msg['read_time'] == null)); + + $ret[$msg['sender']] = [$msg['send_time'], ($msg['read_time'] == null)]; } else { - if (isset($ret[$msg['receiver']])) { - continue; - } - $ret[$msg['receiver']] = array($msg['send_time'], 0); + if (isset($ret[$msg['receiver']])) continue; + + $ret[$msg['receiver']] = [$msg['send_time'], 0]; } } $res = []; foreach ($ret as $name => $con) { - $res[] = [$con[0], $con[1], $name]; + $user = UOJUser::query($name); + $res[] = [ + $con[0], + $con[1], + $name, + HTML::avatar_addr($user, 128), + UOJUser::getRealname($user), + UOJUser::getUserColor($user), + ]; } + usort($res, function ($a, $b) { return -strcmp($a[0], $b[0]); }); + return json_encode($res); } function getHistory() { $username = Auth::id(); - if (!isset($_GET['conversationName']) || !validateUsername($_GET['conversationName'])) { + $receiver = UOJRequest::user(UOJRequest::GET, 'conversationName'); + $page_num = UOJRequest::uint(UOJRequest::GET, 'pageNumber'); + if (!$receiver || $receiver['username'] === $username) { return '[]'; } - if (!isset($_GET['pageNumber']) || !validateUInt($_GET['pageNumber'])) { + if (!$page_num) { // false, null, or zero return '[]'; } - $conversationName = $_GET['conversationName']; - $pageNumber = ($_GET['pageNumber'] - 1) * 10; - DB::query("update user_msg set read_time = now() where sender = '$conversationName' and receiver = '$username' and read_time is null"); + DB::update([ + "update user_msg", + "set", ["read_time" => DB::now()], + "where", [ + "sender" => $receiver['username'], + "receiver" => $username, + "read_time" => null, + ] + ]); - $result = DB::query("select * from user_msg where (sender = '$username' and receiver = '$conversationName') or (sender = '$conversationName' and receiver = '$username') order by send_time DESC limit $pageNumber, 11"); - $ret = array(); - while ($msg = DB::fetch($result)) { - $ret[] = array($msg['message'], $msg['send_time'], $msg['read_time'], $msg['id'], ($msg['sender'] == $username)); + $result = DB::selectAll([ + "select * from user_msg", + "where", DB::lor([ + DB::land([ + "sender" => $username, + "receiver" => $receiver['username'] + ]), + DB::land([ + "sender" => $receiver['username'], + "receiver" => $username + ]) + ]), + "order by send_time DESC", DB::limit(($page_num - 1) * 10, 11) + ]); + $ret = []; + foreach ($result as $msg) { + $ret[] = [ + $msg['message'], + $msg['send_time'], + $msg['read_time'], + $msg['id'], + ($msg['sender'] === $username), + ]; } return json_encode($ret); } -/* - function deleteMsg($msgId) { - return 1; - $str = << -

私信

+

私信

-
- -