<commit_before><?php

class CommentController extends BaseController {

    public function showComments($groupName = 'all')
    {
        $groupName = shadow($groupName);

        if (Auth::check() && !Route::input('group') && $groupName == 'all'
            && @Auth::user()->settings['homepage_subscribed'])
        {
            $groupName = 'subscribed';
        }

        if (Auth::guest() && in_array($groupName, ['subscribed', 'moderated', 'observed']))
        {
            return Redirect::route('login_form')->with('info_msg', 'Wybrana funkcja dostępna jest wyłącznie dla zalogowanych użytkowników.');
        }

        if (class_exists('Groups\\'. studly_case($groupName)))
        {
            $class = 'Groups\\'. studly_case($groupName);
            $builder = with(new $class)->comments();
        }
        else
        {
            $group = Group::shadow($groupName)->firstOrFail();
            $group->checkAccess();

            $builder = $group->comments();
        }

        $builder->orderBy('created_at', 'desc')->with(['user']);

        // Paginate
        $comments = $this->cachedPaginate($builder, Settings::get('entries_per_page'), 10);

        $group_name = $groupName;

        $blockedUsers = array();

        if (Auth::check())
        {
            $blockedUsers = (array) Auth::user()->blockedUsers();
        }

        return View::make('comments.list', compact('group', 'comments', 'group_name', 'blockedUsers'));
    }

    public function getCommentSource()
    {
        $class = (Input::get('type') == 'comment') ? 'Comment' : 'CommentReply';

        $comment = $class::findOrFail(Input::get('id'));

        if (Auth::user()->getKey() != $comment->user_id)
        {
            App::abort(403, 'Access denied');
        }

        return Response::json(['status' => 'ok', 'source' => $comment->text_source]);
    }

    public function addComment()
    {
        $validator = Comment::validate(Input::all());

        if ($validator->fails())
        {
            return Response::json(['status' => 'error', 'error' => $validator->messages()->first()]);
        }

        $content = Content::findOrFail(Input::get('id'));

        if (Auth::user()->isBanned($content->group))
        {
            return Response::json(['status' => 'error', 'error' => 'Zostałeś zbanowany w tej grupie']);
        }

        $comment = new Comment([
            'text' => Input::get('text'),
        ]);

        $comment->user()->associate(Auth::user());
        $comment->content()->associate($content);
        $comment->group()->associate($content->group);

        $comment->save();

        // Send notifications to mentioned users
        $this->sendNotifications(Input::get('text'), function($notification) use ($content, $comment)
        {
            $notification->type = 'comment';
            $notification->setTitle($comment->text);
            $notification->content()->associate($content);
            $notification->comment()->associate($comment);
            $notification->save(); // todo
        });

        $comment = View::make('comments.widget', compact('comment'))->render();

        return Response::json(['status' => 'ok', 'comment' => $comment]);
    }

