user_parser.rb 2.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
module Banzai
  module ReferenceParser
    class UserParser < BaseParser
      self.reference_type = :user

      def referenced_by(nodes)
        group_ids = []
        user_ids = []
        project_ids = []

        nodes.each do |node|
          if node.has_attribute?('data-group')
            group_ids << node.attr('data-group').to_i
          elsif node.has_attribute?(self.class.data_attribute)
            user_ids << node.attr(self.class.data_attribute).to_i
          elsif node.has_attribute?('data-project')
            project_ids << node.attr('data-project').to_i
          end
        end

        find_users_for_groups(group_ids) | find_users(user_ids) |
          find_users_for_projects(project_ids)
      end

      def nodes_visible_to_user(user, nodes)
        group_attr = 'data-group'
        groups = lazy { grouped_objects_for_nodes(nodes, Group, group_attr) }
        visible = []
        remaining = []

        nodes.each do |node|
          if node.has_attribute?(group_attr)
            node_group = groups[node.attr(group_attr).to_i]

            if node_group &&
              can?(user, :read_group, node_group)
              visible << node
            end
          # Remaining nodes will be processed by the parent class'
          # implementation of this method.
          else
            remaining << node
          end
        end

        visible + super(current_user, remaining)
      end

      def nodes_user_can_reference(current_user, nodes)
        project_attr = 'data-project'
        author_attr = 'data-author'

        projects = lazy { projects_for_nodes(nodes) }
        users = lazy { grouped_objects_for_nodes(nodes, User, author_attr) }

        nodes.select do |node|
          project_id = node.attr(project_attr)
          user_id = node.attr(author_attr)

          if project && project_id && project.id == project_id.to_i
            true
          elsif project_id && user_id
            project = projects[project_id.to_i]
            user = users[user_id.to_i]

            project && user ? project.team.member?(user) : false
          else
            true
          end
        end
      end

      def find_users(ids)
        return [] if ids.empty?

76
        collection_objects_for_ids(User, ids)
77 78 79 80 81 82 83 84 85 86 87
      end

      def find_users_for_groups(ids)
        return [] if ids.empty?

        User.joins(:group_members).where(members: { source_id: ids }).to_a
      end

      def find_users_for_projects(ids)
        return [] if ids.empty?

88 89
        collection_objects_for_ids(Project, ids).
          flat_map { |p| p.team.members.to_a }
90 91 92 93
      end
    end
  end
end