Fix new credo warnings

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-11-26 14:30:46 +01:00
parent 52e624bb88
commit 51afec1856
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
20 changed files with 77 additions and 72 deletions

View file

@ -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],

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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),

View file

@ -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

View file

@ -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/, "<br>"), 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/, "<br>"), mentions, tags}
end
@doc """
Use the data-media-id attributes to extract media from body text
"""

View file

@ -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}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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()))

View file

@ -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

View file

@ -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!()

View file

@ -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/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/

View file

@ -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

View file

@ -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

View file

@ -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())

View file

@ -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