Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-exz committed Oct 3, 2023
1 parent 5a8964d commit 2881ca7
Show file tree
Hide file tree
Showing 25 changed files with 612 additions and 526 deletions.
489 changes: 0 additions & 489 deletions bot/commands.rb

This file was deleted.

29 changes: 29 additions & 0 deletions bot/commands/action_create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class ActionCreate
def self.call(client:,data:,match:)
action = Action.new(
problem: match['expression'][/problem:(.*) action:/, 1],
action: match['expression'][/ action:(.*)$/, 1],
channel: data.channel
)

if action.save
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.action.created.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
else
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.action.failed.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
end
19 changes: 19 additions & 0 deletions bot/commands/action_delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class ActionDelete
def self.call(client:,data:,match:)
Action.where(
problem: match['expression'][/problem:(.*)/, 1],
channel: data.channel
).delete_all

client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.action.deleted.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
16 changes: 16 additions & 0 deletions bot/commands/answer_delete_custom_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class AnswerDeleteCustomText
def self.call(client:,data:)
Answer.where(channel_id: data.channel).delete_all

client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.answer.deleted.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
15 changes: 15 additions & 0 deletions bot/commands/answer_disable_hide_reason.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class AnswerDisableHideReason
def self.call(client:,data:)
Answer.where(channel_id: data.channel).update_all(hide_reason: false)
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.disable.hide_reason.text', name: client.self.name),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
23 changes: 23 additions & 0 deletions bot/commands/answer_enable_hide_reason.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class AnswerEnableHideReason
def self.call(client:,data:)
if Answer.where(channel_id: data.channel).update_all(hide_reason: true)
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.enable.hide_reason.text', name: client.self.name),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
else
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.enable.hide_reason.failed.text', name: client.self.name),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
end
37 changes: 37 additions & 0 deletions bot/commands/answer_set_custom_text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class AnswerSetCustomText
def self.call(client:,data:,match:)
custom_text = match['expression']
Answer.where(channel_id: data.channel).delete_all
answer = Answer.new
answer.body = custom_text
answer.channel_id = data.channel

if answer.save

client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.answer.created.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)

client.web_client.chat_postMessage(
channel: data.channel,
text: custom_text,
thread_ts: data.thread_ts || data.ts,
as_user: true
)
else
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.answer.failed.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
end
16 changes: 16 additions & 0 deletions bot/commands/channel_reminder_disabled.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class ChannelReminderDisabled
def self.call(client:,data:)
channel = Channel.where(slack_channel_id: data.channel).first
channel.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
16 changes: 16 additions & 0 deletions bot/commands/channel_reminder_enabled.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class ChannelReminderEnabled
def self.call(client:,data:)
channel = Channel.where(slack_channel_id: data.channel).first
channel.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
16 changes: 16 additions & 0 deletions bot/commands/checked.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class Checked
def self.call(client:,data:)
message_processor = MessageProcessor.new
message_processor.disable_message_from_remind(data: data)
client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.thread.checked'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
67 changes: 67 additions & 0 deletions bot/commands/create_duty_for_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class CreateDutyForUser
def self.call(client:,data:,match:)
message_processor = MessageProcessor.new
slack_web_client = Slack::Web::Client.new

begin
channel_info = slack_web_client.channels_info(channel: data.channel)
rescue
channel_info = Hash.new
channel_info['channel'] = Hash.new
channel_info['channel']['id'] = data.channel
channel_info['channel']['name'] = nil
channel_info['channel']['value'] = nil
end

channel = Channel.where(slack_channel_id: data.channel).first
if channel.blank?
channel = Channel.new
channel.slack_channel_id = channel_info['channel']['id']
channel.name = channel_info['channel']['name']
channel.description = channel_info['channel']['value']
channel.save
end

user_name = match['expression'][/<@(.+)>/, 1]
message_processor.collectUserInfoBySlackUserId(user_name)
user = User.where(slack_user_id: user_name).first

duty = Duty.where(user_id: user_name, channel_id: data.channel).first
if duty.blank?
duty = Duty.new
duty.duty_from = ActiveSupport::TimeZone.new(user.tz).local_to_utc(match['expression'][/from (\d+:\d+) /, 1].to_time)
duty.duty_to = ActiveSupport::TimeZone.new(user.tz).local_to_utc(match['expression'][/.* to (\d+:\d+)$/, 1].to_time)
duty.channel_id = data.channel
duty.user_id = user.id
duty.enabled = true
duty.save
client.say(
channel: data.channel,
text: I18n.t('commands.duty.created.text', fH: duty.duty_from.hour, fM: duty.duty_from.min, tH: duty.duty_to.hour, tM: duty.duty_to.min, status: duty.enabled),
thread_ts: data.thread_ts || data.ts
)
set_user_on_duty(data: data, client: client, slack_user_id: user_name)
else
client.say(
channel: data.channel,
text: I18n.t('commands.duty.exist.text', fH: duty.duty_from.hour, fM: duty.duty_from.min, tH: duty.duty_to.hour, tM: duty.duty_to.min, status: duty.enabled),
thread_ts: data.thread_ts || data.ts
)
end
end

