Skip to content

Commit

Permalink
Fix credo warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
ghenry committed Aug 24, 2023
1 parent 85e3081 commit 7024372
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 46 deletions.
4 changes: 1 addition & 3 deletions lib/sentrypeer/alerts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ defmodule Sentrypeer.Alerts do

integrations ->
Enum.each(integrations, fn %Integration{} = integration ->
Task.Supervisor.start_child(Sentrypeer.TaskSupervisor, fn ->
send_alert_based_on_type(integration, number_or_ip_address)
end)
send_alert_based_on_type(integration, number_or_ip_address)
end)
end
end
Expand Down
26 changes: 15 additions & 11 deletions lib/sentrypeer/alerts/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,26 @@ defmodule Sentrypeer.Alerts.Email do
true ->
Logger.debug("Sending email alert to #{integration.destination}")

new()
|> to(integration.destination)
|> from({"SentryPeer Alerts", "[email protected]"})
|> subject(integration.subject)
|> html_body(
integration.message <> "<br><br><b>#{number_or_ip_address}</b> has been seen."
)
|> text_body(
integration.message <> "<br><br><b>#{number_or_ip_address}</b> has been seen."
)
|> Mailer.deliver()
send_email(integration, number_or_ip_address)

false ->
Logger.debug("Email integration disabled.")

{:ok, "Integration disabled"}
end
end

defp send_email(integration, number_or_ip_address) do
Task.Supervisor.start_child(Sentrypeer.TaskSupervisor, fn ->
new()
|> to(integration.destination)
|> from({"SentryPeer Alerts", "[email protected]"})
|> subject(integration.subject)
|> html_body(integration.message <> "<br><br><b>#{number_or_ip_address}</b> has been seen.")
|> text_body(integration.message <> "<br><br><b>#{number_or_ip_address}</b> has been seen.")
|> Mailer.deliver()

Logger.debug("Email alert sent.")
end)
end
end
38 changes: 22 additions & 16 deletions lib/sentrypeer/alerts/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,7 @@ defmodule Sentrypeer.Alerts.Slack do
true ->
Logger.debug("Sending Slack alert to #{integration.destination}")

case HTTPoison.post(
integration.destination,
to_json(integration, number_or_ip_address),
Alerts.post_headers(),
Alerts.post_options()
) do
{:ok, res} ->
Logger.debug("Slack alert sent: #{inspect(res)}")

{:ok, "Slack alert sent."}

{:error, error} ->
Logger.debug("Slack alert not sent: #{inspect(error)}")

{:error, "Slack alert not sent."}
end
send_to_slack(integration, number_or_ip_address)

false ->
Logger.debug("Slack integration disabled.")
Expand All @@ -50,6 +35,27 @@ defmodule Sentrypeer.Alerts.Slack do
end
end

defp send_to_slack(integration, number_or_ip_address) do
Task.Supervisor.start_child(Sentrypeer.TaskSupervisor, fn ->
case HTTPoison.post(
integration.destination,
to_json(integration, number_or_ip_address),
Alerts.post_headers(),
Alerts.post_options()
) do
{:ok, res} ->
Logger.debug("Slack alert sent: #{inspect(res)}")

{:ok, "Slack alert sent."}

{:error, error} ->
Logger.debug("Slack alert not sent: #{inspect(error)}")

{:error, "Slack alert not sent."}
end
end)
end

defp to_json(integration, number_or_ip_address) do
%{
blocks: [
Expand Down
38 changes: 22 additions & 16 deletions lib/sentrypeer/alerts/webhook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,7 @@ defmodule Sentrypeer.Alerts.Webhook do
true ->
Logger.debug("Sending Webhook alert to #{integration.destination}")

case HTTPoison.post(
integration.destination,
to_json(integration, number_or_ip_address),
Alerts.post_headers(),
Alerts.post_options()
) do
{:ok, res} ->
Logger.debug("Webhook alert sent: #{inspect(res)}")

{:ok, "Webhook alert sent."}

{:error, error} ->
Logger.debug("Webhook alert not sent: #{inspect(error)}")

{:error, "Webhook alert not sent."}
end
post_webhook(integration, number_or_ip_address)

false ->
Logger.debug("Webhook integration disabled.")
Expand All @@ -59,4 +44,25 @@ defmodule Sentrypeer.Alerts.Webhook do
}
|> Poison.encode!()
end

defp post_webhook(integration, number_or_ip_address) do
Task.Supervisor.start_child(Sentrypeer.TaskSupervisor, fn ->
case HTTPoison.post(
integration.destination,
to_json(integration, number_or_ip_address),
Alerts.post_headers(),
Alerts.post_options()
) do
{:ok, res} ->
Logger.debug("Webhook alert sent: #{inspect(res)}")

{:ok, "Webhook alert sent."}

{:error, error} ->
Logger.debug("Webhook alert not sent: #{inspect(error)}")

{:error, "Webhook alert not sent."}
end
end)
end
end

0 comments on commit 7024372

Please sign in to comment.