Skip to content

Commit

Permalink
Add config.link_karma_threshold.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Oct 23, 2014
1 parent 0bb6230 commit b198a45
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ gem "lita-karma"

### Optional attributes

* `cooldown` (Integer, nil) - Controls how long a user must wait after modifying a term before they can modify it again. The value should be an integer number of seconds. Set it to `nil` to disable rate limiting. Default: `300` (5 minutes.
* `cooldown` (Integer, nil) - Controls how long a user must wait after modifying a term before they can modify it again. The value should be an integer number of seconds. Set it to `nil` to disable rate limiting. Default: `300` (5 minutes).
* `link_karma_threshold` (Integer, nil) - Controls how many points a term must have before it can be linked to other terms or before terms can be linked to it. Treated as an absolute value, so it applies to both positive and negative karma. Set it to `nil` to allow all terms to be linked regardless of karma. Default: `10`.
* `term_pattern` (Regexp) - Determines what Lita will recognize as a valid term for tracking karma. Default: `/[\[\]\p{Word}\._|\{\}]{2,}/`.
* `term_normalizer` (Proc) - A custom callable that determines how each term will be normalized before being stored in Redis. The proc should take one argument, the term as matched via regular expression, and return one value, the normalized version of the term.

Expand Down
23 changes: 14 additions & 9 deletions lib/lita/handlers/karma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Handlers
# Tracks karma points for arbitrary terms.
class Karma < Handler
config :cooldown, types: [Integer, nil], default: 300
config :link_karma_threshold, types: [Integer, nil], default: 10
config :term_pattern, type: Regexp, default: /[\[\]\p{Word}\._|\{\}]{2,}/
config :term_normalizer do
validate do |value|
Expand Down Expand Up @@ -68,6 +69,18 @@ def link(response)
response.matches.each do |match|
term1, term2 = normalize_term(match[0]), normalize_term(match[1])

if config.link_karma_threshold
threshold = config.link_karma_threshold.abs

_total_score, term2_score, _links = scores_for(term2)
_total_score, term1_score, _links = scores_for(term1)

if term1_score.abs < threshold || term2_score.abs < threshold
response.reply "Terms must have less than -#{threshold} or more than #{threshold} karma to be linked or linked to."
return
end
end

if redis.sadd("links:#{term1}", term2)
redis.sadd("linked_to:#{term2}", term1)
response.reply "#{term2} has been linked to #{term1}."
Expand Down Expand Up @@ -293,15 +306,7 @@ def scores_for(term)
end

def set_cooldown(term, user_id)
cooldown = Lita.config.handlers.karma.cooldown

if cooldown
redis.setex(
"cooldown:#{user_id}:#{term}",
cooldown.to_i,
1
)
end
redis.setex("cooldown:#{user_id}:#{term}", config.cooldown.to_i, 1) if config.cooldown
end
end

Expand Down
40 changes: 40 additions & 0 deletions spec/lita/handlers/karma_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

before do
registry.config.handlers.karma.cooldown = nil
registry.config.handlers.karma.link_karma_threshold = nil
described_class.routes.clear
subject.define_routes(payload)
end
Expand Down Expand Up @@ -180,6 +181,45 @@
/foo: 3 \(1\), linked to: ba[rz]: 1, ba[rz]: 1/
)
end

context "when link_karma_threshold is set" do
before do
registry.config.handlers.karma.link_karma_threshold = 1
end

it "doesn't allow a term to be linked if both are below the threshold" do
send_command("foo += bar")
expect(replies.last).to include("must have less than")
end

it "doesn't allow a term to be linked if it's below the threshold" do
send_command("foo++")
send_command("foo += bar")
expect(replies.last).to include("must have less than")
end

it "doesn't allow a term to be linked to another term below the threshold" do
send_command("bar++")
send_command("foo += bar")
expect(replies.last).to include("must have less than")
end

it "allows links if both terms meet the threshold" do
send_command("foo++ bar++")
send_command("foo += bar")
expect(replies.last).to include("has been linked")
send_command("bar += foo")
expect(replies.last).to include("has been linked")
end

it "uses the absolute value for terms with negative karma" do
send_command("foo-- bar--")
send_command("foo += bar")
expect(replies.last).to include("has been linked")
send_command("bar += foo")
expect(replies.last).to include("has been linked")
end
end
end

describe "#unlink" do
Expand Down

0 comments on commit b198a45

Please sign in to comment.