2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
module DiscourseChatIntegration::Provider::SlackProvider
|
2017-10-10 00:54:10 -04:00
|
|
|
class SlackMessage
|
|
|
|
def initialize(raw_message, transcript)
|
|
|
|
@raw = raw_message
|
|
|
|
@transcript = transcript
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
if user
|
2019-07-04 14:50:07 -04:00
|
|
|
user["_transcript_username"]
|
2017-10-10 00:54:10 -04:00
|
|
|
elsif @raw.key?("username")
|
2019-07-04 14:50:07 -04:00
|
|
|
# This is for bot messages
|
|
|
|
@raw["username"].gsub(' ', '_')
|
2017-10-10 00:54:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar
|
|
|
|
user["profile"]["image_24"] if user
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
channel_id = @transcript.channel_id
|
|
|
|
ts = @raw['ts'].gsub('.', '')
|
|
|
|
"https://slack.com/archives/#{channel_id}/p#{ts}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
2017-11-27 03:07:54 -05:00
|
|
|
text = @raw['text'].nil? ? "" : @raw['text']
|
2017-10-10 00:54:10 -04:00
|
|
|
|
2021-04-22 13:49:21 -04:00
|
|
|
pre = {}
|
|
|
|
|
|
|
|
# Extract code blocks and replace with placeholder
|
|
|
|
text = text.gsub(/```(.*?)```/m) do |match|
|
|
|
|
key = "pre:" + SecureRandom.alphanumeric(50)
|
|
|
|
pre[key] = HTMLEntities.new.decode $1
|
|
|
|
"\n```\n#{key}\n```\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
# # Extract inline code and replace with placeholder
|
|
|
|
text = text.gsub(/(?<!`)`([^`]+?)`(?!`)/) do |match|
|
|
|
|
key = "pre:" + SecureRandom.alphanumeric(50)
|
|
|
|
pre[key] = HTMLEntities.new.decode $1
|
|
|
|
"`#{key}`"
|
|
|
|
end
|
|
|
|
|
2017-10-10 00:54:10 -04:00
|
|
|
# Format links (don't worry about special cases @ # !)
|
|
|
|
text = text.gsub(/<(.*?)>/) do |match|
|
|
|
|
group = $1
|
|
|
|
parts = group.split('|')
|
2021-04-22 13:49:21 -04:00
|
|
|
link = parts[0].start_with?('@', '#', '!') ? nil : parts[0]
|
2017-10-10 00:54:10 -04:00
|
|
|
text = parts.length > 1 ? parts[1] : parts[0]
|
|
|
|
|
|
|
|
if parts[0].start_with?('@')
|
2019-07-04 14:50:07 -04:00
|
|
|
user_id = parts[0][1..-1]
|
|
|
|
if user = @transcript.users[user_id]
|
|
|
|
user_name = user['_transcript_username']
|
|
|
|
else
|
|
|
|
user_name = user_id
|
|
|
|
end
|
|
|
|
next "@#{user_name}"
|
2017-10-10 00:54:10 -04:00
|
|
|
end
|
|
|
|
|
2021-04-22 13:49:21 -04:00
|
|
|
if link.nil?
|
|
|
|
text
|
|
|
|
elsif link == text
|
|
|
|
"<#{link}>"
|
|
|
|
else
|
|
|
|
"[#{text}](#{link})"
|
|
|
|
end
|
2017-10-10 00:54:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Add an extra * to each side for bold
|
2021-04-22 13:49:21 -04:00
|
|
|
text = text.gsub(/\*.*?\*/) do |match|
|
2017-10-10 00:54:10 -04:00
|
|
|
"*#{match}*"
|
|
|
|
end
|
|
|
|
|
2021-04-22 13:49:21 -04:00
|
|
|
# Add an extra ~ to each side for strikethrough
|
|
|
|
text = text.gsub(/~.*?~/) do |match|
|
|
|
|
"~#{match}~"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Replace emoji - with _
|
|
|
|
text = text.gsub(/:[a-z0-9_-]+:/) do |match|
|
|
|
|
match.gsub("-") { "_" }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Restore pre-formatted code block content
|
|
|
|
pre.each do |key, value|
|
|
|
|
text = text.gsub(key) { value }
|
|
|
|
end
|
|
|
|
|
2017-10-10 00:54:10 -04:00
|
|
|
text
|
|
|
|
end
|
|
|
|
|
2017-11-27 03:07:54 -05:00
|
|
|
def attachments_string
|
|
|
|
string = ""
|
|
|
|
string += "\n" if !attachments.empty?
|
|
|
|
attachments.each do |attachment|
|
|
|
|
string += " - #{attachment}\n"
|
|
|
|
end
|
|
|
|
string
|
|
|
|
end
|
|
|
|
|
|
|
|
def processed_text_with_attachments
|
|
|
|
self.text + attachments_string
|
|
|
|
end
|
|
|
|
|
2017-10-10 00:54:10 -04:00
|
|
|
def raw_text
|
2017-11-27 03:07:54 -05:00
|
|
|
raw_text = @raw['text'].nil? ? "" : @raw['text']
|
|
|
|
raw_text += attachments_string
|
|
|
|
raw_text
|
2017-10-10 00:54:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def attachments
|
|
|
|
attachments = []
|
|
|
|
|
|
|
|
return attachments unless @raw.key?('attachments')
|
|
|
|
|
|
|
|
@raw["attachments"].each do |attachment|
|
|
|
|
next unless attachment.key?("fallback")
|
|
|
|
attachments << attachment["fallback"]
|
|
|
|
end
|
|
|
|
|
|
|
|
attachments
|
|
|
|
end
|
|
|
|
|
|
|
|
def ts
|
|
|
|
@raw["ts"]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def user
|
|
|
|
return nil unless user_id = @raw["user"]
|
2019-07-04 14:50:07 -04:00
|
|
|
@transcript.users[user_id]
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-10-10 00:54:10 -04:00
|
|
|
end
|
|
|
|
end
|