events_helper.rb 4.65 KB
Newer Older
1 2
module EventsHelper
  def link_to_author(event)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
3
    author = event.author
4

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
5 6
    if author
      link_to author.name, user_path(author.username)
7 8 9 10 11 12 13 14 15 16 17 18 19 20
    else
      event.author_name
    end
  end

  def event_action_name(event)
    target = if event.target_type
               event.target_type.titleize.downcase
             else
               'project'
             end

    [event.action_name, target].join(" ")
  end
21

22
  def event_filter_link(key, tooltip)
23 24 25 26 27 28 29 30
    key = key.to_s
    inactive = if @event_filter.active? key
                 nil
               else
                 'inactive'
               end

    content_tag :div, class: "filter_icon #{inactive}" do
31
      link_to request.path, class: 'has_tooltip event_filter_link', id: "#{key}_event_filter", 'data-original-title' => tooltip do
32
        content_tag :i, nil, class: icon_for_event[key]
33 34 35
      end
    end
  end
36 37 38

  def icon_for_event
    {
39 40 41 42
      EventFilter.push     => 'fa fa-upload',
      EventFilter.merged   => 'fa fa-check-square-o',
      EventFilter.comments => 'fa fa-comments',
      EventFilter.team     => 'fa fa-user',
43 44
    }
  end
45 46 47

  def event_feed_title(event)
    if event.issue?
48
      "#{event.author_name} #{event.action_name} issue ##{event.target_iid}: #{event.issue_title} at #{event.project_name}"
49
    elsif event.merge_request?
50
      "#{event.author_name} #{event.action_name} MR ##{event.target_iid}: #{event.merge_request_title} at #{event.project_name}"
51
    elsif event.push?
52
      "#{event.author_name} #{event.push_action_name} #{event.ref_type} #{event.ref_name} at #{event.project_name}"
53
    elsif event.membership_changed?
54
      "#{event.author_name} #{event.action_name} #{event.project_name}"
55 56
    elsif event.note? && event.note_commit?
      "#{event.author_name} commented on #{event.note_target_type} #{event.note_short_commit_id} at #{event.project_name}"
57
    elsif event.note?
58
      "#{event.author_name} commented on #{event.note_target_type} ##{truncate event.note_target_iid} at #{event.project_name}"
59 60 61 62 63 64 65 66 67 68
    else
      ""
    end
  end

  def event_feed_url(event)
    if event.issue?
      project_issue_url(event.project, event.issue)
    elsif event.merge_request?
      project_merge_request_url(event.project, event.merge_request)
69 70
    elsif event.note? && event.note_commit?
      project_commit_url(event.project, event.note_target)
71 72 73 74 75 76 77 78 79 80
    elsif event.note?
      if event.note_target
        if event.note_commit?
          project_commit_path(event.project, event.note_commit_id, anchor: dom_id(event.target))
        elsif event.note_project_snippet?
          project_snippet_path(event.project, event.note_target)
        else
          event_note_target_path(event)
        end
      end
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    elsif event.push?
      if event.push_with_commits?
        if event.commits_count > 1
          project_compare_url(event.project, from: event.commit_from, to: event.commit_to)
        else
          project_commit_url(event.project, id: event.commit_to)
        end
      else
        project_commits_url(event.project, event.ref_name)
      end
    end
  end

  def event_feed_summary(event)
    if event.issue?
      render "events/event_issue", issue: event.issue
    elsif event.push?
      render "events/event_push", event: event
99 100
    elsif event.merge_request?
      render "events/event_merge_request", merge_request: event.merge_request
101 102
    elsif event.push?
      render "events/event_push", event: event
103 104
    elsif event.note?
      render "events/event_note", note: event.note
105 106
    end
  end
107 108 109 110 111

  def event_note_target_path(event)
    if event.note? && event.note_commit?
      project_commit_path(event.project, event.note_target)
    else
112
      polymorphic_path([event.project, event.note_target], anchor: dom_id(event.target))
113 114
    end
  end
115 116 117 118

  def event_note_title_html(event)
    if event.note_target
      if event.note_commit?
119
        link_to project_commit_path(event.project, event.note_commit_id, anchor: dom_id(event.target)), class: "commit_short_id" do
120 121 122 123
          "#{event.note_target_type} #{event.note_short_commit_id}"
        end
      elsif event.note_project_snippet?
        link_to(project_snippet_path(event.project, event.note_target)) do
124
          "#{event.note_target_type} ##{truncate event.note_target_id}"
125 126 127
        end
      else
        link_to event_note_target_path(event) do
128
          "#{event.note_target_type} ##{truncate event.note_target_iid}"
129 130 131 132 133 134 135 136
        end
      end
    else
      content_tag :strong do
        "(deleted)"
      end
    end
  end
137 138

  def event_note(text)
139
    text = first_line_in_markdown(text, 150)
140
    sanitize(text, tags: %w(a img b pre code p))
141
  end
142 143 144 145 146 147

  def event_commit_title(message)
    escape_once(truncate(message.split("\n").first, length: 70))
  rescue
    "--broken encoding"
  end
148
end