Skip to content

Commit

Permalink
refactor rtm module
Browse files Browse the repository at this point in the history
  • Loading branch information
perzanko committed Sep 4, 2019
1 parent f33ab69 commit 83a335f
Showing 1 changed file with 86 additions and 52 deletions.
138 changes: 86 additions & 52 deletions lib/devton_slack/rtm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,15 @@ defmodule DevtonSlack.Rtm do
{:ok, state}
end

def handle_event(message = %{type: "message"}, slack, state)
when String.contains?(message.text, "Do Not Disturb"), do:
{:ok, state}
def handle_event(message = %{type: "message"}, slack, state) do
try do
command = Cli.handle_command(message.text)
Logger.info("Message: '#{message.text}', Command: '#{inspect(command)}', channel '#{message.channel}'")
case command do
{:subscribe, %{tag: tag, time: time, day: day}} ->
result = Devton.Subscriptions.create_subscription(
%{
"tags" => tag,
"time" => time,
"day" => day,
"user_name" => slack.users[message.user].name,
"user_id" => message.user,
"user_tz" => slack.users[message.user].tz,
"workspace_id" => slack.team.id,
"workspace_name" => slack.team.name,
}
)
case result do
{:error, :invalid_day} ->
send_message(Message.invalid_day, message.channel, slack)
{:error, :invalid_time} ->
send_message(Message.invalid_time, message.channel, slack)
{:error, _} ->
send_message(Message.invalid_command, message.channel, slack)
x ->
indicate_typing(message.channel, slack)
end
{:unsubscribe, %{id: id}} ->
indicate_typing(message.channel, slack)
Devton.Subscriptions.deactivate_subscription(%{"uuid" => id})
{:tags, %{top: top}} ->
indicate_typing(message.channel, slack)
tags = Devton.Library.get_top_tags(String.to_integer(top))
send_message(Message.tags(tags), message.channel, slack)
{:help} ->
indicate_typing(message.channel, slack)
send_message(Message.help, message.channel, slack)
{:status} ->
indicate_typing(message.channel, slack)
subscriptions = Devton.Subscriptions.get_subscriptions(
%{
"user_id" => message.user,
"workspace_id" => slack.team.id,
}
)
send_message(Message.status(subscriptions), message.channel, slack)
{:invalid_command} ->
indicate_typing(message.channel, slack)
send_message(Message.invalid_command, message.channel, slack)
case can_process_command(message.text) do
false ->
{:ok, state}
true ->
execute_command(message, slack)
{:ok, state}
end
{:ok, state}
rescue
_ -> {:ok, state}
end
Expand All @@ -82,4 +36,84 @@ defmodule DevtonSlack.Rtm do
{:ok, state}
end
def handle_info(_, _, state), do: {:ok, state}

defp execute_command(message, slack) do
command = Cli.handle_command(message.text)
Logger.info("Message: '#{message.text}', Command: '#{inspect(command)}', channel '#{message.channel}'")
case command do
{:subscribe, %{tag: tag, time: time, day: day}} ->
run_command(:subscribe, tag, time, day, message, slack)
{:unsubscribe, %{id: id}} ->
run_command(:unsubscribe, id, message, slack)
{:tags, %{top: top}} ->
run_command(:tags, top, message, slack)
{:help} ->
run_command(:help, message, slack)
{:status} ->
run_command(:status, message, slack)
{:invalid_command} ->
run_command(:invalid_command, message, slack)
end
end

defp run_command(:subscribe, tag, time, day, message, slack) do
result = Devton.Subscriptions.create_subscription(
%{
"tags" => tag,
"time" => time,
"day" => day,
"user_name" => slack.users[message.user].name,
"user_id" => message.user,
"user_tz" => slack.users[message.user].tz,
"workspace_id" => slack.team.id,
"workspace_name" => slack.team.name,
}
)
case result do
{:error, :invalid_day} ->
send_message(Message.invalid_day, message.channel, slack)
{:error, :invalid_time} ->
send_message(Message.invalid_time, message.channel, slack)
{:error, _} ->
send_message(Message.invalid_command, message.channel, slack)
x ->
indicate_typing(message.channel, slack)
end
end

defp run_command(:unsubscribe, id, message, slack) do
indicate_typing(message.channel, slack)
Devton.Subscriptions.deactivate_subscription(%{"uuid" => id})
end

defp run_command(:tags, top, message, slack) do
indicate_typing(message.channel, slack)
tags = Devton.Library.get_top_tags(String.to_integer(top))
send_message(Message.tags(tags), message.channel, slack)
end

defp run_command(:help, message, slack) do
indicate_typing(message.channel, slack)
send_message(Message.help, message.channel, slack)
end

defp run_command(:status, message, slack) do
indicate_typing(message.channel, slack)
subscriptions = Devton.Subscriptions.get_subscriptions(
%{
"user_id" => message.user,
"workspace_id" => slack.team.id,
}
)
send_message(Message.status(subscriptions), message.channel, slack)
end

defp run_command(:invalid_command, message, slack) do
indicate_typing(message.channel, slack)
send_message(Message.invalid_command, message.channel, slack)
end

defp can_process_command(text) do
!String.contains?(text, "Do Not Disturb")
end
end

0 comments on commit 83a335f

Please sign in to comment.