2018-07-23 09:32:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 17:11:48 +00:00
|
|
|
module Banzai
|
|
|
|
module Filter
|
|
|
|
# HTML filter that appends state information to issuable links.
|
2020-01-21 12:08:31 +00:00
|
|
|
# Runs as a post-process filter as issuable state might change while
|
2016-08-24 17:11:48 +00:00
|
|
|
# Markdown is in the cache.
|
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
|
|
|
class IssuableStateFilter < HTML::Pipeline::Filter
|
|
|
|
VISIBLE_STATES = %w(closed merged).freeze
|
|
|
|
|
|
|
|
def call
|
2017-04-19 14:38:46 +00:00
|
|
|
return doc unless context[:issuable_state_filter_enabled]
|
|
|
|
|
2018-04-03 13:45:17 +00:00
|
|
|
context = RenderContext.new(project, current_user)
|
|
|
|
extractor = Banzai::IssuableExtractor.new(context)
|
2016-08-24 17:11:48 +00:00
|
|
|
issuables = extractor.extract([doc])
|
|
|
|
|
|
|
|
issuables.each do |node, issuable|
|
2020-02-24 03:09:05 +00:00
|
|
|
next if !can_read_cross_project? && cross_referenced?(issuable)
|
2017-12-11 14:21:06 +00:00
|
|
|
|
2018-03-20 08:29:15 +00:00
|
|
|
if VISIBLE_STATES.include?(issuable.state) && issuable_reference?(node.inner_html, issuable)
|
2019-08-30 06:16:01 +00:00
|
|
|
state = moved_issue?(issuable) ? s_("IssuableStatus|moved") : issuable.state
|
|
|
|
node.content += " (#{state})"
|
2016-08-24 17:11:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-30 06:16:01 +00:00
|
|
|
def moved_issue?(issuable)
|
|
|
|
issuable.instance_of?(Issue) && issuable.moved?
|
|
|
|
end
|
|
|
|
|
2018-03-20 08:29:15 +00:00
|
|
|
def issuable_reference?(text, issuable)
|
2018-10-15 08:36:07 +00:00
|
|
|
CGI.unescapeHTML(text) == issuable.reference_link_text(project || group)
|
|
|
|
end
|
|
|
|
|
2020-02-24 03:09:05 +00:00
|
|
|
def cross_referenced?(issuable)
|
2018-10-15 08:36:07 +00:00
|
|
|
return true if issuable.project != project
|
|
|
|
return true if issuable.respond_to?(:group) && issuable.group != group
|
|
|
|
|
|
|
|
false
|
2018-03-20 08:29:15 +00:00
|
|
|
end
|
|
|
|
|
2017-12-11 14:21:06 +00:00
|
|
|
def can_read_cross_project?
|
|
|
|
Ability.allowed?(current_user, :read_cross_project)
|
|
|
|
end
|
|
|
|
|
2016-08-24 17:11:48 +00:00
|
|
|
def current_user
|
|
|
|
context[:current_user]
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
context[:project]
|
|
|
|
end
|
2018-03-20 08:29:15 +00:00
|
|
|
|
|
|
|
def group
|
|
|
|
context[:group]
|
|
|
|
end
|
2016-08-24 17:11:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|