Beispiel #1
0
 /**
  * Render the likes into a string.
  *
  * @param Collection $likesCollection  The likes to render.
  *
  * @param string     $viewAllLikesLink The link to view all of the likes for the content.
  *
  * @return string
  */
 public function render(Collection $likesCollection, $viewAllLikesLink)
 {
     $numLikesToList = $this->settings->get('posts.likes_to_show', 3);
     $numOtherLikes = $likesCollection->count() - $numLikesToList;
     $userId = $this->guard->user()->getAuthIdentifier();
     $likes = [];
     $likesCollection = $likesCollection->filter(function (Like $like) use(&$likes, &$numLikesToList, $userId) {
         if ($like->user->id === $userId) {
             $like->user->name = $this->lang->get('likes.current_user');
             $likes[] = $like;
             $numLikesToList--;
             return false;
         }
         return true;
     });
     $numLikesInCollection = $likesCollection->count();
     if ($numLikesInCollection > 0 && $numLikesToList > 0) {
         if ($numLikesInCollection < $numLikesToList) {
             $numLikesToList = $numLikesInCollection;
         }
         $randomLikes = $likesCollection->random($numLikesToList);
         if (!is_array($randomLikes)) {
             // random returns a single model if $numLikesToList is 1...
             $randomLikes = array($randomLikes);
         }
         foreach ($randomLikes as $key => $like) {
             $likes[] = $like;
         }
     }
     return $this->viewFactory->make('likes.list', compact('numOtherLikes', 'likes', 'viewAllLikesLink'))->render();
 }
 /**
  * Render the profile link for a given user.
  *
  * @param User $user          The user to render the profile link for
  *                            or null to render the link for the current user.
  * @param bool $includeAvatar Whether to include the user's avatar in the link.
  * @param bool $useStyledName Whether to apply the usergroup styling to the username.
  *
  * @return string The rendered profile link.
  */
 public function renderProfileLink(User $user = null, $includeAvatar = false, $useStyledName = true)
 {
     if (is_null($user)) {
         $user = $this->guard->user();
     }
     return view('user.profile_link', compact('user', 'includeAvatar', 'useStyledName'))->render();
 }
Beispiel #3
0
 /**
  * Get all users active in the last x minutes
  *
  * @param int    $minutes  The number of minutes which are considered as "online time"
  * @param string $orderBy
  * @param string $orderDir
  * @param int    $num      The number of users to return. Set to 0 to get all users
  *
  * @return mixed
  */
 public function online($minutes = 15, $orderBy = 'last_visit', $orderDir = 'desc', $num = 20)
 {
     // If the user visited the logout page as last he's not online anymore
     /** @var Builder $baseQuery */
     $baseQuery = $this->userModel->where('last_visit', '>=', new \DateTime("{$minutes} minutes ago"))->where('last_page', '!=', 'auth/logout')->orderBy('users.' . $orderBy, $orderDir);
     // No need to add anymore if the user has permission to view anyone
     if (!$this->permissionChecker->hasPermission('user', null, 'canViewAllOnline')) {
         // First get the id of our setting
         $settingId = Setting::where('name', 'user.showonline')->first()->id;
         // Now join the correct setting_values row
         $baseQuery->leftJoin('setting_values', function ($join) use($settingId) {
             $join->on('setting_values.user_id', '=', 'users.id')->where('setting_values.setting_id', '=', $settingId);
         });
         // Either the setting is true or not set...
         $baseQuery->where(function ($query) {
             $query->where('setting_values.value', true)->orWhereNull('setting_values.value');
             // ... or we're querying our row at the moment
             if ($this->guard->check()) {
                 $query->orWhere('users.id', '=', $this->guard->user()->id);
             }
         });
     }
     if ($num > 0) {
         return $baseQuery->paginate($num, ['users.*']);
     }
     return $baseQuery->get(['users.*']);
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     if ($this->auth->check()) {
         return new RedirectResponse(url('/'));
     }
     return $next($request);
 }
Beispiel #5
0
 /**
  * Check whether the current user has permission to perform this request.
  *
  * @param Guard $guard
  *
  * @return bool
  */
 public function authorize(Guard $guard)
 {
     if (!$guard->check()) {
         return false;
     }
     // TODO: Check user permissions here...
     return true;
 }
