public function threadsWithNewMessagesForPoll() { $updatedThreadsIds = $this->threadsWithNewMessages(); $updatedThreads = Thread::whereIn('id', $updatedThreadsIds)->get(); $updatedThreads = $updatedThreads->filter(function ($thread) { return strtotime($thread->updated_at) >= strtotime($thread->getParticipantFromUser($this->id)->last_poll); }); foreach ($updatedThreads as $thread) { foreach ($thread->messages as $message) { $message->timeDiffForHumen = $message->created_at->diffForHumans(); $message->senderDetails = $message->user->first_name . " " . $message->user->last_name; $message->layout = $message->user_id == $this->id ? "right" : "left"; } } return $updatedThreads; }
public function leaveThread($id) { try { $thread = Thread::findOrFail($id); } catch (ModelNotFoundException $e) { return false; } Thread::find($id)->removeParticipant($this->id); return true; }
/** * 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('message'); } $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('message/' . $id); }