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
This commit is contained in:
David Taylor 2017-11-27 08:07:54 +00:00 committed by Guo Xiang Tan
parent 6ed0deedf8
commit b69abe3913
3 changed files with 27 additions and 4 deletions

View File

@ -24,7 +24,7 @@ module DiscourseChat::Provider::SlackProvider
end end
def text def text
text = @raw["text"] text = @raw['text'].nil? ? "" : @raw['text']
# Format links (don't worry about special cases @ # !) # Format links (don't worry about special cases @ # !)
text = text.gsub(/<(.*?)>/) do |match| text = text.gsub(/<(.*?)>/) do |match|
@ -49,8 +49,23 @@ module DiscourseChat::Provider::SlackProvider
text text
end 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 def raw_text
@raw['text'] raw_text = @raw['text'].nil? ? "" : @raw['text']
raw_text += attachments_string
raw_text
end end
def attachments def attachments

View File

@ -148,7 +148,7 @@ module DiscourseChat::Provider::SlackProvider
), ),
type: "select", type: "select",
options: first_message_options = @messages[ [(first_message_number - 20), 0].max .. last_message_number] 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", type: "select",
options: @messages[first_message_number..(last_message_number + 20)] 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 } }
} }
], ],
} }

View File

@ -159,6 +159,11 @@ RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do
expect(transcript.messages.first.url).to eq("https://slack.com/archives/G1234/p1501093331439776") expect(transcript.messages.first.url).to eq("https://slack.com/archives/G1234/p1501093331439776")
end 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 it 'gives correct first and last messages' do
expect(transcript.first_message_number).to eq(0) expect(transcript.first_message_number).to eq(0)
expect(transcript.last_message_number).to eq(transcript.messages.length - 1) 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 # The timestamps should match up to the actual messages
expect(first_ui[:ts]).to eq(transcript.first_message.ts) expect(first_ui[:ts]).to eq(transcript.first_message.ts)
expect(last_ui[:ts]).to eq(transcript.last_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 end
end end