/** * Returns all threads with new messages * * @return array */ public function threadsWithNewMessages() { $threadsWithNewMessages = []; $participants = Participant::where('user_id', $this->id)->lists('last_read', 'thread_id'); /** * @todo: see if we can fix this more in the future. * Illuminate\Foundation is not available through composer, only in laravel/framework which * I don't want to include as a dependency for this package...it's overkill. So let's * exclude this check in the testing environment. */ if ($_ENV['APP_ENV'] == 'testing' || !str_contains(\Illuminate\Foundation\Application::VERSION, '5.0')) { $participants = $participants->all(); } if ($participants) { $threads = Thread::whereIn('id', array_keys($participants))->get(); foreach ($threads as $thread) { if ($thread->updated_at > $participants[$thread->id]) { $threadsWithNewMessages[] = $thread->id; } } } return $threadsWithNewMessages; }
public function createRoom() { $input = Input::all(); $thread = Thread::create(['subject' => $input['subject']]); // Message $message = Message::create(['thread_id' => $thread->id, 'user_id' => Auth::user()->id, 'body' => $input['message']]); // Sender Participant::create(['thread_id' => $thread->id, 'user_id' => Auth::user()->id, 'last_read' => new Carbon()]); // Recipients if (Input::has('recipients')) { $thread->addParticipants($input['recipients']); } $data = array('room' => $thread->id, 'message' => array('body' => Str::words($message->body, 60), 'user_id' => Auth::user()->id)); event(new ChatRoomCreated($data)); return redirect(App::getLocale() . '/chat'); }
/** * 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); }