Class: Banzai::Filter::UserReferenceFilter

Inherits:
ReferenceFilter
  • Object
show all
Defined in:
lib/banzai/filter/user_reference_filter.rb

Overview

HTML filter that replaces user or group references with links.

A special `@all` reference is also supported.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ReferenceFilter

#data_attribute, #each_node, #element_node?, #escape_once, #ignore_ancestor_query, #project, #reference_class, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #text_node?, #validate, #yield_valid_link

Class Method Details

.referenced_by(node) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/banzai/filter/user_reference_filter.rb', line 24

def self.referenced_by(node)
  if node.has_attribute?('data-group')
    group = Group.find(node.attr('data-group')) rescue nil
    return unless group

    { user: group.users }
  elsif node.has_attribute?('data-user')
    { user: LazyReference.new(User, node.attr('data-user')) }
  elsif node.has_attribute?('data-project')
    project = Project.find(node.attr('data-project')) rescue nil
    return unless project

    { user: project.team.members.flatten }
  end
end

.references_in(text) ⇒ Object

Public: Find `@user` user references in text

UserReferenceFilter.references_in(text) do |match, username|
  "<a href=...>@#{user}</a>"
end

text - String text to search.

Yields the String match, and the String user name.

Returns a String replaced with the return of the block.


18
19
20
21
22
# File 'lib/banzai/filter/user_reference_filter.rb', line 18

def self.references_in(text)
  text.gsub(User.reference_pattern) do |match|
    yield match, $~[:user]
  end
end

.user_can_reference?(user, node, context) ⇒ Boolean


49
50
51
52
53
54
55
56
57
58
59
# File 'lib/banzai/filter/user_reference_filter.rb', line 49

def self.user_can_reference?(user, node, context)
  # Only team members can reference `@all`
  if node.has_attribute?('data-project')
    project = Project.find(node.attr('data-project')) rescue nil
    return false unless project

    user && project.team.member?(user)
  else
    super
  end
end

.user_can_see_reference?(user, node, context) ⇒ Boolean


40
41
42
43
44
45
46
47
# File 'lib/banzai/filter/user_reference_filter.rb', line 40

def self.user_can_see_reference?(user, node, context)
  if node.has_attribute?('data-group')
    group = Group.find(node.attr('data-group')) rescue nil
    Ability.abilities.allowed?(user, :read_group, group)
  else
    super
  end
end

Instance Method Details

#callObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/banzai/filter/user_reference_filter.rb', line 61

def call
  return doc if project.nil?

  ref_pattern = User.reference_pattern
  ref_pattern_start = /\A#{ref_pattern}\z/

  each_node do |node|
    if text_node?(node)
      replace_text_when_pattern_matches(node, ref_pattern) do |content|
        user_link_filter(content)
      end
    elsif element_node?(node)
      yield_valid_link(node) do |link, text|
        if link =~ ref_pattern_start
          replace_link_node_with_href(node, link) do
            user_link_filter(link, link_text: text)
          end
        end
      end
    end
  end

  doc
end

Replace `@user` user references in text with links to the referenced user's profile page.

text - String text to replace references in.

Returns a String with `@user` references replaced with links. All links have `gfm` and `gfm-project_member` class names attached for styling.


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/banzai/filter/user_reference_filter.rb', line 93

def user_link_filter(text, link_text: nil)
  self.class.references_in(text) do |match, username|
    if username == 'all'
      link_to_all(link_text: link_text)
    elsif namespace = Namespace.find_by(path: username)
      link_to_namespace(namespace, link_text: link_text) || match
    else
      match
    end
  end
end