public function restore() { parent::restore(); // TODO: Change the autogenerated stub if ($this->thread->getLatestMessageAttribute()->user_id != $this->user_id || $this->thread->getLatestMessageAttribute()->type == 'notification') { Event::fire(new NewMessage($this)); } }
/** * 存储提醒消息 */ 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', '', '消息添加成功'); }
/** * Shows a message thread. * * @param $id * @return mixed */ public function user($id) { if (Auth::id() != $id) { $user = User::find($id); $thread = Thread::between([Auth::id(), $id])->first(); if ($thread == null) { $thread = Thread::create(['subject' => "Thread between 2 users"]); Participant::create(['thread_id' => $thread->id, 'user_id' => Auth::user()->id, 'last_read' => new Carbon()]); $thread->addParticipants([$id]); } $thread->markAsRead(Auth::id()); return view('messenger.show', compact('thread', 'id', 'user')); } else { return redirect('messages'); } }
/** * Kick a user from a conversation. * * @return mixed */ public function kickUser($conversation, $user) { if (!$user) { Flash::error('User not found'); return redirect('conversations'); } try { $conversation->getParticipantFromUser(Auth::id()); } catch (ModelNotFoundException $e) { Flash::error('The requested conversation does not exist.'); return redirect('conversations'); } if (!$conversation->canManage) { Flash::error('You do not have permission to do this.'); return redirect('conversations'); } $participant = Participant::where('thread_id', '=', $conversation->id)->where('user_id', '=', $user->id)->first(); if (!$participant) { Flash::error('User not found'); return redirect('conversations/' . $conversation->id); } $thread = $participant->thread; if ($user->name == Auth::user()->name) { Flash::error('You can\'t kick yourself!'); return redirect('conversations/' . $conversation->id); } $firstParticipant = $conversation->participants()->first(); if ($user->name == $firstParticipant->name) { Flash::error('You can\'t kick the person who started the conversation.'); return redirect('conversations/' . $conversation->id); } $conversation->messages()->where('user_id', '=', $user->id)->delete(); $participant->delete(); Flash::success('Kicked user "' . $user->name . '" from the conversation'); if ($conversation->participants()->count() > 0) { return redirect('conversations/' . $conversation->id); } else { $conversation->messages()->delete(); $conversation->participants()->delete(); $conversation->delete(); return redirect('conversations/'); } return redirect('conversations/'); }
/** * 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); } } } }
/** * Store a newly created resource in storage. * POST /api/{resource}. * * @return Response */ public function store() { $data = $this->request->json()->get($this->resourceKeySingular); if (!$data) { return $this->errorWrongArgs('Empty data'); } $data['user_id'] = Authorizer::getResourceOwnerId(); $validator = Validator::make($data, $this->rulesForCreate()); if ($validator->fails()) { return $this->errorWrongArgs($validator->messages()); } $this->unguardIfNeeded(); $item = $this->model->create($data); /* Assignment_Transaction::create([ ]);*/ $thread = Thread::create(['subject' => 'user-' . $item->id]); Participant::create(['thread_id' => $thread->id, 'user_id' => Authorizer::getResourceOwnerId(), 'last_read' => new Carbon()]); $thread->addParticipants([18]); return $this->respondWithItem($item); }
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); }
/** * 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 (getenv('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 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); }
public function createNewThread() { if (Thread::where('subject', Input::get('subject'))->first() == null) { $thread = Thread::create(['subject' => Input::get('subject')]); Participant::create(['thread_id' => $thread->id, 'user_id' => Authorizer::getResourceOwnerId(), 'last_read' => new Carbon()]); $thread->addParticipants([Input::get('participant_id')]); return $thread; } return Thread::where('subject', Input::get('subject'))->first()->get(); }
/** * 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; }
/** * 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 * * @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); }
/** * 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]); } } }
/** * 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]); }