lita-karma 2.1.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -1
- data/Rakefile +5 -0
- data/lib/lita/handlers/karma.rb +23 -29
- data/lita-karma.gemspec +3 -3
- data/spec/lita/handlers/karma_spec.rb +62 -20
- data/spec/spec_helper.rb +2 -0
- metadata +9 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f7807772d594056ec3c21dae05d056be2394e3a
|
4
|
+
data.tar.gz: d28351489ce8234e359a15657bccb4cc87f4e0a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae64368983385ac07aa53548fdb5f91276b4d15c323cf47845208b46f4b1e7d1bf1fdc8ef041daee27bcf5eadb33821d0a7132eda98a2739bd8fdbec626248f3
|
7
|
+
data.tar.gz: f5b8da41d0caa619038e2518a400312d54afd9a005e807c04d39f83a71f798f68b07e4a7df8abd14de9470ec8266990decea488038e037605a941f1a6d118e22
|
data/README.md
CHANGED
@@ -18,7 +18,8 @@ gem "lita-karma"
|
|
18
18
|
|
19
19
|
### Optional attributes
|
20
20
|
|
21
|
-
* `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.
|
21
|
+
* `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).
|
22
|
+
* `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`.
|
22
23
|
* `term_pattern` (Regexp) - Determines what Lita will recognize as a valid term for tracking karma. Default: `/[\[\]\p{Word}\._|\{\}]{2,}/`.
|
23
24
|
* `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.
|
24
25
|
|
data/Rakefile
CHANGED
data/lib/lita/handlers/karma.rb
CHANGED
@@ -4,19 +4,18 @@ module Lita
|
|
4
4
|
module Handlers
|
5
5
|
# Tracks karma points for arbitrary terms.
|
6
6
|
class Karma < Handler
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
config :cooldown, types: [Integer, nil], default: 300
|
8
|
+
config :link_karma_threshold, types: [Integer, nil], default: 10
|
9
|
+
config :term_pattern, type: Regexp, default: /[\[\]\p{Word}\._|\{\}]{2,}/
|
10
|
+
config :term_normalizer do
|
11
|
+
validate do |value|
|
12
|
+
"must be a callable object" unless value.respond_to?(:call)
|
13
|
+
end
|
11
14
|
end
|
12
15
|
|
13
16
|
on :loaded, :upgrade_data
|
14
17
|
on :loaded, :define_routes
|
15
18
|
|
16
|
-
def self.default_config(config)
|
17
|
-
config.cooldown = 300
|
18
|
-
end
|
19
|
-
|
20
19
|
def upgrade_data(payload)
|
21
20
|
return if redis.exists("support:reverse_links")
|
22
21
|
redis.keys("links:*").each do |key|
|
@@ -29,11 +28,8 @@ module Lita
|
|
29
28
|
end
|
30
29
|
|
31
30
|
def define_routes(payload)
|
32
|
-
self.class.term_pattern =
|
33
|
-
Lita.config.handlers.karma.term_pattern || TERM_PATTERN
|
34
|
-
|
35
31
|
define_static_routes
|
36
|
-
define_dynamic_routes(term_pattern.source)
|
32
|
+
define_dynamic_routes(config.term_pattern.source)
|
37
33
|
end
|
38
34
|
|
39
35
|
def increment(response)
|
@@ -73,6 +69,18 @@ module Lita
|
|
73
69
|
response.matches.each do |match|
|
74
70
|
term1, term2 = normalize_term(match[0]), normalize_term(match[1])
|
75
71
|
|
72
|
+
if config.link_karma_threshold
|
73
|
+
threshold = config.link_karma_threshold.abs
|
74
|
+
|
75
|
+
_total_score, term2_score, _links = scores_for(term2)
|
76
|
+
_total_score, term1_score, _links = scores_for(term1)
|
77
|
+
|
78
|
+
if term1_score.abs < threshold || term2_score.abs < threshold
|
79
|
+
response.reply "Terms must have less than -#{threshold} or more than #{threshold} karma to be linked or linked to."
|
80
|
+
return
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
76
84
|
if redis.sadd("links:#{term1}", term2)
|
77
85
|
redis.sadd("linked_to:#{term2}", term1)
|
78
86
|
response.reply "#{term2} has been linked to #{term1}."
|
@@ -258,10 +266,8 @@ HELP
|
|
258
266
|
end
|
259
267
|
|
260
268
|
def normalize_term(term)
|
261
|
-
|
262
|
-
|
263
|
-
if term_normalizer.respond_to?(:call)
|
264
|
-
term_normalizer.call(term)
|
269
|
+
if config.term_normalizer
|
270
|
+
config.term_normalizer.call(term)
|
265
271
|
else
|
266
272
|
term.to_s.downcase.strip
|
267
273
|
end
|
@@ -300,19 +306,7 @@ HELP
|
|
300
306
|
end
|
301
307
|
|
302
308
|
def set_cooldown(term, user_id)
|
303
|
-
cooldown
|
304
|
-
|
305
|
-
if cooldown
|
306
|
-
redis.setex(
|
307
|
-
"cooldown:#{user_id}:#{term}",
|
308
|
-
cooldown.to_i,
|
309
|
-
1
|
310
|
-
)
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
def term_pattern
|
315
|
-
self.class.term_pattern
|
309
|
+
redis.setex("cooldown:#{user_id}:#{term}", config.cooldown.to_i, 1) if config.cooldown
|
316
310
|
end
|
317
311
|
end
|
318
312
|
|
data/lita-karma.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "lita-karma"
|
3
|
-
spec.version = "
|
3
|
+
spec.version = "3.0.0"
|
4
4
|
spec.authors = ["Jimmy Cuadra"]
|
5
5
|
spec.email = ["jimmy@jimmycuadra.com"]
|
6
6
|
spec.description = %q{A Lita handler for tracking karma points for arbitrary terms.}
|
@@ -14,11 +14,11 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
15
|
spec.require_paths = ["lib"]
|
16
16
|
|
17
|
-
spec.add_runtime_dependency "lita", ">=
|
17
|
+
spec.add_runtime_dependency "lita", ">= 4.0"
|
18
18
|
|
19
19
|
spec.add_development_dependency "bundler", "~> 1.3"
|
20
20
|
spec.add_development_dependency "rake"
|
21
|
-
spec.add_development_dependency "rspec", "
|
21
|
+
spec.add_development_dependency "rspec", ">= 3.0.0"
|
22
22
|
spec.add_development_dependency "simplecov"
|
23
23
|
spec.add_development_dependency "coveralls"
|
24
24
|
end
|
@@ -4,23 +4,26 @@ describe Lita::Handlers::Karma, lita_handler: true do
|
|
4
4
|
let(:payload) { double("payload") }
|
5
5
|
|
6
6
|
before do
|
7
|
-
|
7
|
+
registry.config.handlers.karma.cooldown = nil
|
8
|
+
registry.config.handlers.karma.link_karma_threshold = nil
|
8
9
|
described_class.routes.clear
|
9
10
|
subject.define_routes(payload)
|
10
11
|
end
|
11
12
|
|
12
|
-
it {
|
13
|
-
it {
|
14
|
-
it {
|
15
|
-
it {
|
16
|
-
it {
|
17
|
-
it {
|
18
|
-
it
|
19
|
-
|
20
|
-
|
21
|
-
it {
|
22
|
-
it {
|
23
|
-
it {
|
13
|
+
it { is_expected.to route("foo++").to(:increment) }
|
14
|
+
it { is_expected.to route("foo--").to(:decrement) }
|
15
|
+
it { is_expected.to route("foo~~").to(:check) }
|
16
|
+
it { is_expected.to route_command("karma best").to(:list_best) }
|
17
|
+
it { is_expected.to route_command("karma worst").to(:list_worst) }
|
18
|
+
it { is_expected.to route_command("karma modified").to(:modified) }
|
19
|
+
it do
|
20
|
+
is_expected.to route_command("karma delete").with_authorization_for(:karma_admins).to(:delete)
|
21
|
+
end
|
22
|
+
it { is_expected.to route_command("karma").to(:list_best) }
|
23
|
+
it { is_expected.to route_command("foo += bar").to(:link) }
|
24
|
+
it { is_expected.to route_command("foo -= bar").to(:unlink) }
|
25
|
+
it { is_expected.not_to route("+++++").to(:increment) }
|
26
|
+
it { is_expected.not_to route("-----").to(:decrement) }
|
24
27
|
|
25
28
|
describe "#update_data" do
|
26
29
|
before { subject.redis.flushdb }
|
@@ -28,8 +31,8 @@ describe Lita::Handlers::Karma, lita_handler: true do
|
|
28
31
|
it "adds reverse link data for all linked terms" do
|
29
32
|
subject.redis.sadd("links:foo", ["bar", "baz"])
|
30
33
|
subject.upgrade_data(payload)
|
31
|
-
expect(subject.redis.sismember("linked_to:bar", "foo")).to
|
32
|
-
expect(subject.redis.sismember("linked_to:baz", "foo")).to
|
34
|
+
expect(subject.redis.sismember("linked_to:bar", "foo")).to be(true)
|
35
|
+
expect(subject.redis.sismember("linked_to:baz", "foo")).to be(true)
|
33
36
|
end
|
34
37
|
|
35
38
|
it "skips the update if it's already been done" do
|
@@ -57,7 +60,7 @@ describe Lita::Handlers::Karma, lita_handler: true do
|
|
57
60
|
end
|
58
61
|
|
59
62
|
it "replies with a warning if term increment is on cooldown" do
|
60
|
-
|
63
|
+
registry.config.handlers.karma.cooldown = 10
|
61
64
|
send_message("foo++")
|
62
65
|
send_message("foo++")
|
63
66
|
expect(replies.last).to match(/cannot modify foo/)
|
@@ -93,7 +96,7 @@ describe Lita::Handlers::Karma, lita_handler: true do
|
|
93
96
|
end
|
94
97
|
|
95
98
|
it "replies with a warning if term increment is on cooldown" do
|
96
|
-
|
99
|
+
registry.config.handlers.karma.cooldown = 10
|
97
100
|
send_message("foo--")
|
98
101
|
send_message("foo--")
|
99
102
|
expect(replies.last).to match(/cannot modify foo/)
|
@@ -178,6 +181,45 @@ MSG
|
|
178
181
|
/foo: 3 \(1\), linked to: ba[rz]: 1, ba[rz]: 1/
|
179
182
|
)
|
180
183
|
end
|
184
|
+
|
185
|
+
context "when link_karma_threshold is set" do
|
186
|
+
before do
|
187
|
+
registry.config.handlers.karma.link_karma_threshold = 1
|
188
|
+
end
|
189
|
+
|
190
|
+
it "doesn't allow a term to be linked if both are below the threshold" do
|
191
|
+
send_command("foo += bar")
|
192
|
+
expect(replies.last).to include("must have less than")
|
193
|
+
end
|
194
|
+
|
195
|
+
it "doesn't allow a term to be linked if it's below the threshold" do
|
196
|
+
send_command("foo++")
|
197
|
+
send_command("foo += bar")
|
198
|
+
expect(replies.last).to include("must have less than")
|
199
|
+
end
|
200
|
+
|
201
|
+
it "doesn't allow a term to be linked to another term below the threshold" do
|
202
|
+
send_command("bar++")
|
203
|
+
send_command("foo += bar")
|
204
|
+
expect(replies.last).to include("must have less than")
|
205
|
+
end
|
206
|
+
|
207
|
+
it "allows links if both terms meet the threshold" do
|
208
|
+
send_command("foo++ bar++")
|
209
|
+
send_command("foo += bar")
|
210
|
+
expect(replies.last).to include("has been linked")
|
211
|
+
send_command("bar += foo")
|
212
|
+
expect(replies.last).to include("has been linked")
|
213
|
+
end
|
214
|
+
|
215
|
+
it "uses the absolute value for terms with negative karma" do
|
216
|
+
send_command("foo-- bar--")
|
217
|
+
send_command("foo += bar")
|
218
|
+
expect(replies.last).to include("has been linked")
|
219
|
+
send_command("bar += foo")
|
220
|
+
expect(replies.last).to include("has been linked")
|
221
|
+
end
|
222
|
+
end
|
181
223
|
end
|
182
224
|
|
183
225
|
describe "#unlink" do
|
@@ -227,7 +269,7 @@ MSG
|
|
227
269
|
|
228
270
|
describe "#delete" do
|
229
271
|
before do
|
230
|
-
|
272
|
+
robot.auth.add_user_to_group!(user, :karma_admins)
|
231
273
|
end
|
232
274
|
|
233
275
|
it "deletes the term" do
|
@@ -278,8 +320,8 @@ MSG
|
|
278
320
|
|
279
321
|
describe "custom term patterns and normalization" do
|
280
322
|
before do
|
281
|
-
|
282
|
-
|
323
|
+
registry.config.handlers.karma.term_pattern = /[<:]([^>:]+)[>:]/
|
324
|
+
registry.config.handlers.karma.term_normalizer = lambda do |term|
|
283
325
|
term.to_s.downcase.strip.sub(/[<:]([^>:]+)[>:]/, '\1')
|
284
326
|
end
|
285
327
|
described_class.routes.clear
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lita-karma
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jimmy Cuadra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lita
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 3.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 3.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -133,11 +133,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.2.
|
136
|
+
rubygems_version: 2.2.2
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: A Lita handler for tracking karma points for arbitrary terms.
|
140
140
|
test_files:
|
141
141
|
- spec/lita/handlers/karma_spec.rb
|
142
142
|
- spec/spec_helper.rb
|
143
|
-
has_rdoc:
|