2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'slack-notifier'
|
|
|
|
|
|
|
|
module ChatMessage
|
|
|
|
class BaseMessage
|
2020-03-09 13:42:32 +05:30
|
|
|
RELATIVE_LINK_REGEX = /!\[[^\]]*\]\((\/uploads\/[^\)]*)\)/.freeze
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_reader :markdown
|
2018-03-17 18:26:18 +05:30
|
|
|
attr_reader :user_full_name
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_reader :user_name
|
|
|
|
attr_reader :user_avatar
|
|
|
|
attr_reader :project_name
|
|
|
|
attr_reader :project_url
|
2019-12-04 20:38:33 +05:30
|
|
|
attr_reader :commit_message_html
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def initialize(params)
|
|
|
|
@markdown = params[:markdown] || false
|
2020-03-09 13:42:32 +05:30
|
|
|
@project_name = params[:project_name] || params.dig(:project, :path_with_namespace)
|
2017-08-17 22:00:37 +05:30
|
|
|
@project_url = params.dig(:project, :web_url) || params[:project_url]
|
2018-03-17 18:26:18 +05:30
|
|
|
@user_full_name = params.dig(:user, :name) || params[:user_full_name]
|
2017-08-17 22:00:37 +05:30
|
|
|
@user_name = params.dig(:user, :username) || params[:user_name]
|
|
|
|
@user_avatar = params.dig(:user, :avatar_url) || params[:user_avatar]
|
2019-12-04 20:38:33 +05:30
|
|
|
@commit_message_html = params[:commit_message_html] || false
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def user_combined_name
|
|
|
|
if user_full_name.present?
|
|
|
|
"#{user_full_name} (#{user_name})"
|
|
|
|
else
|
|
|
|
user_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def summary
|
2017-08-17 22:00:37 +05:30
|
|
|
return message if markdown
|
|
|
|
|
|
|
|
format(message)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def pretext
|
|
|
|
summary
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def fallback
|
2018-11-08 19:23:39 +05:30
|
|
|
format(message)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def attachments
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def activity
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def message
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def format(string)
|
2020-03-09 13:42:32 +05:30
|
|
|
Slack::Notifier::LinkFormatter.format(format_relative_links(string))
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_relative_links(string)
|
|
|
|
string.gsub(RELATIVE_LINK_REGEX, "#{project_url}\\1")
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def attachment_color
|
|
|
|
'#345'
|
|
|
|
end
|
|
|
|
|
|
|
|
def link(text, url)
|
|
|
|
"[#{text}](#{url})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretty_duration(seconds)
|
|
|
|
parse_string =
|
|
|
|
if duration < 1.hour
|
|
|
|
'%M:%S'
|
|
|
|
else
|
|
|
|
'%H:%M:%S'
|
|
|
|
end
|
|
|
|
|
|
|
|
Time.at(seconds).utc.strftime(parse_string)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|