Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protection against flood loop #17

Closed
Nakilon opened this issue Oct 3, 2015 · 2 comments
Closed

Protection against flood loop #17

Nakilon opened this issue Oct 3, 2015 · 2 comments

Comments

@Nakilon
Copy link

Nakilon commented Oct 3, 2015

You may be interested in:

  1. respecting https://api.slack.com/docs/rate-limits
  2. ignoring message from itself
    These things together can loop and flood the server with messages.

I'm lazy to write tests so I'll just leave my monkey-patch for gem v0.3.0. Instead of rewriting dispatch one probably could wrap each "message" callback in one for check but it would be harder with dynamic adding callbacks.

Slack::RealTime::Api::Message.module_eval do
  alias :message_old :message
  def message *args
    message_old *args
    sleep 1
  end
end

Slack::RealTime::Client.class_eval do
  alias :old_initialize :initialize
  def initialize *args
    old_initialize *args
    @callbacks = Hash.new{ |h, k| h[k] = [] }
  end
  protected
  def dispatch event
    return false unless event.data
    data = JSON.parse event.data
    return false unless type = data["type"]
    return false unless callbacks = @callbacks[type]
    callbacks.each do |c|
      c.call data unless type == "message" && self.self["id"] == data["user"]
    end.empty? ^ true
  end
end
@dblock
Copy link
Collaborator

dblock commented Oct 3, 2015

Explicitly ignoring messages from self is definitely something that should be on by default. Would love a proper PR for this if I don't get to it.

@dblock
Copy link
Collaborator

dblock commented Oct 16, 2015

I don't think this belongs in slack-ruby-client because a message is a message and downstream clients may want to notice that their own message was delivered. However I've implemented this in dblock/slack-ruby-bot@3a54456 for slack-ruby-bot as a feature. Closing this here.

@dblock dblock closed this as completed Oct 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants