From b19550502ee98a981e449fd6b2e4b088bbce0ab6 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 10 Oct 2017 12:54:10 +0800 Subject: [PATCH] Fix the build. --- .../provider/slack/slack_message.rb | 80 ++++++++++++++++++ .../provider/slack/slack_provider.rb | 7 +- .../provider/slack/slack_transcript.rb | 81 +------------------ 3 files changed, 85 insertions(+), 83 deletions(-) create mode 100644 lib/discourse_chat/provider/slack/slack_message.rb diff --git a/lib/discourse_chat/provider/slack/slack_message.rb b/lib/discourse_chat/provider/slack/slack_message.rb new file mode 100644 index 0000000..685c0db --- /dev/null +++ b/lib/discourse_chat/provider/slack/slack_message.rb @@ -0,0 +1,80 @@ +module DiscourseChat::Provider::SlackProvider + class SlackMessage + def initialize(raw_message, transcript) + @raw = raw_message + @transcript = transcript + end + + def username + if user + user['name'] + elsif @raw.key?("username") + @raw["username"] + 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 + text = @raw["text"] + + # Format links (don't worry about special cases @ # !) + text = text.gsub(/<(.*?)>/) do |match| + group = $1 + parts = group.split('|') + link = parts[0].start_with?('@', '#', '!') ? '' : parts[0] + text = parts.length > 1 ? parts[1] : parts[0] + + if parts[0].start_with?('@') + user = @transcript.users.find { |u| u["id"] == parts[0].gsub('@', '') } + next "@#{user['name']}" + end + + "[#{text}](#{link})" + end + + # Add an extra * to each side for bold + text = text.gsub(/\*(.*?)\*/) do |match| + "*#{match}*" + end + + text + end + + def raw_text + @raw['text'] + 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 + + def user + return nil unless user_id = @raw["user"] + @transcript.users.find { |u| u["id"] == user_id } + end + end +end diff --git a/lib/discourse_chat/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb index 844dfee..b771516 100644 --- a/lib/discourse_chat/provider/slack/slack_provider.rb +++ b/lib/discourse_chat/provider/slack/slack_provider.rb @@ -139,6 +139,7 @@ module DiscourseChat::Provider::SlackProvider end end -require_relative "slack_message_formatter.rb" -require_relative "slack_transcript.rb" -require_relative "slack_command_controller.rb" +require_relative "slack_message_formatter" +require_relative "slack_transcript" +require_relative "slack_message" +require_relative "slack_command_controller" diff --git a/lib/discourse_chat/provider/slack/slack_transcript.rb b/lib/discourse_chat/provider/slack/slack_transcript.rb index 05df894..155baf0 100644 --- a/lib/discourse_chat/provider/slack/slack_transcript.rb +++ b/lib/discourse_chat/provider/slack/slack_transcript.rb @@ -1,83 +1,4 @@ module DiscourseChat::Provider::SlackProvider - class SlackMessage - def initialize(raw_message, transcript) - @raw = raw_message - @transcript = transcript - end - - def username - if user - user['name'] - elsif @raw.key?("username") - @raw["username"] - 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 - text = @raw["text"] - - # Format links (don't worry about special cases @ # !) - text = text.gsub(/<(.*?)>/) do |match| - group = $1 - parts = group.split('|') - link = parts[0].start_with?('@', '#', '!') ? '' : parts[0] - text = parts.length > 1 ? parts[1] : parts[0] - - if parts[0].start_with?('@') - user = @transcript.users.find { |u| u["id"] == parts[0].gsub('@', '') } - next "@#{user['name']}" - end - - "[#{text}](#{link})" - end - - # Add an extra * to each side for bold - text = text.gsub(/\*(.*?)\*/) do |match| - "*#{match}*" - end - - text - end - - def raw_text - @raw['text'] - 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 - - def user - return nil unless user_id = @raw["user"] - @transcript.users.find { |u| u["id"] == user_id } - end - end - class SlackTranscript attr_reader :users, :channel_id, :messages @@ -273,7 +194,7 @@ module DiscourseChat::Provider::SlackProvider return false unless response.kind_of? Net::HTTPSuccess json = JSON.parse(response.body) return false unless json['ok'] - json['members'] + @users = json['members'] end def load_chat_history(count: 500)