    public function addReply()
    {
        $validator = CommentReply::validate(Input::all());

        if ($validator->fails())
        {
            return Response::json(['status' => 'error', 'error' => $validator->messages()->first()]);
        }

        $parent = Comment::findOrFail(Input::get('id'));
        $content = $parent->content;

        if (Auth::user()->isBanned($content->group))
        {
            return Response::json(['status' => 'error', 'error' => 'Zostałeś zbanowany w tej grupie']);
        }

        $comment = new CommentReply([
            'text' => Input::get('text')
        ]);

        $comment->user()->associate(Auth::user());

        $parent->replies()->save($comment);

        $content->increment('comments');

        // Send notifications to mentioned users
        $this->sendNotifications(Input::get('text'), function($notification) use ($comment, $content){
            $notification->type = 'comment_reply';
            $notification->setTitle($comment->text);
            $notification->content()->associate($content);
            $notification->commentReply()->associate($comment);
            $notification->save(); // todo
        });
<commit_msg>Rename comments field to comments_count in CommentController<commit_after><?php

class CommentController extends BaseController {

    public function showComments($groupName = 'all')
    {
        $groupName = shadow($groupName);

        if (Auth::check() && !Route::input('group') && $groupName == 'all'
            && @Auth::user()->settings['homepage_subscribed'])
        {
            $groupName = 'subscribed';
        }

        if (Auth::guest() && in_array($groupName, ['subscribed', 'moderated', 'observed']))
        {
            return Redirect::route('login_form')->with('info_msg', 'Wybrana funkcja dostępna jest wyłącznie dla zalogowanych użytkowników.');
        }

        if (class_exists('Groups\\'. studly_case($groupName)))
        {
            $class = 'Groups\\'. studly_case($groupName);
            $builder = with(new $class)->comments();
        }
        else
        {
            $group = Group::shadow($groupName)->firstOrFail();
            $group->checkAccess();

            $builder = $group->comments();
        }

        $builder->orderBy('created_at', 'desc')->with(['user']);

        // Paginate
        $comments = $this->cachedPaginate($builder, Settings::get('entries_per_page'), 10);

        $group_name = $groupName;

        $blockedUsers = array();

        if (Auth::check())
        {
            $blockedUsers = (array) Auth::user()->blockedUsers();
        }

        return View::make('comments.list', compact('group', 'comments', 'group_name', 'blockedUsers'));
    }

    public function getCommentSource()
    {
        $class = (Input::get('type') == 'comment') ? 'Comment' : 'CommentReply';

        $comment = $class::findOrFail(Input::get('id'));

        if (Auth::user()->getKey() != $comment->user_id)
        {
            App::abort(403, 'Access denied');
        }

        return Response::json(['status' => 'ok', 'source' => $comment->text_source]);
    }

    public function addComment()
    {
        $validator = Comment::validate(Input::all());

        if ($validator->fails())
        {
            return Response::json(['status' => 'error', 'error' => $validator->messages()->first()]);
        }

        $content = Content::findOrFail(Input::get('id'));

        if (Auth::user()->isBanned($content->group))
        {
            return Response::json(['status' => 'error', 'error' => 'Zostałeś zbanowany w tej grupie']);
        }

        $comment = new Comment([
            'text' => Input::get('text'),
        ]);

        $comment->user()->associate(Auth::user());
        $comment->content()->associate($content);
        $comment->group()->associate($content->group);

        $comment->save();

        // Send notifications to mentioned users
        $this->sendNotifications(Input::get('text'), function($notification) use ($content, $comment)
        {
            $notification->type = 'comment';
            $notification->setTitle($comment->text);
            $notification->content()->associate($content);
            $notification->comment()->associate($comment);
            $notification->save(); // todo
        });

        $comment = View::make('comments.widget', compact('comment'))->render();

        return Response::json(['status' => 'ok', 'comment' => $comment]);
    }

    public function addReply()
    {
        $validator = CommentReply::validate(Input::all());

        if ($validator->fails())
        {
            return Response::json(['status' => 'error', 'error' => $validator->messages()->first()]);
        }

        $parent = Comment::findOrFail(Input::get('id'));
        $content = $parent->content;

        if (Auth::user()->isBanned($content->group))
        {
            return Response::json(['status' => 'error', 'error' => 'Zostałeś zbanowany w tej grupie']);
        }

        $comment = new CommentReply([
            'text' => Input::get('text')
        ]);

        $comment->user()->associate(Auth::user());

        $parent->replies()->save($comment);

        $content->increment('comments_count');

        // Send notifications to mentioned users
        $this->sendNotifications(Input::get('text'), function($notification) use ($comment, $content){
            $notification->type = 'comment_reply';
            $notification->setTitle($comment->text);
            $notification->content()->associate($content);
            $notification->commentReply()->associate($comment);
            $notification->save(); // todo
        });

{'fraction_non_alphanumeric': 0.1876753844426984, 'fraction_numerical': 0.0011224604332697272, 'mean_word_length': 2.487279843444227, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4652980', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2396, 'n_words': 414}