lib/lita/handlers/locker_labels.rb in lita-locker-1.0.10 vs lib/lita/handlers/locker_labels.rb in lita-locker-1.1.0

- old
+ new

@@ -1,5 +1,9 @@ +# frozen_string_literal: true + +require 'locker/list' + module Lita module Handlers # Label-related handlers class LockerLabels < Handler namespace 'Locker' @@ -8,68 +12,81 @@ include ::Locker::Misc include ::Locker::Regex include ::Locker::Resource route( - /^locker\slabel\slist$/, + /^locker\slabel\slist/, :list, command: true, + kwargs: { page: { default: 1 } }, help: { t('help.label.list.syntax') => t('help.label.list.desc') } ) route( - /^locker\slabel\screate\s#{LABELS_REGEX}$/, + /^locker\slabel\screate\s#{LABELS_REGEX}#{COMMENT_REGEX}$/, :create, command: true, help: { t('help.label.create.syntax') => t('help.label.create.desc') } ) route( - /^locker\slabel\sdelete\s#{LABELS_REGEX}$/, + /^locker\slabel\sdelete\s#{LABELS_REGEX}#{COMMENT_REGEX}$/, :delete, command: true, help: { t('help.label.delete.syntax') => t('help.label.delete.desc') } ) route( - /^locker\slabel\sshow\s#{LABEL_REGEX}$/, + /^locker\slabel\sshow\s#{LABEL_REGEX}#{COMMENT_REGEX}$/, :show, command: true, help: { t('help.label.show.syntax') => t('help.label.show.desc') } ) route( - /^locker\slabel\sadd\s#{RESOURCES_REGEX}\sto\s#{LABEL_REGEX}$/, + /^locker\slabel\sadd\s#{RESOURCES_REGEX}\sto\s#{LABEL_REGEX}#{COMMENT_REGEX}$/, :add, command: true, help: { t('help.label.add.syntax') => t('help.label.add.desc') } ) route( - /^locker\slabel\sremove\s#{RESOURCES_REGEX}\sfrom\s#{LABEL_REGEX}$/, + /^locker\slabel\sremove\s#{RESOURCES_REGEX}\sfrom\s#{LABEL_REGEX}#{COMMENT_REGEX}$/, :remove, command: true, help: { t('help.label.remove.syntax') => t('help.label.remove.desc') } ) def list(response) - after 0 do - should_rate_limit = false + begin + list = ::Locker::List.new(Label, config.per_page, response.extensions[:kwargs][:page]) + rescue ArgumentError + return response.reply(t('list.invalid_page_type')) + end - Label.list.each_slice(5) do |slice| - if should_rate_limit - sleep 3 - else - should_rate_limit = true - end + return response.reply(t('list.page_outside_range', pages: list.pages)) unless list.valid_page? - slice.each do |n| - l = Label.new(n) - response.reply(unlocked(t('label.desc', name: n, state: l.state.value))) - end + message = list.requested_page.map do |key| + label = Label.new(key) + + state = label.state.value.to_s + + case state + when 'unlocked' + unlocked(t('label.desc', name: key, state: state)) + when 'locked' + locked(t('label.desc', name: key, state: state)) + else + # This case shouldn't happen, but it will if a label + # gets saved with some other value for `state`. + t('label.desc', name: key, state: state) end - end + end.join("\n") + + message += "\n#{t('list.paginate', page: list.page, pages: list.pages)}" if list.multiple_pages? + + response.reply(message) end def create(response) names = response.match_data['labels'].split(/,\s*/) results = [] @@ -102,10 +119,10 @@ def show(response) name = response.match_data['label'] return response.reply(failed(t('label.does_not_exist', name: name))) unless Label.exists?(name) l = Label.new(name) - return response.reply(t('label.has_no_resources', name: name)) unless l.membership.count > 0 + return response.reply(t('label.has_no_resources', name: name)) unless l.membership.count.positive? res = [] l.membership.each do |member| res.push(member) end response.reply(t('label.resources', name: name, resources: res.join(', ')))