Beispiel #6
0
 /**
  * Create a new searchlog
  *
  * @param array $details Details about the searchlog.
  *
  * @return mixed
  */
 public function create(array $details = [])
 {
     $details = array_merge(['id' => md5(uniqid(microtime(), true)), 'keywords' => '', 'as_topics' => true, 'user_id' => $this->guard->user()->id, 'topics' => '', 'posts' => ''], $details);
     if ($details['user_id'] < 0) {
         $details['user_id'] = null;
     }
     $searchlog = $this->searchModel->create($details);
     return $searchlog;
 }
 /**
  * @param ReversibleModerationRequest $request
  *
  * @param ModerationLoggerInterface   $moderationLogger
  * @param Guard                       $guard
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function reverse(ReversibleModerationRequest $request, ModerationLoggerInterface $moderationLogger, Guard $guard)
 {
     $options = $request->getModerationOptions();
     foreach ($request->getModeratableContent() as $content) {
         $request->getModeration()->reverse($content, $options);
     }
     $moderationLogger->logReverseFromRequest($guard->user(), $request);
     return redirect()->back();
 }
Beispiel #8
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     if ($this->auth->guest()) {
         if ($request->ajax()) {
             return response('Unauthorized.', 401);
         } else {
             return redirect()->guest('auth/login');
         }
     }
     return $next($request);
 }
Beispiel #9
0
 /**
  * Check whether the current user has liked the post.
  *
  * @return bool Whether the post has been liked by the current user.
  */
 public function hasLikedPost()
 {
     if ($this->guard->check()) {
         $user = $this->guard->user();
         $containsLike = $this->wrappedObject->likes->contains(function ($key, Like $like) use(&$likes, &$numLikesToList, $user) {
             if ($like->user->id === $user->getAuthIdentifier()) {
                 return true;
             }
             return false;
         });
         return $containsLike !== false;
     }
     return false;
 }
Beispiel #10
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     // The route settings aren't loaded at this point so we need to get it manually
     $options = $this->getOptions($this->router, $request);
     if ($this->isDebugBarRequest($request)) {
         return $next($request);
     }
     if (!isset($options['noOnline']) || $options['noOnline'] !== true) {
         if ($this->guard->check()) {
             $this->guard->user()->update(['last_visit' => new \DateTime(), 'last_page' => $request->path()]);
         }
     }
     return $next($request);
 }
Beispiel #11
0
 /**
  * @return bool
  */
 public function isOnline()
 {
     $minutes = $this->settings->get('wio.minutes', 15);
     // This user was logging out at last
     if ($this->wrappedObject->last_page == 'auth/logout') {
         return false;
     }
     // This user isn't online
     if (new \DateTime($this->wrappedObject->last_visit) < new \DateTime("{$minutes} minutes ago")) {
         return false;
     }
     // The user is online, now permissions
     // We're either testing our own account or have permissions to view everyone
     if ($this->permissionChecker->hasPermission('user', null, 'canViewAllOnline') || $this->guard->user()->id == $this->wrappedObject->id) {
         return true;
     }
     // Next we need to get the setting for this user
     // First get the id of our setting
     $settingId = Setting::where('name', 'user.showonline')->first()->id;
     // Now the value
     $settingValue = SettingValue::where('user_id', '=', $this->wrappedObject->id)->where('setting_id', '=', $settingId)->first();
     // Either the value isn't set (good) or true (better), let's show this user as online
     if ($settingValue == null || $settingValue->value == true) {
         return true;
     }
     // Still here? Then the viewing user doesn't have the permissions and we show him as offline
     return false;
 }
Beispiel #12
0
 /**
  * Add a post to a topic.
  *
  * @param Topic $topic       The topic to add a post to.
  * @param array $postDetails The details of the post to add.
  *
  * @return mixed
  */
 public function addPostToTopic(Topic $topic, array $postDetails)
 {
     $postDetails = array_merge(['user_id' => $this->guard->user()->id, 'username' => null, 'content' => '', 'content_parsed' => ''], $postDetails);
     $postDetails['content_parsed'] = $this->formatter->parse($postDetails['content'], [MessageFormatter::ME_USERNAME => $this->guard->user()->name]);
     // TODO: Parser options...
     if ($postDetails['user_id'] > 0) {
         $postDetails['username'] = User::find($postDetails['user_id'])->name;
     } else {
         $postDetails['user_id'] = null;
         if ($postDetails['username'] == trans('general.guest')) {
             $postDetails['username'] = null;
         }
     }
     $post = $topic->posts()->create($postDetails);
     if ($post !== false) {
         $topic->increment('num_posts');
         $topic->update(['last_post_id' => $post['id']]);
         $topic->forum->increment('num_posts');
         $topic->forum->update(['last_post_id' => $post->id, 'last_post_user_id' => $postDetails['user_id']]);
     }
     if ($post->user_id > 0) {
         $post->author->increment('num_posts');
     }
     return $post;
 }
Beispiel #13
0
 /**
  * Create a new topic
  *
  * @param array $details Details about the topic.
  *
  * @return mixed
  */
 public function create(array $details = [])
 {
     $details = array_merge(['title' => '', 'forum_id' => 0, 'user_id' => $this->guard->user()->id, 'username' => null, 'first_post_id' => 0, 'last_post_id' => 0, 'views' => 0, 'num_posts' => 0, 'content' => ''], $details);
     $details['slug'] = $this->createSlugForTitle($details['title']);
     if ($details['user_id'] > 0) {
         $details['username'] = User::find($details['user_id'])->name;
         // TODO: Use User Repository!
     } else {
         $details['user_id'] = null;
         if ($details['username'] == trans('general.guest')) {
             $details['username'] = null;
         }
     }
     $topic = null;
     $this->dbManager->transaction(function () use($details, &$topic) {
         $topic = $this->topicModel->create(['title' => $details['title'], 'slug' => $details['slug'], 'forum_id' => $details['forum_id'], 'user_id' => $details['user_id'], 'username' => $details['username']]);
         $firstPost = $this->postRepository->addPostToTopic($topic, ['content' => $details['content'], 'username' => $details['username']]);
         $topic->update(['first_post_id' => $firstPost->id, 'last_post_id' => $firstPost->id, 'num_posts' => 1]);
     });
     $topic->forum->increment('num_topics');
     if ($topic->user_id > 0) {
         $topic->author->increment('num_topics');
     }
     return $topic;
 }
Beispiel #14
0
 /**
  * Toggle a like on or off for a given piece of content for the current user.
  *
  * @param \Illuminate\Database\Eloquent\Model|LikeableTrait $content The content to toggle the like for.
  *
  * @return null|LikeModel Null if a like was removed,a  like model instance if one was created.
  */
 public function toggleLikeForContent(Model $content)
 {
     if (($user = $this->guard->user()) !== null) {
         $existingLike = $this->likesModel->where('user_id', '=', $user->getAuthIdentifier())->where('content_id', '=', $content->id)->where('content_type', '=', get_class($content))->first();
         if ($existingLike !== null) {
             $existingLike->delete();
             $user->decrement('num_likes_made');
             $content->decrementNumLikes();
         } else {
             $newLike = $this->likesModel->create(['user_id' => $user->getAuthIdentifier(), 'content_type' => get_class($content), 'content_id' => $content->getKey()]);
             $user->increment('num_likes_made');
             $content->incrementNumLikes();
             return $newLike;
         }
     }
     return null;
 }
 /**
  * @param int                $id
  * @param ParticipantRequest $request
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function postNewParticipant($id, ParticipantRequest $request)
 {
     /** @var Conversation $conversation */
     $conversation = $this->conversationRepository->find($id);
     if (!$conversation || !$conversation->participants->contains($this->guard->user())) {
         throw new ConversationNotFoundException();
     }
     try {
         $this->conversationRepository->addParticipants($conversation, $request->getUseridArray('participants'));
     } catch (ConversationCantSendToSelfException $exception) {
         return redirect()->route('conversations.newParticipant', ['id' => $conversation->id])->withInput()->withErrors(['participants' => $exception->getMessage()]);
     } catch (ConversationAlreadyParticipantException $exception) {
         return redirect()->route('conversations.newParticipant', ['id' => $conversation->id])->withInput()->withErrors(['participants' => $exception->getMessage()]);
     }
     return redirect()->route('conversations.read', ['id' => $conversation->id])->withSuccess('Added participants');
 }
Beispiel #16
0
 /**
  * Filters a forum collection by the "canView" permission
  *
  * @param Collection $forums
  *
  * @return Collection
  */
 private function filterUnviewableForums(Collection $forums)
 {
     return $forums->filter(function (Forum $forum) {
         return $this->permissionChecker->hasPermission('forum', $forum->getContentId(), $forum::getViewablePermission(), $this->guard->user());
     });
 }
Beispiel #17
0
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  \Closure                 $next
  *
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     // Share the authenticated user to make sure we have a decorated version of it
     $this->viewFactory->share('auth_user', $this->guard->user());
     return $next($request);
 }
Beispiel #18
0
 /**
  * @return bool
  */
 public function has_value()
 {
     return $this->userProfileFields->hasForProfileField($this->guard->user(), $this->getWrappedObject());
 }