2017-08-03 10:48:33 -04:00
|
|
|
module DiscourseChat::Provider::SlackProvider
|
|
|
|
class SlackMessage
|
|
|
|
def initialize(raw_message, transcript)
|
|
|
|
@raw = raw_message
|
|
|
|
@transcript = transcript
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
if user
|
|
|
|
return user['name']
|
|
|
|
elsif @raw.key?("username")
|
|
|
|
return @raw["username"]
|
2017-08-15 10:44:51 -04:00
|
|
|
else
|
|
|
|
return nil
|
2017-08-03 10:48:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def avatar
|
|
|
|
return nil unless user
|
|
|
|
return user["profile"]["image_24"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
channel_id = @transcript.channel_id
|
|
|
|
ts = @raw['ts'].gsub('.', '')
|
|
|
|
|
|
|
|
return "https://slack.com/archives/#{channel_id}/p#{ts}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
|
|
|
text = @raw["text"]
|
|
|
|
|
|
|
|
# Format links (don't worry about special cases @ # !)
|
2017-08-03 19:47:04 -04:00
|
|
|
text = text.gsub(/<(.*?)>/) do |match|
|
2017-08-03 10:48:33 -04:00
|
|
|
group = $1
|
|
|
|
parts = group.split('|')
|
|
|
|
link = parts[0].start_with?('@', '#', '!') ? '' : parts[0]
|
|
|
|
text = parts.length > 1 ? parts[1] : parts[0]
|
2017-08-03 19:47:04 -04:00
|
|
|
|
|
|
|
if parts[0].start_with?('@')
|
|
|
|
user = @transcript.users.find { |u| u["id"] == parts[0].gsub('@', '') }
|
|
|
|
next "@#{user['name']}"
|
|
|
|
end
|
|
|
|
|
2017-08-03 10:48:33 -04:00
|
|
|
"[#{text}](#{link})"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add an extra * to each side for bold
|
2017-08-03 19:47:04 -04:00
|
|
|
text = text.gsub(/\*(.*?)\*/) do |match|
|
2017-08-03 10:48:33 -04:00
|
|
|
"*#{match}*"
|
|
|
|
end
|
|
|
|
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
2017-08-03 19:47:04 -04:00
|
|
|
def raw_text
|
|
|
|
@raw['text']
|
|
|
|
end
|
|
|
|
|
2017-08-03 10:48:33 -04:00
|
|
|
def attachments
|
|
|
|
attachments = []
|
|
|
|
|
|
|
|
return attachments unless @raw.key?('attachments')
|
|
|
|
|
|
|
|
@raw["attachments"].each do |attachment|
|
|
|
|
next unless attachment.key?("fallback")
|
|
|
|
attachments << attachment["fallback"]
|
|
|
|
end
|
|
|
|
|
|
|
|
return attachments
|
|
|
|
end
|
|
|
|
|
2017-08-03 19:47:04 -04:00
|
|
|
def ts
|
|
|
|
@raw["ts"]
|
|
|
|
end
|
|
|
|
|
2017-08-03 10:48:33 -04:00
|
|
|
private
|
|
|
|
def user
|
|
|
|
return nil unless user_id = @raw["user"]
|
|
|
|
users = @transcript.users
|
|
|
|
user = users.find { |u| u["id"] == user_id }
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class SlackTranscript
|
2017-08-15 10:44:51 -04:00
|
|
|
attr_reader :users, :channel_id, :messages
|
2017-08-03 10:48:33 -04:00
|
|
|
|
2017-08-15 10:44:51 -04:00
|
|
|
def initialize(channel_name:, channel_id:)
|
2017-08-15 03:26:03 -04:00
|
|
|
@channel_name = channel_name
|
2017-08-15 10:44:51 -04:00
|
|
|
@channel_id = channel_id
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
@first_message_index = 0
|
|
|
|
@last_message_index = -1 # We can use negative array indicies to select the last message - fancy!
|
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
def set_first_message_by_ts(ts)
|
|
|
|
message_index = @messages.find_index { |m| m.ts == ts }
|
|
|
|
@first_message_index = message_index if message_index
|
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
def set_last_message_by_ts(ts)
|
|
|
|
message_index = @messages.find_index { |m| m.ts == ts }
|
|
|
|
@last_message_index = message_index if message_index
|
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
def set_first_message_by_index(val)
|
|
|
|
@first_message_index = val if @messages[val]
|
|
|
|
end
|
2017-08-03 10:48:33 -04:00
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
def set_last_message_by_index(val)
|
|
|
|
@last_message_index = val if @messages[val]
|
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2017-08-15 11:19:24 -04:00
|
|
|
# Apply a heuristic to decide which is the first message in the current conversation
|
|
|
|
def guess_first_message(skip_messages: 5) # Can skip the last n messages
|
|
|
|
|
|
|
|
possible_first_messages = @messages[0..-skip_messages]
|
|
|
|
|
|
|
|
# Work through the messages in order. If a gap is found, this could be the first message
|
|
|
|
new_first_message_index = nil
|
|
|
|
previous_message_ts = @messages[-skip_messages].ts.split('.').first.to_i
|
|
|
|
possible_first_messages.each_with_index do |message, index|
|
|
|
|
|
|
|
|
# Calculate the time since the last message
|
|
|
|
this_ts = message.ts.split('.').first.to_i
|
|
|
|
time_since_previous_message = this_ts - previous_message_ts
|
|
|
|
|
|
|
|
# If greater than 3 minutes, this could be the first message
|
|
|
|
if time_since_previous_message > 3.minutes
|
|
|
|
new_first_message_index = index
|
|
|
|
end
|
|
|
|
|
|
|
|
previous_message_ts = this_ts
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_first_message_index
|
|
|
|
@first_message_index = new_first_message_index
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
def first_message
|
|
|
|
return @messages[@first_message_index]
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_message
|
|
|
|
return @messages[@last_message_index]
|
|
|
|
end
|
|
|
|
|
|
|
|
# These two methods convert potentially negative array indices into positive ones
|
|
|
|
def first_message_number
|
|
|
|
return @first_message_index < 0 ? @messages.length + @first_message_index : @first_message_index
|
|
|
|
end
|
|
|
|
def last_message_number
|
|
|
|
return @last_message_index < 0 ? @messages.length + @last_message_index : @last_message_index
|
2017-08-03 10:48:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_transcript
|
|
|
|
post_content = "[quote]\n"
|
2017-08-15 03:26:03 -04:00
|
|
|
post_content << "[**#{I18n.t('chat_integration.provider.slack.transcript.view_on_slack', name: @channel_name)}**](#{first_message.url})\n"
|
2017-08-03 10:48:33 -04:00
|
|
|
|
|
|
|
all_avatars = {}
|
|
|
|
|
|
|
|
last_username = ''
|
|
|
|
|
2017-08-03 19:47:04 -04:00
|
|
|
transcript_messages = @messages[@first_message_index..@last_message_index]
|
|
|
|
|
|
|
|
transcript_messages.each do |m|
|
2017-08-03 10:48:33 -04:00
|
|
|
same_user = m.username == last_username
|
|
|
|
last_username = m.username
|
|
|
|
|
|
|
|
unless same_user
|
|
|
|
if avatar = m.avatar
|
|
|
|
all_avatars[m.username] ||= avatar
|
|
|
|
end
|
|
|
|
|
|
|
|
post_content << "\n"
|
|
|
|
post_content << "![#{m.username}] " if m.avatar
|
|
|
|
post_content << "**@#{m.username}:** "
|
|
|
|
end
|
|
|
|
|
|
|
|
post_content << m.text
|
|
|
|
|
|
|
|
m.attachments.each do |attachment|
|
|
|
|
post_content << "\n> #{attachment}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
post_content << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
post_content << "[/quote]\n\n"
|
|
|
|
|
|
|
|
all_avatars.each do |username, url|
|
|
|
|
post_content << "[#{username}]: #{url}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
return post_content
|
|
|
|
end
|
|
|
|
|
2017-08-03 12:24:49 -04:00
|
|
|
def build_slack_ui
|
|
|
|
post_content = build_transcript
|
|
|
|
secret = DiscourseChat::Helper.save_transcript(post_content)
|
|
|
|
link = "#{Discourse.base_url}/chat-transcript/#{secret}"
|
|
|
|
|
2017-08-03 19:47:04 -04:00
|
|
|
return { text: "<#{link}|#{I18n.t("chat_integration.provider.slack.transcript.post_to_discourse")}>",
|
|
|
|
attachments: [
|
|
|
|
{
|
2017-08-15 03:26:03 -04:00
|
|
|
pretext: I18n.t("chat_integration.provider.slack.transcript.first_message_pretext", n: @messages.length - first_message_number),
|
|
|
|
fallback: "#{first_message.username} - #{first_message.raw_text}",
|
2017-08-03 19:47:04 -04:00
|
|
|
color: "#007AB8",
|
2017-08-15 03:26:03 -04:00
|
|
|
author_name: first_message.username,
|
|
|
|
author_icon: first_message.avatar,
|
|
|
|
text: first_message.raw_text,
|
2017-08-03 19:47:04 -04:00
|
|
|
footer: I18n.t("chat_integration.provider.slack.transcript.posted_in", name: @channel_name),
|
2017-08-15 03:26:03 -04:00
|
|
|
ts: first_message.ts,
|
|
|
|
callback_id: last_message.ts,
|
2017-08-03 19:47:04 -04:00
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
name: "first_message",
|
|
|
|
text: I18n.t("chat_integration.provider.slack.transcript.change_first_message"),
|
|
|
|
type: "select",
|
2017-08-15 03:26:03 -04:00
|
|
|
options: first_message_options = @messages[ [(first_message_number - 20), 0].max .. last_message_number]
|
2017-08-03 19:47:04 -04:00
|
|
|
.map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } }
|
|
|
|
}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2017-08-15 03:26:03 -04:00
|
|
|
pretext: I18n.t("chat_integration.provider.slack.transcript.last_message_pretext", n: @messages.length - last_message_number),
|
|
|
|
fallback: "#{last_message.username} - #{last_message.raw_text}",
|
2017-08-03 19:47:04 -04:00
|
|
|
color: "#007AB8",
|
2017-08-15 03:26:03 -04:00
|
|
|
author_name: last_message.username,
|
|
|
|
author_icon: last_message.avatar,
|
|
|
|
text: last_message.raw_text,
|
2017-08-03 19:47:04 -04:00
|
|
|
footer: I18n.t("chat_integration.provider.slack.transcript.posted_in", name: @channel_name),
|
2017-08-15 03:26:03 -04:00
|
|
|
ts: last_message.ts,
|
|
|
|
callback_id: first_message.ts,
|
2017-08-03 19:47:04 -04:00
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
name: "last_message",
|
|
|
|
text: I18n.t("chat_integration.provider.slack.transcript.change_last_message"),
|
|
|
|
type: "select",
|
2017-08-15 03:26:03 -04:00
|
|
|
options: @messages[first_message_number..(last_message_number + 20)]
|
2017-08-03 19:47:04 -04:00
|
|
|
.map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } }
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
]
|
2017-08-03 12:24:49 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
def load_user_data
|
2017-08-03 12:24:49 -04:00
|
|
|
http = Net::HTTP.new("slack.com", 443)
|
|
|
|
http.use_ssl = true
|
|
|
|
|
|
|
|
req = Net::HTTP::Post.new(URI('https://slack.com/api/users.list'))
|
|
|
|
req.set_form_data(token: SiteSetting.chat_integration_slack_access_token)
|
|
|
|
response = http.request(req)
|
|
|
|
return false unless response.kind_of? Net::HTTPSuccess
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
return false unless json['ok']
|
2017-08-15 03:26:03 -04:00
|
|
|
|
|
|
|
@users = json['members']
|
|
|
|
|
2017-08-03 12:24:49 -04:00
|
|
|
end
|
|
|
|
|
2017-08-15 10:44:51 -04:00
|
|
|
def load_chat_history(count: 500)
|
2017-08-03 12:24:49 -04:00
|
|
|
http = Net::HTTP.new("slack.com", 443)
|
|
|
|
http.use_ssl = true
|
|
|
|
|
|
|
|
req = Net::HTTP::Post.new(URI('https://slack.com/api/channels.history'))
|
|
|
|
|
|
|
|
data = {
|
|
|
|
token: SiteSetting.chat_integration_slack_access_token,
|
2017-08-15 10:44:51 -04:00
|
|
|
channel: @channel_id,
|
2017-08-03 19:47:04 -04:00
|
|
|
count: count
|
2017-08-03 12:24:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
req.set_form_data(data)
|
|
|
|
response = http.request(req)
|
|
|
|
return false unless response.kind_of? Net::HTTPSuccess
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
return false unless json['ok']
|
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
raw_messages = json['messages'].reverse
|
2017-08-03 12:24:49 -04:00
|
|
|
|
2017-08-15 03:26:03 -04:00
|
|
|
# Build some message objects
|
|
|
|
@messages = []
|
|
|
|
raw_messages.each_with_index do |message, index|
|
|
|
|
next unless message["type"] == "message"
|
|
|
|
this_message = SlackMessage.new(message, self)
|
|
|
|
@messages << this_message
|
|
|
|
end
|
2017-08-03 19:47:04 -04:00
|
|
|
|
2017-08-03 12:24:49 -04:00
|
|
|
end
|
2017-08-03 10:48:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|