BigW Consortium Gitlab

projects_finder.rb 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9
# ProjectsFinder
#
# Used to filter Projects  by set of params
#
# Arguments:
#   current_user - which user use
#   project_ids_relation: int[] - project ids to use
#   params:
#     trending: boolean
10
#     owned: boolean
11 12 13 14 15 16 17 18 19
#     non_public: boolean
#     starred: boolean
#     sort: string
#     visibility_level: int
#     tags: string[]
#     personal: boolean
#     search: string
#     non_archived: boolean
#
20
class ProjectsFinder < UnionFinder
21 22
  attr_accessor :params
  attr_reader :current_user, :project_ids_relation
23

24 25 26 27 28 29 30
  def initialize(params: {}, current_user: nil, project_ids_relation: nil)
    @params = params
    @current_user = current_user
    @project_ids_relation = project_ids_relation
  end

  def execute
vanadium23 committed
31 32 33 34 35 36 37 38
    user = params.delete(:user)
    collection =
      if user
        PersonalProjectsFinder.new(user).execute(current_user)
      else
        init_collection
      end

39 40 41 42 43 44 45 46 47 48
    collection = by_ids(collection)
    collection = by_personal(collection)
    collection = by_starred(collection)
    collection = by_trending(collection)
    collection = by_visibilty_level(collection)
    collection = by_tags(collection)
    collection = by_search(collection)
    collection = by_archived(collection)

    sort(collection)
49 50 51 52
  end

  private

53
  def init_collection
54 55 56 57 58 59
    if current_user
      collection_with_user
    else
      collection_without_user
    end
  end
Zeger-Jan van de Weg committed
60

61 62 63
  def collection_with_user
    if owned_projects?
      current_user.owned_projects
64
    else
65 66 67
      if private_only?
        current_user.authorized_projects
      else
68
        Project.public_or_visible_to_user(current_user)
69
      end
70
    end
71 72 73 74 75 76 77 78 79 80 81 82 83 84
  end

  # Builds a collection for an anonymous user.
  def collection_without_user
    if private_only? || owned_projects?
      Project.none
    else
      Project.public_to_user
    end
  end

  def owned_projects?
    params[:owned].present?
  end
85

86 87
  def private_only?
    params[:non_public].present?
88
  end
89 90

  def by_ids(items)
91
    project_ids_relation ? items.where(id: project_ids_relation) : items
92 93 94 95 96 97 98 99 100 101
  end

  def union(items)
    find_union(items, Project).with_route
  end

  def by_personal(items)
    (params[:personal].present? && current_user) ? items.personal(current_user) : items
  end

102 103 104 105
  def by_starred(items)
    (params[:starred].present? && current_user) ? items.starred_by(current_user) : items
  end

106 107 108 109
  def by_trending(items)
    params[:trending].present? ? items.trending : items
  end

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  def by_visibilty_level(items)
    params[:visibility_level].present? ? items.where(visibility_level: params[:visibility_level]) : items
  end

  def by_tags(items)
    params[:tag].present? ? items.tagged_with(params[:tag]) : items
  end

  def by_search(items)
    params[:search] ||= params[:name]
    params[:search].present? ? items.search(params[:search]) : items
  end

  def sort(items)
    params[:sort].present? ? items.sort(params[:sort]) : items
  end

  def by_archived(projects)
128 129 130 131 132 133 134 135 136 137 138 139 140
    if params[:non_archived]
      projects.non_archived
    elsif params.key?(:archived) # Back-compatibility with the places where `params[:archived]` can be set explicitly to `false`
      if params[:archived] == 'only'
        projects.archived
      elsif Gitlab::Utils.to_boolean(params[:archived])
        projects
      else
        projects.non_archived
      end
    else
      projects
    end
141
  end
142
end