debian-mirror-gitlab/app/services/preview_markdown_service.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class PreviewMarkdownService < BaseService
def execute
2017-09-10 17:25:29 +05:30
text, commands = explain_quick_actions(params[:text])
2017-08-17 22:00:37 +05:30
users = find_user_references(text)
2019-02-15 15:39:39 +05:30
suggestions = find_suggestions(text)
2017-08-17 22:00:37 +05:30
success(
text: text,
users: users,
2019-02-15 15:39:39 +05:30
suggestions: suggestions,
2019-03-02 22:35:43 +05:30
commands: commands.join(' ')
2017-08-17 22:00:37 +05:30
)
end
private
2017-09-10 17:25:29 +05:30
def explain_quick_actions(text)
2019-05-30 16:15:17 +05:30
return text, [] unless %w(Issue MergeRequest Commit).include?(commands_target_type)
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
quick_actions_service = QuickActions::InterpretService.new(project, current_user)
quick_actions_service.explain(text, find_commands_target)
2017-08-17 22:00:37 +05:30
end
def find_user_references(text)
extractor = Gitlab::ReferenceExtractor.new(project, current_user)
extractor.analyze(text, author: current_user)
extractor.users.map(&:username)
end
2019-02-15 15:39:39 +05:30
def find_suggestions(text)
2019-05-30 16:15:17 +05:30
return [] unless params[:preview_suggestions]
2019-02-15 15:39:39 +05:30
2019-05-30 16:15:17 +05:30
Banzai::SuggestionsParser.parse(text)
2019-02-15 15:39:39 +05:30
end
2017-08-17 22:00:37 +05:30
def find_commands_target
2018-11-20 20:47:30 +05:30
QuickActions::TargetService
.new(project, current_user)
2019-05-30 16:15:17 +05:30
.execute(commands_target_type, commands_target_id)
2017-08-17 22:00:37 +05:30
end
2019-05-30 16:15:17 +05:30
def commands_target_type
params[:quick_actions_target_type]
2017-08-17 22:00:37 +05:30
end
2019-05-30 16:15:17 +05:30
def commands_target_id
params[:quick_actions_target_id]
2017-08-17 22:00:37 +05:30
end
end