From f10eeb426a8d1903c8c91f61ead84023ff1f2a8f Mon Sep 17 00:00:00 2001 From: mr-exz Date: Mon, 23 Dec 2024 23:21:11 +0300 Subject: [PATCH] code refactoring --- bot/commands/channel_auto_answer.rb | 31 +++++++++++++++++++ bot/commands/channel_auto_answer_disable.rb | 19 ------------ bot/commands/channel_auto_answer_enable.rb | 19 ------------ bot/commands/channel_reminder.rb | 27 ++++++++++++++++ bot/commands/channel_reminder_disabled.rb | 19 ------------ bot/commands/channel_reminder_enabled.rb | 19 ------------ .../channel_tag_reporter_in_thread.rb | 28 +++++++++++++++++ .../channel_tag_reporter_in_thread_disable.rb | 19 ------------ .../channel_tag_reporter_in_thread_enable.rb | 19 ------------ bot/commands/help.rb | 9 ++---- bot/commands/main.rb | 9 ++---- bot/whoisondutytodayslackbot.rb | 24 ++++---------- 12 files changed, 98 insertions(+), 144 deletions(-) create mode 100644 bot/commands/channel_auto_answer.rb delete mode 100644 bot/commands/channel_auto_answer_disable.rb delete mode 100644 bot/commands/channel_auto_answer_enable.rb create mode 100644 bot/commands/channel_reminder.rb delete mode 100644 bot/commands/channel_reminder_disabled.rb delete mode 100644 bot/commands/channel_reminder_enabled.rb create mode 100644 bot/commands/channel_tag_reporter_in_thread.rb delete mode 100644 bot/commands/channel_tag_reporter_in_thread_disable.rb delete mode 100644 bot/commands/channel_tag_reporter_in_thread_enable.rb diff --git a/bot/commands/channel_auto_answer.rb b/bot/commands/channel_auto_answer.rb new file mode 100644 index 0000000..86fd384 --- /dev/null +++ b/bot/commands/channel_auto_answer.rb @@ -0,0 +1,31 @@ +module WhoIsOnDutyTodaySlackBotModule + module Commands + class ChannelAutoAnswer < SlackRubyBot::Commands::Base + DESCRIPTION = 'Bot will answer on any message in channel at working time'.freeze + EXAMPLE = 'channel auto_answer_enabled= example `channel auto_answer_enabled=true`'.freeze + + def self.call(client:, data:, match:) + value = match['expression'].split('=').last.strip + + unless %w[true false].include?(value) + client.say( + channel: data.channel, + text: "Invalid value for auto-answer. Please use 'true' or 'false'.", + thread_ts: data.thread_ts || data.ts + ) + return + end + + channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) + channel.update(auto_answer_enabled: value) + channel.save + + client.say( + channel: data.channel, + text: "Channel auto-answer has been set to '#{value}'.", + thread_ts: data.thread_ts || data.ts + ) + end + end + end +end \ No newline at end of file diff --git a/bot/commands/channel_auto_answer_disable.rb b/bot/commands/channel_auto_answer_disable.rb deleted file mode 100644 index 0aa2fc2..0000000 --- a/bot/commands/channel_auto_answer_disable.rb +++ /dev/null @@ -1,19 +0,0 @@ -module WhoIsOnDutyTodaySlackBotModule - module Commands - class ChannelAutoAnswerDisable < SlackRubyBot::Commands::Base - - DESCRIPTION = 'Bot stop answer on any message in channel at working time'.freeze - EXAMPLE = '`channel auto answer disable`'.freeze - def self.call(client:, data:) - channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) - channel.update(auto_answer_enabled: false) - channel.save - client.say( - channel: data.channel, - text: 'Auto answer has been enabled for this disabled.', - thread_ts: data.thread_ts || data.ts - ) - end - end - end -end \ No newline at end of file diff --git a/bot/commands/channel_auto_answer_enable.rb b/bot/commands/channel_auto_answer_enable.rb deleted file mode 100644 index 0c02f90..0000000 --- a/bot/commands/channel_auto_answer_enable.rb +++ /dev/null @@ -1,19 +0,0 @@ -module WhoIsOnDutyTodaySlackBotModule - module Commands - class ChannelAutoAnswerEnable < SlackRubyBot::Commands::Base - - DESCRIPTION = 'Bot will answer on any message in channel at working time'.freeze - EXAMPLE = '`channel auto answer enable`'.freeze - def self.call(client:, data:) - channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) - channel.update(auto_answer_enabled: true) - channel.save - client.say( - channel: data.channel, - text: 'Auto answer has been enabled for this channel.', - thread_ts: data.thread_ts || data.ts - ) - end - end - end -end \ No newline at end of file diff --git a/bot/commands/channel_reminder.rb b/bot/commands/channel_reminder.rb new file mode 100644 index 0000000..f1a9706 --- /dev/null +++ b/bot/commands/channel_reminder.rb @@ -0,0 +1,27 @@ +module WhoIsOnDutyTodaySlackBotModule + module Commands + class ChannelReminder < SlackRubyBot::Commands::Base + DESCRIPTION = 'Enable or disable channel reminders.'.freeze + EXAMPLE = 'example `channel reminder=true` or `channel reminder=false`'.freeze + + def self.call(client:, data:, match:) + value = match['expression'].split('=').last.strip + + unless %w[true false].include?(value) + client.say(channel: data.channel, text: "Invalid value for reminder. Please use 'true' or 'false'.") + return + end + + channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) + channel.update(reminder_enabled: value) + channel.save + + client.say( + channel: data.channel, + text: "Channel reminder has been set to '#{value}'.", + thread_ts: data.thread_ts || data.ts + ) + end + end + end +end diff --git a/bot/commands/channel_reminder_disabled.rb b/bot/commands/channel_reminder_disabled.rb deleted file mode 100644 index d84a27b..0000000 --- a/bot/commands/channel_reminder_disabled.rb +++ /dev/null @@ -1,19 +0,0 @@ -module WhoIsOnDutyTodaySlackBotModule - module Commands - class ChannelReminderDisabled - - DESCRIPTION = 'Will disable reminders for unanswered messages in the channel.'.freeze - EXAMPLE = '`channel reminder disabled`'.freeze - def self.call(client:, data:) - channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) - channel.update(reminder_enabled: false) - channel.save - client.say( - channel: data.channel, - text: I18n.t('commands.channel.reminder.disabled.text'), - thread_ts: data.thread_ts || data.ts - ) - end - end - end -end diff --git a/bot/commands/channel_reminder_enabled.rb b/bot/commands/channel_reminder_enabled.rb deleted file mode 100644 index ac623f4..0000000 --- a/bot/commands/channel_reminder_enabled.rb +++ /dev/null @@ -1,19 +0,0 @@ -module WhoIsOnDutyTodaySlackBotModule - module Commands - class ChannelReminderEnabled - - DESCRIPTION = 'Will enable reminders for unanswered messages in the channel. Bot will send links to threads without responses each 15 min to duty person in direct message'.freeze - EXAMPLE = '`channel reminder enabled`'.freeze - def self.call(client:, data:) - channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) - channel.update(reminder_enabled: true) - channel.save - client.say( - channel: data.channel, - text: I18n.t('commands.channel.reminder.enabled.text'), - thread_ts: data.thread_ts || data.ts - ) - end - end - end -end diff --git a/bot/commands/channel_tag_reporter_in_thread.rb b/bot/commands/channel_tag_reporter_in_thread.rb new file mode 100644 index 0000000..aff6396 --- /dev/null +++ b/bot/commands/channel_tag_reporter_in_thread.rb @@ -0,0 +1,28 @@ +module WhoIsOnDutyTodaySlackBotModule + module Commands + class ChannelTagReporterInThread < SlackRubyBot::Commands::Base + + DESCRIPTION = 'Enable or disable tagging the reporter in the thread.'.freeze + EXAMPLE = 'example `channel tag_reporter_enabled=true` or `channel tag_reporter_enabled=false`'.freeze + + def self.call(client:, data:, match:) + value = match['expression'].split('=').last.strip + + unless %w[true false].include?(value) + client.say(channel: data.channel, text: "Invalid value for tagging the reporter. Please use 'true' or 'false'.") + return + end + + channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) + channel.update(tag_reporter_enabled: value) + channel.save + + client.say( + channel: data.channel, + text: "Tagging the reporter in the thread has been set to '#{value}'.", + thread_ts: data.thread_ts || data.ts + ) + end + end + end +end \ No newline at end of file diff --git a/bot/commands/channel_tag_reporter_in_thread_disable.rb b/bot/commands/channel_tag_reporter_in_thread_disable.rb deleted file mode 100644 index 2c79b94..0000000 --- a/bot/commands/channel_tag_reporter_in_thread_disable.rb +++ /dev/null @@ -1,19 +0,0 @@ -module WhoIsOnDutyTodaySlackBotModule - module Commands - class ChannelTagReporterInThreadDisable < SlackRubyBot::Commands::Base - - DESCRIPTION = 'Bot will not tag the reporter in the thread.'.freeze - EXAMPLE = '`channel tag reporter in thread disable`'.freeze - def self.call(client:, data:) - channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) - channel.update(tag_reporter_enabled: false) - channel.save - client.say( - channel: data.channel, - text: 'Tagging the reporter in the thread has been disabled.', - thread_ts: data.thread_ts || data.ts - ) - end - end - end -end \ No newline at end of file diff --git a/bot/commands/channel_tag_reporter_in_thread_enable.rb b/bot/commands/channel_tag_reporter_in_thread_enable.rb deleted file mode 100644 index 50a9e34..0000000 --- a/bot/commands/channel_tag_reporter_in_thread_enable.rb +++ /dev/null @@ -1,19 +0,0 @@ -module WhoIsOnDutyTodaySlackBotModule - module Commands - class ChannelTagReporterInThreadEnable < SlackRubyBot::Commands::Base - - DESCRIPTION = 'Bot will tag the reporter in the thread.'.freeze - EXAMPLE = '`channel tag reporter in thread enable`'.freeze - def self.call(client:, data:) - channel = Channel.find_or_initialize_by(slack_channel_id: data.channel) - channel.update(tag_reporter_enabled: true) - channel.save - client.say( - channel: data.channel, - text: 'Tagging the reporter in the thread has been enabled.', - thread_ts: data.thread_ts || data.ts - ) - end - end - end -end \ No newline at end of file diff --git a/bot/commands/help.rb b/bot/commands/help.rb index be69306..b253aba 100644 --- a/bot/commands/help.rb +++ b/bot/commands/help.rb @@ -27,12 +27,9 @@ def self.generate_help_text Checked, DutyCreate, CreateDutyForUser, - ChannelReminderEnabled, - ChannelReminderDisabled, - ChannelAutoAnswerEnable, - ChannelAutoAnswerDisable, - ChannelTagReporterInThreadEnable, - ChannelTagReporterInThreadDisable, + ChannelReminder, + ChannelAutoAnswer, + ChannelTagReporterInThread, ChannelLabelsStatistic, ChannelLabelsList, ChannelLabelsMerge, diff --git a/bot/commands/main.rb b/bot/commands/main.rb index bf2ef1e..b9390e7 100644 --- a/bot/commands/main.rb +++ b/bot/commands/main.rb @@ -7,8 +7,6 @@ require_relative 'answer_enable_hide_reason' require_relative 'answer_set_custom_text' require_relative 'call_duty_person' -require_relative 'channel_reminder_disabled' -require_relative 'channel_reminder_enabled' require_relative 'checked' require_relative 'create_duty_for_user' require_relative 'duty_create' @@ -25,13 +23,12 @@ require_relative 'channel_labels_statistic' require_relative 'channel_labels_list' require_relative 'channel_labels_merge' +require_relative 'channel_reminder' +require_relative 'channel_tag_reporter_in_thread' +require_relative 'channel_auto_answer' require_relative 'thread_labels_clean' require_relative 'thread_labels' require_relative 'git_commits' -require_relative 'channel_tag_reporter_in_thread_enable' -require_relative 'channel_tag_reporter_in_thread_disable' -require_relative 'channel_auto_answer_enable' -require_relative 'channel_auto_answer_disable' module WhoIsOnDutyTodaySlackBotModule module Commands diff --git a/bot/whoisondutytodayslackbot.rb b/bot/whoisondutytodayslackbot.rb index 11b01b0..f83807c 100644 --- a/bot/whoisondutytodayslackbot.rb +++ b/bot/whoisondutytodayslackbot.rb @@ -44,28 +44,16 @@ class WhoIsOnDutyTodaySlackBot < SlackRubyBot::Bot WhoIsOnDutyTodaySlackBotModule::Commands::CreateDutyForUser.call(client: client, data: data, match: match) end - command 'channel reminder enabled' do |client, data| - WhoIsOnDutyTodaySlackBotModule::Commands::ChannelReminderEnabled.call(client: client, data: data) + command '^channel reminder_enabled=.*$' do |client, data, match| + WhoIsOnDutyTodaySlackBotModule::Commands::ChannelReminder.call(client: client, data: data, match: match) end - command 'channel reminder disabled' do |client, data| - WhoIsOnDutyTodaySlackBotModule::Commands::ChannelReminderDisabled.call(client: client, data: data) + command '/^channel auto_answer=.*$/' do |client, data, match| + WhoIsOnDutyTodaySlackBotModule::Commands::ChannelAutoAnswer.call(client: client, data: data, match: match) end - command 'channel auto answer enable' do |client, data| - WhoIsOnDutyTodaySlackBotModule::Commands::ChannelAutoAnswerEnable.call(client: client, data: data) - end - - command 'channel auto answer disable' do |client, data| - WhoIsOnDutyTodaySlackBotModule::Commands::ChannelAutoAnswerDisable.call(client: client, data: data) - end - - command 'channel tag reporter in thread enable' do |client, data| - WhoIsOnDutyTodaySlackBotModule::Commands::ChannelTagReporterInThreadEnable.call(client: client, data: data) - end - - command 'channel tag reporter in thread disable' do |client, data| - WhoIsOnDutyTodaySlackBotModule::Commands::ChannelTagReporterInThreadDisable.call(client: client, data: data) + command '^channel tag_reporter_enabled=.*$/' do |client, data, match| + WhoIsOnDutyTodaySlackBotModule::Commands::ChannelTagReporterInThread.call(client: client, data: data, match: match) end command 'duty update' do |client, data, match|