public function hasMember($userId) { if (isset($this->id) && (int) $this->id > 0) { return ChatMember::whereRaw('chat_id = ? and user_id = ?', array($this->id, $userId))->get()->count() > 0; } return false; }
/** * Display a listing of messages. * * @param int $chatId Chat ID * @param int|null $lastShownMessageId Last shown message ID * @param int $size Reponse size * @return Response */ public function getList($chatId, $lastShownMessageId = null, $size = 50) { if ((int) $chatId > 0) { if (!is_null($chat = Chat::find((int) $chatId)) && !ChatMember::whereRaw('chat_id = ? and user_id = ?', array($chatId, Auth::user()->id))->get()->isEmpty()) { $result = array(); $size = (int) $size > 0 ? (int) $size : 50; $unread = array(); if (is_null($lastShownMessageId)) { MessageUnread::whereRaw('chat_id = ? and user_id = ? and message_id not in (select b.message_id from messages_removed b where b.chat_id = chat_id and user_id = ' . Auth::user()->id . ')', array($chat->id, Auth::user()->id))->get()->each(function ($message) use(&$unread) { $unread[] = $message->message_id; }); } $cleared = ChatCleared::whereRaw('chat_id = ? and user_id = ?', array($chat->id, Auth::user()->id))->orderBy('message_id', 'desc')->get()->first(); $query = 'select a.* from messages a where a.chat_id = ' . $chat->id . ' and a.id not in (select b.message_id from messages_removed b where b.chat_id = a.chat_id and b.user_id = ' . Auth::user()->id . ')'; if (count($unread) > 0) { $query .= ' and a.id not in (' . implode(',', $unread) . ')'; } if ($cleared) { $query .= ' and a.id > ' . $cleared->message_id; } if (!is_null($lastShownMessageId) && (int) $lastShownMessageId > 0) { $query .= ' and a.id < ' . $lastShownMessageId; } $query .= ' order by a.timestamp desc limit ' . $size; $messages = DB::select($query); if (count($unread) > 0) { $unreadMessages = DB::table('messages')->whereRaw('chat_id = ? and id in (' . implode(',', $unread) . ')', array($chat->id))->orderBy('timestamp', 'desc'); $messages = array_merge($unreadMessages->get(), $messages); } foreach ($messages as $message) { $data = (new Message((array) $message))->getAsArray(); if (in_array($message->id, $unread)) { $data['unread'] = true; } $result['messages'][] = $data; } return $this->respond($result); } else { return $this->respondWithError('Chat doesn\'t exist'); } } else { return $this->respondWithError('Chat doesn\'t exist'); } }
/** * Except a new user to the chat * * @param int|null $chatId Chat ID * @return Response */ public function exceptUser($chatId = null, $userId = null) { if ((int) $chatId > 0 && !is_null($chat = Chat::find((int) $chatId)) && $chat->hasMember(Auth::user()->id)) { if ($chat->isGroup()) { if ((int) $userId > 0 && !is_null($user = User::find((int) $userId)) && ChatMember::whereRaw('chat_id = ? and user_id = ?', array($chatId, $user->id))->get()->count() == 1) { if ($chat->owner_id != $user->id) { ChatMember::whereRaw('chat_id = ? and user_id = ?', array($chatId, $user->id))->delete(); $this->setChatIsUpdated((int) $chatId); return $this->respond(array('chat_id' => $chatId, 'owner_id' => $chat->owner_id, 'topic' => $chat->topic, 'timestamp' => $chat->getTimestamp(), 'users' => $chat->getUsersArray())); } else { return $this->respondWithError('Can\'t exclude chat owner'); } } else { return $this->respondWithError('User doesn\'t exist'); } } else { return $this->respondWithError('Can\'t exclude user from private chat'); } } else { return $this->respondWithError('Chat doesn\'t exist'); } }