public function read() { $thread = Thread::find($this->request->get('id')); if (!$thread) { return return_rest('0', compact('count'), '该消息不存在'); } $thread->markAsRead($this->user()->id); return return_rest('1', compact('count'), '已读消息'); }
public function unreadQueryThreads($assignment) { $unreadThreads = []; if ($threads = Thread::where('subject', 'LIKE', '%query-' . $assignment->id . '%')->get()) { foreach ($threads as $thread) { if ($thread->isUnread(Authorizer::getResourceOwnerId())) { array_push($unreadThreads, $thread->subject); } } } return $unreadThreads; }
/** * Returns all public threads with new messages * * @return array */ public function threadsPublicWithNewMessages() { $threadsWithNewMessages = []; //achar threads publicas //$threads = Thread::getAllLatest()->where('public','=', 1)->get(); $threads = Thread::getFiveLatestPublic(); if ($threads) { foreach ($threads as $thread) { if (!$thread->hasParticipant($this->id)) { $threadsWithNewMessages[] = $thread->id; } } } return $threadsWithNewMessages; }
/** * Leave a conversation * * @param $id * @return void */ public function leaveConversation($id) { try { $thread = Thread::findOrFail($id); $thread->getParticipantFromUser(Auth::id()); } catch (ModelNotFoundException $e) { Flash::error('The requested conversation does not exist.'); return redirect('conversations'); } $participant = $thread->getParticipantFromUser(Auth::id()); $user = $participant->user; $thread->messages()->where('user_id', '=', $user->id)->delete(); $participant->delete(); if ($thread->participants()->count() == 0) { $thread->delete(); } Flash::success('You have left the conversation.'); return redirect('conversations'); }
/** * Get signed in user's profile. */ public function getUser(Request $request) { $user = User::find($request['user']['sub']); $user->roles = $user->roles()->get(); $user->isAdmin = $user->hasRole('admin'); $user->profile = $user->profile; $user->locations = $user->locations; $user->notifications = $user->getNotifications(); $user->notificationsNotRead = $user->countNotificationsNotRead(); $threads = Thread::forUser($user->id)->latest('updated_at')->get(); $user->threads = $threads; $roles = Role::all(['id', 'display_name']); $activities = ActivityModel::where('user_id', $user->id)->get(); return Response::json(['user' => $user, 'roles' => $roles, 'activities' => $activities]); }
/** * 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); } } } }
/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { parent::boot($router); // # Route Models $router->model('role', 'Fetch404\\Core\\Models\\Role'); $router->model('user', 'Fetch404\\Core\\Models\\User'); $router->model('news', 'Fetch404\\Core\\Models\\News'); $router->model('tag', 'Fetch404\\Core\\Models\\Tag'); $router->model('profile_post', 'Fetch404\\Core\\Models\\ProfilePost'); $router->model('post', 'Fetch404\\Core\\Models\\Post'); $router->model('topic', 'Fetch404\\Core\\Models\\Topic'); $router->model('channel', 'Fetch404\\Core\\Models\\Channel'); $router->model('category', 'Fetch404\\Core\\Models\\Category'); $router->model('report', 'Fetch404\\Core\\Models\\Report'); $router->model('conversation', 'Cmgmyr\\Messenger\\Models\\Thread'); $router->bind('conversation', function ($value) { return Thread::findOrFail($value); }); }
/** * 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 ($_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; }
/** * 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); }
/** * Bootstrap any application services. * * @return void */ public function boot() { // // Set up the view composers view()->composer('core.partials.layouts.master', function ($view) { $site_title = Setting::where('name', '=', 'sitename')->first(); $site_theme = Setting::where('name', '=', 'bootswatch_theme')->first(); $navbar_style = Setting::where('name', '=', 'navbar_style')->first(); $recaptcha_enabled = Setting::where('name', '=', 'recaptcha')->first(); $view->with('recaptcha_enabled', $recaptcha_enabled != null ? $recaptcha_enabled->value : '0'); $view->with('site_title', $site_title != null ? e($site_title->value) : 'Fetch404'); $view->with('theme_id', $site_theme != null ? e($site_theme->value) : '1'); $view->with('navbar_style', $navbar_style != null ? e($navbar_style->value) : '0'); if (Auth::check()) { $user = Auth::user(); $view->with('user', $user); $notifications = $user->notifications; $notifications = $notifications->sortByDesc(function ($item) { return $item->created_at; }); $notifications = $notifications->filter(function ($item) { return time() - strtotime($item->created_at) < 60 * 60 * (24 * 3); }); $view->with('notifications', $notifications->take(5)); $messages = Thread::forUserWithNewMessages($user->id)->get(); $messages = $messages->sortByDesc(function ($item) { return $item->created_at; }); $messages = $messages->filter(function ($item) use($user) { return time() - strtotime($item->created_at) < 60 * 60 * (24 * 3) && $item->isUnread($user->id); }); $view->with('messages', $messages); if ($user->can('viewReports')) { $reports = Report::all(); $reports = $reports->sortByDesc(function ($item) { return $item->updated_at; }); $reports = $reports->filter(function ($item) { return !$item->isClosed(); }); $view->with('reports', $reports); } } }); view()->composer('core.admin.layouts.default', function ($view) { $site_title = Setting::where('name', '=', 'sitename')->first(); $site_theme = Setting::where('name', '=', 'bootswatch_theme')->first(); $navbar_style = Setting::where('name', '=', 'navbar_style')->first(); $view->with('site_title', $site_title != null ? e($site_title->value) : 'Fetch404'); $view->with('theme_id', $site_theme != null ? e($site_theme->value) : '1'); $view->with('navbar_style', $navbar_style != null ? e($navbar_style->value) : '0'); $user = Auth::user(); $view->with('user', $user); if ($user->can('viewReports')) { $reports = Report::all(); $reports = $reports->sortByDesc(function ($item) { return $item->updated_at; }); $reports = $reports->filter(function ($item) use($user) { return !$item->isClosed(); }); $view->with('reports', $reports); } }); view()->composer('core.admin.general', function ($view) { $site_title = Setting::where('name', '=', 'sitename')->first(); $site_theme = Setting::where('name', '=', 'bootswatch_theme')->first(); $navbar_style = Setting::where('name', '=', 'navbar_style')->first(); $recaptcha_enabled = Setting::where('name', '=', 'recaptcha')->first(); $recaptcha_key = Setting::where('name', '=', 'recaptcha_key')->first(); $view->with('site_title', $site_title != null ? e($site_title->value) : 'Fetch404'); $view->with('theme_id', $site_theme != null ? e($site_theme->value) : '1'); $view->with('navbar_style', $navbar_style != null ? e($navbar_style->value) : '0'); $view->with('recaptcha_enabled', $recaptcha_enabled != null ? $recaptcha_enabled->value == 'true' ? 'true' : 'false' : 'false'); $view->with('recaptcha_key', $recaptcha_key != null ? e($recaptcha_key->value) : ''); }); view()->composer('core.admin.index', function ($view) { $date = new Carbon(); $date->subWeek(); $users = User::where('created_at', '>', $date->toDateTimeString())->get(); $view->with('latest_users', $users); $view->with('roles', Role::all()); }); view()->composer('core.admin.partials.sidebar', function ($view) { $user = Auth::user(); if ($user->can('viewReports')) { $reports = Report::all(); $reports = $reports->sortByDesc(function ($item) { return $item->updated_at; }); $reports = $reports->filter(function ($item) use($user) { return !$item->isClosed(); }); $view->with('reports', $reports); } }); view()->composer('core.auth.register', function ($view) { $recaptcha_enabled = Setting::where('name', '=', 'recaptcha')->first(); $recaptcha_key = Setting::where('name', '=', 'recaptcha_key')->first(); $view->with('recaptcha_enabled', $recaptcha_enabled != null ? $recaptcha_enabled->value == 'true' ? 'true' : 'false' : 'false'); $view->with('recaptcha_key', $recaptcha_key != null ? e($recaptcha_key->value) : ''); }); view()->composer('core.forum.partials.latest-threads', function ($view) { $threads = Topic::all()->take(5); $threads = $threads->filter(function ($item) { return $item != null && $item->channel != null && $item->channel->category != null && $item->channel->category->canView(Auth::user()) && $item->channel->canView(Auth::user()); }); $threads = $threads->sortByDesc(function ($item) { return $item->getLatestPost()->created_at; }); $view->with('threads', $threads); }); view()->composer('core.forum.partials.online-users', function ($view) { $online = User::where('is_online', '=', 1)->orderBy('name', 'asc')->get(); $view->with('users', $online); }); view()->composer('core.forum.partials.stats', function ($view) { $users = User::all(); $latestUser = User::latest('created_at')->first(); $view->with('users', $users); $view->with('latestUser', $latestUser); }); view()->composer('core.forum.partials.latest-statuses', function ($view) { $statuses = ProfilePost::latest('created_at')->take(5); $statuses = $statuses->filter(function (ProfilePost $item) { return !$item->toUser->isBanned(); }); $statuses = $statuses->sortByDesc(function ($item) { return $item->getLatestPost()->created_at; }); $view->with('statuses', $statuses); }); }
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(); }
public function newMessages() { return Thread::forUserWithNewMessages(Auth::user()->id)->latest('updated_at')->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(); } }
/** * 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::to('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::to('messages/' . $id); }
/** * Send Email notification of new internal messages * * @param Message $message */ function sendEmailNotification(Message $message) { $subject = 'c-SPOT internal message notification'; $thread = Thread::find($message->thread_id); $thread_subject = $thread->subject; $message_body = $message->body; foreach ($thread->participants as $key => $recipient) { $user = $recipient->user; # check if user actually wants to be notified if ($user->notify_by_email) { Mail::send('cspot.emails.notification', ['user' => $user, 'subject' => $subject, 'messi' => $message], function ($msg) use($user, $subject) { $msg->from(findAdmins()[0]->email, 'c-SPOT Admin'); $msg->to($user->email, $user->fullName); $msg->subject($subject); }); } } }
/** * Retrieves a specific innovation with its related data * @param $id * @param ConversationRepository $conversationRepository * @return \Illuminate\View\View */ public function show($id, ConversationRepository $conversationRepository) { $innovation = $this->repo->retrieve($id); $check_chat = $conversationRepository->chatExists($id); $currentUserId = Auth::user()->id; $chatWithInnovator = $conversationRepository->checkChatWithInnovator($id); $mother = User::where('id', '=', $innovation->moderator_id)->first(); $users = User::where('userCategory', '=', 3)->get(); $investors = User::where('userCategory', '=', 2)->get(); // All threads that user is participating in $threads = Thread::forUser($currentUserId)->where('innovation_id', '=', $id)->get(); $threads_count = $threads->count(); if (\Auth::user()->isInvestor()) { if (Thread::where('innovation_id', '=', $id)->where('user_id', '=', \Auth::user()->id)->exists() == true) { $thread = Thread::where('innovation_id', '=', $id)->where('user_id', '=', \Auth::user()->id)->first(); } } if (\Auth::user()->isMother()) { if (Thread::where('innovation_id', '=', $id)->where('user_id', '=', \Auth::user()->id)->exists() == true) { $thread_mother = Thread::where('innovation_id', '=', $id)->where('user_id', '=', \Auth::user()->id)->first(); } else { $thread_mother = null; } } $funds = $this->repo->getPortfolio($id); $totalNeeded = $this->repo->getInnovationFund($id); return view('innovation.show', compact('totalNeeded', 'funds', 'thread_mother', 'thread', 'investors', 'mother', 'users', 'chatWithInnovator', 'innovation', 'id', 'check_chat', 'message', 'threads', 'threads_count', 'currentUserId')); }
/** * Mark a specific thread as read, for ajax use * * @param $id */ public function read($id) { $thread = Thread::find($id); if (!$thread) { abort(404); } $thread->markAsRead(Auth::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]); }
/** * 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]); }
/** * Get a user's conversations. * * @return Thread */ public function getConversations() { return Thread::forUser($this->id); }