gitlab-org--gitlab-foss/app/models/project_services/chat_message/base_message.rb

83 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
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
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
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]
@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]
end
def user_combined_name
if user_full_name.present?
"#{user_full_name} (#{user_name})"
else
user_name
end
end
def summary
2017-04-04 11:10:21 -04:00
return message if markdown
format(message)
end
def pretext
summary
end
def fallback
format(message)
end
def attachments
raise NotImplementedError
end
2017-04-03 13:16:24 -04:00
def activity
raise NotImplementedError
end
private
def message
raise NotImplementedError
end
def format(string)
Slack::Notifier::LinkFormatter.format(string)
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