Class: Lita::Handlers::Karma

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/karma.rb

Overview

Tracks karma points for arbitrary terms.

Instance Method Summary collapse

Instance Method Details

#check(response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lita/handlers/karma.rb', line 43

def check(response)
  output = []

  response.matches.each do |match|
    term = normalize_term(match[0])
    total_score, own_score, links = scores_for(term)

    string = "#{term}: #{total_score}"
    unless links.empty?
      string << " (#{own_score}), linked to: #{links.join(", ")}"
    end
    output << string
  end

  response.reply *output
end

#decrement(response) ⇒ Object



39
40
41
# File 'lib/lita/handlers/karma.rb', line 39

def decrement(response)
  modify(response, -1)
end

#define_routes(payload) ⇒ Object



30
31
32
33
# File 'lib/lita/handlers/karma.rb', line 30

def define_routes(payload)
  define_static_routes
  define_dynamic_routes(config.term_pattern.source)
end

#delete(response) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lita/handlers/karma.rb', line 126

def delete(response)
  term = response.message.body.sub(/^karma delete /, "")

  redis.del("modified:#{term}")
  redis.del("links:#{term}")
  redis.smembers("linked_to:#{term}").each do |key|
    redis.srem("links:#{key}", term)
  end
  redis.del("linked_to:#{term}")

  if redis.zrem("terms", term)
    response.reply("#{term} has been deleted.")
  else
    response.reply("#{term} does not exist.")
  end
end

#increment(response) ⇒ Object



35
36
37
# File 'lib/lita/handlers/karma.rb', line 35

def increment(response)
  modify(response, 1)
end


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lita/handlers/karma.rb', line 68

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}."
    else
      response.reply "#{term2} is already linked to #{term1}."
    end
  end
end

#list_best(response) ⇒ Object



60
61
62
# File 'lib/lita/handlers/karma.rb', line 60

def list_best(response)
  list(response, :zrevrange)
end

#list_worst(response) ⇒ Object



64
65
66
# File 'lib/lita/handlers/karma.rb', line 64

def list_worst(response)
  list(response, :zrange)
end

#modified(response) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lita/handlers/karma.rb', line 106

def modified(response)
  term = normalize_term(response.args[1])

  if term.empty?
    response.reply "Format: #{robot.name}: karma modified TERM"
    return
  end

  user_ids = redis.smembers("modified:#{term}")

  if user_ids.empty?
    response.reply "#{term} has never been modified."
  else
    output = user_ids.map do |id|
      User.find_by_id(id).name
    end.join(", ")
    response.reply output
  end
end


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lita/handlers/karma.rb', line 93

def unlink(response)
  response.matches.each do |match|
    term1, term2 = normalize_term(match[0]), normalize_term(match[1])

    if redis.srem("links:#{term1}", term2)
      redis.srem("linked_to:#{term2}", term1)
      response.reply "#{term2} has been unlinked from #{term1}."
    else
      response.reply "#{term2} is not linked to #{term1}."
    end
  end
end

#upgrade_data(payload) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/lita/handlers/karma.rb', line 19

def upgrade_data(payload)
  return if redis.exists("support:reverse_links")
  redis.keys("links:*").each do |key|
    term = key.sub(/^links:/, "")
    redis.smembers(key).each do |link|
      redis.sadd("linked_to:#{link}", term)
    end
  end
  redis.incr("support:reverse_links")
end