refactor(credo): Refactor to appease new credo checks (complexity and logging)

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2023-04-19 18:33:06 +02:00
parent 8141bb0acb
commit eda2761032
13 changed files with 169 additions and 132 deletions

View File

@@ -283,14 +283,10 @@ defmodule Mobilizon.Service.ActorSuspension do
{:ok, actor}
{:error, error} ->
Logger.error("Error while removing an upload file",
error: inspect(error),
actor: Actor.preferred_username_and_domain(actor),
file_url: url
Logger.error(
"Error while removing an upload file: #{inspect(error)}, actor: #{Actor.preferred_username_and_domain(actor)}, file_url: #{url}"
)
Logger.debug(inspect(error))
{:ok, actor}
end
end

View File

@@ -185,9 +185,8 @@ defmodule Mobilizon.Service.Auth.Applications do
end
def generate_access_token_for_device_flow(client_id, device_code) do
Logger.debug("Generating access token for application device with",
client_id: client_id,
device_code: device_code
Logger.debug(
"Generating access token for application device with client_id=#{client_id}, device_code=#{device_code}"
)
case Applications.get_application_device_activation_by_device_code(client_id, device_code) do

View File

@@ -27,7 +27,7 @@ defmodule Mobilizon.Service.Export.Participants.CSV do
@spec export(Event.t(), Keyword.t()) ::
{:ok, String.t()} | {:error, :failed_to_save_upload | :export_dependency_not_installed}
def export(%Event{id: event_id} = event, options \\ []) do
def export(%Event{} = event, options \\ []) do
if ready?() do
filename = "#{ShortUUID.encode!(Ecto.UUID.generate())}.csv"
full_path = Path.join([export_path(@extension), filename])
@@ -36,18 +36,7 @@ defmodule Mobilizon.Service.Export.Participants.CSV do
case Repo.transaction(
fn ->
event_id
|> Events.participant_for_event_export_query(Keyword.get(options, :roles, []))
|> Repo.stream()
|> Stream.map(&to_list/1)
|> NimbleCSV.RFC4180.dump_to_iodata()
|> add_header_column()
|> Stream.each(fn line -> IO.write(file, line) end)
|> Stream.run()
with {:error, err} <- save_csv_upload(full_path, filename, event) do
Repo.rollback(err)
end
produce_file(event, filename, full_path, file, options)
end,
timeout: :infinity
) do
@@ -63,6 +52,23 @@ defmodule Mobilizon.Service.Export.Participants.CSV do
end
end
@spec produce_file(Event.t(), String.t(), String.t(), File.io_device(), Keyword.t()) ::
no_return()
defp produce_file(%Event{id: event_id} = event, filename, full_path, file, options) do
event_id
|> Events.participant_for_event_export_query(Keyword.get(options, :roles, []))
|> Repo.stream()
|> Stream.map(&to_list/1)
|> NimbleCSV.RFC4180.dump_to_iodata()
|> add_header_column()
|> Stream.each(fn line -> IO.write(file, line) end)
|> Stream.run()
with {:error, err} <- save_csv_upload(full_path, filename, event) do
Repo.rollback(err)
end
end
defp add_header_column(stream) do
Stream.concat([Enum.join(columns(), ","), "\n"], stream)
end

View File

@@ -95,9 +95,8 @@ defmodule Mobilizon.Service.Notifier.Email do
end
defp can_send_activity?(activity, user, options) do
Logger.warn("Can't check if user #{inspect(user)} can be sent an activity",
activity: inspect(activity),
options: inspect(options)
Logger.warn(
"Can't check if user #{inspect(user)} can be sent an activity (#{inspect(activity)}) (#{inspect(options)})"
)
false

View File

@@ -48,9 +48,7 @@ defmodule Mobilizon.Service.Workers.Helper do
queue_atom = String.to_existing_atom(unquote(queue))
worker_args = worker_args ++ Helper.worker_args(queue_atom)
unquote(caller_module)
|> apply(:new, [params, worker_args])
|> Oban.insert()
Oban.insert(unquote(caller_module).new(params, worker_args))
end
end
end