Created
November 9, 2016 18:25
-
-
Save aldoanizio/47cb88c2145688e22e4480b2186a4d86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ChatController extends ControllerBase | |
{ | |
public function indexAction() | |
{ | |
// Logged User | |
$userId = $this->session->get('user')['user_id']; | |
// Load conversations | |
$conversations = []; | |
$convQuery = Message::query() | |
->columns('MAX(message_id) as message_id, MAX(createdAt) as createdAt') | |
->where('(from_user_id = :from_user_id: OR to_user_id = :to_user_id:) AND from_user_id IN (SELECT user_id FROM User) AND to_user_id IN (SELECT user_id FROM User)') | |
->groupBy('conversation_id') | |
->bind(['from_user_id' => $userId, 'to_user_id' => $userId]) | |
->execute(); | |
foreach ($convQuery as $res) { | |
$message = Message::findFirst($res->message_id); | |
$convUser = $message->from_user_id == $userId ? User::findFirst($message->to_user_id): User::findFirst($message->from_user_id); | |
$conversations[] = [ | |
'message_id' => $res->message_id, | |
'content' => $message->content, | |
'conversation_id' => $message->conversation_id, | |
'createdAt' => $message->createdAt, | |
'user' => $convUser, | |
]; | |
} | |
// Get conversations users | |
$conversationsUsersIds = array_map(function($value){ | |
return $value['user']->user_id; | |
}, $conversations); | |
// New conversation | |
$newChatId = $this->request->getPost('new_chat_id'); | |
if ($newChatId && in_array($newChatId, $conversationsUsersIds) == false) { | |
$newChatUser = User::findFirst($newChatId); | |
$this->view->newChat = [ | |
'conversation_id' => Message::buildConversationId($userId, $newChatUser->user_id), | |
'user' => $newChatUser, | |
]; | |
} | |
$this->view->user = $this->session->get('user'); | |
usort($conversations, function($a, $b) { | |
return $b['message_id'] - $a['message_id']; | |
}); | |
$this->view->conversations = $conversations; | |
$this->view->setMainView('pages/chat'); | |
} | |
/** | |
* Carregar mensagens de acordo com a conversa | |
* | |
* @return json | |
*/ | |
public function messagesAction() | |
{ | |
$conversationId = $this->request->getQuery('conversation_id'); | |
if ($this->isValidConversation($conversationId)) { | |
// Order | |
$orderBy = $this->request->getQuery('order', null, 'message_id DESC'); | |
// Limit | |
$limit = $this->request->getQuery('limit', null, 10); | |
// Binds & Types | |
$binds = ['conversationId' => $conversationId]; | |
// Start Query | |
$query = 'conversation_id = :conversationId:'; | |
// Filter Id Lower Than | |
if ($this->request->getQuery('filter_id_lt')) { | |
$query .= ' AND message_id < :filter_id_lt:'; | |
$binds['filter_id_lt'] = $this->request->getQuery('filter_id_lt'); | |
} | |
// Filter Id Higher Than | |
if ($this->request->getQuery('filter_id_gt')) { | |
$query .= ' AND message_id > :filter_id_gt:'; | |
$binds['filter_id_gt'] = $this->request->getQuery('filter_id_gt'); | |
} | |
// Load messages | |
$messages = Message::query()->where($query, $binds)->orderBy($orderBy)->limit($limit)->execute()->toArray(); | |
$json['messages'] = []; | |
foreach ($messages as $msg) { | |
$json['messages'][] = array( | |
'message_id' => $msg['message_id'], | |
'createdAt' => $msg['createdAt'], | |
'conversation_id' => $msg['conversation_id'], | |
'from_user' => \User::findFirst($msg['from_user_id']), | |
'to_user' => \User::findFirst($msg['to_user_id']), | |
'content' => $msg['content'], | |
); | |
} | |
$json['success'] = true; | |
} else { | |
$json['success'] = false; | |
} | |
$this->response->setStatusCode(200, "OK"); | |
$this->response->setHeader("Content-Type", "application/json"); | |
$this->response->setContent(json_encode($json)); | |
$this->response->send(); | |
} | |
/** | |
* Carregar mensagens de acordo com a conversa | |
* | |
* @return json | |
*/ | |
public function sendMessageAction() | |
{ | |
// Logged User | |
$userId = $this->session->get('user')['user_id']; | |
$conversationId = $this->request->getPost('conversation_id'); | |
$toUserId = $this->request->getPost('to_user_id'); | |
if (empty($this->request->getPost('content')) == false && $this->isValidConversation($conversationId, $toUserId)) { | |
// Get conversation | |
$conversation = Message::findByConversation_id($conversationId)->getFirst(); | |
$message = new Message(); | |
$message->from_user_id = $userId; | |
$message->content = $this->request->getPost('content'); | |
if (empty($conversation) == false) { | |
$message->conversation_id = $conversation->conversation_id; | |
$message->to_user_id = $conversation->from_user_id == $userId ? $conversation->to_user_id : $conversation->from_user_id; | |
} else { | |
$message->to_user_id = $toUserId; | |
$message->conversation_id = Message::buildConversationId($userId, $toUserId); | |
} | |
$message->save(); | |
$json['success'] = true; | |
$json['message_id'] = $message->getWriteConnection()->lastInsertId(); | |
} else { | |
$json['success'] = false; | |
} | |
$this->response->setStatusCode(200, "OK"); | |
$this->response->setHeader("Content-Type", "application/json"); | |
$this->response->setContent(json_encode($json)); | |
$this->response->send(); | |
} | |
/** | |
* Check if conversation belongs to user | |
* | |
* @param integer $conversationId Conversation Id | |
* @param integer $conversationId Conversation Id | |
* @return boolean | |
*/ | |
private function isValidConversation($conversationId, $toUserId = null) | |
{ | |
// Logged User | |
$userId = $this->session->get('user')['user_id']; | |
// Get conversation | |
$conversation = Message::findByConversation_id($conversationId)->getFirst(); | |
if (empty($conversation) == false) { | |
return in_array($userId, [$conversation->from_user_id, $conversation->to_user_id]); | |
} | |
// Validate new conversation | |
if (empty($conversation) && $toUserId) { | |
return Message::buildConversationId($userId, $toUserId) == $conversationId; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment