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

Commit

Permalink
Add Message#reply_privately.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Aug 28, 2013
1 parent b1e141a commit b752132
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/lita/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,13 @@ def match(pattern)
def reply(*strings)
@robot.send_messages(source, *strings)
end

# Replies by sending the given strings back to the user who sent the
# message directly, even if the message was sent in a room.
# @param strings [String, Array<String>] The strings to send back.
# @return [void]
def reply_privately(*strings)
@robot.send_messages(Source.new(source.user), *strings)

This comment has been minimized.

Copy link
@zacstewart

zacstewart Sep 13, 2013

This breaks adapters (Campfire) that don't support private messaging. Without a room in the source, there's no way to send the message anywhere.

This comment has been minimized.

Copy link
@jimmycuadra

jimmycuadra Sep 13, 2013

Author Collaborator

I'll probably be looking into a new API for Source/Message objects in a future major upgrade. For the time being, if particular chat service doesn't support all the features of the robot's API, it's up to the adapter to provide either fallbacks or warnings in the case that something it doesn't support is used.

This comment has been minimized.

Copy link
@zacstewart

zacstewart Sep 14, 2013

Yeah. I don't disagree with adapters having to provide fallbacks for cases like not supporting private messaging, but in this particular case, modifying the Source instead of having a private flag on the Message or something makes it impossible. The adapter aught to be able to decide how to handle private messaging, instead of being told a message came from a different source than it did.

The only fallback possible would be to send the "private" message to every Campfire room the user is in. Or pick one of them at random or something.

This comment has been minimized.

Copy link
@jimmycuadra

jimmycuadra Sep 14, 2013

Author Collaborator

Good call. I'll look into getting this issue fixed this week. Tracking at #18.

This comment has been minimized.

Copy link
@jimmycuadra

jimmycuadra Sep 17, 2013

Author Collaborator

@zacstewart Give the master branch a try. I think this new API should be what you want. Let me know if you have any issues with it. If not, I will release a new gem. Relevant changes: v2.4.0...7da6524

end
end
end
15 changes: 15 additions & 0 deletions spec/lita/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@
subject.reply("foo", "bar")
end
end

describe "#reply_privately" do
it "sends strings directly to the source user" do
subject = described_class.new(
robot,
"Hello",
Lita::Source.new("Carl", "#room")
)
expect(robot).to receive(:send_messages) do |source, *strings|
expect(source.room).to be_nil
expect(strings).to eq(["foo", "bar"])
end
subject.reply_privately("foo", "bar")
end
end
end

0 comments on commit b752132

Please sign in to comment.