diff options
author | Reuben Pereira <reuben453@gmail.com> | 2018-07-15 17:25:04 +0530 |
---|---|---|
committer | Reuben Pereira <reuben453@gmail.com> | 2018-07-15 17:25:04 +0530 |
commit | 8cc646fa0ec05820c83a163df74605f04c8877ea (patch) | |
tree | a3e80aa47a43ccd254770e0113969b3d1198195d /lib/banzai | |
parent | a08b5144fe64cf7afde116334c91e95f54c9f4fb (diff) | |
download | gitlab-ce-8cc646fa0ec05820c83a163df74605f04c8877ea.tar.gz |
Remove nodes_visible_to_user from ProjectParser and memoize readable_project_ids
Diffstat (limited to 'lib/banzai')
-rw-r--r-- | lib/banzai/reference_parser/project_parser.rb | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/lib/banzai/reference_parser/project_parser.rb b/lib/banzai/reference_parser/project_parser.rb index 2bf02e3df53..a329af42681 100644 --- a/lib/banzai/reference_parser/project_parser.rb +++ b/lib/banzai/reference_parser/project_parser.rb @@ -1,35 +1,28 @@ module Banzai module ReferenceParser class ProjectParser < BaseParser + include Gitlab::Utils::StrongMemoize + self.reference_type = :project def references_relation Project end - def nodes_visible_to_user(user, nodes) - nodes_projects_hash = lazy { projects_for_nodes(nodes) } - project_attr = 'data-project' - - readable_project_ids = projects_readable_by_user(nodes_projects_hash.values, user) - - nodes.select do |node| - if node.has_attribute?(project_attr) - readable_project_ids.include?(nodes_projects_hash[node].try(:id)) - else - true - end - end - end - private # Returns an Array of Project ids that can be read by the given user. # # projects - The projects to reduce down to those readable by the user. # user - The User for which to check the projects - def projects_readable_by_user(projects, user) - Project.public_or_visible_to_user(user).where("projects.id IN (?)", projects.map(&:id)).pluck(:id) + def readable_project_ids_for(projects, user) + strong_memoize(:readable_project_ids_for) do + Project.public_or_visible_to_user(user).where("projects.id IN (?)", projects.map(&:id)).pluck(:id) + end + end + + def can_read_reference?(user, ref_project, node) + readable_project_ids_for(@projects_for_nodes.values, user).include?(ref_project.try(:id)) end end end |