Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Subteam mentions #64

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions lib/lita/adapters/slack/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,51 @@ def remove_formatting(message)
# https://api.slack.com/docs/formatting
message = message.gsub(/
< # opening angle bracket
(?<type>[@#!])? # link type
(?: # Start type & subtype
(?<type>[@#!]) # link type
(?<subtype>[^^>]+# start optional subtype
(?=\^) # subtype is only present with separator
)? # end subtype
(?: # start optional subtype argument 1
\^ # subtype argument separator
(?<arg1>[^^>]+ # subtype argument 1 capture
(?=\^) # subtype argument is only present with separator
) # end subtype argument capture
)? # end subtype argument
(?: # start optional subtype argument 2
\^ # subtype argument separator
(?<arg2>[^^>]+ # subtype argument 2 capture
(?=\^) # subtype argument is only present with separator
) # end subtype argument capture
)? # end subtype argument
\^? # subtype & link separator
)? # end of type & subtype
(?<link>[^>|]+) # link
(?:\| # start of |label (optional)
(?<label>[^>]+) # label
)? # end of label
> # closing angle bracket
/ix) do

type = Regexp.last_match[:type]
subtype = Regexp.last_match[:subtype]
arguments = [Regexp.last_match[:arg1], Regexp.last_match[:arg2]].compact
link = Regexp.last_match[:link]
label = Regexp.last_match[:label]

case Regexp.last_match[:type]
# It is hard to generalize the test for an argument vs. an optional link.
# For now, assume links have a fixed set of protocols
if subtype && link !~ /^(https?|ftp|gopher|skype|mailto):/
arguments << link
link = nil
end

# Without a subtype, the first aguement could be the link
unless subtype || link
link = arguments.first
end

case type
when '@'
if label
label
Expand All @@ -92,7 +126,18 @@ def remove_formatting(message)
end

when '!'
"@#{link}" if ['channel', 'group', 'everyone'].include? link
if subtype.nil?
"@#{link}" if ['channel', 'group', 'everyone'].include? link
else
# In the future we may want to actually process <!date…>
# commands, to allow us to interpret them for adapters. But
# for now falling back to the label is sufficient.
if link
"#{label} (#{link})"
else
label
end
end
else
link = link.gsub /^mailto:/, ''
if label && !(link.include? label)
Expand Down
73 changes: 73 additions & 0 deletions spec/lita/adapters/slack/message_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,24 @@
end
end

context "changes <!subteam…> links to @group_name" do
let(:data) do
{
"type" => "message",
"channel" => "C2147483705",
"text" => "foo <!subteam^S0DFLF55X|@group_name> bar",
}
end
it "removes formatting" do
expect(Lita::Message).to receive(:new).with(
robot,
"foo @group_name bar",
source
).and_return(message)
subject.handle
end
end

context "removes remove formatting around <http> links" do
let(:data) do
{
Expand Down Expand Up @@ -439,6 +457,61 @@
end
end

context "remove formatting around <!date> commands" do
let(:data) do
{
"type" => "message",
"channel" => "C2147483705",
"text" => "foo <!date^1392734382^Posted {date_num} {time_secs}|Posted 2014-02-18 6:39:42 AM> bar",
}
end
it "removes formatting" do
expect(Lita::Message).to receive(:new).with(
robot,
"foo Posted 2014-02-18 6:39:42 AM bar",
source
).and_return(message)
subject.handle
end
end

context "remove formatting around <!date> commands with an optional URL" do
let(:data) do
{
"type" => "message",
"channel" => "C2147483705",
"text" => "foo <!date^1392734382^{date_short}^https://example.com/|Feb 18, 2014 PST> bar",
}
end
it "removes formatting" do
expect(Lita::Message).to receive(:new).with(
robot,
"foo Feb 18, 2014 PST (https://example.com/) bar",
source
).and_return(message)
subject.handle
end
end

context "removes formatting from Slack’s unofficial example command" do
let(:data) do
{
"type" => "message",
"channel" => "C2147483705",
"text" => "bar <!foo^C12345^{foo}^http://www.example.com/|label> baz",
}
end

it "removes formatting" do
expect(Lita::Message).to receive(:new).with(
robot,
"bar label (http://www.example.com/) baz",
source
).and_return(message)
subject.handle
end
end

context "change multiple links at once" do
let(:data) do
{
Expand Down