def self.set_user_on_duty(data:, client:, slack_user_id:)
Duty.where(channel_id: data.channel).where(user_id: slack_user_id).update_all(enabled: true)
Duty.where(channel_id: data.channel).where.not(user_id: slack_user_id).update_all(enabled: false)
client.say(
channel: data.channel,
text: I18n.t('commands.duty.enabled-for-user.text', name: slack_user_id),
thread_ts: data.thread_ts || data.ts
)
end

end
end
end
54 changes: 54 additions & 0 deletions bot/commands/duty_create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class DutyCreate
def self.call(client:,data:,match:)
message_processor = MessageProcessor.new
slack_web_client = Slack::Web::Client.new
begin
channel_info = slack_web_client.channels_info(channel: data.channel)
rescue
channel_info = Hash.new
channel_info['channel'] = Hash.new
channel_info['channel']['id'] = data.channel
channel_info['channel']['name'] = nil
channel_info['channel']['value'] = nil
end

channel = Channel.where(slack_channel_id: data.channel).first
if channel.blank?
channel = Channel.new
channel.slack_channel_id = channel_info['channel']['id']
channel.name = channel_info['channel']['name']
channel.description = channel_info['channel']['value']
channel.save
end

message_processor.collectUserInfo(data: data)
user = User.where(slack_user_id: data.user).first

duty = Duty.where(user_id: data.user, channel_id: data.channel).first
if duty.blank?
duty = Duty.new
duty.duty_from = ActiveSupport::TimeZone.new(user.tz).local_to_utc(match['expression'][/from (\d+:\d+) /, 1].to_time)
duty.duty_to = ActiveSupport::TimeZone.new(user.tz).local_to_utc(match['expression'][/.* to (\d+:\d+)$/, 1].to_time)
duty.channel_id = data.channel
duty.user_id = user.id
duty.enabled = true
duty.save
client.say(
channel: data.channel,
text: I18n.t('commands.duty.created.text', fH: duty.duty_from.hour, fM: duty.duty_from.min, tH: duty.duty_to.hour, tM: duty.duty_to.min, status: duty.enabled),
thread_ts: data.thread_ts || data.ts
)
i_am_on_duty(data: data, client: client)
else
client.say(
channel: data.channel,
text: I18n.t('commands.duty.exist.text', fH: duty.duty_from.hour, fM: duty.duty_from.min, tH: duty.duty_to.hour, tM: duty.duty_to.min, status: duty.enabled),
thread_ts: data.thread_ts || data.ts
)
end
end
end
end
end
24 changes: 24 additions & 0 deletions bot/commands/duty_delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class DutyDelete
def self.call(client:,data:)
duty = Duty.where(user_id: data.user, channel_id: data.channel).first

if duty.blank?
client.say(
channel: data.channel,
text: I18n.t('commands.duty.exist.error'),
thread_ts: data.thread_ts || data.ts
)
else
duty.delete
client.say(
channel: data.channel,
text: I18n.t('commands.duty.deleted.text'),
thread_ts: data.thread_ts || data.ts
)
end
end
end
end
end
17 changes: 17 additions & 0 deletions bot/commands/duty_set_opsgenie_escalation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class DutySetOpsgenieEscalation
def self.call(client:,data:,match:)
opsgenie_escalation_name = match['expression']
Duty.where(channel_id: data.channel).update_all(opsgenie_escalation_name: opsgenie_escalation_name)

client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.opsgenie-escalation-name.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
17 changes: 17 additions & 0 deletions bot/commands/duty_sync_with_opsgenie_schedule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module WhoIsOnDutyTodaySlackBotModule
module Commands
class DutySyncWithOpsgenieSchedule
def self.call(client:,data:,match:)
opsgenie_schedule_name = match['expression']
Duty.where(channel_id: data.channel).update_all(opsgenie_schedule_name: opsgenie_schedule_name)

client.web_client.chat_postMessage(
channel: data.channel,
text: I18n.t('commands.opsgenie-schedule-name.text'),
thread_ts: data.thread_ts || data.ts,
as_user: true
)
end
end
end
end
Loading

0 comments on commit 2881ca7

Please sign in to comment.