lita-slack-karma-sync 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fee7b665492d9f275ed838e0d339722d0d4e5437
4
+ data.tar.gz: b99fc863dd61eb9e20298a6e7fff7fe976a8a332
5
+ SHA512:
6
+ metadata.gz: 8af05ac63334d5551896ff5fd0ecfc3bb680cc3aa051a697d1b2b9e877cdd99e231f194b961b8eacd31219f6b7457534cf7325b2004edb95cbf7ced9f89be9d8
7
+ data.tar.gz: fbd522dfc7c8ac6064aa0fc8aaeb41fa3987fea335aea0f844099a56e4b50554e0ad84d43003a0220bb6b5b87906ad3965a9d958edd20da9ffa7688f0a917c8a
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Steven Harman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,117 @@
1
+ # Lita + Slack + Karma? You need to Sync!
2
+
3
+ Are you a Slack user? And does your team also use `lita-karma`? This plugin can
4
+ be used to keep your karma terms synced up with your Slack name.
5
+
6
+ ![lizlemon++](doc/liz-lemon-self-high-five.gif)
7
+
8
+ ### What's that now?
9
+
10
+ When using Slack and `lita-karma` you can give another user, or any term in the world, some karma like so:
11
+
12
+ ```
13
+ > lizlemon++
14
+ > Litabot: lizlemon: 40
15
+ > computers--
16
+ > Litabot: computers: -10
17
+ > ruby~~
18
+ > Litabot: ruby: 12 (15), linked to: rubby: 3
19
+ ```
20
+
21
+ And that works great for general terms, but there can be many ways to address a
22
+ user. "Liz Lemon" from the above example could be addressed in any of the
23
+ following ways:
24
+
25
+ * her mention name: `> lizlemon++`
26
+ * her mention name preceded with "`@`": `> @lizlemon++`
27
+ * her Slack UUID: `> U03BX9ZA9++`
28
+ * her Slack UUID preceded with "`@`": `> @U03BX9ZA9++`
29
+ * her full name with a "`:`" delimiters: `> :Liz Lemon:++`
30
+ * And each of the above could different based on capitalization!
31
+
32
+ The problem is these all become unique "Karma Terms" and Liz Lemon's karma has
33
+ been spread over a number of terms. To combat this, you can use `lita-karma`'s
34
+ `term_normalizer` to normalize to a single, consistent "term" for users. The
35
+ Slack UUID is a great choice for that. The wrinkle then is that you're left
36
+ with this:
37
+
38
+ ```
39
+ > lizlemon++
40
+ > Litabot: U03BX9ZA9: 41
41
+ > Litabot karma
42
+ > Litabot:
43
+ U09BZ9AF7: 45
44
+ U08FA0EX1: 43
45
+ tacos: 42
46
+ U03BX9ZA9: 41
47
+ ```
48
+
49
+ Who are those users? That's where this plugin can help! :thumbsup:
50
+
51
+ ## Installation
52
+
53
+ Add `lita-slack-karma-sync` to your Lita instance's Gemfile:
54
+
55
+ ``` ruby
56
+ gem "lita-slack-karma-sync"
57
+ ```
58
+
59
+ ## Configuration
60
+
61
+ In your `lita_config.rb`:
62
+
63
+ 1. Create a `proc` that will normalize a given UUID and name to a consistent
64
+ string. Something like this:
65
+
66
+ ```ruby
67
+ normalized_karma_user_term = ->(user_id, user_name) {
68
+ "@#{user_id} (#{user_name})" #=> @UUID (Liz Lemon)
69
+ }
70
+ ```
71
+
72
+ 1. Configure `lita-karma`'s `term_normalizer` to try to find a user for the
73
+ "term", and then normalize them via the proc defined above.
74
+
75
+ ```ruby
76
+ config.handlers.karma.term_normalizer = lambda do |full_term|
77
+ term = full_term.to_s.strip.sub(/[<:]([^>:]+)[>:]/, '\1')
78
+ user = Lita::User.fuzzy_find(term)
79
+
80
+ if user
81
+ normalized_karma_user_term.call(user.id, user.name)
82
+ else
83
+ term.downcase
84
+ end
85
+ end
86
+ ```
87
+
88
+ 1. Tell `lita-slack-karma-sync` to use the same proc to normalize user terms:
89
+
90
+ ```ruby
91
+ config.handlers.slack_karma_sync.user_term_normalizer = normalized_karma_user_term
92
+ ```
93
+
94
+ ## Usage
95
+
96
+ Give folks karma!
97
+
98
+ ```
99
+ > lizlemon++
100
+ > Litabot: @U03BX9ZA9 (Liz Lemon): 43
101
+ > @lizlemon++
102
+ > Litabot: @U03BX9ZA9 (Liz Lemon): 44
103
+ > liz++
104
+ > Litabot: @U03BX9ZA9 (Liz Lemon): 45
105
+ > :Liz Lemon:++
106
+ > Litabot: @U03BX9ZA9 (Liz Lemon): 46
107
+ > Litabot karma
108
+ > Litabot:
109
+ @U03BX9ZA9 (Liz Lemon): 46
110
+ @U09BZ9AF7 (Tracy Jordan): 45
111
+ tacos: 42
112
+ @U08FA0EX1 (Jack Donaghy): 41
113
+ ```
114
+
115
+ ## License
116
+
117
+ See the [LICENSE](LICENSE) file.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ require "lita"
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ )]
6
+
7
+ require "lita/handlers/slack_karma_sync"
8
+
@@ -0,0 +1,78 @@
1
+ require 'lita-karma'
2
+
3
+ module Lita
4
+ module Handlers
5
+ class SlackKarmaSync < Handler
6
+ TERMS_KEY = 'terms'.freeze
7
+ ANYNAME_GLOB = '*'.freeze
8
+
9
+ on :slack_user_created, :update_karma_terms
10
+
11
+ config :user_term_normalizer do
12
+ validate do |value|
13
+ t('callable_required') unless value.respond_to?(:call)
14
+ end
15
+ end
16
+
17
+ def update_karma_terms(payload)
18
+ user_id = payload.fetch(:slack_user).id
19
+
20
+ new_term = find_term(user_id)
21
+ existing_term = find_existing_term_for(user_id)
22
+ return if existing_term == new_term
23
+
24
+ copy_links(from: existing_term, to: new_term)
25
+ copy_karma(from: existing_term, to: new_term)
26
+ existing_term.delete
27
+ end
28
+
29
+ private
30
+
31
+ def find_existing_term_for(user_id)
32
+ terms = karma_redis.zscan_each(TERMS_KEY, match: normalized_user_term(user_id)).to_a
33
+ terms = terms.map { |term, _score|
34
+ find_term(term, normalize: false)
35
+ }
36
+
37
+ fail "Found unexpected terms: #{}" if terms.size > 1
38
+
39
+ terms.first || NoMatchingTerm.new
40
+ end
41
+
42
+ def copy_karma(from:, to:)
43
+ karma_redis.zincrby(TERMS_KEY, from.own_score, to.to_s)
44
+ end
45
+
46
+ def copy_links(from:, to:)
47
+ from.links.each do |linked_term|
48
+ to.link(linked_term)
49
+ end
50
+ end
51
+
52
+ def find_term(raw_term, normalize: true)
53
+ Karma::Term.new(robot, raw_term, normalize: normalize)
54
+ end
55
+
56
+ def normalized_user_term(user_id)
57
+ config.user_term_normalizer.call(user_id, ANYNAME_GLOB)
58
+ end
59
+
60
+ def karma_redis
61
+ @karma_redis ||= Redis::Namespace.new('handlers:karma', redis: Lita.redis)
62
+ end
63
+
64
+ class NoMatchingTerm
65
+ def eql?(other)
66
+ self.class.equal?(other.class)
67
+ end
68
+ alias == eql?
69
+
70
+ def hash
71
+ self.class.hash
72
+ end
73
+ end
74
+ end
75
+
76
+ Lita.register_handler(SlackKarmaSync)
77
+ end
78
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-slack-karma-sync"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Steven Harman"]
5
+ spec.email = ["steven@harmanly.com"]
6
+ spec.description = "Sync Slack user names with your lita-karma"
7
+ spec.summary = "Are you a Slack user? And does your team also use `lita-karma`? This plugin can be used to keep your karma terms synced up with your Slack name."
8
+ spec.homepage = "https://github.com/stevenharman/lita-slack-karma-sync"
9
+ spec.license = "MIT"
10
+ spec.metadata = { "lita_plugin_type" => "handler" }
11
+
12
+ spec.files = `git ls-files`.split($/).reject {|f| f =~ %r(doc\/.+\.gif\z)}
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.required_ruby_version = ">= 2.1.0"
17
+
18
+ spec.add_runtime_dependency "lita", ">= 4.2"
19
+ spec.add_runtime_dependency "lita-slack", ">= 1.2.0"
20
+ spec.add_runtime_dependency "lita-karma", ">= 3.0.2"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rack-test"
25
+ spec.add_development_dependency "rspec", ">= 3.0.0"
26
+ end
@@ -0,0 +1,4 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ slack_karma_sync:
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::SlackKarmaSync, :lita_handler,
4
+ additional_lita_handlers: [Lita::Handlers::Karma::Chat, Lita::Handlers::Karma::Config] do
5
+
6
+ after do
7
+ Lita::Handlers::Karma::Chat.routes.clear
8
+ end
9
+
10
+ it { is_expected.to route_event(:slack_user_created).to(:update_karma_terms) }
11
+
12
+ context 'interacting with Karma' do
13
+ let!(:liz1) { Lita::User.create('U03BX9ZA9', name: 'Liz Lemon', mention_name: 'lizlemon') }
14
+ let(:handler_config) { registry.config.handlers }
15
+ let(:format_user_karma_term) { ->(user_id, user_name) { "@#{user_id} (#{user_name})" } }
16
+ let(:term_normalizer) {
17
+ ->(full_term) {
18
+ term = full_term.to_s.strip.sub(/[<:]([^>:]+)[>:]/, '\1')
19
+ user = Lita::User.fuzzy_find(term)
20
+
21
+ if user
22
+ format_user_karma_term.call(user.id, user.name)
23
+ else
24
+ term.downcase
25
+ end
26
+ }
27
+ }
28
+
29
+ before do
30
+ handler_config.karma.cooldown = nil
31
+ handler_config.karma.link_karma_threshold = nil
32
+ handler_config.karma.term_normalizer = term_normalizer
33
+ handler_config.slack_karma_sync.user_term_normalizer = format_user_karma_term
34
+ end
35
+
36
+ it "updates the user's karma and links to reflect their updated name" do
37
+ send_message('lizlemon++')
38
+ expect(replies.last).to eq('@U03BX9ZA9 (Liz Lemon): 1')
39
+
40
+ send_message('tacos++')
41
+ send_command('lizlemon += tacos')
42
+
43
+ send_message('lizlemon~~')
44
+ expect(replies.last).to eq('@U03BX9ZA9 (Liz Lemon): 2 (1), linked to: tacos: 1')
45
+
46
+ slack_user = rename_user(liz1, name: 'Liz2 Lemmonn', mention_name: 'liz2')
47
+ subject.update_karma_terms(slack_user: slack_user)
48
+
49
+ send_message('liz2~~')
50
+ expect(replies.last).to eq('@U03BX9ZA9 (Liz2 Lemmonn): 2 (1), linked to: tacos: 1')
51
+ end
52
+
53
+ def rename_user(user, name:, mention_name:)
54
+ liz2 = Lita::User.create(liz1.id, name: name, mention_name: mention_name)
55
+ double('SlackUser', id: liz2.id, name: liz2.mention_name, real_name: liz2.name)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,6 @@
1
+ require "lita-slack-karma-sync"
2
+ require "lita/rspec"
3
+
4
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
5
+ # was generated with Lita 4, the compatibility mode should be left disabled.
6
+ Lita.version_3_compatibility_mode = false
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-slack-karma-sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Harman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lita-slack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: lita-karma
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 3.0.0
111
+ description: Sync Slack user names with your lita-karma
112
+ email:
113
+ - steven@harmanly.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - LICENSE
121
+ - README.md
122
+ - Rakefile
123
+ - lib/lita-slack-karma-sync.rb
124
+ - lib/lita/handlers/slack_karma_sync.rb
125
+ - lita-slack-karma-sync.gemspec
126
+ - locales/en.yml
127
+ - spec/lita/handlers/slack_karma_sync_spec.rb
128
+ - spec/spec_helper.rb
129
+ homepage: https://github.com/stevenharman/lita-slack-karma-sync
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ lita_plugin_type: handler
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 2.1.0
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.4.6
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Are you a Slack user? And does your team also use `lita-karma`? This plugin
154
+ can be used to keep your karma terms synced up with your Slack name.
155
+ test_files:
156
+ - spec/lita/handlers/slack_karma_sync_spec.rb
157
+ - spec/spec_helper.rb