Example #1
0
 /**
  * Display p2p chat by user ID.
  *
  * @param int $userId User ID
  * @param int $size Response size
  * @return Response
  */
 public function getChatByUser($userId, $size = 20)
 {
     if ((int) $userId > 0 && !is_null($user = User::find((int) $userId))) {
         if (!is_null($chat = $user->getChatWithUser(Auth::user()->id))) {
             $size = (int) $size > 0 ? (int) $size : 20;
             $unread = array('messages' => array(), 'chats' => array());
             MessageUnread::whereRaw('chat_id = ? and user_id = ?', array($chat->id, Auth::user()->id))->get()->each(function ($message) use(&$unread) {
                 $unread['messages'][] = $message->message_id;
                 $unread['chats'][] = $message->chat_id;
             });
             $cleared = ChatCleared::whereRaw('chat_id = ? and user_id = ?', array($chat->id, Auth::user()->id))->get()->first();
             $query = 'select a.* from messages a where a.chat_id = ' . $chat->id;
             if (count($unread['messages']) > 0) {
                 $query .= ' and a.id not in (' . implode(',', $unread['messages']) . ')';
             }
             if ($cleared) {
                 $query .= ' and a.id > ' . $cleared->message_id;
             }
             $query .= ' and id not in (select b.message_id from messages_removed b where b.chat_id = chat_id)' . ' order by a.timestamp desc limit ' . $size;
             $messages = DB::select($query);
             if (count($unread['messages']) > 0) {
                 $unreadMessages = DB::table('messages')->whereRaw('chat_id = ? and id in (' . implode(',', $unread['messages']) . ')' . ' and id not in (select b.message_id from messages_removed b where b.chat_id = chat_id)', array($chat->id))->orderBy('timestamp', 'desc');
                 $messages = array_merge($messages, $unreadMessages->get());
             }
             $result = array();
             if (count($messages) > 0) {
                 $result = array('chat_id' => $chat->id, 'topic' => $chat->topic, 'timestamp' => $chat->getTimestamp(), 'users' => $chat->getUsersArray(), 'messages' => array());
                 foreach ($messages as $message) {
                     $data = (new Message((array) $message))->getAsArray();
                     if (in_array($message->chat_id, $unread['chats'])) {
                         $data['unread'] = true;
                     }
                     $result['messages'][] = $data;
                 }
             }
             return $this->respond($result);
         } else {
             return $this->respondWithCustomStatusCode('Chat doesnt exist', 404, 1100);
         }
     } else {
         return $this->respondWithError('User doesn\'t exist');
     }
 }
Example #2
0
 /**
  * Clear the chat.
  *
  * @param $chatId Chat ID
  * @return Response
  */
 public function clearChatHistory($chatId)
 {
     if (($chat = Chat::find($chatId)) && $chat->hasMember(Auth::user()->id)) {
         $lastMessage = Message::where('chat_id', $chat->id)->orderBy('id', 'desc')->take(1)->get()->first();
         ChatCleared::whereRaw('chat_id = ? and user_id = ?', array($chat->id, Auth::user()->id))->delete();
         $cleared = new ChatCleared();
         $cleared->chat_id = $chat->id;
         $cleared->user_id = Auth::user()->id;
         $cleared->message_id = $lastMessage->id;
         $cleared->save();
         return $this->respondNoContent();
     } else {
         return $this->respondWithError('Chat isn\'t found');
     }
 }