|
|
|
@ -73,7 +73,7 @@ class Chat extends Model |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getLatestChat($currentUserId, $userId, $latestMessages) |
|
|
|
public function getLatestChat($currentUserId, $userId, $latestMessages) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$items = self::getPreviousMessages( |
|
|
|
$items = $this->getPreviousMessages( |
|
|
|
$currentUserId, |
|
|
|
$currentUserId, |
|
|
|
$userId, |
|
|
|
$userId, |
|
|
|
0, |
|
|
|
0, |
|
|
|
@ -113,14 +113,12 @@ class Chat extends Model |
|
|
|
|
|
|
|
|
|
|
|
$chats = []; |
|
|
|
$chats = []; |
|
|
|
foreach ($chatHistory as $userId => $time) { |
|
|
|
foreach ($chatHistory as $userId => $time) { |
|
|
|
$total = self::getCountMessagesExchangeBetweenUsers($userId, $currentUserId); |
|
|
|
$total = $this->getCountMessagesExchangeBetweenUsers($userId, $currentUserId); |
|
|
|
|
|
|
|
|
|
|
|
$start = $total - $latestMessages; |
|
|
|
$start = $total - $latestMessages; |
|
|
|
|
|
|
|
|
|
|
|
if ($start < 0) { |
|
|
|
if ($start < 0) { |
|
|
|
$start = 0; |
|
|
|
$start = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
$items = $this->getMessages($userId, $currentUserId, 1, $start, $latestMessages); |
|
|
|
$items = $this->getMessages($userId, $currentUserId, $start, $latestMessages); |
|
|
|
$chats[$userId]['items'] = $items; |
|
|
|
$chats[$userId]['items'] = $items; |
|
|
|
$chats[$userId]['window_user_info'] = api_get_user_info($userId); |
|
|
|
$chats[$userId]['window_user_info'] = api_get_user_info($userId); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -140,7 +138,7 @@ class Chat extends Model |
|
|
|
// $chat->setUserStatus(1); |
|
|
|
// $chat->setUserStatus(1); |
|
|
|
|
|
|
|
|
|
|
|
$chatList = Session::read('openChatBoxes'); |
|
|
|
$chatList = Session::read('openChatBoxes'); |
|
|
|
$chats = self::getAllLatestChats($chatList); |
|
|
|
$chats = $this->getAllLatestChats($chatList); |
|
|
|
$return = [ |
|
|
|
$return = [ |
|
|
|
'user_status' => $this->getUserStatus(), |
|
|
|
'user_status' => $this->getUserStatus(), |
|
|
|
'me' => get_lang('Me'), |
|
|
|
'me' => get_lang('Me'), |
|
|
|
@ -191,33 +189,39 @@ class Chat extends Model |
|
|
|
$fromUserId, |
|
|
|
$fromUserId, |
|
|
|
$toUserId, |
|
|
|
$toUserId, |
|
|
|
$visibleMessages = 1, |
|
|
|
$visibleMessages = 1, |
|
|
|
$previousMessageCount = 5 |
|
|
|
$previousMessageCount = 5, |
|
|
|
|
|
|
|
$orderBy = '' |
|
|
|
) { |
|
|
|
) { |
|
|
|
$toUserId = (int) $toUserId; |
|
|
|
$toUserId = (int) $toUserId; |
|
|
|
$fromUserId = (int) $fromUserId; |
|
|
|
$fromUserId = (int) $fromUserId; |
|
|
|
|
|
|
|
$visibleMessages = (int) $visibleMessages; |
|
|
|
|
|
|
|
$previousMessageCount = (int) $previousMessageCount; |
|
|
|
|
|
|
|
|
|
|
|
$total = $this->getCountMessagesExchangeBetweenUsers($fromUserId, $toUserId); |
|
|
|
$total = $this->getCountMessagesExchangeBetweenUsers($fromUserId, $toUserId); |
|
|
|
|
|
|
|
|
|
|
|
$show = $total - $visibleMessages; |
|
|
|
$show = $total - $visibleMessages; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($show < $previousMessageCount) { |
|
|
|
|
|
|
|
$show = $previousMessageCount; |
|
|
|
|
|
|
|
} |
|
|
|
$from = $show - $previousMessageCount; |
|
|
|
$from = $show - $previousMessageCount; |
|
|
|
|
|
|
|
|
|
|
|
if ($from < 0) { |
|
|
|
if ($from < 0) { |
|
|
|
return []; |
|
|
|
return []; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $this->getMessages($fromUserId, $toUserId, $visibleMessages, $from, $previousMessageCount); |
|
|
|
return $this->getMessages($fromUserId, $toUserId, $from, $previousMessageCount, $orderBy); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @param int $fromUserId |
|
|
|
* @param int $fromUserId |
|
|
|
* @param int $toUserId |
|
|
|
* @param int $toUserId |
|
|
|
* @param int $visibleMessages |
|
|
|
* @param int $start |
|
|
|
* @param int $start |
|
|
|
* @param int $end |
|
|
|
* @param int $end |
|
|
|
* @param string $orderBy |
|
|
|
* |
|
|
|
* |
|
|
|
* @return array |
|
|
|
* @return array |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function getMessages($fromUserId, $toUserId, $visibleMessages, $start, $end, $orderBy = '') |
|
|
|
public function getMessages($fromUserId, $toUserId, $start, $end, $orderBy = '') |
|
|
|
{ |
|
|
|
{ |
|
|
|
$toUserId = (int) $toUserId; |
|
|
|
$toUserId = (int) $toUserId; |
|
|
|
$fromUserId = (int) $fromUserId; |
|
|
|
$fromUserId = (int) $fromUserId; |
|
|
|
@ -227,8 +231,8 @@ class Chat extends Model |
|
|
|
if (empty($toUserId) || empty($fromUserId)) { |
|
|
|
if (empty($toUserId) || empty($fromUserId)) { |
|
|
|
return []; |
|
|
|
return []; |
|
|
|
} |
|
|
|
} |
|
|
|
$orderBy = Database::escape_string($orderBy); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$orderBy = Database::escape_string($orderBy); |
|
|
|
if (empty($orderBy)) { |
|
|
|
if (empty($orderBy)) { |
|
|
|
$orderBy = 'ORDER BY id ASC'; |
|
|
|
$orderBy = 'ORDER BY id ASC'; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -262,17 +266,14 @@ class Chat extends Model |
|
|
|
$userInfo = $users[$fromUserId]; |
|
|
|
$userInfo = $users[$fromUserId]; |
|
|
|
$toUserInfo = $users[$toUserId]; |
|
|
|
$toUserInfo = $users[$toUserId]; |
|
|
|
|
|
|
|
|
|
|
|
$item = [ |
|
|
|
$items[$chat['id']] = [ |
|
|
|
'id' => $chat['id'], |
|
|
|
'id' => $chat['id'], |
|
|
|
's' => '0', |
|
|
|
'message' => Security::remove_XSS($chat['message']), |
|
|
|
'f' => $fromUserId, |
|
|
|
'date' => api_strtotime($chat['sent'], 'UTC'), |
|
|
|
'm' => Security::remove_XSS($chat['message']), |
|
|
|
|
|
|
|
'recd' => $chat['recd'], |
|
|
|
'recd' => $chat['recd'], |
|
|
|
'from_user_info' => $userInfo, |
|
|
|
'from_user_info' => $userInfo, |
|
|
|
'to_user_info' => $toUserInfo, |
|
|
|
'to_user_info' => $toUserInfo, |
|
|
|
'date' => api_strtotime($chat['sent'], 'UTC'), |
|
|
|
|
|
|
|
]; |
|
|
|
]; |
|
|
|
$items[$chat['id']] = $item; |
|
|
|
|
|
|
|
$_SESSION['openChatBoxes'][$fromUserId] = api_strtotime($chat['sent'], 'UTC'); |
|
|
|
$_SESSION['openChatBoxes'][$fromUserId] = api_strtotime($chat['sent'], 'UTC'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -284,8 +285,21 @@ class Chat extends Model |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function heartbeat() |
|
|
|
public function heartbeat() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
$chatHistory = Session::read('chatHistory'); |
|
|
|
$currentUserId = api_get_user_id(); |
|
|
|
$currentUserId = api_get_user_id(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update current chats |
|
|
|
|
|
|
|
if (!empty($chatHistory) && is_array($chatHistory)) { |
|
|
|
|
|
|
|
foreach ($chatHistory as $fromUserId => &$data) { |
|
|
|
|
|
|
|
$userInfo = api_get_user_info($fromUserId, true); |
|
|
|
|
|
|
|
$count = $this->getCountMessagesExchangeBetweenUsers($fromUserId, $currentUserId); |
|
|
|
|
|
|
|
$chatItems = $this->getLatestChat($fromUserId, $currentUserId, 5); |
|
|
|
|
|
|
|
$data['window_user_info'] = $userInfo; |
|
|
|
|
|
|
|
$data['items'] = $chatItems; |
|
|
|
|
|
|
|
$data['total_messages'] = $count; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT * FROM ".$this->table." |
|
|
|
$sql = "SELECT * FROM ".$this->table." |
|
|
|
WHERE |
|
|
|
WHERE |
|
|
|
to_user = '".$currentUserId."' AND recd = 0 |
|
|
|
to_user = '".$currentUserId."' AND recd = 0 |
|
|
|
@ -297,29 +311,6 @@ class Chat extends Model |
|
|
|
$chatList[$chat['from_user']][] = $chat; |
|
|
|
$chatList[$chat['from_user']][] = $chat; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$chatHistory = Session::read('chatHistory'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update current chats |
|
|
|
|
|
|
|
if (!empty($chatHistory) && is_array($chatHistory)) { |
|
|
|
|
|
|
|
foreach ($chatHistory as $fromUserId => &$data) { |
|
|
|
|
|
|
|
$userInfo = api_get_user_info($fromUserId, true); |
|
|
|
|
|
|
|
$count = $this->getCountMessagesExchangeBetweenUsers($fromUserId, $currentUserId); |
|
|
|
|
|
|
|
$chatItems = $this->getLatestChat($fromUserId, $currentUserId, 5); |
|
|
|
|
|
|
|
$item = [ |
|
|
|
|
|
|
|
'window_user_info' => $userInfo, |
|
|
|
|
|
|
|
'items' => $chatItems, |
|
|
|
|
|
|
|
'total_messages' => $count, |
|
|
|
|
|
|
|
'user_info' => [ |
|
|
|
|
|
|
|
'user_name' => $userInfo['complete_name'], |
|
|
|
|
|
|
|
'online' => $userInfo['user_is_online'], |
|
|
|
|
|
|
|
'avatar' => $userInfo['avatar_small'], |
|
|
|
|
|
|
|
'user_id' => $userInfo['user_id'], |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
$data = $item; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($chatList as $fromUserId => $messages) { |
|
|
|
foreach ($chatList as $fromUserId => $messages) { |
|
|
|
$userInfo = api_get_user_info($fromUserId, true); |
|
|
|
$userInfo = api_get_user_info($fromUserId, true); |
|
|
|
$count = $this->getCountMessagesExchangeBetweenUsers($fromUserId, $currentUserId); |
|
|
|
$count = $this->getCountMessagesExchangeBetweenUsers($fromUserId, $currentUserId); |
|
|
|
@ -332,24 +323,14 @@ class Chat extends Model |
|
|
|
$_SESSION['openChatBoxes'][$fromUserId] = api_strtotime($chat['sent'], 'UTC'); |
|
|
|
$_SESSION['openChatBoxes'][$fromUserId] = api_strtotime($chat['sent'], 'UTC'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$item = [ |
|
|
|
$chatHistory[$fromUserId] = [ |
|
|
|
'window_user_info' => api_get_user_info($fromUserId), |
|
|
|
'window_user_info' => $userInfo, |
|
|
|
'items' => $chatItems, |
|
|
|
|
|
|
|
'total_messages' => $count, |
|
|
|
'total_messages' => $count, |
|
|
|
'user_info' => [ |
|
|
|
'items' => $chatItems, |
|
|
|
'user_name' => $userInfo['complete_name'], |
|
|
|
|
|
|
|
'online' => $userInfo['user_is_online'], |
|
|
|
|
|
|
|
'avatar' => $userInfo['avatar_small'], |
|
|
|
|
|
|
|
'user_id' => $userInfo['user_id'], |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
$chatHistory[$fromUserId] = $item; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Session::write('chatHistory', $chatHistory); |
|
|
|
/*if (!empty($_SESSION['openChatBoxes'])) { |
|
|
|
|
|
|
|
|
|
|
|
if (!empty($_SESSION['openChatBoxes'])) { |
|
|
|
|
|
|
|
foreach ($_SESSION['openChatBoxes'] as $userId => $time) { |
|
|
|
foreach ($_SESSION['openChatBoxes'] as $userId => $time) { |
|
|
|
if (!isset($_SESSION['tsChatBoxes'][$userId])) { |
|
|
|
if (!isset($_SESSION['tsChatBoxes'][$userId])) { |
|
|
|
$now = time() - $time; |
|
|
|
$now = time() - $time; |
|
|
|
@ -357,12 +338,6 @@ class Chat extends Model |
|
|
|
$message = sprintf(get_lang('SentAtX'), $time); |
|
|
|
$message = sprintf(get_lang('SentAtX'), $time); |
|
|
|
|
|
|
|
|
|
|
|
if ($now > 180) { |
|
|
|
if ($now > 180) { |
|
|
|
$item = [ |
|
|
|
|
|
|
|
's' => '2', |
|
|
|
|
|
|
|
'f' => $userId, |
|
|
|
|
|
|
|
'm' => $message, |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($chatHistory[$userId])) { |
|
|
|
if (isset($chatHistory[$userId])) { |
|
|
|
$chatHistory[$userId]['items'][] = $item; |
|
|
|
$chatHistory[$userId]['items'][] = $item; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -370,8 +345,7 @@ class Chat extends Model |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}*/ |
|
|
|
|
|
|
|
|
|
|
|
Session::write('chatHistory', $chatHistory); |
|
|
|
Session::write('chatHistory', $chatHistory); |
|
|
|
|
|
|
|
|
|
|
|
$sql = "UPDATE ".$this->table." |
|
|
|
$sql = "UPDATE ".$this->table." |
|
|
|
@ -418,7 +392,7 @@ class Chat extends Model |
|
|
|
$_SESSION['openChatBoxes'][$to_user_id] = api_strtotime($now, 'UTC'); |
|
|
|
$_SESSION['openChatBoxes'][$to_user_id] = api_strtotime($now, 'UTC'); |
|
|
|
|
|
|
|
|
|
|
|
if ($sanitize) { |
|
|
|
if ($sanitize) { |
|
|
|
$messagesan = self::sanitize($message); |
|
|
|
$messagesan = $this->sanitize($message); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$messagesan = $message; |
|
|
|
$messagesan = $message; |
|
|
|
} |
|
|
|
} |
|
|
|
|