From b69abe3913fc63094deba614694c9b11ef22f5bf Mon Sep 17 00:00:00 2001 From: David Taylor Date: Mon, 27 Nov 2017 08:07:54 +0000 Subject: [PATCH] Include attachments in message selection dropdowns (#13) * Include attachments in message selection dropdowns * DRY up message attachment string generation * Add specs for attachments in UI --- .../provider/slack/slack_message.rb | 19 +++++++++++++++++-- .../provider/slack/slack_transcript.rb | 4 ++-- .../provider/slack/slack_transcript_spec.rb | 8 ++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/discourse_chat/provider/slack/slack_message.rb b/lib/discourse_chat/provider/slack/slack_message.rb index 685c0db..b2a80c0 100644 --- a/lib/discourse_chat/provider/slack/slack_message.rb +++ b/lib/discourse_chat/provider/slack/slack_message.rb @@ -24,7 +24,7 @@ module DiscourseChat::Provider::SlackProvider end def text - text = @raw["text"] + text = @raw['text'].nil? ? "" : @raw['text'] # Format links (don't worry about special cases @ # !) text = text.gsub(/<(.*?)>/) do |match| @@ -49,8 +49,23 @@ module DiscourseChat::Provider::SlackProvider text end + 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 + def raw_text - @raw['text'] + raw_text = @raw['text'].nil? ? "" : @raw['text'] + raw_text += attachments_string + raw_text end def attachments diff --git a/lib/discourse_chat/provider/slack/slack_transcript.rb b/lib/discourse_chat/provider/slack/slack_transcript.rb index f33d884..f24c23d 100644 --- a/lib/discourse_chat/provider/slack/slack_transcript.rb +++ b/lib/discourse_chat/provider/slack/slack_transcript.rb @@ -148,7 +148,7 @@ module DiscourseChat::Provider::SlackProvider ), type: "select", options: first_message_options = @messages[ [(first_message_number - 20), 0].max .. last_message_number] - .map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } } + .map { |m| { text: "#{m.username}: #{m.processed_text_with_attachments}", value: m.ts } } } ], }, @@ -176,7 +176,7 @@ module DiscourseChat::Provider::SlackProvider ), type: "select", options: @messages[first_message_number..(last_message_number + 20)] - .map { |m| { text: "#{m.username}: #{m.text}", value: m.ts } } + .map { |m| { text: "#{m.username}: #{m.processed_text_with_attachments}", value: m.ts } } } ], } diff --git a/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb index 063128a..0cf262f 100644 --- a/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb +++ b/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb @@ -159,6 +159,11 @@ RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do expect(transcript.messages.first.url).to eq("https://slack.com/archives/G1234/p1501093331439776") end + it 'includes attachments in raw text' do + transcript.set_first_message_by_ts('1501615820.949638') + expect(transcript.first_message.raw_text).to eq("\n - Discourse can now be integrated with Mattermost! - @david\n") + 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) @@ -242,6 +247,9 @@ RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do # 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) + + # Raw text should be used + expect(first_ui[:text]).to eq(transcript.first_message.raw_text) end end end