Make regex-to-string descriptor reusable
This commit is contained in:
parent
ba3aa4f86d
commit
1459d64508
@ -6,6 +6,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
|
|||||||
require Pleroma.Constants
|
require Pleroma.Constants
|
||||||
|
|
||||||
alias Pleroma.Object.Updater
|
alias Pleroma.Object.Updater
|
||||||
|
alias Pleroma.Web.ActivityPub.MRF.Utils
|
||||||
|
|
||||||
@moduledoc "Reject or force-unlisted emojis with certain URLs or names"
|
@moduledoc "Reject or force-unlisted emojis with certain URLs or names"
|
||||||
|
|
||||||
@ -215,19 +216,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
|
|||||||
|
|
||||||
@impl Pleroma.Web.ActivityPub.MRF.Policy
|
@impl Pleroma.Web.ActivityPub.MRF.Policy
|
||||||
def describe do
|
def describe do
|
||||||
# This horror is needed to convert regex sigils to strings
|
|
||||||
mrf_emoji =
|
mrf_emoji =
|
||||||
Pleroma.Config.get(:mrf_emoji, [])
|
Pleroma.Config.get(:mrf_emoji, [])
|
||||||
|> Enum.map(fn {key, value} ->
|
|> Enum.map(fn {key, value} ->
|
||||||
{key,
|
{key, Enum.map(value, &Utils.describe_regex_or_string/1)}
|
||||||
Enum.map(value, fn
|
|
||||||
pattern ->
|
|
||||||
if not is_binary(pattern) do
|
|
||||||
inspect(pattern)
|
|
||||||
else
|
|
||||||
pattern
|
|
||||||
end
|
|
||||||
end)}
|
|
||||||
end)
|
end)
|
||||||
|> Enum.into(%{})
|
|> Enum.into(%{})
|
||||||
|
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
|
defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
|
||||||
require Pleroma.Constants
|
require Pleroma.Constants
|
||||||
|
|
||||||
|
alias Pleroma.Web.ActivityPub.MRF.Utils
|
||||||
|
|
||||||
@moduledoc "Reject or Word-Replace messages with a keyword or regex"
|
@moduledoc "Reject or Word-Replace messages with a keyword or regex"
|
||||||
|
|
||||||
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
|
||||||
@ -128,7 +130,6 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def describe do
|
def describe do
|
||||||
# This horror is needed to convert regex sigils to strings
|
|
||||||
mrf_keyword =
|
mrf_keyword =
|
||||||
Pleroma.Config.get(:mrf_keyword, [])
|
Pleroma.Config.get(:mrf_keyword, [])
|
||||||
|> Enum.map(fn {key, value} ->
|
|> Enum.map(fn {key, value} ->
|
||||||
@ -136,21 +137,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
|
|||||||
Enum.map(value, fn
|
Enum.map(value, fn
|
||||||
{pattern, replacement} ->
|
{pattern, replacement} ->
|
||||||
%{
|
%{
|
||||||
"pattern" =>
|
"pattern" => Utils.describe_regex_or_string(pattern),
|
||||||
if not is_binary(pattern) do
|
|
||||||
inspect(pattern)
|
|
||||||
else
|
|
||||||
pattern
|
|
||||||
end,
|
|
||||||
"replacement" => replacement
|
"replacement" => replacement
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern ->
|
pattern ->
|
||||||
if not is_binary(pattern) do
|
Utils.describe_regex_or_string(pattern)
|
||||||
inspect(pattern)
|
|
||||||
else
|
|
||||||
pattern
|
|
||||||
end
|
|
||||||
end)}
|
end)}
|
||||||
end)
|
end)
|
||||||
|> Enum.into(%{})
|
|> Enum.into(%{})
|
||||||
|
15
lib/pleroma/web/activity_pub/mrf/utils.ex
Normal file
15
lib/pleroma/web/activity_pub/mrf/utils.ex
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ActivityPub.MRF.Utils do
|
||||||
|
@spec describe_regex_or_string(String.t() | Regex.t()) :: String.t()
|
||||||
|
def describe_regex_or_string(pattern) do
|
||||||
|
# This horror is needed to convert regex sigils to strings
|
||||||
|
if not is_binary(pattern) do
|
||||||
|
inspect(pattern)
|
||||||
|
else
|
||||||
|
pattern
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
19
test/pleroma/web/activity_pub/mrf/utils_test.exs
Normal file
19
test/pleroma/web/activity_pub/mrf/utils_test.exs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ActivityPub.MRF.UtilsTest do
|
||||||
|
use Pleroma.DataCase, async: true
|
||||||
|
|
||||||
|
alias Pleroma.Web.ActivityPub.MRF.Utils
|
||||||
|
|
||||||
|
describe "describe_regex_or_string/1" do
|
||||||
|
test "describes regex" do
|
||||||
|
assert "~r/foo/i" == Utils.describe_regex_or_string(~r/foo/i)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns string as-is" do
|
||||||
|
assert "foo" == Utils.describe_regex_or_string("foo")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user