public function showAll()
 {
     $user = Confide::user();
     $conversations = Conversation::where('sender', '=', $user->id)->orWhere('receiver', '=', $user->id)->orderBy('created_at', 'desc')->get();
     foreach ($conversations as $conversation) {
         $conversation->sender = User::find($conversation->sender);
         $conversation->receiver = User::find($conversation->receiver);
     }
     return View::make('home/conversation/show-all', compact('user', 'conversations'));
 }
 /**
  * Store a newly created message in storage.
  *
  * @return Response
  */
 public function store()
 {
     $rules = array('body' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Response::json(['success' => false, 'result' => $validator->messages()]);
     }
     $conversation = Conversation::where('name', Input::get('conversation'))->first();
     $params = array('conversation_id' => $conversation->id, 'body' => Input::get('body'), 'user_id' => Input::get('user_id'), 'created_at' => new DateTime());
     $message = Message::create($params);
     // Create Message Notifications
     $messages_notifications = array();
     foreach ($conversation->users()->get() as $user) {
         array_push($messages_notifications, new MessageNotification(array('user_id' => $user->id, 'conversation_id' => $conversation->id, 'read' => false)));
     }
     $message->messages_notifications()->saveMany($messages_notifications);
     // Publish Data To Redis
     $data = array('room' => Input::get('conversation'), 'message' => array('body' => Str::words($message->body, 5), 'user_id' => Input::get('user_id')));
     Event::fire(ChatMessagesEventHandler::EVENT, array(json_encode($data)));
     return Response::json(['success' => true, 'result' => $message]);
 }
Ejemplo n.º 3
0
 public function get_conversation($conHash)
 {
     $ajaxRequest = false;
     $thread = false;
     $numbered = false;
     if (Request::ajax()) {
         $ajaxRequest = true;
     }
     $userid = Sentry::user()->id;
     /*There should be some protection about sniffing other messages*/
     /*Let check if this conversation exist*/
     $conThread = Conversation::where('id', '=', $conHash)->first();
     if (!$conThread) {
         return Redirect::to_action('messages@index');
     } elseif ($conThread->user_id != $userid && $conThread->receiver_id != $userid) {
         /*now we are looking if this conversation is ours*/
         return Redirect::to_action('messages@index');
     }
     $conversation = Message::with('author')->where('conversation_id', '=', $conHash)->order_by('date', 'asc')->get();
     if (!$ajaxRequest) {
         //This gets conversation that user started or we started..
         $conversation2 = Conversation::with('Author')->where('receiver_id', '=', Sentry::user()->id)->or_where(function ($query) {
             $query->where('user_id', '=', Sentry::user()->id);
         })->order_by('date', 'desc')->get();
         $view = View::make('messages.index');
         $view->cons = $conversation2;
         $view->ifgetverb = true;
         $view->title = "Mesajlar";
         $view->conversation = $conversation;
         $view->coninfo = $conThread;
         return $view;
     } else {
         $view = View::make('messages.ajaxrequest');
         $view->conversation = $conversation;
         $view->coninfo = $conThread;
         return $view;
     }
 }
 public function addUserToConversation()
 {
     $user = Auth::user();
     $input = Input::all();
     $conversationType = $input['type'];
     $conversationID = $input['conversationID'];
     $newConversationUsers = json_decode($input['userlist'], true);
     //check if a single conversation already exists
     if ($conversationType && $conversationType == 'single') {
         $otherUser = $newConversationUsers[0]['id'] == $user->id ? $newConversationUsers[1]['id'] : $newConversationUsers[0]['id'];
         $singleConversation = DB::select(DB::raw("select distinct *\n\t\t\t\tfrom conversation_user u\n\t\t\t\tjoin conversations c on c.id = u.conversation_id\n\t\t\t\twhere c.type = 'single' and (u.user_id = :currentUser or u.user_id = :otherUser)\n\t\t\t\tgroup by u.conversation_id\n\t\t\t\thaving count(u.conversation_id) = 2"), array('currentUser' => $user->id, 'otherUser' => $otherUser));
         if ($singleConversation) {
             return $singleConversation[0]->conversation_id;
         }
     }
     //create a new conversation if input doesn't have conversation id
     if (!$conversationID) {
         $conversation = new Conversation();
         $conversation->name = "Untitled Conversation";
         $conversation->lastest_message = "The room has been created !";
         $conversation->leader_id = $user['id'];
         $conversation->type = $conversationType;
         $conversation->save();
         $conversationID = $conversation->id;
     } else {
         Conversation::where('id', '=', $conversationID)->update(['type' => $conversationType]);
     }
     $currentConversationUsers = ConversationUser::where('conversation_id', '=', $conversationID)->get();
     foreach (json_decode($input['userlist']) as $key => $value) {
         # code...
         //print_r($value);
         $duplicateRecord = ConversationUser::where('user_id', '=', $value->id)->where('conversation_id', '=', $conversationID)->get();
         if (count($duplicateRecord) <= 0) {
             $conversationUser = new ConversationUser();
             $conversationUser->conversation_id = $conversationID;
             $conversationUser->user_id = $value->id;
             $conversationUser->save();
             // add user notification status
             $notificationStatus = new GroupNotificationStatus();
             $notificationStatus->conversation_id = $conversationID;
             $notificationStatus->user_id = $value->id;
             $notificationStatus->save();
         }
     }
     foreach ($currentConversationUsers as $key => $value) {
         $result = array_search(intval($value->user_id), array_column($newConversationUsers, 'id'));
         if (!$result && intval($value->user_id) != intval($user->id)) {
             ConversationUser::where('user_id', '=', $value->user_id)->where('conversation_id', '=', $conversationID)->delete();
         }
     }
     return $conversationID;
 }
Ejemplo n.º 5
0
 public function store()
 {
     if (FEUsersHelper::isLogged()) {
         $current_user_id = Session::get('user')['id'];
         $r_user_id = Input::get('r_user_id');
         $conversations = Conversation::where(function ($query) use($current_user_id, $r_user_id) {
             $query->where('user1_id', '=', $current_user_id)->where('user2_id', '=', $r_user_id);
         })->orWhere(function ($query) use($current_user_id, $r_user_id) {
             $query->where('user1_id', '=', $r_user_id)->where('user2_id', '=', $current_user_id);
         })->orderBy('updated_at', 'DESC')->get();
         $message = new Message();
         if ($conversations->count() > 0) {
             $conversation = $conversations->first();
             $message->content = Input::get('content');
             $message->s_user_id = $current_user_id;
             $message->conversation_id = $conversation->id;
             $message->save();
             $conversation->updated_at = date('Y-m-d H:m:s');
             $conversation->save();
             return Redirect::to('message/' . $conversation->id);
         } else {
             $conversation = new Conversation();
             $conversation->user1_id = $current_user_id;
             $conversation->user2_id = Input::get('r_user_id');
             $conversation->save();
             $message->content = Input::get('content');
             $message->s_user_id = $current_user_id;
             $message->conversation_id = $conversation->id;
             $message->save();
             return Redirect::to('message/' . $conversation->id);
         }
     }
     return Redirect::to('/');
 }
 public function getByName($name)
 {
     return $this->model->where('name', $name)->firstOrFail();
 }