예제 #1
0
 public function sendMessage()
 {
     try {
         $rules = array('body' => 'required', 'conversation' => 'required', 'user_id' => 'required');
         $validator = Validator::make(Input::all(), $rules);
         if ($validator->fails()) {
             return Response::json(['success' => false, 'result' => $validator->messages()]);
         }
         $thread = Thread::findOrFail(Input::get('conversation'));
     } catch (ModelNotFoundException $e) {
         Session::flash('error_message', 'The thread with ID: ' . Input::get('conversation') . ' was not found.');
         return redirect(App::getLocale() . '/chat');
     }
     $thread->activateAllParticipants();
     $params = array('thread_id' => $thread->id, 'user_id' => Input::get('user_id'), 'body' => Input::get('body'), 'created_at' => new DateTime());
     // Message
     $message = Message::create($params);
     $data = array('room' => Input::get('conversation'), 'message' => array('body' => Str::words($message->body, 60), 'user_id' => Input::get('user_id')));
     // Add replier as a participant
     $participant = Participant::firstOrCreate(['thread_id' => $thread->id, 'user_id' => Auth::user()->id]);
     $participant->last_read = new Carbon();
     $participant->save();
     // Recipients
     if (Input::has('recipients')) {
         $thread->addParticipants(Input::get('recipients'));
     }
     event(new ChatMessageCreated($data));
     return Response::json(['success' => true, 'result' => $message]);
 }
예제 #2
0
 /**
  * Adds a new message to a current thread
  *
  * @param $id
  * @return mixed
  */
 public function update($id)
 {
     try {
         $thread = Thread::findOrFail($id);
     } catch (ModelNotFoundException $e) {
         Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
         return redirect('messages');
     }
     $thread->activateAllParticipants();
     // Message
     Message::create(['thread_id' => $thread->id, 'user_id' => Auth::id(), 'body' => Input::get('message')]);
     // Add replier as a participant
     $participant = Participant::firstOrCreate(['thread_id' => $thread->id, 'user_id' => Auth::user()->id]);
     $participant->last_read = new Carbon();
     $participant->save();
     // Recipients
     if (Input::has('recipients')) {
         $thread->addParticipants(Input::get('recipients'));
     }
     return redirect('messages/' . $id);
 }