From 9604edaf882fb761688c9faade887bfa5e25438c Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 4 Aug 2017 13:06:57 +0100 Subject: [PATCH] Allow selecting first transcript message using a URL --- .../provider/slack/slack_command_controller.rb | 9 +++++++-- .../provider/slack/slack_transcript_helper.rb | 6 ++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/discourse_chat/provider/slack/slack_command_controller.rb b/lib/discourse_chat/provider/slack/slack_command_controller.rb index 87ed41e..8d636db 100644 --- a/lib/discourse_chat/provider/slack/slack_command_controller.rb +++ b/lib/discourse_chat/provider/slack/slack_command_controller.rb @@ -54,7 +54,11 @@ module DiscourseChat::Provider::SlackProvider requested_messages = 10 - if tokens.size > 1 + first_message_ts = nil + slack_url_regex = /^https:\/\/\S+\.slack\.com\/archives\/\S+\/p([0-9]{16})\/?$/ + if tokens.size > 1 && match = slack_url_regex.match(tokens[1]) + first_message_ts = match.captures[0].insert(10, '.') + elsif tokens.size > 1 begin requested_messages = Integer(tokens[1], 10) rescue ArgumentError @@ -64,7 +68,8 @@ module DiscourseChat::Provider::SlackProvider transcript = SlackTranscript.load_transcript(slack_channel_id: slack_channel_id, channel_name: channel_name, - requested_messages: requested_messages) + requested_messages: requested_messages, + first_message_ts: first_message_ts) return { text: I18n.t("chat_integration.provider.slack.transcript.error") } unless transcript diff --git a/lib/discourse_chat/provider/slack/slack_transcript_helper.rb b/lib/discourse_chat/provider/slack/slack_transcript_helper.rb index 17f15b2..77074b8 100644 --- a/lib/discourse_chat/provider/slack/slack_transcript_helper.rb +++ b/lib/discourse_chat/provider/slack/slack_transcript_helper.rb @@ -101,10 +101,8 @@ module DiscourseChat::Provider::SlackProvider @last_message = this_message if index == raw_messages.length - 1 end - if first_message_ts && last_message_ts - @first_message = @messages.find { |m| m.ts == first_message_ts } - @last_message = @messages.find { |m| m.ts == last_message_ts } - end + @first_message = @messages.find { |m| m.ts == first_message_ts } || @first_message if first_message_ts + @last_message = @messages.find { |m| m.ts == last_message_ts } || @last_message if last_message_ts @first_message_index = @messages.index(@first_message) @last_message_index = @messages.index(@last_message)