Module: JsChat::FloodProtection

Included in:
User
Defined in:
lib/jschat/flood_protection.rb

Instance Method Summary collapse

Instance Method Details

#detect_floodingObject



27
28
29
# File 'lib/jschat/flood_protection.rb', line 27

def detect_flooding
  @flooding = @activity_log.size > 10 and @activity_log.sort.inject { |i, sum| sum.to_i - i.to_i } - @activity_log.first.to_i < 0.5
end

#flooding?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/jschat/flood_protection.rb', line 31

def flooding?
  @flooding
end

#remove_old_activity_logsObject



35
36
37
# File 'lib/jschat/flood_protection.rb', line 35

def remove_old_activity_logs
  @activity_log.delete_if { |l| l + 5 < Time.now.utc }
end

#seen!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jschat/flood_protection.rb', line 8

def seen!
  @activity_log ||= []
  @activity_log << Time.now.utc
  @activity_log.shift if @activity_log.size > 50
  remove_old_activity_logs
  detect_flooding

  if flooding?
    if @still_flooding
      raise JsChat::Errors::StillFlooding
    else
      @still_flooding = true
      raise JsChat::Errors::Flooding.new(:flooding, 'Please wait a few seconds before responding')
    end
  elsif @still_flooding
    @still_flooding = false
  end
end