<commit_before># frozen_string_literal: true

class DashboardsController < ApplicationController
  def show
    @user = current_user
    @groups = @user.groups

    @relevant_issues = IssueDecorator.decorate_collection(
      current_user.issues_near_locations.order(updated_at: :desc)
      .page(params[:relevant_issues_page]).per(10)
    )

    subscribed_threads =
      current_user
      .subscribed_threads.order_by_latest_message.page(params[:subscribed_threads_page]).per(12)

    @subscribed_threads = ThreadListDecorator.decorate_collection(
      subscribed_threads.includes(:issue, latest_message: %i[component created_by])
    )
    @unviewed_thread_ids = MessageThread.unviewed_thread_ids(user: current_user, threads: subscribed_threads)

    deadline_threads = ThreadList.with_upcoming_deadlines(current_user, 30).includes(:issue, :latest_message)
    @deadline_threads = ThreadListDecorator.decorate_collection deadline_threads

    prioritised_threads =
      current_user
      .prioritised_threads.order("priority desc").order_by_latest_message
      .includes(:issue, latest_message: %i[component created_by])
      .page(params[:prioritised_threads_page]).per(20)

    @prioritised_threads = ThreadListDecorator.decorate_collection(prioritised_threads)
    @user_priorities = current_user.thread_priorities.where(thread: prioritised_threads).to_a
    @planning_applications = PlanningApplicationDecorator.decorate_collection(
      current_user
      .planning_applications_near_locations.ordered.page(params[:planning_page]).per(10).includes(:issue, :users)
    )
  end

  def deadlines
    cal = Icalendar::Calendar.new
    ThreadList.with_upcoming_deadlines(User.find_by(public_token: params[:public_token]), 50).each do |thread|
      thread.to_icals.each { |evt| cal.add_event(evt) }
    end
    render plain: cal.to_ical
  end

  def search
    # Ideally, this would be delegated to the different controllers.

    @query = params[:query]

    # Threads, with permission check
    # duplicated logic from authorization rules
    # as we can't paginate AND use permitted_to
    threads = MessageThread.search(include: [:group, :issue, messages: :created_by]) do
      fulltext params[:query] do
        boost_fields id: 4, title: 2, tags_string: 1
      end<commit_msg>Remove component from some controller includes<commit_after># frozen_string_literal: true

class DashboardsController < ApplicationController
  def show
    @user = current_user
    @groups = @user.groups

    @relevant_issues = IssueDecorator.decorate_collection(
      current_user.issues_near_locations.order(updated_at: :desc)
      .page(params[:relevant_issues_page]).per(10)
    )

    subscribed_threads =
      current_user
      .subscribed_threads.order_by_latest_message.page(params[:subscribed_threads_page]).per(12)

    @subscribed_threads = ThreadListDecorator.decorate_collection(
      subscribed_threads.includes(:issue, latest_message: %i[created_by])
    )
    @unviewed_thread_ids = MessageThread.unviewed_thread_ids(user: current_user, threads: subscribed_threads)

    deadline_threads = ThreadList.with_upcoming_deadlines(current_user, 30).includes(:issue, :latest_message)
    @deadline_threads = ThreadListDecorator.decorate_collection deadline_threads

    prioritised_threads =
      current_user
      .prioritised_threads.order("priority desc").order_by_latest_message
      .includes(:issue, latest_message: %i[created_by])
      .page(params[:prioritised_threads_page]).per(20)

    @prioritised_threads = ThreadListDecorator.decorate_collection(prioritised_threads)
    @user_priorities = current_user.thread_priorities.where(thread: prioritised_threads).to_a
    @planning_applications = PlanningApplicationDecorator.decorate_collection(
      current_user
      .planning_applications_near_locations.ordered.page(params[:planning_page]).per(10).includes(:issue, :users)
    )
  end

  def deadlines
    cal = Icalendar::Calendar.new
    ThreadList.with_upcoming_deadlines(User.find_by(public_token: params[:public_token]), 50).each do |thread|
      thread.to_icals.each { |evt| cal.add_event(evt) }
    end
    render plain: cal.to_ical
  end

  def search
    # Ideally, this would be delegated to the different controllers.

    @query = params[:query]

    # Threads, with permission check
    # duplicated logic from authorization rules
    # as we can't paginate AND use permitted_to
    threads = MessageThread.search(include: [:group, :issue, messages: :created_by]) do
      fulltext params[:query] do
        boost_fields id: 4, title: 2, tags_string: 1
      end
{'fraction_non_alphanumeric': 0.08859934853420195, 'fraction_numerical': 0.006514657980456026, 'mean_word_length': 4.714640198511166, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '7473630', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1361, 'n_words': 246}