2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
require 'slack-notifier'
|
|
|
|
|
2016-11-25 14:36:37 -05:00
|
|
|
module ChatMessage
|
2015-02-20 08:49:26 -05:00
|
|
|
class BaseMessage
|
2017-04-04 11:10:21 -04:00
|
|
|
attr_reader :markdown
|
2017-10-06 16:00:50 -04:00
|
|
|
attr_reader :user_full_name
|
2017-04-04 11:10:21 -04:00
|
|
|
attr_reader :user_name
|
|
|
|
attr_reader :user_avatar
|
|
|
|
attr_reader :project_name
|
|
|
|
attr_reader :project_url
|
2017-04-04 05:16:24 -04:00
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
def initialize(params)
|
2017-04-04 11:10:21 -04:00
|
|
|
@markdown = params[:markdown] || false
|
|
|
|
@project_name = params.dig(:project, :path_with_namespace) || params[:project_name]
|
|
|
|
@project_url = params.dig(:project, :web_url) || params[:project_url]
|
2017-10-06 16:00:50 -04:00
|
|
|
@user_full_name = params.dig(:user, :name) || params[:user_full_name]
|
2017-04-04 11:10:21 -04:00
|
|
|
@user_name = params.dig(:user, :username) || params[:user_name]
|
|
|
|
@user_avatar = params.dig(:user, :avatar_url) || params[:user_avatar]
|
2015-02-19 00:02:57 -05:00
|
|
|
end
|
|
|
|
|
2017-10-06 16:00:50 -04:00
|
|
|
def user_combined_name
|
|
|
|
if user_full_name.present?
|
|
|
|
"#{user_full_name} (#{user_name})"
|
|
|
|
else
|
|
|
|
user_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 04:28:30 -04:00
|
|
|
def summary
|
2017-04-04 11:10:21 -04:00
|
|
|
return message if markdown
|
|
|
|
|
|
|
|
format(message)
|
2015-02-19 00:02:57 -05:00
|
|
|
end
|
|
|
|
|
2018-06-11 04:28:30 -04:00
|
|
|
def pretext
|
|
|
|
summary
|
|
|
|
end
|
|
|
|
|
2015-12-07 07:23:23 -05:00
|
|
|
def fallback
|
2018-06-11 04:28:30 -04:00
|
|
|
format(message)
|
2015-12-07 07:23:23 -05:00
|
|
|
end
|
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
def attachments
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2017-04-03 13:16:24 -04:00
|
|
|
def activity
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def message
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def format(string)
|
|
|
|
Slack::Notifier::LinkFormatter.format(string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachment_color
|
|
|
|
'#345'
|
|
|
|
end
|
2017-01-18 05:38:59 -05:00
|
|
|
|
|
|
|
def link(text, url)
|
|
|
|
"[#{text}](#{url})"
|
|
|
|
end
|
2017-05-03 03:25:13 -04:00
|
|
|
|
|
|
|
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
|
2015-02-19 00:02:57 -05:00
|
|
|
end
|
|
|
|
end
|