class Lita::Handlers::SlackKarmaSync
Constants
- ANYNAME_GLOB
- TERMS_KEY
Public Instance Methods
update_karma_terms(payload)
click to toggle source
# File lib/lita/handlers/slack_karma_sync.rb, line 17 def update_karma_terms(payload) user_id = payload.fetch(:slack_user).id log.debug("Slack user created: #{user_id}") new_term = find_term(user_id) existing_term = find_existing_term_for(user_id) return if existing_term == new_term log.debug("Updating Karma term for User: #{user_id}. #{existing_term.to_s} => #{new_term.to_s}") copy_links(from: existing_term, to: new_term) copy_karma(from: existing_term, to: new_term) existing_term.delete end
Private Instance Methods
copy_karma(from:, to:)
click to toggle source
# File lib/lita/handlers/slack_karma_sync.rb, line 44 def copy_karma(from:, to:) karma_redis.zincrby(TERMS_KEY, from.own_score, to.to_s) end
copy_links(from:, to:)
click to toggle source
# File lib/lita/handlers/slack_karma_sync.rb, line 48 def copy_links(from:, to:) from.links.each do |linked_term| to.link(linked_term) end end
find_existing_term_for(user_id)
click to toggle source
# File lib/lita/handlers/slack_karma_sync.rb, line 33 def find_existing_term_for(user_id) terms = karma_redis.zscan_each(TERMS_KEY, match: normalized_user_term(user_id)).to_a terms = terms.map { |term, _score| find_term(term, normalize: false) } fail "Found unexpected terms: #{}" if terms.size > 1 terms.first || NoMatchingTerm.new end
find_term(raw_term, normalize: true)
click to toggle source
# File lib/lita/handlers/slack_karma_sync.rb, line 54 def find_term(raw_term, normalize: true) Karma::Term.new(robot, raw_term, normalize: normalize) end
karma_redis()
click to toggle source
# File lib/lita/handlers/slack_karma_sync.rb, line 62 def karma_redis @karma_redis ||= Redis::Namespace.new('handlers:karma', redis: Lita.redis) end
normalized_user_term(user_id)
click to toggle source
# File lib/lita/handlers/slack_karma_sync.rb, line 58 def normalized_user_term(user_id) config.user_term_normalizer.call(user_id, ANYNAME_GLOB) end