Ejemplo n.º 1
0
 /**
  * Display a listing of chats.
  *
  * @param int|null $lastChatId Last chat ID
  * @param int $size Response size
  * @return Response
  */
 public function getList($lastChatId = null, $size = 20)
 {
     $list = array();
     $ids = array();
     ChatMember::where('user_id', Auth::user()->id)->get()->each(function ($member) use(&$ids) {
         $ids[] = $member->chat_id;
     });
     if (count($ids) > 0) {
         $size = (int) $size > 0 ? (int) $size : 20;
         $unread = array();
         MessageUnread::whereRaw('user_id = ? and message_id not in (select b.message_id from messages_removed b where b.chat_id = chat_id)', array(Auth::user()->id))->get()->each(function ($message) use(&$unread) {
             $unread[] = $message->chat_id;
         });
         $query = 'select * from chats a where a.id in (' . implode(', ', $ids) . ') ' . (($lastChatId = (int) $lastChatId) > 0 ? 'and a.id < ' . $lastChatId . ' ' : '') . 'and (select count(b.id) from messages b where b.chat_id=a.id ' . 'and b.id not in (select c.message_id from messages_removed c where c.chat_id = a.id) ' . 'and b.id > coalesce((select d.message_id from chats_cleared d where d.user_id = ' . Auth::user()->id . ' and d.chat_id = a.id), 0)) > 0 ' . 'order by a.timestamp desc limit ' . $size;
         $result = DB::select($query);
         foreach ($result as $array) {
             $chat = new Chat((array) $array);
             $data = array('chat_id' => $chat->id, 'owner_id' => $chat->owner_id, 'topic' => $chat->topic, 'timestamp' => $chat->getTimestamp(), 'users' => $chat->getUsersArray());
             if (in_array($chat->id, $unread)) {
                 $data['unread'] = true;
             }
             if (!is_null($lastMessage = $chat->getLastMessage())) {
                 $data['last_message'] = $lastMessage->getAsArray();
             }
             $list[] = $data;
         }
     }
     return $this->respond($list);
 }
Ejemplo n.º 2
0
 public function getLastMessage()
 {
     $conn = Connection::getInstance("read");
     if ($chats = $this->getChatsID()) {
         $max = 0;
         foreach ($chats as $chat_id) {
             $chat = new Chat($chat_id);
             if ($m = $chat->getLastMessage()) {
                 //print_r($m);
                 $tstamp = strtotime($m['time']);
                 if ($tstamp > $max) {
                     //echo $tstamp . " ";
                     $max = $tstamp;
                     $latest_message = $m;
                 }
             }
         }
         return $latest_message;
     } else {
         return false;
     }
 }
Ejemplo n.º 3
0
                foreach ($messages as $id) {
                    $message = new Message($id);
                    $sender = new User($message->getSenderID());
                    $time = strtotime($message->getTime());
                    $new_message .= "\n                        <div id=\"{$id}\" class=\"animated fadeIn chatbox-user right\">\n                            <a href=\"javascript:;\" class=\"chat-avatar pull-right\"> \n                                <img src=\"img/faceless.jpg\" class=\"img-circle\" title=\"user name\" alt=\"\">\n                            </a>\n\n                            <div class=\"message\">\n                                <div class=\"panel\">\n                                    <div class=\"panel-heading\">\n                                        {$sender->getFullName()}\n                                    </div>\n\n                                    <div class=\"panel-body\">\n                                        <p>{$message->getMessage()}</p>\n                                    </div>\n                                </div>\n\n                                <small class=\"chat-time\">\n                                <i class=\"ti-time mr5\"></i>\n                                <span data-livestamp=\"{$time}\"></span>\n                                <i class=\"ti-check text-success\"></i>\n                                </small>\n\n                            </div>\n                        </div>";
                }
                echo $new_message;
            }
        } elseif (Tools::valuePost("action") == "send_chat") {
            /*SEND NEW CHAT MESSAGE TO THE SERVER*/
            if (!empty(Tools::valuePost("message"))) {
                echo $chat->sendMessage($user->getID(), Tools::valuePost("message"));
            }
        }
    } elseif (Tools::valuePost("action") == "notification") {
        /*CHECK NEW CHAT MESSAGES FROM THE SERVER*/
        $chats = $user->checkMessages();
        if ($chats) {
            $count = count($chats);
            foreach ($chats as $id) {
                $chat = new Chat($id);
                $message_row = $chat->getLastMessage();
                $sender = new User($message_row['from_user_id']);
                $time = strtotime($message_row['time']);
                $end_product[] = array("count" => $count, "id" => $id, "notifications" => "New message", "name" => $sender->getFullname(), "img" => $sender->getProfilePictureURL(), "timestamp" => $time, "message" => $message_row['message'], "url" => "message.php?chat_id={$id}");
            }
            $end_product = json_encode($end_product);
            echo $end_product;
        }
    }
}