From 2d0e1b113879b704e7e50e2684e80e81086b6789 Mon Sep 17 00:00:00 2001 From: Nikolay Vashchenko Date: Sat, 27 May 2017 07:15:20 +0300 Subject: [PATCH 1/4] more consistent handling of message attachments --- lib/lita/adapters/slack/message_handler.rb | 35 ++++++++++++++-------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/lita/adapters/slack/message_handler.rb b/lib/lita/adapters/slack/message_handler.rb index b1572b2..60f3d47 100644 --- a/lib/lita/adapters/slack/message_handler.rb +++ b/lib/lita/adapters/slack/message_handler.rb @@ -39,17 +39,28 @@ def handle attr_reader :type def body - normalized_message = if data["text"] - data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}") - end + normalized_text = if data["text"] + data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}") + end - normalized_message = remove_formatting(normalized_message) unless normalized_message.nil? + normalized_text = remove_formatting(normalized_text) unless normalized_text.nil? - attachment_text = Array(data["attachments"]).map do |attachment| - attachment["text"] || attachment["fallback"] - end + lines = Array(data["attachments"]).inject([normalized_text]){|total, att| total.concat parse_attachment(att)} + lines.compact.join("\n") + end - ([normalized_message] + attachment_text).compact.join("\n") + def parse_attachment(a) + lines = [] + lines << a["pretext"] + lines << a["author_name"] + lines << a["title"] + lines << a["text"] + Array(a["fields"]).map do |field| + lines << field["title"] + lines << field["value"] + end + lines << a["fallback"] if lines.none? + lines.compact.map(&:strip).reject(&:empty?) end def remove_formatting(message) @@ -118,7 +129,7 @@ def dispatch_message(user) source.private_message! if channel && channel[0] == "D" message = Message.new(robot, body, source) message.command! if source.private_message? - message.extensions[:slack] = { timestamp: data["ts"] } + message.extensions[:slack] = { timestamp: data["ts"], attachments: data["attachments"] } log.debug("Dispatching message to Lita from #{user.id}.") robot.receive(message) end @@ -195,15 +206,13 @@ def log end # Types of messages Lita should dispatch to handlers. - def supported_message_subtypes - %w(me_message) - end + SUPPORTED_MESSAGE_SUBTYPES = %w(me_message) def supported_subtype? subtype = data["subtype"] if subtype - supported_message_subtypes.include?(subtype) + SUPPORTED_MESSAGE_SUBTYPES.include?(subtype) else true end From 8b1b423735519108367be8fd3a07f58543712694 Mon Sep 17 00:00:00 2001 From: Nikolay Vashchenko Date: Sun, 28 May 2017 19:25:54 +0300 Subject: [PATCH 2/4] improving attachment serialization --- lib/lita/adapters/slack/message_handler.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/lita/adapters/slack/message_handler.rb b/lib/lita/adapters/slack/message_handler.rb index 60f3d47..716bad9 100644 --- a/lib/lita/adapters/slack/message_handler.rb +++ b/lib/lita/adapters/slack/message_handler.rb @@ -52,14 +52,12 @@ def body def parse_attachment(a) lines = [] lines << a["pretext"] - lines << a["author_name"] lines << a["title"] - lines << a["text"] + lines << a["text"] || a["fallback"] Array(a["fields"]).map do |field| lines << field["title"] lines << field["value"] end - lines << a["fallback"] if lines.none? lines.compact.map(&:strip).reject(&:empty?) end From ee356fe8dda5895c843f6b3349a30e3ac6fb4aab Mon Sep 17 00:00:00 2001 From: Nikolay Vashchenko Date: Sun, 28 May 2017 19:29:10 +0300 Subject: [PATCH 3/4] covering attachment keeping with test --- .../adapters/slack/message_handler_spec.rb | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/spec/lita/adapters/slack/message_handler_spec.rb b/spec/lita/adapters/slack/message_handler_spec.rb index c449b0c..8ae3f1e 100644 --- a/spec/lita/adapters/slack/message_handler_spec.rb +++ b/spec/lita/adapters/slack/message_handler_spec.rb @@ -114,26 +114,37 @@ end end - context "when the message has attach" do + context "when the message has attach", focus: true do + + let(:data) do { - "type" => "message", + "type"=>"message", "channel" => "C2147483705", "user" => "U023BECGF", - "text" => "Hello", - "attachments" => [{"text" => "attached hello"}] + "text" => "Text", + "attachments" => + [{ + "fallback" => "attached fallback", + "text" => "attached text", + "pretext"=>"attached pretext", + "title"=>"attached title", + "fields" => [{ "title" => "attached title", "value" => "attached value" }] + }] } end - it "recives attachment text" do + it "receives both serialized and raw attachments" do expect(Lita::Message).to receive(:new).with( robot, - "Hello\nattached hello", + "Text\nattached pretext\nattached title\nattached text\nattached title\nattached value", source ).and_return(message) subject.handle + expect(message.extensions[:slack][:attachments]).to eq(data["attachments"]) end + end context "when the message is nil" do From b0ffefc1c5f564b51107af2263e4a99992ecf2c1 Mon Sep 17 00:00:00 2001 From: Nikolay Vashchenko Date: Sun, 28 May 2017 19:29:59 +0300 Subject: [PATCH 4/4] styling and metadata --- lib/lita/adapters/slack/message_handler.rb | 26 ++++++++++++------- .../adapters/slack/message_handler_spec.rb | 11 +++----- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/lita/adapters/slack/message_handler.rb b/lib/lita/adapters/slack/message_handler.rb index 716bad9..d6c07d4 100644 --- a/lib/lita/adapters/slack/message_handler.rb +++ b/lib/lita/adapters/slack/message_handler.rb @@ -39,22 +39,25 @@ def handle attr_reader :type def body - normalized_text = if data["text"] - data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}") - end + normalized_text = nil + if data["text"] + normalized_text = data["text"].sub(/^\s*<@#{robot_id}>/, "@#{robot.mention_name}") + end normalized_text = remove_formatting(normalized_text) unless normalized_text.nil? - lines = Array(data["attachments"]).inject([normalized_text]){|total, att| total.concat parse_attachment(att)} + lines = Array(data["attachments"]).inject([normalized_text]){ + |total, att| total.concat parse_attachment(att) + } lines.compact.join("\n") end - def parse_attachment(a) + def parse_attachment(attachment) lines = [] - lines << a["pretext"] - lines << a["title"] - lines << a["text"] || a["fallback"] - Array(a["fields"]).map do |field| + lines << attachment["pretext"] + lines << attachment["title"] + lines << attachment["text"] || attachment["fallback"] + Array(attachment["fields"]).map do |field| lines << field["title"] lines << field["value"] end @@ -182,7 +185,10 @@ def handle_reaction item_user = User.find_by_id(data["item_user"]) || User.create(data["item_user"]) # build a payload following slack convention for reactions - payload = { user: user, name: data["reaction"], item_user: item_user, item: data["item"], event_ts: data["event_ts"] } + payload = { + user: user, name: data["reaction"], item_user: item_user, + item: data["item"], event_ts: data["event_ts"] + } # trigger the appropriate slack reaction event robot.trigger("slack_#{type}".to_sym, payload) diff --git a/spec/lita/adapters/slack/message_handler_spec.rb b/spec/lita/adapters/slack/message_handler_spec.rb index 8ae3f1e..e529c36 100644 --- a/spec/lita/adapters/slack/message_handler_spec.rb +++ b/spec/lita/adapters/slack/message_handler_spec.rb @@ -114,12 +114,10 @@ end end - context "when the message has attach", focus: true do - - + context "when the message has attach" do let(:data) do { - "type"=>"message", + "type" =>"message", "channel" => "C2147483705", "user" => "U023BECGF", "text" => "Text", @@ -127,8 +125,8 @@ [{ "fallback" => "attached fallback", "text" => "attached text", - "pretext"=>"attached pretext", - "title"=>"attached title", + "pretext" => "attached pretext", + "title" =>"attached title", "fields" => [{ "title" => "attached title", "value" => "attached value" }] }] } @@ -144,7 +142,6 @@ subject.handle expect(message.extensions[:slack][:attachments]).to eq(data["attachments"]) end - end context "when the message is nil" do