/** * 存储提醒消息 */ public function store() { //推送内容 $subject = $this->request->get('subject'); $thread = Thread::create(['subject' => $subject]); //生成message $message = Message::create(['thread_id' => $thread->id, 'user_id' => $this->user()->id, 'body' => $this->request->get('message')]); // Sender Participant::create(['thread_id' => $thread->id, 'user_id' => $this->user()->id]); // Recipients if ($this->request->has('recipients')) { $thread->addParticipants($this->request->get('recipients')); } return return_rest('1', '', '消息添加成功'); }
/** * 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); } } } }
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); }
/** * 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(); }
/** * 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); }
/** * Delete a message */ public function delete($id) { try { $message = Message::findOrFail($id); } catch (ModelNotFoundException $e) { Session::flash('error_message', 'The message with ID: ' . $id . ' was not found.'); return redirect('messages'); } if (Auth::user()->id == $message->user_id || Auth::user()->isAdmin()) { // get thread ID and check if this was the last remaining message in the thread! // then we have to delete the thread as well!!! $thread_id = $message->thread_id; $message->delete(); Session::flash('error_message', 'Message deleted.'); $msgs = Message::where('thread_id', $thread_id)->get(); if (count($msgs) == 0) { $thread = Thread::find($thread_id)->delete(); return redirect('messages'); } } else { Session::flash('error_message', 'You can only delete your own messages.'); return redirect('messages'); } return redirect()->back(); }
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]); }
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); }
/** * 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(); } }
/** * send message via internal messenger * @param string $subject * @param string $message * @param string $recipient user_id * @param bool $email send email or not */ function sendInternalMessage($subject, $message, $recipient_id, $email = true) { $thread = Thread::create(['subject' => $subject]); // Message $message = Message::create(['thread_id' => $thread->id, 'user_id' => Auth::user()->id, 'body' => $message]); // Sender Participant::create(['thread_id' => $thread->id, 'user_id' => Auth::user()->id, 'last_read' => new Carbon()]); // Add Recipients $thread->addParticipant([$recipient_id]); if ($email) { sendEmailNotification($message); } return $thread->id; }
public static function boot() { parent::boot(); // Since using Participant::restored triggers before the message is actually saved, let's fire the event when the message is created. Message::created(self::raiseEvent()); }
/** * Stores a new message thread * * @return mixed */ public function store() { $input = Input::all(); $recipient_id = $input['recipient']; $sender_id = JWTAuth::parseToken()->authenticate()->id; if ($sender_id == $recipient_id) { return response()->json(['success' => 'false', 'message' => 'Error. Your ID and receipient ID cannot be same.']); } if ($sender_id > $recipient_id) { $subject = $recipient_id . ' ' . $sender_id; $thread = Thread::firstOrCreate(['subject' => $subject]); } else { $subject = $sender_id . ' ' . $recipient_id; $thread = Thread::firstOrCreate(['subject' => $subject]); } // Message Message::create(['thread_id' => $thread->id, 'user_id' => $sender_id, 'body' => $input['message']]); if (!$thread->participantsUserIds()->unique()->search($recipient_id)) { //if first time thread is created, no record of participants in thread // Sender Participant::create(['thread_id' => $thread->id, 'user_id' => $sender_id, 'last_read' => new Carbon()]); // Recipients $thread->addParticipants([$recipient_id]); } return response()->json(['success' => 'true', 'message' => 'Message sent.', 'thread_id' => $thread->id]); }
/** * 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]); }