From 51afec1856cc4cf14e5d82e18cda569c377d4664 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 26 Nov 2021 14:30:46 +0100 Subject: [PATCH] Fix new credo warnings Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/activity_pub.ex | 4 +-- lib/federation/activity_pub/permission.ex | 2 +- lib/federation/activity_pub/transmogrifier.ex | 6 ++-- lib/federation/activity_pub/types/comments.ex | 36 +++++++++---------- lib/federation/web_finger/web_finger.ex | 7 +--- lib/federation/web_finger/xml_builder.ex | 8 ++--- lib/graphql/api/utils.ex | 6 +++- lib/graphql/resolvers/media.ex | 2 +- lib/graphql/resolvers/user.ex | 8 ++--- lib/mix/tasks/mobilizon/instance.ex | 2 +- lib/mix/tasks/mobilizon/users/show.ex | 2 +- lib/service/address/address.ex | 3 +- lib/service/clean_orphan_media.ex | 3 +- lib/service/export/participants/csv.ex | 6 +++- lib/service/export/participants/ods.ex | 6 +++- lib/service/formatter/formatter.ex | 2 ++ lib/service/workers/build_search.ex | 4 +-- .../workers/legacy_notifier_builder.ex | 6 +++- lib/web/proxy/reverse_proxy.ex | 34 ++++++++++-------- test/service/export/icalendar_test.exs | 2 +- 20 files changed, 77 insertions(+), 72 deletions(-) diff --git a/lib/federation/activity_pub/activity_pub.ex b/lib/federation/activity_pub/activity_pub.ex index 7dd39c604..a2852aa44 100644 --- a/lib/federation/activity_pub/activity_pub.ex +++ b/lib/federation/activity_pub/activity_pub.ex @@ -155,7 +155,7 @@ defmodule Mobilizon.Federation.ActivityPub do end # Create an activity from an event - @spec event_to_activity(%Event{}, boolean()) :: Activity.t() + @spec event_to_activity(Event.t(), boolean()) :: Activity.t() defp event_to_activity(%Event{} = event, local \\ true) do %Activity{ recipients: [@public_ap_adress], @@ -166,7 +166,7 @@ defmodule Mobilizon.Federation.ActivityPub do end # Create an activity from a comment - @spec comment_to_activity(%Comment{}, boolean()) :: Activity.t() + @spec comment_to_activity(Comment.t(), boolean()) :: Activity.t() defp comment_to_activity(%Comment{} = comment, local \\ true) do %Activity{ recipients: [@public_ap_adress], diff --git a/lib/federation/activity_pub/permission.ex b/lib/federation/activity_pub/permission.ex index 309255282..7e78b317c 100644 --- a/lib/federation/activity_pub/permission.ex +++ b/lib/federation/activity_pub/permission.ex @@ -77,7 +77,7 @@ defmodule Mobilizon.Federation.ActivityPub.Permission do @spec can_manage_group_object?( existing_object_permissions(), - %Actor{url: String.t()}, + Actor.t(), object() ) :: boolean() defp can_manage_group_object?(permission, %Actor{url: actor_url} = actor, object) do diff --git a/lib/federation/activity_pub/transmogrifier.ex b/lib/federation/activity_pub/transmogrifier.ex index df8ebf385..0d7f61e8b 100644 --- a/lib/federation/activity_pub/transmogrifier.ex +++ b/lib/federation/activity_pub/transmogrifier.ex @@ -967,10 +967,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do defp do_handle_incoming_reject_invite(invite_object, %Actor{} = actor_rejecting) do with {:invite, {:ok, %Member{role: :invited, actor_id: actor_id} = member}} <- {:invite, get_member(invite_object)}, - {:same_actor, true} <- {:same_actor, actor_rejecting.id == actor_id}, - {:ok, activity, member} <- - Actions.Reject.reject(:invite, member, false) do - {:ok, activity, member} + {:same_actor, true} <- {:same_actor, actor_rejecting.id == actor_id} do + Actions.Reject.reject(:invite, member, false) end end diff --git a/lib/federation/activity_pub/types/comments.ex b/lib/federation/activity_pub/types/comments.ex index fbdd58a81..32ec11905 100644 --- a/lib/federation/activity_pub/types/comments.ex +++ b/lib/federation/activity_pub/types/comments.ex @@ -149,25 +149,23 @@ defmodule Mobilizon.Federation.ActivityPub.Types.Comments do ), tags <- ConverterUtils.fetch_tags(tags), mentions <- Map.get(args, :mentions, []) ++ ConverterUtils.fetch_mentions(mentions), - lang <- Map.get(args, :language, "und"), - args <- - Map.merge(args, %{ - actor_id: Map.get(args, :actor_id), - text: text, - mentions: mentions, - tags: tags, - event: event, - in_reply_to_comment: in_reply_to_comment, - in_reply_to_comment_id: - if(is_nil(in_reply_to_comment), do: nil, else: Map.get(in_reply_to_comment, :id)), - origin_comment_id: - if(is_nil(in_reply_to_comment), - do: nil, - else: Comment.get_thread_id(in_reply_to_comment) - ), - language: if(lang == "und", do: LanguageDetection.detect(:comment, args), else: lang) - }) do - args + lang <- Map.get(args, :language, "und") do + Map.merge(args, %{ + actor_id: Map.get(args, :actor_id), + text: text, + mentions: mentions, + tags: tags, + event: event, + in_reply_to_comment: in_reply_to_comment, + in_reply_to_comment_id: + if(is_nil(in_reply_to_comment), do: nil, else: Map.get(in_reply_to_comment, :id)), + origin_comment_id: + if(is_nil(in_reply_to_comment), + do: nil, + else: Comment.get_thread_id(in_reply_to_comment) + ), + language: if(lang == "und", do: LanguageDetection.detect(:comment, args), else: lang) + }) end end diff --git a/lib/federation/web_finger/web_finger.ex b/lib/federation/web_finger/web_finger.ex index 7a952b436..9f1ae34ec 100644 --- a/lib/federation/web_finger/web_finger.ex +++ b/lib/federation/web_finger/web_finger.ex @@ -189,7 +189,7 @@ defmodule Mobilizon.Federation.WebFinger do {:ok, String.t()} | {:error, :link_not_found} | {:error, any()} defp find_webfinger_endpoint(domain) when is_binary(domain) do with {:ok, %Tesla.Env{status: 200, body: body}} <- - fetch_document("http://#{domain}/.well-known/host-meta"), + HostMetaClient.get("http://#{domain}/.well-known/host-meta"), link_template when is_binary(link_template) <- find_link_from_template(body) do {:ok, link_template} else @@ -258,11 +258,6 @@ defmodule Mobilizon.Federation.WebFinger do {:error, :link_not_found} end - @spec fetch_document(String.t()) :: Tesla.Env.result() - defp fetch_document(endpoint) do - with {:error, err} <- HostMetaClient.get(endpoint), do: {:error, err} - end - @spec address_invalid(String.t()) :: false | {:error, :invalid_address} defp address_invalid(address) do with %URI{host: host, scheme: scheme} <- URI.parse(address), diff --git a/lib/federation/web_finger/xml_builder.ex b/lib/federation/web_finger/xml_builder.ex index a45df0dde..9dd40af1c 100644 --- a/lib/federation/web_finger/xml_builder.ex +++ b/lib/federation/web_finger/xml_builder.ex @@ -39,9 +39,7 @@ defmodule Mobilizon.Federation.WebFinger.XmlBuilder do defp to_xml(content) when is_binary(content), do: to_string(content) defp to_xml(content) when is_list(content) do - content - |> Enum.map(&to_xml/1) - |> Enum.join() + Enum.map_join(content, &to_xml/1) end defp to_xml(%NaiveDateTime{} = time), do: NaiveDateTime.to_iso8601(time) @@ -49,9 +47,7 @@ defmodule Mobilizon.Federation.WebFinger.XmlBuilder do @spec make_open_tag(tag :: atom, attributes :: map()) :: String.t() defp make_open_tag(tag, attributes) do attributes_string = - attributes - |> Enum.map(fn {attribute, value} -> "#{attribute}=\"#{value}\"" end) - |> Enum.join(" ") + Enum.map_join(attributes, " ", fn {attribute, value} -> "#{attribute}=\"#{value}\"" end) [to_string(tag), attributes_string] |> Enum.join(" ") |> String.trim() end diff --git a/lib/graphql/api/utils.ex b/lib/graphql/api/utils.ex index bde6ce86a..8318df97e 100644 --- a/lib/graphql/api/utils.ex +++ b/lib/graphql/api/utils.ex @@ -21,7 +21,7 @@ defmodule Mobilizon.GraphQL.API.Utils do text |> Formatter.html_escape("text/plain") |> Formatter.linkify(options) - |> (fn {text, mentions, tags} -> {String.replace(text, ~r/\r?\n/, "
"), mentions, tags} end).() + |> inject_new_lines() end def format_input(text, "text/html", options) do @@ -30,6 +30,10 @@ defmodule Mobilizon.GraphQL.API.Utils do |> Formatter.linkify(options) end + defp inject_new_lines({text, mentions, tags}) do + {String.replace(text, ~r/\r?\n/, "
"), mentions, tags} + end + @doc """ Use the data-media-id attributes to extract media from body text """ diff --git a/lib/graphql/resolvers/media.ex b/lib/graphql/resolvers/media.ex index 9a7ce5d3f..64f163e43 100644 --- a/lib/graphql/resolvers/media.ex +++ b/lib/graphql/resolvers/media.ex @@ -15,7 +15,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Media do See Mobilizon.Web.Resolvers.Event.create_event/3 """ def media(%{picture_id: media_id} = _parent, _args, _resolution) do - with {:ok, media} <- do_fetch_media(media_id), do: {:ok, media} + do_fetch_media(media_id) end def media(%{picture: media} = _parent, _args, _resolution), do: {:ok, media} diff --git a/lib/graphql/resolvers/user.ex b/lib/graphql/resolvers/user.ex index afbd0909f..7e85a3e6e 100644 --- a/lib/graphql/resolvers/user.ex +++ b/lib/graphql/resolvers/user.ex @@ -559,11 +559,9 @@ defmodule Mobilizon.GraphQL.Resolvers.User do Enum.each(actors, fn actor -> actor_performing = Keyword.get(options, :actor_performing, actor) Actions.Delete.delete(actor, actor_performing, true) - end), - # Delete user - {:ok, user} <- - Users.delete_user(user, reserve_email: Keyword.get(options, :reserve_email, activated)) do - {:ok, user} + end) do + # Delete user + Users.delete_user(user, reserve_email: Keyword.get(options, :reserve_email, activated)) end end diff --git a/lib/mix/tasks/mobilizon/instance.ex b/lib/mix/tasks/mobilizon/instance.ex index 0bbbd0c69..3d3dccc66 100644 --- a/lib/mix/tasks/mobilizon/instance.ex +++ b/lib/mix/tasks/mobilizon/instance.ex @@ -181,7 +181,7 @@ defmodule Mix.Tasks.Mobilizon.Instance do else shell_error( "The task would have overwritten the following files:\n" <> - (will_overwrite |> Enum.map(&"- #{&1}\n") |> Enum.join("")) <> + Enum.map_join(will_overwrite, "", &"- #{&1}\n") <> "Rerun with `-f/--force` to overwrite them." ) end diff --git a/lib/mix/tasks/mobilizon/users/show.ex b/lib/mix/tasks/mobilizon/users/show.ex index c559f87f7..1b54eb96d 100644 --- a/lib/mix/tasks/mobilizon/users/show.ex +++ b/lib/mix/tasks/mobilizon/users/show.ex @@ -47,7 +47,7 @@ defmodule Mix.Tasks.Mobilizon.Users.Show do defp display_actors(actors) do """ Identities (#{length(actors)}): - #{actors |> Enum.map(&display_actor/1) |> Enum.join("")} + #{Enum.map_join(actors, &display_actor/1)} """ end diff --git a/lib/service/address/address.ex b/lib/service/address/address.ex index 59cc8682c..97392712b 100644 --- a/lib/service/address/address.ex +++ b/lib/service/address/address.ex @@ -57,8 +57,7 @@ defmodule Mobilizon.Service.Address do name: if(defined?(postal_code), do: "#{description} (#{postal_code})", else: description), alternative_name: [locality, country] - |> Enum.filter(& &1) - |> Enum.filter(&(&1 != description)) + |> Enum.filter(&(&1 && &1 != description)) |> Enum.join(", ") } end diff --git a/lib/service/clean_orphan_media.ex b/lib/service/clean_orphan_media.ex index 136815eb0..d8ab94dd3 100644 --- a/lib/service/clean_orphan_media.ex +++ b/lib/service/clean_orphan_media.ex @@ -46,10 +46,9 @@ defmodule Mobilizon.Service.CleanOrphanMedia do [from: "posts_medias", param: "media_id"], [from: "comments_medias", param: "media_id"] ] - |> Enum.map(fn [from: from, param: param] -> + |> Enum.map_join(" UNION ", fn [from: from, param: param] -> "SELECT 1 FROM #{from} WHERE #{from}.#{param} = m0.id" end) - |> Enum.join(" UNION ") |> (&"NOT EXISTS(#{&1})").() @spec find_media(Keyword.t()) :: list(list(Media.t())) diff --git a/lib/service/export/participants/csv.ex b/lib/service/export/participants/csv.ex index b33fbb581..90aa737a6 100644 --- a/lib/service/export/participants/csv.ex +++ b/lib/service/export/participants/csv.ex @@ -41,7 +41,7 @@ defmodule Mobilizon.Service.Export.Participants.CSV do |> Repo.stream() |> Stream.map(&to_list/1) |> NimbleCSV.RFC4180.dump_to_iodata() - |> (fn stream -> Stream.concat([Enum.join(columns(), ","), "\n"], stream) end).() + |> add_header_column() |> Stream.each(fn line -> IO.write(file, line) end) |> Stream.run() @@ -63,6 +63,10 @@ defmodule Mobilizon.Service.Export.Participants.CSV do end end + defp add_header_column(stream) do + Stream.concat([Enum.join(columns(), ","), "\n"], stream) + end + @spec save_csv_upload(String.t(), String.t(), Event.t()) :: {:ok, Export.t()} | {:error, atom() | Ecto.Changeset.t()} defp save_csv_upload(full_path, filename, %Event{id: event_id, title: title}) do diff --git a/lib/service/export/participants/ods.ex b/lib/service/export/participants/ods.ex index ef73d25fe..679d418f2 100644 --- a/lib/service/export/participants/ods.ex +++ b/lib/service/export/participants/ods.ex @@ -39,7 +39,7 @@ defmodule Mobilizon.Service.Export.Participants.ODS do |> Events.participant_for_event_export_query(Keyword.get(options, :roles, [])) |> Repo.all() |> Enum.map(&to_list/1) - |> (fn data -> Enum.concat([columns()], data) end).() + |> add_header_columns() |> generate_ods() File.write!(full_path, content) @@ -62,6 +62,10 @@ defmodule Mobilizon.Service.Export.Participants.ODS do end end + defp add_header_columns(data) do + Enum.concat([columns()], data) + end + defp generate_ods(data) do data |> Jason.encode!() diff --git a/lib/service/formatter/formatter.ex b/lib/service/formatter/formatter.ex index e30b1a996..32f515ffa 100644 --- a/lib/service/formatter/formatter.ex +++ b/lib/service/formatter/formatter.ex @@ -15,6 +15,8 @@ defmodule Mobilizon.Service.Formatter do alias Mobilizon.Web.Endpoint + # https://github.com/rrrene/credo/issues/912 + # credo:disable-for-next-line Credo.Check.Readability.MaxLineLength @link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui @markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/ diff --git a/lib/service/workers/build_search.ex b/lib/service/workers/build_search.ex index fd94524e6..26b7fc7b4 100644 --- a/lib/service/workers/build_search.ex +++ b/lib/service/workers/build_search.ex @@ -56,8 +56,6 @@ defmodule Mobilizon.Service.Workers.BuildSearch do end defp get_tags_string(%Event{tags: tags}) do - tags - |> Enum.map(& &1.title) - |> Enum.join(" ") + Enum.map_join(tags, " ", & &1.title) end end diff --git a/lib/service/workers/legacy_notifier_builder.ex b/lib/service/workers/legacy_notifier_builder.ex index 8fe29e3ef..eed20f9ac 100644 --- a/lib/service/workers/legacy_notifier_builder.ex +++ b/lib/service/workers/legacy_notifier_builder.ex @@ -77,12 +77,16 @@ defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do ) do event_uuid |> Events.get_event_by_uuid_with_preload() - |> (fn %Event{organizer_actor: %Actor{id: actor_id}} -> [actor_id] end).() + |> organizer_actor_id() |> users_from_actor_ids(Keyword.fetch!(options, :author_id)) end defp users_to_notify(_, _), do: [] + defp organizer_actor_id(%Event{organizer_actor: %Actor{id: actor_id}}) do + [actor_id] + end + @spec users_from_actor_ids(list(), integer() | String.t()) :: list(Users.t()) defp users_from_actor_ids(actor_ids, author_id) do actor_ids diff --git a/lib/web/proxy/reverse_proxy.ex b/lib/web/proxy/reverse_proxy.ex index 9ac999386..79382f669 100644 --- a/lib/web/proxy/reverse_proxy.ex +++ b/lib/web/proxy/reverse_proxy.ex @@ -294,20 +294,22 @@ defmodule Mobilizon.Web.ReverseProxy do headers |> downcase_headers() |> Enum.filter(fn {k, _} -> k in @keep_req_headers end) - |> (fn headers -> - headers = headers ++ Keyword.get(opts, :req_headers, []) + |> maybe_keep_user_agent(opts) + end - if Keyword.get(opts, :keep_user_agent, false) do - List.keystore( - headers, - "user-agent", - 0, - {"user-agent", Mobilizon.user_agent()} - ) - else - headers - end - end).() + defp maybe_keep_user_agent(headers, opts) do + headers = headers ++ Keyword.get(opts, :req_headers, []) + + if Keyword.get(opts, :keep_user_agent, false) do + List.keystore( + headers, + "user-agent", + 0, + {"user-agent", Mobilizon.user_agent()} + ) + else + headers + end end @spec build_resp_headers(list(tuple()), Keyword.t()) :: list(tuple()) @@ -316,7 +318,11 @@ defmodule Mobilizon.Web.ReverseProxy do |> Enum.filter(fn {k, _} -> k in @keep_resp_headers end) |> build_resp_cache_headers(opts) |> build_resp_content_disposition_header(opts) - |> (fn headers -> headers ++ Keyword.get(opts, :resp_headers, []) end).() + |> maybe_add_headers_from_opts(opts) + end + + defp maybe_add_headers_from_opts(headers, opts) do + headers ++ Keyword.get(opts, :resp_headers, []) end @spec build_resp_cache_headers(list(tuple()), Keyword.t()) :: list(tuple()) diff --git a/test/service/export/icalendar_test.exs b/test/service/export/icalendar_test.exs index 0cdadc93b..2d20e449e 100644 --- a/test/service/export/icalendar_test.exs +++ b/test/service/export/icalendar_test.exs @@ -19,7 +19,7 @@ defmodule Mobilizon.Service.ICalendarTest do VERSION:2.0 PRODID:-//Elixir ICalendar//Mobilizon #{Mobilizon.Config.instance_version()}//EN BEGIN:VEVENT - CATEGORIES:#{event.tags |> Enum.map(& &1.title) |> Enum.join(",")} + CATEGORIES:#{Enum.map_join(event.tags, ",", & &1.title)} DESCRIPTION:Ceci est une description avec une première phrase assez longue\\,\\n puis sur une seconde ligne DTEND:#{Value.to_ics(event.ends_on)}Z DTSTAMP:#{Value.to_ics(event.publish_at)}Z