strlen($_POST['message']) || strlen($_POST['message']) > 65535) { return 'fail'; } $receiver = UOJRequest::user(UOJRequest::POST, 'receiver'); if (!$receiver) { return 'fail'; } $message = $_POST['message']; if ($receiver['username'] === Auth::id()) { return 'fail'; } 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(); $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']] = [$msg['send_time'], ($msg['read_time'] == null), $msg['message']]; } else { if (isset($ret[$msg['receiver']])) continue; $ret[$msg['receiver']] = [$msg['send_time'], 0, $msg['message']]; } } $res = []; foreach ($ret as $name => $con) { $user = UOJUser::query($name); $res[] = [ $con[0], $con[1], $name, HTML::avatar_addr($user, 128), UOJUser::getRealname($user), UOJUser::getUserColor($user), $con[2], ]; } usort($res, function ($a, $b) { return -strcmp($a[0], $b[0]); }); return json_encode($res); } function getHistory() { $username = Auth::id(); $receiver = UOJRequest::user(UOJRequest::GET, 'conversationName'); $page_num = UOJRequest::uint(UOJRequest::GET, 'pageNumber'); if (!$receiver || $receiver['username'] === $username) { return '[]'; } if (!$page_num) { // false, null, or zero return '[]'; } DB::update([ "update user_msg", "set", ["read_time" => DB::now()], "where", [ "sender" => $receiver['username'], "receiver" => $username, "read_time" => null, ] ]); $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); } if (isset($_POST['user_msg'])) { die(handleMsgPost()); } elseif (isset($_GET['getConversations'])) { die(getConversations()); } elseif (isset($_GET['getHistory'])) { die(getHistory()); } ?>

私信