module AutovoiceStore
AutovoiceStore — per-channel interval/last-run state and global award tally. Public: load_state, save_state, normalize_state, enabled?, set_enabled!, clamp_interval, due_for_run?, should_attempt_award?, activity_cutoff, touch_activity!, mark_run!, increment_total!, global_total Depends: RabbotDb Tests: test/lib/autovoice_store_test.rb
Constants
- DEFAULT_INTERVAL_MIN
- MAX_INTERVAL_MIN
- MIN_INTERVAL_MIN
- PLUGIN
- STATE_KEY
- TOTAL_KEY
Public Instance Methods
Source
# File lib/autovoice/store.rb, line 94 def activity_cutoff(state) parse_time(state["last_run_at"]) end
Source
# File lib/autovoice/store.rb, line 63 def clamp_interval(minutes) value = minutes.to_i value = DEFAULT_INTERVAL_MIN if value.zero? [[value, MIN_INTERVAL_MIN].max, MAX_INTERVAL_MIN].min end
Source
# File lib/autovoice/store.rb, line 69 def due_for_run?(state, now:, force: false) return true if force last_run = parse_time(state["last_run_at"]) return true unless last_run elapsed_min = (now - last_run) / 60.0 elapsed_min >= state["interval_min"].to_i end
Source
# File lib/autovoice/store.rb, line 55 def enabled?(network:, channel:, store: RabbotDb) store.autovoice_enabled?(network: network, channel: channel) end
Source
# File lib/autovoice/store.rb, line 115 def global_total(store: RabbotDb) store.get_plugin_value(plugin: PLUGIN, key: TOTAL_KEY).to_i end
Source
# File lib/autovoice/store.rb, line 109 def increment_total!(store: RabbotDb) total = global_total(store: store) + 1 store.set_plugin_value(plugin: PLUGIN, key: TOTAL_KEY, value: total.to_s) total end
Source
# File lib/autovoice/store.rb, line 24 def load_state(network:, channel:, store: RabbotDb) raw = store.get_plugin_json( plugin: PLUGIN, key: STATE_KEY, network: network, channel: channel, default: {} ) normalize_state(raw) end
Source
# File lib/autovoice/store.rb, line 105 def mark_run!(state, now:) normalize_state(state).merge("last_run_at" => RabbotDb.iso_time(now)) end
Source
# File lib/autovoice/store.rb, line 45 def normalize_state(raw) data = raw.is_a?(Hash) ? raw : {} interval = clamp_interval(data["interval_min"]) { "interval_min" => interval, "last_run_at" => data["last_run_at"].to_s.strip, "last_activity_at" => data["last_activity_at"].to_s.strip } end
Source
# File lib/autovoice/store.rb, line 119 def parse_time(value) return nil if value.nil? || value.to_s.strip.empty? RabbotDb.parse_time(value) end
Source
# File lib/autovoice/store.rb, line 35 def save_state(network:, channel:, state:, store: RabbotDb) store.set_plugin_json( plugin: PLUGIN, key: STATE_KEY, network: network, channel: channel, value: normalize_state(state) ) end
Source
# File lib/autovoice/store.rb, line 59 def set_enabled!(network:, channel:, enabled:, store: RabbotDb) store.set_autovoice(network: network, channel: channel, enabled: enabled) end
Source
# File lib/autovoice/store.rb, line 79 def should_attempt_award?(state, now:, force: false) return true if force activity_at = parse_time(state["last_activity_at"]) return false unless activity_at last_run = parse_time(state["last_run_at"]) if last_run.nil? cutoff = now - AutovoiceCandidates::LOOKBACK_SEC return activity_at >= cutoff end activity_at > last_run end
Source
# File lib/autovoice/store.rb, line 98 def touch_activity!(network:, channel:, at: Time.now, store: RabbotDb) state = load_state(network: network, channel: channel, store: store) updated = normalize_state(state).merge("last_activity_at" => RabbotDb.iso_time(at)) save_state(network: network, channel: channel, state: updated, store: store) updated end