From ad09bdb3764e529d1f0682b5395f34bc4849bd22 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Thu, 4 Feb 2021 17:23:21 +0400 Subject: [PATCH] Improve readability --- lib/pleroma/config/deprecation_warnings.ex | 8 +++--- .../web/activity_pub/mrf/reject_non_public.ex | 2 +- .../web/activity_pub/mrf/simple_policy.ex | 27 ++++++++++++------- .../mrf/user_allow_list_policy.ex | 2 +- .../web/activity_pub/mrf/vocabulary_policy.ex | 2 +- .../web/mastodon_api/views/instance_view.ex | 21 +++++++-------- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/pleroma/config/deprecation_warnings.ex b/lib/pleroma/config/deprecation_warnings.ex index 887470de9..029ee8b65 100644 --- a/lib/pleroma/config/deprecation_warnings.ex +++ b/lib/pleroma/config/deprecation_warnings.ex @@ -23,7 +23,7 @@ defmodule Pleroma.Config.DeprecationWarnings do def check_simple_policy_tuples do has_strings = Config.get([:mrf_simple]) - |> Enum.any?(fn {_, v} -> Enum.any?(v, fn e -> is_binary(e) end) end) + |> Enum.any?(fn {_, v} -> Enum.any?(v, &is_binary/1) end) if has_strings do Logger.warn(""" @@ -81,8 +81,7 @@ defmodule Pleroma.Config.DeprecationWarnings do end def check_quarantined_instances_tuples do - has_strings = - Config.get([:instance, :quarantined_instances]) |> Enum.any?(fn e -> is_binary(e) end) + has_strings = Config.get([:instance, :quarantined_instances]) |> Enum.any?(&is_binary/1) if has_strings do Logger.warn(""" @@ -119,8 +118,7 @@ defmodule Pleroma.Config.DeprecationWarnings do end def check_transparency_exclusions_tuples do - has_strings = - Config.get([:mrf, :transparency_exclusions]) |> Enum.any?(fn e -> is_binary(e) end) + has_strings = Config.get([:mrf, :transparency_exclusions]) |> Enum.any?(&is_binary/1) if has_strings do Logger.warn(""" diff --git a/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex b/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex index b9d3e52c7..dbb7ca0df 100644 --- a/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex +++ b/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex @@ -47,7 +47,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublic do @impl true def describe, - do: {:ok, %{mrf_rejectnonpublic: Config.get(:mrf_rejectnonpublic) |> Enum.into(%{})}} + do: {:ok, %{mrf_rejectnonpublic: Config.get(:mrf_rejectnonpublic) |> Map.new()}} @impl true def config_description do diff --git a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex index 00a74ead8..c631cc85f 100644 --- a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex @@ -262,14 +262,16 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do mrf_simple_excluded = Config.get(:mrf_simple) - |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn {v, _} -> v in exclusions end)} end) + |> Enum.map(fn {rule, instances} -> + {rule, Enum.reject(instances, fn {host, _} -> host in exclusions end)} + end) mrf_simple = mrf_simple_excluded - |> Enum.map(fn {k, v} -> - {k, Enum.map(v, fn {instance, _} -> instance end)} + |> Enum.map(fn {rule, instances} -> + {rule, Enum.map(instances, fn {host, _} -> host end)} end) - |> Enum.into(%{}) + |> Map.new() # This is for backwards compatibility. We originally didn't sent # extra info like a reason why an instance was rejected/quarantined/etc. @@ -277,12 +279,19 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do # to add an extra "info" key. mrf_simple_info = mrf_simple_excluded - |> Enum.map(fn {k, v} -> {k, Enum.reject(v, fn {_, reason} -> reason == "" end)} end) - |> Enum.reject(fn {_, v} -> v == [] end) - |> Enum.map(fn {k, l} -> - {k, l |> Enum.map(fn {i, r} -> {i, %{"reason" => r}} end) |> Enum.into(%{})} + |> Enum.map(fn {rule, instances} -> + {rule, Enum.reject(instances, fn {_, reason} -> reason == "" end)} end) - |> Enum.into(%{}) + |> Enum.reject(fn {_, instances} -> instances == [] end) + |> Enum.map(fn {rule, instances} -> + instances = + instances + |> Enum.map(fn {host, reason} -> {host, %{"reason" => reason}} end) + |> Map.new() + + {rule, instances} + end) + |> Map.new() {:ok, %{mrf_simple: mrf_simple, mrf_simple_info: mrf_simple_info}} end diff --git a/lib/pleroma/web/activity_pub/mrf/user_allow_list_policy.ex b/lib/pleroma/web/activity_pub/mrf/user_allow_list_policy.ex index 1bcb3688b..52fb02a84 100644 --- a/lib/pleroma/web/activity_pub/mrf/user_allow_list_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/user_allow_list_policy.ex @@ -37,7 +37,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.UserAllowListPolicy do def describe do mrf_user_allowlist = Config.get([:mrf_user_allowlist], []) - |> Enum.into(%{}, fn {k, v} -> {k, length(v)} end) + |> Map.new(fn {k, v} -> {k, length(v)} end) {:ok, %{mrf_user_allowlist: mrf_user_allowlist}} end diff --git a/lib/pleroma/web/activity_pub/mrf/vocabulary_policy.ex b/lib/pleroma/web/activity_pub/mrf/vocabulary_policy.ex index 20f57f609..602e10b44 100644 --- a/lib/pleroma/web/activity_pub/mrf/vocabulary_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/vocabulary_policy.ex @@ -39,7 +39,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.VocabularyPolicy do @impl true def describe, - do: {:ok, %{mrf_vocabulary: Pleroma.Config.get(:mrf_vocabulary) |> Enum.into(%{})}} + do: {:ok, %{mrf_vocabulary: Pleroma.Config.get(:mrf_vocabulary) |> Map.new()}} @impl true def config_description do diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index f62b52a64..ef208062b 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -95,22 +95,19 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do {:ok, data} = MRF.describe() data - |> Map.merge(%{ - quarantined_instances: - quarantined - |> Enum.map(fn {instance, _reason} -> instance end) - }) + |> Map.put( + :quarantined_instances, + Enum.map(quarantined, fn {instance, _reason} -> instance end) + ) # This is for backwards compatibility. We originally didn't sent # extra info like a reason why an instance was rejected/quarantined/etc. # Because we didn't want to break backwards compatibility it was decided # to add an extra "info" key. - |> Map.merge(%{ - quarantined_instances_info: %{ - "quarantined_instances" => - quarantined - |> Enum.map(fn {instance, reason} -> {instance, %{"reason" => reason}} end) - |> Enum.into(%{}) - } + |> Map.put(:quarantined_instances_info, %{ + "quarantined_instances" => + quarantined + |> Enum.map(fn {instance, reason} -> {instance, %{"reason" => reason}} end) + |> Map.new() }) else %{}