class Lita::Handlers::Locker

Top-level class for Locker

Public Instance Methods

give(response) click to toggle source
# File lib/lita/handlers/locker.rb, line 134
def give(response)
  name = response.match_data['label'].rstrip

  return response.reply(failed(t('subject.does_not_exist', name: name))) unless Label.exists?(name)
  l = Label.new(name)
  return response.reply(failed(t('give.not_owned', label: name))) unless l.locked?
  owner_mention = render_template('mention', name: l.owner.mention_name, id: l.owner.id)
  unless l.owner == response.user
    return response.reply(t('give.not_owner',
                            label: name,
                            owner: l.owner.name,
                            mention: owner_mention))
  end
  recipient_name = response.match_data['username'].rstrip
  recipient = Lita::User.fuzzy_find(recipient_name)
  return response.reply(t('user.unknown', user: recipient_name)) unless recipient

  response.reply(attempt_give(name, response.user, recipient))
end
lock(response) click to toggle source
# File lib/lita/handlers/locker.rb, line 74
def lock(response)
  name = response.match_data['label'].rstrip

  return response.reply(failed(t('label.does_not_exist', name: name))) unless Label.exists?(name)
  l = Label.new(name)
  return response.reply(failed(t('label.no_resources', name: name))) unless l.membership.count > 0
  return response.reply(t('label.self_lock', name: name, user: response.user.name)) if l.owner == response.user
  return response.reply(success(t('label.lock', name: name))) if l.lock!(response.user.id)

  response.reply(label_ownership(name))
end
observe(response) click to toggle source
# File lib/lita/handlers/locker.rb, line 104
def observe(response)
  name = response.match_data['label']
  user = response.user
  return response.reply(failed(t('label.does_not_exist', name: name))) unless Label.exists?(name)
  l = Label.new(name)
  return response.reply(t('observe.already_observing', name: name, user: user.name)) if l.observer?(user.id)
  l.add_observer!(user.id)
  response.reply(t('observe.now_observing', name: name, user: user.name))
end
setup_redis(_payload) click to toggle source
# File lib/lita/handlers/locker.rb, line 69
def setup_redis(_payload)
  Label.redis = redis
  Resource.redis = redis
end
steal(response) click to toggle source
# File lib/lita/handlers/locker.rb, line 124
def steal(response)
  name = response.match_data['label'].rstrip

  return response.reply(failed(t('subject.does_not_exist', name: name))) unless Label.exists?(name)
  l = Label.new(name)
  return response.reply(t('steal.already_unlocked', label: name)) unless l.locked?

  response.reply(attempt_steal(name, response.user))
end
unlock(response) click to toggle source
# File lib/lita/handlers/locker.rb, line 86
def unlock(response)
  name = response.match_data['label'].rstrip

  return response.reply(failed(t('subject.does_not_exist', name: name))) unless Label.exists?(name)
  l = Label.new(name)
  return response.reply(success(t('label.is_unlocked', name: name))) unless l.locked?

  response.reply(attempt_unlock(name, response.user))

  return if l.locked?
  mention_names = l.observers
                   .map { |observer| render_template('mention', name: observer.mention_name, id: observer.id) }
                   .reject { |mention| mention == '' }
                   .sort
                   .join(' ')
  response.reply(t('label.unlocked_no_queue', name: name, mention: mention_names)) unless mention_names.empty?
end
unobserve(response) click to toggle source
# File lib/lita/handlers/locker.rb, line 114
def unobserve(response)
  name = response.match_data['label']
  user = response.user
  return response.reply(failed(t('label.does_not_exist', name: name))) unless Label.exists?(name)
  l = Label.new(name)
  return response.reply(t('observe.were_not_observing', name: name, user: user.name)) unless l.observer?(user.id)
  l.remove_observer!(user.id)
  response.reply(t('observe.stopped_observing', name: name, user: user.name))
end

Private Instance Methods

attempt_give(name, giver, recipient) click to toggle source
# File lib/lita/handlers/locker.rb, line 156
def attempt_give(name, giver, recipient)
  label = Label.new(name)
  return t('give.self', user: giver.name) if recipient == giver
  old_owner = label.owner
  label.give!(recipient.id)
  mention = render_template('mention', name: recipient.mention_name, id: recipient.id)
  success(t('give.given', label: name, giver: old_owner.name, recipient: recipient.name, mention: mention))
end
attempt_steal(name, user) click to toggle source
# File lib/lita/handlers/locker.rb, line 165
def attempt_steal(name, user)
  label = Label.new(name)
  return t('steal.self', user: user.name) if label.owner == user
  old_owner = label.owner
  label.steal!(user.id)
  mention = render_template('mention', name: old_owner.mention_name, id: old_owner.id)
  success(t('steal.stolen', label: name, thief: user.name, victim: old_owner.name, mention: mention))
end
attempt_unlock(name, user) click to toggle source
# File lib/lita/handlers/locker.rb, line 174
def attempt_unlock(name, user)
  label = Label.new(name)
  if label.owner == user
    label.unlock!
    if label.locked?
      mention = render_template('mention', name: label.owner.mention_name, id: label.owner.id)
      failed(t('label.now_locked_by', name: name, owner: label.owner.name, mention: mention))
    else
      success(t('label.unlock', name: name))
    end
  else
    mention = render_template('mention', name: label.owner.mention_name, id: label.owner.id)
    failed(t('label.owned_unlock', name: name, owner_name: label.owner.name, mention: mention, time: label.held_for))
  end
end