<commit_before><?php

use Lio\Comments\CommentRepository;
use Lio\Comments\Comment;

use Lio\Tags\TagRepository;

class ForumController extends BaseController
{
    protected $categories;
    protected $comments;

    protected $threadsPerPage = 20;
    protected $commentsPerPage = 20;

    public function __construct(CommentRepository $comments, TagRepository $tags)
    {
        $this->comments = $comments;
        $this->tags     = $tags;

        View::share('forumSections', Config::get('forum.sections'));
        $this->setNewSectionCounts();
    }

    public function getComment($thread, $commentId)
    {
        // Holy shit worst code ever made..
        // LYLAS!
        $comment = Comment::findOrFail($commentId);
        $before = Comment::where('parent_id', '=', $comment->parent_id)->where('created_at', '<', $comment->created_at)->count();
        $page = round($before / $this->commentsPerPage, 0, PHP_ROUND_HALF_DOWN) + 1;

        return Redirect::to(action('ForumThreadController@show', [$thread]) . '?page=' . $page . '#comment-' . $commentId);
    }

    // does the awful code ever end?
    public function getDelete($commentId)
    {
        $comment = $this->comments->requireById($commentId);
        if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
        $this->view('forum.delete', compact('comment'));
    }

    public function postDelete($commentId)
    {
        $comment = $this->comments->requireById($commentId);
        if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
        $comment->delete();
        return Redirect::action('ForumThreadController@index');
    }

    public function getSearch()
    {
        $query = Input::get('query');
        $results = App::make('Lio\Comments\ForumSearch')->searchPaginated($query, $this->threadsPerPage);
        $this->updateUserLastVisited();
        $this->view('forum.search', compact('query', 'results'));
    }

    private function updateUserLastVisited()
    {
        $forumLastVisited = Session::get('forum_last_visited');
        $tags = Input::get('tags');

        if (is_array($forumLastVisited)) {
            View::share('last_visited_timestamp', isset($forumLastVisited[$tags]) ? $forumLastVisited[$tags] : 0);
            $forumLastVisited[$tags] = strtotime('now');
        } else {
            View::share('last_visited_timestamp', 0);
            $forumLastVisited = [$tags => strtotime('now')];
        }

        Session::put('forum_last_visited', $forumLastVisited);
    }

    private function setNewSectionCounts()
    {
        $timestamps = Cache::rememberForever('forum_sidebar_timestamps', function() {
            return App::make('Lio\Caching\ForumSectionTimestampFetcher')->cacheSections(Config::get('forum.sections'));
        });
        $calculator = new Lio\Caching\UserForumSectionUpdateCountCalculator(Config::get('forum.sections'), Session::get('forum_last_visited'), $timestamps);
        $sectionCounts = $calculator->getCounts();
        View::share('sectionCounts', $sectionCounts);
    }
        // update cache for sidebar counts
        $timestamps = App::make('Lio\Caching\ForumSectionTimestampFetcher')->cacheSections(Config::get('forum.sections'));
        Cache::put('forum_sidebar_timestamps', $timestamps, 1440);
}<commit_msg>cleanup more old code from legacy controller<commit_after><?php

use Lio\Comments\CommentRepository;
use Lio\Comments\Comment;

use Lio\Tags\TagRepository;

class ForumController extends BaseController
{
    protected $categories;
    protected $comments;

    protected $threadsPerPage = 20;
    protected $commentsPerPage = 20;

    public function __construct(CommentRepository $comments, TagRepository $tags)
    {
        $this->comments = $comments;
        $this->tags     = $tags;

        View::share('forumSections', Config::get('forum.sections'));
        $this->setNewSectionCounts();
    }

    public function getComment($thread, $commentId)
    {
        // Holy shit worst code ever made..
        // LYLAS!
        $comment = Comment::findOrFail($commentId);
        $before = Comment::where('parent_id', '=', $comment->parent_id)->where('created_at', '<', $comment->created_at)->count();
        $page = round($before / $this->commentsPerPage, 0, PHP_ROUND_HALF_DOWN) + 1;

        return Redirect::to(action('ForumThreadController@show', [$thread]) . '?page=' . $page . '#comment-' . $commentId);
    }

    // does the awful code ever end?
    public function getDelete($commentId)
    {
        $comment = $this->comments->requireById($commentId);
        if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
        $this->view('forum.delete', compact('comment'));
    }

    public function postDelete($commentId)
    {
        $comment = $this->comments->requireById($commentId);
        if (Auth::user()->id != $comment->author_id) return Redirect::to('/');
        $comment->delete();
        return Redirect::action('ForumThreadController@index');
    }

    public function getSearch()
    {
        $query = Input::get('query');
        $results = App::make('Lio\Comments\ForumSearch')->searchPaginated($query, $this->threadsPerPage);
        $this->updateUserLastVisited();
        $this->view('forum.search', compact('query', 'results'));
    }

    private function updateUserLastVisited()
    {
        $forumLastVisited = Session::get('forum_last_visited');
        $tags = Input::get('tags');

        if (is_array($forumLastVisited)) {
            View::share('last_visited_timestamp', isset($forumLastVisited[$tags]) ? $forumLastVisited[$tags] : 0);
            $forumLastVisited[$tags] = strtotime('now');
        } else {
            View::share('last_visited_timestamp', 0);
            $forumLastVisited = [$tags => strtotime('now')];
        }

        Session::put('forum_last_visited', $forumLastVisited);
    }

    private function setNewSectionCounts()
    {
        $timestamps = Cache::rememberForever('forum_sidebar_timestamps', function() {
            return App::make('Lio\Caching\ForumSectionTimestampFetcher')->cacheSections(Config::get('forum.sections'));
        });
        $calculator = new Lio\Caching\UserForumSectionUpdateCountCalculator(Config::get('forum.sections'), Session::get('forum_last_visited'), $timestamps);
        $sectionCounts = $calculator->getCounts();
        View::share('sectionCounts', $sectionCounts);
    }
}
{'fraction_non_alphanumeric': 0.15877957658779576, 'fraction_numerical': 0.00311332503113325, 'mean_word_length': 3.4463667820069204, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 54, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2345745', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1840, 'n_words': 310}