Access to old conversations when scroll down + UI changes BT#12617

pull/2487/head
jmontoyaa 8 years ago
parent 5196a91811
commit 4a7b64a23b
  1. 33
      main/inc/ajax/chat.ajax.php
  2. 213
      main/inc/lib/chat.lib.php
  3. 1
      main/inc/lib/javascript/chat/css/chat.css
  4. 120
      main/inc/lib/javascript/chat/js/chat.js

@ -25,7 +25,7 @@ if ($action == 'preview') {
exit;
}
$to_user_id = isset($_REQUEST['to']) ? $_REQUEST['to'] : null;
$toUserId = isset($_REQUEST['to']) ? $_REQUEST['to'] : null;
$message = isset($_REQUEST['message']) ? $_REQUEST['message'] : null;
if (!isset($_SESSION['chatHistory'])) {
@ -54,27 +54,44 @@ switch ($action) {
$chat->close();
break;
case 'sendchat':
$chat->send(api_get_user_id(), $to_user_id, $message);
$chat->send(api_get_user_id(), $toUserId, $message);
break;
case 'startchatsession':
$chat->startSession();
break;
case 'get_previous_messages':
$userId = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : null;
$visibleMessages = isset($_REQUEST['visible_messages']) ? $_REQUEST['visible_messages'] : null;
if (empty($userId)) {
return '';
}
$items = $chat->getPreviousMessages(
$userId,
api_get_user_id(),
$visibleMessages
);
echo json_encode($items);
exit;
break;
case 'set_status':
$status = isset($_REQUEST['status']) ? intval($_REQUEST['status']) : 0;
$chat->setUserStatus($status);
break;
case 'create_room':
$room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
$room = VideoChat::getChatRoomByUsers(api_get_user_id(), $toUserId);
if ($room === false) {
$createdRoom = VideoChat::createRoom(api_get_user_id(), $to_user_id);
$createdRoom = VideoChat::createRoom(api_get_user_id(), $toUserId);
if ($createdRoom === false) {
echo Display::return_message(get_lang('ChatRoomNotCreated'), 'error');
echo Display::return_message(
get_lang('ChatRoomNotCreated'),
'error'
);
break;
}
$room = VideoChat::getChatRoomByUsers(api_get_user_id(), $to_user_id);
$room = VideoChat::getChatRoomByUsers(api_get_user_id(), $toUserId);
}
$videoChatUrl = api_get_path(WEB_LIBRARY_JS_PATH)."chat/video.php?room={$room['id']}";
@ -85,7 +102,7 @@ switch ($action) {
$chat->send(
api_get_user_id(),
$to_user_id,
$toUserId,
$videoChatLink,
false,
false
@ -96,7 +113,7 @@ switch ($action) {
case 'notify_not_support':
$chat->send(
api_get_user_id(),
$to_user_id,
$toUserId,
get_lang('TheXUserBrowserDoesNotSupportWebRTC')
);
break;

@ -63,23 +63,170 @@ class Chat extends Model
);
}
public function getLatestChat($currentUserId, $userId, $latestMessages)
{
$items = self::getPreviousMessages(
$currentUserId,
$userId,
0,
$latestMessages
);
return array_reverse($items);
}
/**
* @param array $chatHistory
* @param int $latestMessages
* @return mixed
*/
public function getAllLatestChats($chatHistory, $latestMessages = 5)
{
$currentUserId = api_get_user_id();
$chats = [];
if (!empty($chatHistory)) {
foreach ($chatHistory as $chat) {
$userId = $chat['user_info']['user_id'];
$items = self::getLatestChat(
$currentUserId,
$userId,
$latestMessages
);
$chats[$userId]['items'] = $items;
}
}
return $chats;
}
/**
* Starts a chat session and returns JSON array of status and chat history
* @return void (prints output in JSON format)
*/
public function startSession()
{
$items = Session::read('chatHistory');
$chatList = Session::read('chatHistory');
$chats = self::getAllLatestChats($chatList);
$return = array(
'user_status' => $this->getUserStatus(),
'me' => get_lang('Me'),
'user_id' => api_get_user_id(),
'items' => $items
'items' => $chats
);
echo json_encode($return);
exit;
}
/**
* @param int $fromUserId
* @param int $toUserId
* @return mixed
*/
public function getCountMessagesExchangeBetweenUsers($fromUserId, $toUserId)
{
$row = Database::select(
'count(*) as count',
$this->table,
array(
'where' => array(
'(from_user = ? AND to_user = ?) OR (from_user = ? AND to_user = ?) ' => [
$fromUserId,
$toUserId,
$toUserId,
$fromUserId
]
)
),
'first'
);
return $row['count'];
}
/**
* @param int $fromUserId
* @param int $toUserId
* @param int $visibleMessages
* @param int $previousMessageCount messages to show
* @return array
*/
public function getPreviousMessages(
$fromUserId,
$toUserId,
$visibleMessages = 1,
$previousMessageCount = 5
) {
$currentUserId = api_get_user_id();
$toUserId = (int) $toUserId;
$fromUserId = (int) $fromUserId;
$previousMessageCount = (int) $previousMessageCount;
if (empty($toUserId) || empty($fromUserId)) {
return [];
}
$total = self::getCountMessagesExchangeBetweenUsers(
$fromUserId,
$toUserId
);
$show = $total - $visibleMessages;
$from = $show - $previousMessageCount;
if ($from < 0) {
return [];
}
$sql = "SELECT * FROM ".$this->table."
WHERE
(
to_user = $toUserId AND
from_user = $fromUserId)
OR
(
from_user = $toUserId AND
to_user = $fromUserId
)
ORDER BY id ASC
LIMIT $from, $previousMessageCount
";
$result = Database::query($sql);
$rows = Database::store_result($result);
$fromUserInfo = api_get_user_info($fromUserId, true);
$toUserInfo = api_get_user_info($toUserId, true);
$users = [
$fromUserId => $fromUserInfo,
$toUserId => $toUserInfo,
];
$items = [];
$rows = array_reverse($rows);
foreach ($rows as $chat) {
$fromUserId = $chat['from_user'];
$userInfo = $users[$fromUserId];
$username = $userInfo['complete_name'];
if ($currentUserId == $fromUserId) {
$username = get_lang('Me');
}
$chat['message'] = Security::remove_XSS($chat['message']);
$item = array(
'id' => $chat['id'],
's' => '0',
'f' => $fromUserId,
'm' => $chat['message'],
'username' => $username,
'user_info' => [
'username' => $username,
'online' => $userInfo['user_is_online'],
'avatar' => $userInfo['avatar_small'],
'user_id' => $userInfo['user_id']
],
'date' => api_strtotime($chat['sent'], 'UTC')
);
$items[] = $item;
$_SESSION['openChatBoxes'][$fromUserId] = api_strtotime($chat['sent'], 'UTC');
}
//array_unshift($_SESSION['chatHistory'][$fromUserId]['items'], $items);
return $items;
}
/**
* Refreshes the chat windows (usually called every x seconds through AJAX)
* @return void (prints JSON array of chat windows)
@ -99,33 +246,50 @@ class Chat extends Model
}
$items = array();
foreach ($chat_list as $from_user_id => $rows) {
foreach ($chat_list as $fromUserId => $rows) {
$rows = $rows['items'];
$user_info = api_get_user_info($from_user_id, true);
$user_info = api_get_user_info($fromUserId, true);
$count = $this->getCountMessagesExchangeBetweenUsers(
$fromUserId,
$to_user_id
);
$chatItems = self::getLatestChat($fromUserId, $to_user_id, 5);
// Cleaning tsChatBoxes
unset($_SESSION['tsChatBoxes'][$from_user_id]);
unset($_SESSION['tsChatBoxes'][$fromUserId]);
foreach ($rows as $chat) {
$chat['message'] = Security::remove_XSS($chat['message']);
/*$chat['message'] = Security::remove_XSS($chat['message']);
$item = array(
's' => '0',
'f' => $from_user_id,
'f' => $fromUserId,
'm' => $chat['message'],
'username' => $user_info['complete_name'],
'date' => api_strtotime($chat['sent'], 'UTC'),
'id' => $chat['id']
);
$items[$from_user_id]['items'][] = $item;
$items[$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
$items[$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
$items[$from_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
$_SESSION['openChatBoxes'][$from_user_id] = api_strtotime($chat['sent'], 'UTC');
$items[$fromUserId]['items'][] = $item;
$items[$fromUserId]['total_messages'] = $count;
$items[$fromUserId]['user_info']['user_name'] = $user_info['complete_name'];
$items[$fromUserId]['user_info']['online'] = $user_info['user_is_online'];
$items[$fromUserId]['user_info']['avatar'] = $user_info['avatar_small'];*/
$_SESSION['openChatBoxes'][$fromUserId] = api_strtotime($chat['sent'], 'UTC');
}
$_SESSION['chatHistory'][$from_user_id]['items'][] = $item;
$_SESSION['chatHistory'][$from_user_id]['user_info']['user_name'] = $user_info['complete_name'];
$_SESSION['chatHistory'][$from_user_id]['user_info']['online'] = $user_info['user_is_online'];
$_SESSION['chatHistory'][$from_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
$items[$fromUserId]['items'] = $chatItems;
$items[$fromUserId]['total_messages'] = $count;
$items[$fromUserId]['user_info']['user_name'] = $user_info['complete_name'];
$items[$fromUserId]['user_info']['online'] = $user_info['user_is_online'];
$items[$fromUserId]['user_info']['avatar'] = $user_info['avatar_small'];
$items[$fromUserId]['user_info']['user_id'] = $user_info['user_id'];
$_SESSION['chatHistory'][$fromUserId]['items'] = $chatItems;
$_SESSION['chatHistory'][$fromUserId]['total_messages'] = $count;
$_SESSION['chatHistory'][$fromUserId]['user_info']['user_id'] = $user_info['user_id'];
$_SESSION['chatHistory'][$fromUserId]['user_info']['user_name'] = $user_info['complete_name'];
$_SESSION['chatHistory'][$fromUserId]['user_info']['online'] = $user_info['user_is_online'];
$_SESSION['chatHistory'][$fromUserId]['user_info']['avatar'] = $user_info['avatar_small'];
}
if (!empty($_SESSION['openChatBoxes'])) {
@ -156,7 +320,7 @@ class Chat extends Model
WHERE to_user = '".$to_user_id."' AND recd = 0";
Database::query($sql);
echo json_encode(array('items' => $items));
echo json_encode(['items' => $items]);
}
/**
@ -188,7 +352,7 @@ class Chat extends Model
/**
* Sends a message from one user to another user
* @param int $from_user_id The ID of the user sending the message
* @param int $fromUserId The ID of the user sending the message
* @param int $to_user_id The ID of the user receiving the message
* @param string $message Message
* @param boolean $printResult Optional. Whether print the result
@ -197,14 +361,14 @@ class Chat extends Model
* @return void Prints "1"
*/
public function send(
$from_user_id,
$fromUserId,
$to_user_id,
$message,
$printResult = true,
$sanitize = true
) {
$user_friend_relation = SocialManager::get_relation_between_contacts(
$from_user_id,
$fromUserId,
$to_user_id
);
@ -225,25 +389,26 @@ class Chat extends Model
}
$item = array(
"s" => "1",
"f" => $from_user_id,
"f" => $fromUserId,
"m" => $messagesan,
'date' => api_strtotime($now, 'UTC'),
"username" => get_lang('Me')
'username' => get_lang('Me')
);
$_SESSION['chatHistory'][$to_user_id]['items'][] = $item;
$_SESSION['chatHistory'][$to_user_id]['user_info']['user_name'] = $user_info['complete_name'];
$_SESSION['chatHistory'][$to_user_id]['user_info']['online'] = $user_info['user_is_online'];
$_SESSION['chatHistory'][$to_user_id]['user_info']['avatar'] = $user_info['avatar_small'];
$_SESSION['chatHistory'][$to_user_id]['user_info']['user_id'] = $user_info['user_id'];
unset($_SESSION['tsChatBoxes'][$to_user_id]);
$params = array();
$params['from_user'] = intval($from_user_id);
$params['from_user'] = intval($fromUserId);
$params['to_user'] = intval($to_user_id);
$params['message'] = $message;
$params['sent'] = api_get_utc_datetime();
if (!empty($from_user_id) && !empty($to_user_id)) {
if (!empty($fromUserId) && !empty($to_user_id)) {
$this->save($params);
}

@ -51,7 +51,6 @@
}
.chatboxmessagecontent {
}
.chatboxcontent {

@ -29,16 +29,17 @@ var maxChatHeartbeat = 33000;
var chatHeartbeatTime = minChatHeartbeat;
var originalTitle;
var blinkOrder = 0;
var chatboxFocus = new Array();
var newMessages = new Array();
var newMessagesWin = new Array();
var chatBoxes = new Array();
var timer;
var user_status = 0;
var widthBox = 320; // see css class .chatbox
//var ajax_url = 'chat.php'; // This variable is loaded in the template/layout/head.tpl file
function set_user_status(status) {
function set_user_status(status)
{
$.ajax({
url: ajax_url+"?action=set_status",
data: "status="+status,
@ -60,7 +61,7 @@ $(document).ready(function() {
});
/* "On" conditions, divs are created dynamically */
// User name header toogle
// User name header toggle
$('body').on('click', '#chatboxtitlemain', function() {
if (user_status == 1) {
stopChatHeartBeat();
@ -137,14 +138,15 @@ function startChatSession()
$.each(data.items, function(my_user_id, user_items) {
my_items = user_items['items'];
$.each(my_items, function(i, item) {
if (item) { // fix strange ie bug
if (item) {
// fix strange ie bug
if ($("#chatbox_"+my_user_id).length <= 0) {
createChatBox(
my_user_id,
user_items.user_info.user_name,
item.username,
1,
user_items.user_info.online,
user_items.user_info.avatar
item.user_info.online,
item.user_info.avatar
);
}
@ -152,7 +154,7 @@ function startChatSession()
// info message
//$("#chatbox_"+my_user_id+" .chatboxcontent").append('<div class="'+messageLogMe+'"><span class="chatboxinfo">'+item.m+'</span></div>');
} else {
var chatBubble = getChatBox(my_user_id, item);
var chatBubble = createChatBubble(my_user_id, item);
$("#chatbox_"+my_user_id+" .chatboxcontent").append(chatBubble);
}
}
@ -170,11 +172,14 @@ function startChatSession()
}
}
function stopChatHeartBeat() {
function stopChatHeartBeat()
{
clearInterval(timer);
timer = null;
}
function startChatHeartBeat() {
function startChatHeartBeat()
{
timer = setInterval('chatHeartbeat();', chatHeartbeatTime);
}
@ -236,14 +241,15 @@ function chatHeartbeat()
$.each(data.items, function(my_user_id, user_items) {
my_items = user_items['items'];
$.each(my_items, function(i, item) {
if (item) { // fix strange ie bug
if (item) {
// fix strange ie bug
if ($("#chatbox_"+my_user_id).length <= 0) {
createChatBox(
my_user_id,
user_items.user_info.user_name,
item.username,
0,
user_items.user_info.online,
user_items.user_info.avatar
item.user_info.online,
item.user_info.avatar
);
}
if ($("#chatbox_"+my_user_id).css('display') == 'none') {
@ -260,7 +266,7 @@ function chatHeartbeat()
} else {
newMessages[my_user_id] = {'status':true, 'username':item.username};
newMessagesWin[my_user_id]= {'status':true, 'username':item.username};
var chatBubble = getChatBox(my_user_id, item);
var chatBubble = createChatBubble(my_user_id, item);
$("#chatbox_"+my_user_id+" .chatboxcontent").append(chatBubble);
}
@ -288,7 +294,6 @@ function chatHeartbeat()
chatHeartbeatTime = maxChatHeartbeat;
}
}
//timer = setTimeout('chatHeartbeat();',chatHeartbeatTime);
}
}); //ajax
@ -300,7 +305,7 @@ function chatHeartbeat()
* @param item
* @returns {string}
*/
function getChatBox(my_user_id, item)
function createChatBubble(my_user_id, item)
{
var myDiv = 'chatboxmessage_me';
if (my_user_id == item.f) {
@ -332,7 +337,7 @@ function restructureChatBoxes()
if (align == 0) {
$("#chatbox_"+user_id).css('right', '10px');
} else {
width = (align)*(225+7) + 5 + 5;
width = (align)*(widthBox+7) + 5 + 5;
$("#chatbox_"+user_id).css('right', width+'px');
}
align++;
@ -358,11 +363,13 @@ function chatWith(user_id, user_name, status, userImage)
*/
function createChatBox(user_id, chatboxtitle, minimizeChatBox, online, userImage)
{
if ($("#chatbox_"+user_id).length > 0) {
if ($("#chatbox_"+user_id).css('display') == 'none') {
$("#chatbox_"+user_id).css('display','block');
var oldChatBox = $("#chatbox_"+user_id);
if (oldChatBox.length > 0) {
if (oldChatBox.css('display') == 'none') {
oldChatBox.css('display','block');
restructureChatBoxes();
}
$("#chatbox_"+user_id+" .chatboxtextarea").focus();
return;
}
@ -394,7 +401,7 @@ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online, userImage
.addClass('chatboxoptions')
.appendTo(chatboxHead);
if (!!Modernizr.prefixed('RTCPeerConnection', window) &&
if (!!Modernizr.prefixed('RTCPeerConnection', window) &&
(online === '1' || online === 1)
) {
$('<a>')
@ -461,7 +468,7 @@ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online, userImage
if (chatBoxeslength == 0) {
$("#chatbox_"+user_id).css('right', '10px');
} else {
width = (chatBoxeslength)*(225+7) + 5 + 5;
width = (chatBoxeslength)*(widthBox+7) + 5 + 5;
$("#chatbox_"+user_id).css('right', width+'px');
}
@ -505,6 +512,45 @@ function createChatBox(user_id, chatboxtitle, minimizeChatBox, online, userImage
}
});
$("#chatbox_"+user_id).show();
$("#chatbox_"+user_id+" .chatboxcontent").scroll(function () {
var iCurScrollPos = $(this).scrollTop();
if (iCurScrollPos == 0) {
getMoreItems(user_id);
return false;
}
});
}
/**
* @param int userId
*/
function getMoreItems(userId)
{
var visibleMessages = $("#chatbox_"+userId+" .chatboxcontent").find('div').length;
$.ajax({
url: ajax_url+"?action=get_previous_messages&user_id="+userId+"&visible_messages="+visibleMessages,
cache: false,
dataType: "json",
success: function(items) {
$.each(items, function(i, item) {
if (item) {
if ($("#chatbox_"+userId).css('display') == 'none') {
$("#chatbox_"+userId).css('display','block');
restructureChatBoxes();
}
var chatBubble = createChatBubble(userId, item);
$("#chatbox_"+userId+" .chatboxcontent").prepend(chatBubble);
$("#chatbox_"+userId+" .chatboxcontent").scrollTop(
10
);
if ($('#chatbox_'+userId+' .chatboxcontent').css('display') == 'none') {
$('#chatbox_'+userId+' .chatboxhead').toggleClass('chatboxblink');
}
}
});
}
}); //ajax
}
/**
@ -598,18 +644,22 @@ function checkChatBoxInputKey(event, chatboxtextarea, user_id)
to: user_id,
message: message
}, function (data) {
message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;");
var item = {
username: username,
date: moment().unix(),
f: currentUserId,
m: message
};
var bubble = getChatBox(user_id, item);
$("#chatbox_"+user_id+" .chatboxcontent").append(bubble);
$("#chatbox_"+user_id+" .chatboxcontent").scrollTop(
$("#chatbox_"+user_id+" .chatboxcontent")[0].scrollHeight
);
if (data == 1) {
message = message.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;");
var item = {
username: username,
date: moment().unix(),
f: currentUserId,
m: message
};
var bubble = createChatBubble(user_id, item);
$("#chatbox_" + user_id + " .chatboxcontent").append(bubble);
$("#chatbox_" + user_id + " .chatboxcontent").scrollTop(
$("#chatbox_" + user_id + " .chatboxcontent")[0].scrollHeight
);
} else {
$("#chatbox_" + user_id + " .chatboxcontent").append('<i class="fa fa-exclamation-triangle" aria-hidden="true"></i><br />');
}
});
}
chatHeartbeatTime = minChatHeartbeat;

Loading…
Cancel
Save