Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
styling and metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayRys authored and Ridwan Sharif committed Jan 25, 2018
1 parent ee356fe commit b0ffefc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
26 changes: 16 additions & 10 deletions lib/lita/adapters/slack/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions spec/lita/adapters/slack/message_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,19 @@
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",
"attachments" =>
[{
"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" }]
}]
}
Expand All @@ -144,7 +142,6 @@
subject.handle
expect(message.extensions[:slack][:attachments]).to eq(data["attachments"])
end

end

context "when the message is nil" do
Expand Down

0 comments on commit b0ffefc

Please sign in to comment.