S2OJ/web/app/controllers/user_msg.php

437 lines
12 KiB
PHP
Raw Normal View History

2016-07-18 16:39:37 +00:00
<?php
2023-01-16 07:34:51 +00:00
2023-02-05 12:55:14 +00:00
Auth::check() || redirectToLogin();
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
function handleMsgPost() {
if (!isset($_POST['message'])) {
return 'fail';
}
if (0 > strlen($_POST['message']) || strlen($_POST['message']) > 65535) {
return 'fail';
}
2023-01-16 07:34:51 +00:00
$receiver = UOJRequest::user(UOJRequest::POST, 'receiver');
if (!$receiver) {
return 'fail';
}
$message = $_POST['message'];
2016-07-18 16:39:37 +00:00
2023-01-16 07:34:51 +00:00
if ($receiver['username'] === Auth::id()) {
2022-11-11 23:10:34 +00:00
return 'fail';
2016-07-18 16:39:37 +00:00
}
2023-01-16 07:34:51 +00:00
DB::insert([
"insert into user_msg",
"(sender, receiver, message, send_time)",
"values", DB::tuple([Auth::id(), $receiver['username'], $message, DB::now()])
]);
2022-11-11 23:10:34 +00:00
return "ok";
}
function getConversations() {
$username = Auth::id();
2023-01-16 07:34:51 +00:00
$res = DB::selectAll([
"select * from user_msg",
"where", DB::lor([
"sender" => $username,
"receiver" => $username,
]),
"order by send_time DESC"
]);
$ret = [];
foreach ($res as $msg) {
2022-11-11 23:10:34 +00:00
if ($msg['sender'] !== $username) {
if (isset($ret[$msg['sender']])) {
$ret[$msg['sender']][1] |= ($msg['read_time'] == null);
continue;
2016-07-18 16:39:37 +00:00
}
2023-01-16 07:34:51 +00:00
$ret[$msg['sender']] = [$msg['send_time'], ($msg['read_time'] == null), $msg['message']];
2022-11-11 23:10:34 +00:00
} else {
2023-01-16 07:34:51 +00:00
if (isset($ret[$msg['receiver']])) continue;
$ret[$msg['receiver']] = [$msg['send_time'], 0, $msg['message']];
2020-06-25 12:41:16 +00:00
}
2016-07-18 16:39:37 +00:00
}
2022-11-11 23:10:34 +00:00
$res = [];
foreach ($ret as $name => $con) {
2023-01-16 07:34:51 +00:00
$user = UOJUser::query($name);
$res[] = [
$con[0],
$con[1],
$name,
HTML::avatar_addr($user, 128),
UOJUser::getRealname($user),
UOJUser::getUserColor($user),
$con[2],
2023-01-16 07:34:51 +00:00
];
2022-11-11 23:10:34 +00:00
}
2023-01-16 07:34:51 +00:00
2022-11-11 23:10:34 +00:00
usort($res, function ($a, $b) {
return -strcmp($a[0], $b[0]);
});
2023-01-16 07:34:51 +00:00
2022-11-11 23:10:34 +00:00
return json_encode($res);
}
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
function getHistory() {
$username = Auth::id();
2023-01-16 07:34:51 +00:00
$receiver = UOJRequest::user(UOJRequest::GET, 'conversationName');
$page_num = UOJRequest::uint(UOJRequest::GET, 'pageNumber');
if (!$receiver || $receiver['username'] === $username) {
2022-11-11 23:10:34 +00:00
return '[]';
}
2023-01-16 07:34:51 +00:00
if (!$page_num) { // false, null, or zero
2022-11-11 23:10:34 +00:00
return '[]';
}
2016-07-18 16:39:37 +00:00
2023-01-16 07:34:51 +00:00
DB::update([
"update user_msg",
"set", ["read_time" => DB::now()],
"where", [
"sender" => $receiver['username'],
"receiver" => $username,
"read_time" => null,
]
]);
2016-07-18 16:39:37 +00:00
2023-01-16 07:34:51 +00:00
$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),
];
2016-07-18 16:39:37 +00:00
}
2022-11-11 23:10:34 +00:00
return json_encode($ret);
}
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
if (isset($_POST['user_msg'])) {
die(handleMsgPost());
} elseif (isset($_GET['getConversations'])) {
die(getConversations());
} elseif (isset($_GET['getHistory'])) {
die(getHistory());
}
?>
2016-07-18 16:39:37 +00:00
<?php echoUOJPageHeader('私信') ?>
2023-01-16 07:34:51 +00:00
<h1>私信</h1>
2016-07-18 16:39:37 +00:00
2023-01-16 10:25:19 +00:00
<style>
#conversations {
border-radius: var(--bs-border-radius) !important;
}
2023-01-16 10:25:19 +00:00
@media (min-width: 768px) {
.chat-container {
height: calc(100ch - 10rem);
}
#conversations {
border-top-right-radius: 0 !important;
border-bottom-right-radius: 0 !important;
}
2023-01-16 10:25:19 +00:00
}
</style>
<div class="card overflow-hidden-md chat-container">
2023-01-16 07:34:51 +00:00
<div class="row gx-0 flex-grow-1 h-100">
2023-01-28 01:33:50 +00:00
<div class="col-md-3 border-end h-100">
<div class="list-group list-group-flush h-100 overflow-auto" id="conversations"></div>
2016-07-18 16:39:37 +00:00
</div>
2023-01-16 07:34:51 +00:00
<div class="col-md-9 h-100" id="history" style="display: none">
2023-01-16 10:25:19 +00:00
<div class="card h-100 border-0 rounded-0">
2023-01-16 07:34:51 +00:00
<div class="card-header">
2023-01-16 08:25:47 +00:00
<button id="goBack" class="btn-close position-absolute" aria-label="关闭对话"></button>
2023-01-16 07:34:51 +00:00
<div id="conversation-name" class="text-center"></div>
</div>
<div class="card-body overflow-auto" id="history-list-container">
2023-01-16 07:34:51 +00:00
<div id="history-list" style="min-height: 200px;"></div>
2016-07-18 16:39:37 +00:00
</div>
2023-01-16 07:34:51 +00:00
<div class="card-footer bg-transparent">
2023-01-16 08:28:30 +00:00
<ul class="pagination pagination-sm justify-content-between mt-1">
2023-01-16 07:34:51 +00:00
<li class="page-item">
<button class="page-link rounded" id="pageLeft">
<i class="bi bi-chevron-left"></i>
更早的消息
</button>
</li>
<li class="page-item">
<button class="page-link rounded" id="pageRight">
更新的消息
<i class="bi bi-chevron-right"></i>
</button>
</li>
</ul>
<form id="form-message" class="">
<div id="form-group-message" class="flex-grow-1">
<textarea id="input-message" class="form-control" style="resize: none;" data-no-autosize></textarea>
<span id="help-message" class="help-block"></span>
</div>
<div class="text-end mt-2">
<span class="text-muted small"> Ctrl+Enter 键发送</span>
<button type="submit" id="message-submit" class="btn btn-primary flex-shrink-0 ms-3">
发送
<i class="bi bi-send"></i>
</button>
</div>
</form>
2016-07-18 16:39:37 +00:00
</div>
2023-01-16 07:34:51 +00:00
</div>
2016-07-18 16:39:37 +00:00
</div>
</div>
</div>
<script type="text/javascript">
var REFRESH_INTERVAL = 30 * 1000;
2022-11-11 23:10:34 +00:00
$(document).ready(function() {
$.ajaxSetup({
async: false
});
2022-11-11 23:10:34 +00:00
refreshConversations();
setInterval(refreshConversations, REFRESH_INTERVAL);
2022-11-11 23:10:34 +00:00
<?php if (isset($_GET['enter'])) : ?>
enterConversation(<?= json_encode($_GET['enter']) ?>);
<?php endif ?>
});
2016-07-18 16:39:37 +00:00
</script>
<script type="text/javascript">
2023-01-16 10:03:09 +00:00
<?php $enter_user = UOJRequest::user(UOJRequest::GET, 'enter'); ?>
2023-01-16 07:34:51 +00:00
var conversations = {};
var intervalId = 0;
var user_avatar = '<?= HTML::avatar_addr(Auth::user(), 80) ?>';
2023-01-16 10:03:09 +00:00
var enter_user = ['<?= $enter_user['username'] ?>', '<?= UOJUser::getRealname($enter_user) ?>', '<?= UOJUser::getUserColor($enter_user) ?>'];
2023-01-16 07:34:51 +00:00
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
function formatTime(date) {
var d = new Date(date),
hour = '' + d.getHours(),
minute = '' + d.getMinutes();
if (hour.length < 2)
hour = '0' + hour;
if (minute.length < 2)
minute = '0' + minute;
return [hour, minute].join(':');
}
function addButton(conversationName, send_time, type, avatar_addr, realname, color, last_message) {
2023-01-16 07:34:51 +00:00
var now = new Date();
var time = new Date(send_time);
var timeStr = formatDate(send_time);
if (formatDate(now) === timeStr) {
timeStr = formatTime(send_time);
}
2022-11-11 23:10:34 +00:00
$("#conversations").append(
'<div class="list-group-item list-group-item-action p-2 d-flex ' + (type ? 'list-group-item-warning' : '') + '" style="cursor: pointer; user-select: none;" ' +
2022-11-11 23:10:34 +00:00
'onclick="enterConversation(\'' + conversationName + '\')">' +
2023-01-16 07:34:51 +00:00
'<div class="flex-shrink-0 me-3">' +
2023-02-07 11:36:48 +00:00
'<img class="rounded uoj-user-avatar" width="56" height="56" src="' + avatar_addr + '" />' +
2016-07-18 16:39:37 +00:00
'</div>' +
'<div class="flex-grow-1 overflow-hidden">' +
2023-01-16 09:22:47 +00:00
'<div class="d-flex justify-content-between">' +
getUserSpan(conversationName, '', color) +
'<span class="float-end text-muted small flex-shrink-0 lh-lg">' +
2023-01-16 07:34:51 +00:00
timeStr +
'</span>' +
2016-07-18 16:39:37 +00:00
'</div>' +
'<div class="text-muted text-nowrap text-truncate">' +
htmlspecialchars(last_message) +
'</div>' +
'</div>' +
2022-11-11 23:10:34 +00:00
'</div>'
);
}
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
function addBubble(content, send_time, read_time, msgId, conversation, page, type) {
$("#history-list").append(
2023-01-16 07:34:51 +00:00
'<div class="d-flex align-items-end mt-3" style="' + (type ? 'margin-left:20%;' : 'margin-right:20%;') + '">' +
(type ? '' : '<img class="flex-shrink-0 me-2 rounded" width="32" height="32" src="' + conversations[conversation][1] + '" style="user-select: none;" />') +
2023-01-16 07:34:51 +00:00
'<div class="card flex-grow-1">' +
'<div class="card-body px-3 py-2" style="white-space:pre-wrap">' +
2022-11-11 23:10:34 +00:00
htmlspecialchars(content) +
'</div>' +
2023-01-16 07:34:51 +00:00
'<div class="card-footer text-muted px-3 py-1">' +
'<span class="small">' +
'<i class="bi bi-clock"></i> ' + send_time +
'</span>' +
(read_time == null ?
'<span class="float-end" data-bs-toggle="tooltip" data-bs-title="未读"><i class="bi bi-check2"></i></span>' :
'<span class="float-end" data-bs-toggle="tooltip" data-bs-title="' + read_time + '"><i class="bi bi-check2-all"></i></span>') +
2022-11-11 23:10:34 +00:00
'</div>' +
'</div>' +
2023-02-07 11:36:48 +00:00
(type ? '<img class="flex-shrink-0 ms-2 rounded uoj-user-avatar" width="32" height="32" src="' + user_avatar + '" style="user-select: none;" />' : '') +
2016-07-18 16:39:37 +00:00
'</div>'
2022-11-11 23:10:34 +00:00
);
}
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
function submitMessagePost(conversationName) {
if ($('#input-message').val().length == 0 || $('#input-message').val().length >= 65536) {
$('#help-message').text('私信长度必须在1~65535之间。');
$('#form-group-message').addClass('has-error');
return;
2016-07-18 16:39:37 +00:00
}
$('#help-message').text('');
$('#form-group-message').removeClass('has-error');
2023-01-16 07:34:51 +00:00
$.post('/user_msg', {
2022-11-11 23:10:34 +00:00
user_msg: 1,
receiver: conversationName,
message: $('#input-message').val()
}, function(msg) {
$('#input-message').val("");
2016-07-18 16:39:37 +00:00
});
2022-11-11 23:10:34 +00:00
}
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
function refreshHistory(conversation, page) {
2016-07-18 16:39:37 +00:00
$("#history-list").empty();
var ret = false;
2023-01-16 10:03:09 +00:00
$('#conversation-name').html(getUserLink(conversation, conversation == enter_user[0] ? enter_user[1] : conversations[conversation][2], conversation == enter_user[0] ? enter_user[2] : conversations[conversation][3]));
2016-07-18 16:39:37 +00:00
$('#pageShow').text("" + page.toString() + "");
2023-01-16 07:34:51 +00:00
$.get('/user_msg', {
2022-11-11 23:10:34 +00:00
getHistory: '',
conversationName: conversation,
pageNumber: page
2016-07-18 16:39:37 +00:00
}, function(msg) {
2022-11-11 23:10:34 +00:00
var result = JSON.parse(msg);
var cnt = 0,
flag = 0,
F = 0;
if (result.length == 11) flag = 1, F = 1;
result.reverse();
for (msg in result) {
if (flag) {
flag = 0;
continue;
2016-07-18 16:39:37 +00:00
}
2022-11-11 23:10:34 +00:00
var message = result[msg];
addBubble(message[0], message[1], message[2], message[3], conversation, page, message[4]);
2023-01-16 07:34:51 +00:00
if ((++cnt) + 1 == result.length && F) {
break;
}
}
2023-01-16 07:34:51 +00:00
if (result.length == 11) {
ret = true;
2022-11-11 23:10:34 +00:00
}
bootstrap.Tooltip.jQueryInterface.call($('#history-list [data-bs-toggle="tooltip"]'), {
container: $('#history-list'),
});
2016-07-18 16:39:37 +00:00
});
return ret;
2022-11-11 23:10:34 +00:00
}
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
function refreshConversations() {
$("#conversations").empty();
2023-01-16 07:34:51 +00:00
$.get('/user_msg', {
getConversations: 1
2016-07-18 16:39:37 +00:00
}, function(msg) {
var result = JSON.parse(msg);
for (i in result) {
var conversation = result[i];
if (conversation[1] == 1) {
addButton(conversation[2], conversation[0], conversation[1], conversation[3], conversation[4], conversation[5], conversation[6]);
2016-07-18 16:39:37 +00:00
}
2023-01-16 07:34:51 +00:00
conversations[conversation[2]] = [conversation[0], conversation[3], conversation[4], conversation[5]];
2016-07-18 16:39:37 +00:00
}
for (i in result) {
var conversation = result[i];
if (conversation[1] == 0) {
addButton(conversation[2], conversation[0], conversation[1], conversation[3], conversation[4], conversation[5], conversation[6]);
2016-07-18 16:39:37 +00:00
}
}
2022-11-11 23:10:34 +00:00
});
}
2016-07-18 16:39:37 +00:00
2022-11-11 23:10:34 +00:00
function enterConversation(conversationName) {
var slideTime = 300;
var page = 1;
var changeAble = refreshHistory(conversationName, page);
clearInterval(intervalId);
intervalId = setInterval(function() {
changeAble = refreshHistory(conversationName, page);
}, REFRESH_INTERVAL);
2023-01-16 07:34:51 +00:00
$('#history').show();
$('#conversations').addClass('d-none d-md-block')
$("#history-list-container").scrollTop($("#history-list").height());
2023-01-16 07:34:51 +00:00
$('#input-message').unbind('keydown').keydown(function(e) {
if (e.keyCode == 13 && e.ctrlKey) {
$('#message-submit').click();
}
});
2022-11-11 23:10:34 +00:00
$('#form-message').unbind("submit").submit(function() {
submitMessagePost(conversationName);
page = 1;
changeAble = refreshHistory(conversationName, page);
refreshConversations();
$("#history-list-container").scrollTop($("#history-list").height());
2022-11-11 23:10:34 +00:00
return false;
});
$('#goBack').unbind("click").click(function() {
clearInterval(intervalId);
2022-11-11 23:10:34 +00:00
refreshConversations();
2023-01-16 07:34:51 +00:00
$("#history").hide();
$("#conversations").removeClass('d-none d-md-block');
2022-11-11 23:10:34 +00:00
return;
});
$('#pageLeft').unbind("click").click(function() {
if (changeAble) {
page++;
clearInterval(intervalId);
}
2022-11-11 23:10:34 +00:00
changeAble = refreshHistory(conversationName, page);
return false;
});
$('#pageRight').unbind("click").click(function() {
if (page > 1) {
page--;
clearInterval(intervalId);
}
2022-11-11 23:10:34 +00:00
changeAble = refreshHistory(conversationName, page);
return false;
});
}
2016-07-18 16:39:37 +00:00
</script>
<?php echoUOJPageFooter() ?>