2016-11-25 14:36:37 -05:00
|
|
|
module ChatMessage
|
2015-03-05 13:38:23 -05:00
|
|
|
class NoteMessage < BaseMessage
|
|
|
|
attr_reader :note
|
|
|
|
attr_reader :note_url
|
2017-04-04 11:10:21 -04:00
|
|
|
attr_reader :title
|
|
|
|
attr_reader :target
|
2015-03-05 13:38:23 -05:00
|
|
|
|
|
|
|
def initialize(params)
|
2017-04-04 11:10:21 -04:00
|
|
|
super
|
2017-04-04 05:16:24 -04:00
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
params = HashWithIndifferentAccess.new(params)
|
|
|
|
obj_attr = params[:object_attributes]
|
2015-03-05 13:38:23 -05:00
|
|
|
@note = obj_attr[:note]
|
|
|
|
@note_url = obj_attr[:url]
|
2017-04-04 11:10:21 -04:00
|
|
|
@target, @title = case obj_attr[:noteable_type]
|
|
|
|
when "Commit"
|
|
|
|
create_commit_note(params[:commit])
|
|
|
|
when "Issue"
|
|
|
|
create_issue_note(params[:issue])
|
|
|
|
when "MergeRequest"
|
|
|
|
create_merge_note(params[:merge_request])
|
|
|
|
when "Snippet"
|
|
|
|
create_snippet_note(params[:snippet])
|
|
|
|
end
|
2017-04-03 12:39:17 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
def attachments
|
|
|
|
return note if markdown
|
|
|
|
|
|
|
|
description_message
|
2017-04-03 12:39:17 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
def activity
|
|
|
|
{
|
|
|
|
title: "#{user_name} #{link('commented on ' + target, note_url)}",
|
|
|
|
subtitle: "in #{project_link}",
|
|
|
|
text: formatted_title,
|
|
|
|
image: user_avatar
|
|
|
|
}
|
2017-04-03 12:39:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def message
|
2017-04-04 11:10:21 -04:00
|
|
|
"#{user_name} #{link('commented on ' + target, note_url)} in #{project_link}: *#{formatted_title}*"
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_title(title)
|
|
|
|
title.lines.first.chomp
|
|
|
|
end
|
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
def formatted_title
|
|
|
|
format_title(title)
|
|
|
|
end
|
|
|
|
|
2017-04-03 12:39:17 -04:00
|
|
|
def create_issue_note(issue)
|
2017-04-04 11:10:21 -04:00
|
|
|
["issue #{Issue.reference_prefix}#{issue[:iid]}", issue[:title]]
|
2017-04-03 12:39:17 -04:00
|
|
|
end
|
|
|
|
|
2015-03-05 13:38:23 -05:00
|
|
|
def create_commit_note(commit)
|
2017-04-04 05:16:24 -04:00
|
|
|
commit_sha = Commit.truncate_sha(commit[:id])
|
2015-03-05 13:38:23 -05:00
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
["commit #{commit_sha}", commit[:message]]
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_merge_note(merge_request)
|
2017-04-04 11:10:21 -04:00
|
|
|
["merge request #{MergeRequest.reference_prefix}#{merge_request[:iid]}", merge_request[:title]]
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_snippet_note(snippet)
|
2017-04-04 11:10:21 -04:00
|
|
|
["snippet #{Snippet.reference_prefix}#{snippet[:id]}", snippet[:title]]
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def description_message
|
2017-01-18 05:38:59 -05:00
|
|
|
[{ text: format(note), color: attachment_color }]
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def project_link
|
2017-01-18 05:38:59 -05:00
|
|
|
link(project_name, project_url)
|
2015-03-05 13:38:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|