<commit_before>class ProjectsController < ApplicationController
  before_action :ensure_logged_in, only: [:your_dependent_repos, :mute, :unmute,
                                          :unsubscribe, :sync]
  before_action :find_project, only: [:show, :sourcerank, :about, :dependents,
                                      :dependent_repos, :your_dependent_repos,
                                      :versions, :tags, :mute, :unmute, :unsubscribe, :sync]

  def index
    if current_user
      muted_ids = params[:include_muted].present? ? [] : current_user.muted_project_ids
      @versions = current_user.all_subscribed_versions.where.not(project_id: muted_ids).where.not(published_at: nil).newest_first.includes(project: :versions).paginate(per_page: 20, page: page_number)
      @projects = current_user.recommended_projects.limit(7)
      render 'dashboard/home'
    else
      facets = Project.facets(:facet_limit => 30)

      @languages = facets[:languages].language.buckets
      @platforms = facets[:platforms].platform.buckets
      @licenses = facets[:licenses].normalized_licenses.buckets.reject{ |t| t['key'].downcase == 'other' }
      @keywords = facets[:keywords].keywords_array.buckets
    end
  end

  def bus_factor
    problem_repos(:bus_factor_search)
  end

  def unlicensed
    problem_repos(:unlicensed_search)
  end

  def deprecated
    project_scope(:deprecated)
  end

  def removed
    project_scope(:removed)
  end

  def unmaintained
    project_scope(:unmaintained)
  end

  def show
    if incorrect_case?
      if params[:number].present?
        return redirect_to(version_path(@project.to_param.merge(number: params[:number])), :status => :moved_permanently)
      else
        return redirect_to(project_path(@project.to_param), :status => :moved_permanently)
      end
    end
    find_version
    fresh_when([@project, @version])
    @dependencies = (@versions.size > 0 ? (@version || @versions.first).dependencies.includes(project: :versions).order('project_name ASC').limit(100) : [])
    @contributors = @project.contributors.order('count DESC').visible.limit(20)
  end

  def sourcerank

  end

  def about
    find_version
    send_data render_to_string(:about, layout: false), filename: "#{@project.platform.downcase}-#{@project}.ABOUT", type: 'application/text', disposition: 'attachment'
  end

  def dependents
    @dependents = @project.dependent_projects.paginate(page: page_number)
  end

  def dependent_repos
    @dependent_repos = @project.dependent_repositories.open_source.paginate(page: page_number)
  end

  def your_dependent_repos
    @dependent_repos = current_user.your_dependent_repos(@project).paginate(page: page_number)
  end

  def versions
    if incorrect_case?
      return redirect_to(project_versions_path(@project.to_param), :status => :moved_permanently)
    else
      @versions = @project.versions.sort.paginate(page: page_number)
      respond_to do |format|
        format.html
        format.atom
      end
    end
  end

  def tags
    if incorrect_case?
      return redirect_to(project_tags_path(@project.to_param), :status => :moved_permanently)
    else
      if @project.repository.nil?
        @tags = []
      else
        @tags = @project.tags.published.order('published_at DESC').paginate(page: page_number)
      end
      respond_to do |format|
        format.html
        format.atom
      end
    end
  end

  def mute
    current_user.mute(@project)
    flash[:notice] = "Muted #{@project} notifications"
    redirect_back fallback_location: project_path(@project.to_param)
  end

  def unmute
    current_user.unmute(@project)
    flash[:notice] = "Unmuted #{@project} notifications"
    redirect_back fallback_location: project_path(@project.to_param)
  end

  def trending
    orginal_scope = Project.includes(:repository).recently_created.maintained
    scope = current_platform.present? ? orginal_scope.platform(current_platform) : orginal_scope
    scope = current_language.present? ? scope.language(current_language) : scope
    @projects = scope.hacker_news.paginate(page: page_number)
    @platforms = orginal_scope.where('repositories.stargazers_count > 0').group('projects.platform').count.reject{|k,_v| k.blank? }.sort_by{|_k,v| v }.reverse.first(20)
  end

  def unsubscribe

  end

  def sync
    if @project.recently_synced?
      flash[:error] = "Project has already been synced recently"
    else
      @project.manual_sync
      flash[:notice] = "Project has been queued to be resynced"
    end
    redirect_back fallback_location: project_path(@project.to_param)
  end

  def digital_infrastructure
    orginal_scope = Project.digital_infrastructure
    scope = current_platform.present? ? orginal_scope.platform(current_platform) : orginal_scope
    @projects = scope.order('projects.dependent_repos_count DESC').paginate(page: page_number)
    @platforms = orginal_scope.group('projects.platform').count.reject{|k,_v| k.blank? }.sort_by{|_k,v| v }.reverse.first(20)
  end

  def unseen_infrastructure
    orginal_scope = Project.unsung_heroes
    scope = current_platform.present? ? orginal_scope.platform(current_platform) : orginal_scope
    @projects = scope.order('projects.dependent_repos_count DESC').paginate(page: page_number)
    @platforms = orginal_scope.group('projects.platform').count.reject{|k,_v|÷ k.blank? }.sort_by{|_k,v| v }.reverse.first(20)
  end

  private

  def problem_repos(method_name)
    @search = Project.send(method_name, filters: {<commit_msg>Removed weird percent sign<commit_after>class ProjectsController < ApplicationController
  before_action :ensure_logged_in, only: [:your_dependent_repos, :mute, :unmute,
                                          :unsubscribe, :sync]
  before_action :find_project, only: [:show, :sourcerank, :about, :dependents,
                                      :dependent_repos, :your_dependent_repos,
                                      :versions, :tags, :mute, :unmute, :unsubscribe, :sync]

  def index
    if current_user
      muted_ids = params[:include_muted].present? ? [] : current_user.muted_project_ids
      @versions = current_user.all_subscribed_versions.where.not(project_id: muted_ids).where.not(published_at: nil).newest_first.includes(project: :versions).paginate(per_page: 20, page: page_number)
      @projects = current_user.recommended_projects.limit(7)
      render 'dashboard/home'
    else
      facets = Project.facets(:facet_limit => 30)

      @languages = facets[:languages].language.buckets
      @platforms = facets[:platforms].platform.buckets
      @licenses = facets[:licenses].normalized_licenses.buckets.reject{ |t| t['key'].downcase == 'other' }
      @keywords = facets[:keywords].keywords_array.buckets
    end
  end

  def bus_factor
    problem_repos(:bus_factor_search)
  end

  def unlicensed
    problem_repos(:unlicensed_search)
  end

  def deprecated
    project_scope(:deprecated)
  end

  def removed
    project_scope(:removed)
  end

  def unmaintained
    project_scope(:unmaintained)
  end

  def show
    if incorrect_case?
      if params[:number].present?
        return redirect_to(version_path(@project.to_param.merge(number: params[:number])), :status => :moved_permanently)
      else
        return redirect_to(project_path(@project.to_param), :status => :moved_permanently)
      end
    end
    find_version
    fresh_when([@project, @version])
    @dependencies = (@versions.size > 0 ? (@version || @versions.first).dependencies.includes(project: :versions).order('project_name ASC').limit(100) : [])
    @contributors = @project.contributors.order('count DESC').visible.limit(20)
  end

  def sourcerank

  end

  def about
    find_version
    send_data render_to_string(:about, layout: false), filename: "#{@project.platform.downcase}-#{@project}.ABOUT", type: 'application/text', disposition: 'attachment'
  end

  def dependents
    @dependents = @project.dependent_projects.paginate(page: page_number)
  end

  def dependent_repos
    @dependent_repos = @project.dependent_repositories.open_source.paginate(page: page_number)
  end

  def your_dependent_repos
    @dependent_repos = current_user.your_dependent_repos(@project).paginate(page: page_number)
  end

  def versions
    if incorrect_case?
      return redirect_to(project_versions_path(@project.to_param), :status => :moved_permanently)
    else
      @versions = @project.versions.sort.paginate(page: page_number)
      respond_to do |format|
        format.html
        format.atom
      end
    end
  end

  def tags
    if incorrect_case?
      return redirect_to(project_tags_path(@project.to_param), :status => :moved_permanently)
    else
      if @project.repository.nil?
        @tags = []
      else
        @tags = @project.tags.published.order('published_at DESC').paginate(page: page_number)
      end
      respond_to do |format|
        format.html
        format.atom
      end
    end
  end

  def mute
    current_user.mute(@project)
    flash[:notice] = "Muted #{@project} notifications"
    redirect_back fallback_location: project_path(@project.to_param)
  end

  def unmute
    current_user.unmute(@project)
    flash[:notice] = "Unmuted #{@project} notifications"
    redirect_back fallback_location: project_path(@project.to_param)
  end

  def trending
    orginal_scope = Project.includes(:repository).recently_created.maintained
    scope = current_platform.present? ? orginal_scope.platform(current_platform) : orginal_scope
    scope = current_language.present? ? scope.language(current_language) : scope
    @projects = scope.hacker_news.paginate(page: page_number)
    @platforms = orginal_scope.where('repositories.stargazers_count > 0').group('projects.platform').count.reject{|k,_v| k.blank? }.sort_by{|_k,v| v }.reverse.first(20)
  end

  def unsubscribe

  end

  def sync
    if @project.recently_synced?
      flash[:error] = "Project has already been synced recently"
    else
      @project.manual_sync
      flash[:notice] = "Project has been queued to be resynced"
    end
    redirect_back fallback_location: project_path(@project.to_param)
  end

  def digital_infrastructure
    orginal_scope = Project.digital_infrastructure
    scope = current_platform.present? ? orginal_scope.platform(current_platform) : orginal_scope
    @projects = scope.order('projects.dependent_repos_count DESC').paginate(page: page_number)
    @platforms = orginal_scope.group('projects.platform').count.reject{|k,_v| k.blank? }.sort_by{|_k,v| v }.reverse.first(20)
  end

  def unseen_infrastructure
    orginal_scope = Project.unsung_heroes
    scope = current_platform.present? ? orginal_scope.platform(current_platform) : orginal_scope
    @projects = scope.order('projects.dependent_repos_count DESC').paginate(page: page_number)
    @platforms = orginal_scope.group('projects.platform').count.reject{|k,_v| k.blank? }.sort_by{|_k,v| v }.reverse.first(20)
  end

  private

  def problem_repos(method_name)
    @search = Project.send(method_name, filters: {
{'fraction_non_alphanumeric': 0.10788947464097437, 'fraction_numerical': 0.003272132339574623, 'mean_word_length': 3.8216476774758985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4724143', 'n_tokens_mistral': 3534, 'n_tokens_neox': 3360, 'n_words': 629}