/** * 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/'); }
/** * 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; }