debian-mirror-gitlab/app/helpers/events_helper.rb

306 lines
8.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module EventsHelper
2017-08-17 22:00:37 +05:30
ICON_NAMES_BY_EVENT_TYPE = {
2018-03-17 18:26:18 +05:30
'pushed to' => 'commit',
'pushed new' => 'commit',
2020-05-24 23:13:21 +05:30
'updated' => 'commit',
2018-03-17 18:26:18 +05:30
'created' => 'status_open',
'opened' => 'status_open',
'closed' => 'status_closed',
'accepted' => 'fork',
'commented on' => 'comment',
'deleted' => 'remove',
2020-05-24 23:13:21 +05:30
'destroyed' => 'remove',
2018-03-17 18:26:18 +05:30
'imported' => 'import',
'joined' => 'users'
2017-08-17 22:00:37 +05:30
}.freeze
def link_to_author(event, self_added: false)
2014-09-02 18:07:02 +05:30
author = event.author
if author
2017-08-17 22:00:37 +05:30
name = self_added ? 'You' : author.name
link_to name, user_path(author.username), title: name
2014-09-02 18:07:02 +05:30
else
2018-11-20 20:47:30 +05:30
escape_once(event.author_name)
2014-09-02 18:07:02 +05:30
end
end
def event_action_name(event)
2015-04-26 12:48:37 +05:30
target = if event.target_type
2020-07-28 23:09:34 +05:30
if event.design? || event.design_note?
'design'
elsif event.wiki_page?
'wiki page'
elsif event.note?
2015-04-26 12:48:37 +05:30
event.note_target_type
else
event.target_type.titleize.downcase
end
else
'project'
end
2014-09-02 18:07:02 +05:30
[event.action_name, target].join(" ")
end
2018-03-17 18:26:18 +05:30
def event_filter_link(key, text, tooltip)
2014-09-02 18:07:02 +05:30
key = key.to_s
2015-09-11 14:41:01 +05:30
active = 'active' if @event_filter.active?(key)
link_opts = {
2018-11-08 19:23:39 +05:30
class: "event-filter-link",
2015-09-11 14:41:01 +05:30
id: "#{key}_event_filter",
2018-03-17 18:26:18 +05:30
title: tooltip
2015-09-11 14:41:01 +05:30
}
2015-04-26 12:48:37 +05:30
content_tag :li, class: active do
link_to request.path, link_opts do
2018-03-17 18:26:18 +05:30
content_tag(:span, ' ' + text)
end
2014-09-02 18:07:02 +05:30
end
end
2017-08-17 22:00:37 +05:30
def event_filter_visible(feature_key)
2020-07-28 23:09:34 +05:30
return designs_visible? if feature_key == :designs
2017-08-17 22:00:37 +05:30
return true unless @project
@project.feature_available?(feature_key, current_user)
end
2020-07-28 23:09:34 +05:30
def designs_visible?
if @project
design_activity_enabled?(@project)
elsif @group
design_activity_enabled?(@group)
elsif @projects
@projects.with_namespace.include_project_feature.any? { |p| design_activity_enabled?(p) }
else
true
end
end
def design_activity_enabled?(project)
Ability.allowed?(current_user, :read_design_activity, project)
end
2017-08-17 22:00:37 +05:30
def comments_visible?
event_filter_visible(:repository) ||
event_filter_visible(:merge_requests) ||
event_filter_visible(:issues)
end
2015-09-25 12:07:36 +05:30
def event_preposition(event)
2020-04-22 19:07:51 +05:30
if event.wiki_page?
'in the wiki for'
2015-09-25 12:07:36 +05:30
elsif event.milestone?
2020-04-22 19:07:51 +05:30
'in'
elsif event.push_action? || event.commented_action? || event.target
'at'
2015-09-25 12:07:36 +05:30
end
end
2014-09-02 18:07:02 +05:30
def event_feed_title(event)
2015-04-26 12:48:37 +05:30
words = []
words << event.author_name
words << event_action_name(event)
2019-09-04 21:01:54 +05:30
if event.push_action?
2015-04-26 12:48:37 +05:30
words << event.ref_type
words << event.ref_name
words << "at"
2019-09-04 21:01:54 +05:30
elsif event.commented_action?
2016-06-02 11:05:42 +05:30
words << event.note_target_reference
2015-04-26 12:48:37 +05:30
words << "at"
2015-09-25 12:07:36 +05:30
elsif event.milestone?
words << "##{event.target_iid}" if event.target_iid
words << "in"
2020-07-28 23:09:34 +05:30
elsif event.design?
words << event.design.to_reference
words << "in"
elsif event.wiki_page?
words << event.target_title
words << "in"
2015-04-26 12:48:37 +05:30
elsif event.target
2018-12-13 13:39:08 +05:30
prefix =
if event.merge_request?
MergeRequest.reference_prefix
else
Issue.reference_prefix
end
words << "#{prefix}#{event.target_iid}:" if event.target_iid
2015-04-26 12:48:37 +05:30
words << event.target.title if event.target.respond_to?(:title)
words << "at"
2014-09-02 18:07:02 +05:30
end
2015-04-26 12:48:37 +05:30
2019-12-04 20:38:33 +05:30
words << event.resource_parent_name
2015-04-26 12:48:37 +05:30
words.join(" ")
2014-09-02 18:07:02 +05:30
end
def event_feed_url(event)
if event.issue?
2017-09-10 17:25:29 +05:30
project_issue_url(event.project,
2015-04-26 12:48:37 +05:30
event.issue)
2014-09-02 18:07:02 +05:30
elsif event.merge_request?
2017-09-10 17:25:29 +05:30
project_merge_request_url(event.project, event.merge_request)
2016-11-24 13:41:30 +05:30
elsif event.commit_note?
2017-09-10 17:25:29 +05:30
project_commit_url(event.project,
2015-04-26 12:48:37 +05:30
event.note_target)
2014-09-02 18:07:02 +05:30
elsif event.note?
if event.note_target
2018-11-20 20:47:30 +05:30
event_note_target_url(event)
2014-09-02 18:07:02 +05:30
end
2019-09-04 21:01:54 +05:30
elsif event.push_action?
2015-11-26 14:37:03 +05:30
push_event_feed_url(event)
2019-09-04 21:01:54 +05:30
elsif event.created_project_action?
2018-11-20 20:47:30 +05:30
project_url(event.project)
2015-11-26 14:37:03 +05:30
end
end
def push_event_feed_url(event)
if event.push_with_commits? && event.md_ref?
if event.commits_count > 1
2017-09-10 17:25:29 +05:30
project_compare_url(event.project,
2015-11-26 14:37:03 +05:30
from: event.commit_from, to:
event.commit_to)
2014-09-02 18:07:02 +05:30
else
2017-09-10 17:25:29 +05:30
project_commit_url(event.project,
2015-11-26 14:37:03 +05:30
id: event.commit_to)
2014-09-02 18:07:02 +05:30
end
2015-11-26 14:37:03 +05:30
else
2017-09-10 17:25:29 +05:30
project_commits_url(event.project,
2015-11-26 14:37:03 +05:30
event.ref_name)
2014-09-02 18:07:02 +05:30
end
end
def event_feed_summary(event)
if event.issue?
render "events/event_issue", issue: event.issue
2019-09-04 21:01:54 +05:30
elsif event.push_action?
2014-09-02 18:07:02 +05:30
render "events/event_push", event: event
elsif event.merge_request?
render "events/event_merge_request", merge_request: event.merge_request
elsif event.note?
render "events/event_note", note: event.note
end
end
2018-11-20 20:47:30 +05:30
def event_note_target_url(event)
2016-11-24 13:41:30 +05:30
if event.commit_note?
2018-11-20 20:47:30 +05:30
project_commit_url(event.project, event.note_target, anchor: dom_id(event.target))
2016-06-02 11:05:42 +05:30
elsif event.project_snippet_note?
2018-11-20 20:47:30 +05:30
project_snippet_url(event.project, event.note_target, anchor: dom_id(event.target))
2019-02-15 15:39:39 +05:30
elsif event.issue_note?
project_issue_url(event.project, id: event.note_target, anchor: dom_id(event.target))
elsif event.merge_request_note?
project_merge_request_url(event.project, id: event.note_target, anchor: dom_id(event.target))
2020-05-24 23:13:21 +05:30
elsif event.design_note?
design_url(event.note_target, anchor: dom_id(event.note))
2014-09-02 18:07:02 +05:30
else
2020-10-24 23:57:45 +05:30
polymorphic_url([event.project, event.note_target],
2016-06-02 11:05:42 +05:30
anchor: dom_id(event.target))
2014-09-02 18:07:02 +05:30
end
end
2020-04-22 19:07:51 +05:30
def event_wiki_title_html(event)
capture do
2020-07-28 23:09:34 +05:30
concat content_tag(:span, _('wiki page'), class: "event-target-type gl-mr-2")
2020-04-22 19:07:51 +05:30
concat link_to(event.target_title, event_wiki_page_target_url(event),
title: event.target_title,
2020-07-28 23:09:34 +05:30
class: 'has-tooltip event-target-link gl-mr-2')
end
end
def event_design_title_html(event)
capture do
concat content_tag(:span, _('design'), class: "event-target-type gl-mr-2")
concat link_to(event.design.reference_link_text, design_url(event.design),
title: event.target_title,
class: 'has-tooltip event-design event-target-link gl-mr-2')
2020-04-22 19:07:51 +05:30
end
end
def event_wiki_page_target_url(event)
2020-06-23 00:09:42 +05:30
project_wiki_url(event.project, event.target&.canonical_slug || Wiki::HOMEPAGE)
2020-04-22 19:07:51 +05:30
end
2014-09-02 18:07:02 +05:30
def event_note_title_html(event)
if event.note_target
2018-12-13 13:39:08 +05:30
capture do
2020-07-28 23:09:34 +05:30
concat content_tag(:span, event.note_target_type, class: "event-target-type gl-mr-2")
concat link_to(event.note_target_reference, event_note_target_url(event), title: event.target_title, class: 'has-tooltip event-target-link gl-mr-2')
2018-12-13 13:39:08 +05:30
end
2014-09-02 18:07:02 +05:30
else
2016-06-02 11:05:42 +05:30
content_tag(:strong, '(deleted)')
2014-09-02 18:07:02 +05:30
end
end
def event_commit_title(message)
2017-09-10 17:25:29 +05:30
message ||= ''
2016-11-03 12:29:30 +05:30
(message.split("\n").first || "").truncate(70)
2014-09-02 18:07:02 +05:30
rescue
"--broken encoding"
end
2015-04-26 12:48:37 +05:30
2018-12-13 13:39:08 +05:30
def icon_for_event(note, size: 24)
2017-08-17 22:00:37 +05:30
icon_name = ICON_NAMES_BY_EVENT_TYPE[note]
2018-12-13 13:39:08 +05:30
sprite_icon(icon_name, size: size) if icon_name
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
DESIGN_ICONS = {
'created' => 'upload',
'updated' => 'pencil',
'destroyed' => ICON_NAMES_BY_EVENT_TYPE['destroyed'],
'archived' => 'archive'
}.freeze
def design_event_icon(action, size: 24)
icon_name = DESIGN_ICONS[action]
sprite_icon(icon_name, size: size) if icon_name
end
2017-08-17 22:00:37 +05:30
def icon_for_profile_event(event)
if current_path?('users#show')
content_tag :div, class: "system-note-image #{event.action_name.parameterize}-icon" do
icon_for_event(event.action_name)
end
else
content_tag :div, class: 'system-note-image user-avatar' do
2018-12-13 13:39:08 +05:30
author_avatar(event, size: 40)
2017-08-17 22:00:37 +05:30
end
end
end
2018-12-13 13:39:08 +05:30
def inline_event_icon(event)
unless current_path?('users#show')
2020-07-28 23:09:34 +05:30
content_tag :span, class: "system-note-image-inline d-none d-sm-flex gl-mr-2 #{event.action_name.parameterize}-icon align-self-center" do
next design_event_icon(event.action, size: 14) if event.design?
2018-12-13 13:39:08 +05:30
icon_for_event(event.action_name, size: 14)
end
end
end
def event_user_info(event)
content_tag(:div, class: "event-user-info") do
concat content_tag(:span, link_to_author(event), class: "author_name")
concat "&nbsp;".html_safe
concat content_tag(:span, event.author.to_reference, class: "username")
end
end
2020-05-24 23:13:21 +05:30
private
2020-07-28 23:09:34 +05:30
def design_url(design, opts = {})
2020-05-24 23:13:21 +05:30
designs_project_issue_url(
design.project,
design.issue,
opts.merge(vueroute: design.filename)
)
end
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
EventsHelper.prepend_if_ee('EE::EventsHelper')