diff --git a/lib/discourse_chat/provider/slack/slack_command_controller.rb b/lib/discourse_chat/provider/slack/slack_command_controller.rb index 32d5e2a..5ad1bbf 100644 --- a/lib/discourse_chat/provider/slack/slack_command_controller.rb +++ b/lib/discourse_chat/provider/slack/slack_command_controller.rb @@ -68,14 +68,14 @@ module DiscourseChat::Provider::SlackProvider error_message = { text: I18n.t("chat_integration.provider.slack.transcript.error") } - return error_message unless transcript = SlackTranscript.new(channel_name: channel_name) + return error_message unless transcript = SlackTranscript.new(channel_name: channel_name, channel_id: slack_channel_id) return error_message unless transcript.load_user_data - return error_message unless transcript.load_chat_history(slack_channel_id: slack_channel_id) + return error_message unless transcript.load_chat_history if first_message_ts return error_message unless transcript.set_first_message_by_ts(first_message_ts) else - return error_message unless transcript.set_first_message_by_index(-requested_messages) + transcript.set_first_message_by_index(-requested_messages) # Don't fail if this doesn't work, just use the default end return transcript.build_slack_ui @@ -99,9 +99,9 @@ module DiscourseChat::Provider::SlackProvider error_message = { text: I18n.t("chat_integration.provider.slack.transcript.error") } - return error_message unless transcript = SlackTranscript.new(channel_name: "##{json[:channel][:name]}") + return error_message unless transcript = SlackTranscript.new(channel_name: "##{json[:channel][:name]}", channel_id: json[:channel][:id]) return error_message unless transcript.load_user_data - return error_message unless transcript.load_chat_history(slack_channel_id: json[:channel][:id]) + return error_message unless transcript.load_chat_history return error_message unless transcript.set_first_message_by_ts(first_message) return error_message unless transcript.set_last_message_by_ts(last_message) diff --git a/lib/discourse_chat/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb index 1e1496c..9e08c75 100644 --- a/lib/discourse_chat/provider/slack/slack_provider.rb +++ b/lib/discourse_chat/provider/slack/slack_provider.rb @@ -151,5 +151,5 @@ module DiscourseChat::Provider::SlackProvider end require_relative "slack_message_formatter.rb" -require_relative "slack_transcript_helper.rb" +require_relative "slack_transcript.rb" require_relative "slack_command_controller.rb" diff --git a/lib/discourse_chat/provider/slack/slack_transcript_helper.rb b/lib/discourse_chat/provider/slack/slack_transcript.rb similarity index 97% rename from lib/discourse_chat/provider/slack/slack_transcript_helper.rb rename to lib/discourse_chat/provider/slack/slack_transcript.rb index 584c682..cc6c3ee 100644 --- a/lib/discourse_chat/provider/slack/slack_transcript_helper.rb +++ b/lib/discourse_chat/provider/slack/slack_transcript.rb @@ -10,6 +10,8 @@ module DiscourseChat::Provider::SlackProvider return user['name'] elsif @raw.key?("username") return @raw["username"] + else + return nil end end @@ -82,10 +84,11 @@ module DiscourseChat::Provider::SlackProvider end class SlackTranscript - attr_reader :users, :channel_id + attr_reader :users, :channel_id, :messages - def initialize(channel_name:) + def initialize(channel_name:, channel_id:) @channel_name = channel_name + @channel_id = channel_id @first_message_index = 0 @last_message_index = -1 # We can use negative array indicies to select the last message - fancy! @@ -234,7 +237,7 @@ module DiscourseChat::Provider::SlackProvider end - def load_chat_history(slack_channel_id:, count: 500) + def load_chat_history(count: 500) http = Net::HTTP.new("slack.com", 443) http.use_ssl = true @@ -242,7 +245,7 @@ module DiscourseChat::Provider::SlackProvider data = { token: SiteSetting.chat_integration_slack_access_token, - channel: slack_channel_id, + channel: @channel_id, count: count } diff --git a/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb index f2690bf..799ae6e 100644 --- a/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb +++ b/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb @@ -111,23 +111,108 @@ describe 'Slack Command Controller', type: :request do end describe 'post transcript' do + let(:messages_fixture) { + [ + { + "type": "message", + "user": "U6JSSESES", + "text": "Yeah, should make posting slack transcripts much easier", + "ts": "1501801665.062694" + }, + { + "type": "message", + "user": "U5Z773QLS", + "text": "Oooh a new discourse plugin???", + "ts": "1501801643.056375" + }, + { + "type": "message", + "user": "U6E2W7R8C", + "text": "Which one?", + "ts": "1501801634.053761" + }, + { + "type": "message", + "user": "U6JSSESES", + "text": "So, who's interested in the new ?", + "ts": "1501801629.052212" + }, + { + "text": "", + "username": "Test Community", + "bot_id": "B6C6JNUDN", + "attachments": [ + { + "author_name": "@david", + "fallback": "Discourse can now be integrated with Mattermost! - @david", + "text": "Hey , what do you think about this?", + "title": "Discourse can now be integrated with Mattermost! [Announcements] ", + "id": 1, + "title_link": "http://localhost:3000/t/discourse-can-now-be-integrated-with-mattermost/51/4", + "color": "283890", + "mrkdwn_in": [ + "text" + ] + } + ], + "type": "message", + "subtype": "bot_message", + "ts": "1501615820.949638" + }, + { + "type": "message", + "user": "U5Z773QLS", + "text": "Let’s try some *bold text*", + "ts": "1501093331.439776" + }, + + ] + } + before do SiteSetting.chat_integration_slack_access_token = 'abcde' end - it 'generates a transcript properly' do - stub1 = stub_request(:post, "https://slack.com/api/users.list").to_return(body: '{"ok":true,"members":[{"id":"U5Z773QLS","name":"david","profile":{"icon_24":"https://example.com/avatar"}}]}') - stub2 = stub_request(:post, "https://slack.com/api/channels.history").to_return(body: '{"ok":true,"messages":[{"type":"message","user":"U5Z773QLS","text":"And this is a slack message with an attachment: ","attachments":[{"title":"Discourse Meta","title_link":"https:\/\/meta.discourse.org","text":"Discussion about the next-generation open source Discourse forum software","fallback":"Discourse Meta","thumb_url":"https:\/\/discourse-meta.s3-us-west-1.amazonaws.com\/original\/3X\/c\/b\/cb4bec8901221d4a646e45e1fa03db3a65e17f59.png","from_url":"https:\/\/meta.discourse.org","thumb_width":350,"thumb_height":349,"service_icon":"https:\/\/discourse-meta.s3-us-west-1.amazonaws.com\/original\/3X\/c\/b\/cb4bec8901221d4a646e45e1fa03db3a65e17f59.png","service_name":"meta.discourse.org","id":1}],"ts":"1500910064.045243"},{"type":"message","user":"U5Z773QLS","text":"Hello world, this is a slack message","ts":"1500910051.036792"}],"has_more":true}') + context "with valid slack responses" do + before do + stub1 = stub_request(:post, "https://slack.com/api/users.list").to_return(body: '{"ok":true,"members":[{"id":"U5Z773QLS","name":"david","profile":{"icon_24":"https://example.com/avatar"}}]}') + stub2 = stub_request(:post, "https://slack.com/api/channels.history").to_return(body: { ok: true, messages: messages_fixture }.to_json) + end - post "/chat-integration/slack/command.json", - text: "post 2", - channel_name: 'general', - channel_id: 'C6029G78F', - token: token + it 'generates the transcript UI properly' do + post "/chat-integration/slack/command.json", + text: "post", + channel_name: 'general', + channel_id: 'C6029G78F', + token: token - json = JSON.parse(response.body) + json = JSON.parse(response.body) + expect(json["attachments"].length).to eq(2) + end - # expect(json["text"]).to include(I18n.t("chat_integration.provider.slack.post_to_discourse")) + it 'can select by url' do + post "/chat-integration/slack/command.json", + text: "post https://sometestslack.slack.com/archives/C6029G78F/p1501801629052212", + channel_name: 'general', + channel_id: 'C6029G78F', + token: token + + json = JSON.parse(response.body) + expect(json["attachments"].length).to eq(2) + expect(json["attachments"][0]["ts"]).to eq("1501801629.052212") + end + + it 'can select by count' do + post "/chat-integration/slack/command.json", + text: "post 4", + channel_name: 'general', + channel_id: 'C6029G78F', + token: token + + json = JSON.parse(response.body) + expect(json["attachments"].length).to eq(2) + expect(json["attachments"][0]["ts"]).to eq("1501801629.052212") + end end it 'deals with failed API calls correctly' do diff --git a/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb new file mode 100644 index 0000000..6b87414 --- /dev/null +++ b/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb @@ -0,0 +1,232 @@ +require 'rails_helper' + +RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do + + let(:messages_fixture) { + [ + { + "type": "message", + "user": "U6JSSESES", + "text": "Yeah, should make posting slack transcripts much easier", + "ts": "1501801665.062694" + }, + { + "type": "message", + "user": "U5Z773QLS", + "text": "Oooh a new discourse plugin???", + "ts": "1501801643.056375" + }, + { + "type": "message", + "user": "U6E2W7R8C", + "text": "Which one?", + "ts": "1501801634.053761" + }, + { + "type": "message", + "user": "U6JSSESES", + "text": "So, who's interested in the new ?", + "ts": "1501801629.052212" + }, + { + "text": "", + "username": "Test Community", + "bot_id": "B6C6JNUDN", + "attachments": [ + { + "author_name": "@david", + "fallback": "Discourse can now be integrated with Mattermost! - @david", + "text": "Hey , what do you think about this?", + "title": "Discourse can now be integrated with Mattermost! [Announcements] ", + "id": 1, + "title_link": "http://localhost:3000/t/discourse-can-now-be-integrated-with-mattermost/51/4", + "color": "283890", + "mrkdwn_in": [ + "text" + ] + } + ], + "type": "message", + "subtype": "bot_message", + "ts": "1501615820.949638" + }, + { + "type": "message", + "user": "U5Z773QLS", + "text": "Let’s try some *bold text*", + "ts": "1501093331.439776" + }, + + ] + } + + let(:transcript) { described_class.new(channel_name: "#general", channel_id: "G1234") } + before do + SiteSetting.chat_integration_slack_access_token = "abcde" + end + + describe 'loading users' do + it 'loads users correctly' do + stub_request(:post, "https://slack.com/api/users.list") + .with(body: { token: "abcde" }) + .to_return(status: 200, body: { ok: true, members: [{ id: "U5Z773QLS", name: "awesomeguy", profile: { image_24: "https://example.com/avatar" } }] }.to_json) + + expect(transcript.load_user_data).to be_truthy + end + + it 'handles failed connection' do + stub_request(:post, "https://slack.com/api/users.list") + .to_return(status: 500, body: '') + + expect(transcript.load_user_data).to be_falsey + end + + it 'handles slack failure' do + stub_request(:post, "https://slack.com/api/users.list") + .to_return(status: 200, body: { ok: false }.to_json) + + expect(transcript.load_user_data).to be_falsey + end + end + + context 'with loaded users' do + before do + stub_request(:post, "https://slack.com/api/users.list") + .to_return(status: 200, body: { ok: true, members: [{ id: "U5Z773QLS", name: "awesomeguy", profile: { image_24: "https://example.com/avatar" } }] }.to_json) + transcript.load_user_data + end + + describe 'loading history' do + it 'loads messages correctly' do + stub_request(:post, "https://slack.com/api/channels.history") + .with(body: hash_including(token: "abcde", channel: 'G1234')) + .to_return(status: 200, body: { ok: true, messages: messages_fixture }.to_json) + + expect(transcript.load_chat_history).to be_truthy + end + + it 'handles failed connection' do + stub_request(:post, "https://slack.com/api/channels.history") + .to_return(status: 500, body: {}.to_json) + + expect(transcript.load_chat_history).to be_falsey + end + + it 'handles slack failure' do + stub_request(:post, "https://slack.com/api/channels.history") + .to_return(status: 200, body: { ok: false }.to_json) + + expect(transcript.load_chat_history).to be_falsey + end + end + + context 'with loaded messages' do + before do + stub_request(:post, "https://slack.com/api/channels.history") + .with(body: hash_including(token: "abcde", channel: 'G1234')) + .to_return(status: 200, body: { ok: true, messages: messages_fixture }.to_json) + transcript.load_chat_history + end + + it 'loads in chronological order' do # API presents in reverse chronological + expect(transcript.messages.first.ts).to eq('1501093331.439776') + end + + it 'handles bold text' do + expect(transcript.messages.first.text).to eq("Let’s try some **bold text**") + end + + it 'handles links' do + expect(transcript.messages[2].text).to eq("So, who's interested in the new [discourse plugin](https://meta.discourse.org)?") + end + + it 'includes attachments' do + expect(transcript.messages[1].attachments.first).to eq("Discourse can now be integrated with Mattermost! - @david") + end + + it 'can generate URL' do + expect(transcript.messages.first.url).to eq("https://slack.com/archives/G1234/p1501093331439776") + end + + it 'gives correct first and last messages' do + expect(transcript.first_message_number).to eq(0) + expect(transcript.last_message_number).to eq(transcript.messages.length - 1) + + expect(transcript.first_message.ts).to eq('1501093331.439776') + expect(transcript.last_message.ts).to eq('1501801665.062694') + end + + it 'can change first and last messages by index' do + expect(transcript.set_first_message_by_index(999)).to be_falsey + expect(transcript.set_first_message_by_index(1)).to be_truthy + + expect(transcript.set_last_message_by_index(-2)).to be_truthy + + expect(transcript.first_message.ts).to eq('1501615820.949638') + expect(transcript.last_message.ts).to eq('1501801643.056375') + end + + it 'can change first and last messages by ts' do + expect(transcript.set_first_message_by_ts('blah')).to be_falsey + expect(transcript.set_first_message_by_ts('1501615820.949638')).to be_truthy + + expect(transcript.set_last_message_by_ts('1501801629.052212')).to be_truthy + + expect(transcript.first_message_number).to eq(1) + expect(transcript.last_message_number).to eq(2) + end + + it 'handles usernames correctly' do + expect(transcript.first_message.username).to eq('awesomeguy') # Normal user + expect(transcript.messages[2].username).to eq(nil) # Unknown normal user + expect(transcript.messages[1].username).to eq('Test Community') # Bot user + end + + it 'handles avatars correctly' do + expect(transcript.first_message.avatar).to eq("https://example.com/avatar") # Normal user + expect(transcript.messages[1].avatar).to eq(nil) # Bot user + end + + it 'creates a transcript correctly' do + transcript.set_last_message_by_index(1) + + text = transcript.build_transcript + + # Rubocop doesn't like this, but we really do need trailing whitespace in the string + # rubocop:disable TrailingWhitespace + expected = <<~END + [quote] + [**View in #general on Slack**](https://slack.com/archives/G1234/p1501093331439776) + + ![awesomeguy] **@awesomeguy:** Let’s try some **bold text** + + **@Test Community:** + > Discourse can now be integrated with Mattermost! - @david + + [/quote] + + [awesomeguy]: https://example.com/avatar + END + # rubocop:enable TrailingWhitespace + + expect(text).to eq(expected) + end + + it 'creates the slack UI correctly' do + transcript.set_last_message_by_index(1) + ui = transcript.build_slack_ui + + first_ui = ui[:attachments][0] + last_ui = ui[:attachments][1] + + # The callback IDs are used to keep track of what the other option is + expect(first_ui[:callback_id]).to eq(transcript.last_message.ts) + expect(last_ui[:callback_id]).to eq(transcript.first_message.ts) + + # The timestamps should match up to the actual messages + expect(first_ui[:ts]).to eq(transcript.first_message.ts) + expect(last_ui[:ts]).to eq(transcript.last_message.ts) + end + end + end +end