/**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $faker = Faker\Factory::create();
     $faker = Faker\Factory::create();
     $slugify = Slugify::create();
     $users = User::all();
     // Create message threads for all users
     foreach ($users as $user) {
         for ($x = rand(2, 4); $x < 5; $x++) {
             $thread = Thread::create(['subject' => $faker->sentence()]);
             // Message
             Message::create(['thread_id' => $thread->id, 'user_id' => $user->id, 'body' => implode($faker->paragraphs())]);
             // Sender
             Participant::create(['thread_id' => $thread->id, 'user_id' => $user->id]);
             // Recipients
             if (Input::has('recipients')) {
                 for ($i = rand(3, 4); $i < 5; $i++) {
                     $participant = array_rand($users->toArray());
                     $thread->addParticipants($users[$participant]->id);
                 }
             }
             // Create thread replies
             for ($k = rand(2, 5); $k < 5; $k++) {
                 $thread->activateAllParticipants();
                 $randUser = array_rand($users->toArray());
                 // Message
                 Message::create(['thread_id' => $thread->id, 'user_id' => $users[$randUser]->id, 'body' => implode($faker->paragraphs())]);
                 // Add replier as a participant
                 $participant = Participant::firstOrCreate(['thread_id' => $thread->id, 'user_id' => $users[$randUser]->id]);
                 //$participant->last_read = new Carbon;
                 $participant->save();
                 // Recipients
                 if (Input::has('recipients')) {
                     $thread->addParticipants($user->id);
                 }
             }
             if ($thread->getLatestMessageAttribute()->user->id == $user->id) {
                 $thread->markAsRead($user->id);
             }
         }
     }
 }
Exemplo n.º 2
0
 public function update(Request $request, $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' => Sentinel::getUser()->id, 'body' => $request->message]);
     // Add replier as a participant
     $participant = Participant::firstOrCreate(['thread_id' => $thread->id, 'user_id' => Sentinel::getUser()->id]);
     $participant->last_read = new Carbon();
     $participant->save();
     // Recipients
     if ($request->has('recipients')) {
         $thread->addParticipants($request->recipients);
     }
     return redirect('messages/' . $id);
 }
Exemplo n.º 3
0
 /**
  * Adds a new message to a current thread.
  *
  * @param $id
  * @return mixed
  */
 public function update($id)
 {
     try {
         //$thread = Thread::findOrFail($id);
         $thread = Thread::between([Auth::id(), $id])->first();
     } 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 $thread->messages()->where('created_at', '>', Input::get('last'))->with('user')->get();
 }
Exemplo n.º 4
0
 /**
  * Adds users to this thread
  *
  * @param array $participants list of all participants
  * @return void
  */
 public function addParticipants(array $participants)
 {
     if (count($participants)) {
         foreach ($participants as $user_id) {
             Participant::firstOrCreate(['user_id' => $user_id, 'thread_id' => $this->id]);
         }
     }
 }
Exemplo n.º 5
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 = 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'));
     }
     $this->oooPushIt($message);
     return redirect('messages/' . $id);
 }
Exemplo n.º 6
0
 public function updateThread($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'));
     }
     $userId = Auth::user()->id;
     $users = \App\User::whereNotIn('id', $thread->participantsUserIds($userId))->get();
     return view('messages.show', ['page' => 'message', 'thread' => $thread, 'users' => $users]);
 }
Exemplo n.º 7
0
    public function update($id)
    {
        $thread = Thread::findOrFail($id);

        $thread->activateAllParticipants();

        // Message
        Message::create(
            [
                'thread_id' => $thread->id,
                'user_id'   => $this->user->id,
                'body'      => Input::get('message'),
            ]
        );

        // Add replier as a participant
        $participant = Participant::firstOrCreate(
            [
                'thread_id' => $thread->id,
                'user_id'   => $this->user->id
            ]
        );
        $participant->last_read = new Carbon;
        $participant->save();
// Recipients
//if (Input::has('recipients')) {
//$thread->addParticipants(Input::get('recipients'));
//}
return Redirect::to('messages/' . $id);
    }
Exemplo n.º 8
0
 /**
  * Adds a new message to a current thread
  *
  * @param $id
  * @return mixed
  */
 public function update(Request $request, $id)
 {
     $body = $request->input('message');
     $validator = Validator::make(['message' => $body], ['message' => 'required|min:20|max:4500'], ['message.required' => 'A message is required.', 'message.min' => 'Messages must be at least 20 characters long.', 'message.max' => 'Messages can be up to 4500 characters long.']);
     if ($validator->passes()) {
         try {
             $thread = Thread::findOrFail($id);
             $thread->getParticipantFromUser(Auth::id());
         } catch (ModelNotFoundException $e) {
             Flash::error('The requested conversation does not exist.');
             return redirect('conversations');
         }
         $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();
         foreach ($thread->participants as $threadParticipant) {
             if ($threadParticipant->user_id != Auth::id()) {
                 $threadParticipant->last_read = null;
                 $threadParticipant->save();
             }
         }
         return redirect('conversations/' . $id . ($thread->messagesPaginated->hasPages() ? '?page=' . $thread->lastPage : ''));
     } else {
         return redirect('conversations/' . $id)->withErrors($validator->messages())->withInput();
     }
 }
Exemplo n.º 9
0
 /**
  * Update a thread
  * @param $id
  * @param MessagesRequest $request
  * @param $unique_id
  * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function update($id, MessagesRequest $request, $unique_id)
 {
     $input = $request;
     try {
         $thread = Thread::where('innovation_id', '=', $id)->where('unique_id', '=', $unique_id)->first();
     } catch (ModelNotFoundException $e) {
         Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
         return redirect('messages');
     }
     $thread->activateAllParticipants();
     // Message
     $message = Message::create(['thread_id' => $thread->id, 'user_id' => Auth::user()->id, 'body' => htmlentities($input->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'));
     }
     $this->oooPushIt($message);
     return response()->json(['id' => $message->id]);
 }
 /**
  * Adds a new message to a current thread
  *
  * Example URL: PUT /api/v1/messages/1?api_key=30ce6864e2589b01bc002b03aa6a7923
  *
  * @param $id
  * @return mixed
  */
 public function update($id)
 {
     $userId = $this->user->id;
     try {
         $thread = Thread::findOrFail($id);
     } catch (ModelNotFoundException $e) {
         return $this->respondNotFound('Sorry, the message thread was not found.');
     }
     // @todo: run validation
     $thread->activateAllParticipants();
     // Message
     Message::create(['thread_id' => $thread->id, 'user_id' => $userId, 'body' => Input::get('message')]);
     // Add replier as a participant
     $participant = Participant::firstOrCreate(['thread_id' => $thread->id, 'user_id' => $userId]);
     $participant->last_read = new Carbon();
     $participant->save();
     // Recipients
     if (Input::has('recipients')) {
         $thread->addParticipants(Input::get('recipients'));
     }
     return $this->respondWithSaved(['id' => $thread->id]);
 }