Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85823436
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
2 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/changelog.d/filter-whole-word.fix b/changelog.d/filter-whole-word.fix
new file mode 100644
index 000000000..a831a74cb
--- /dev/null
+++ b/changelog.d/filter-whole-word.fix
@@ -0,0 +1 @@
+Filters with whole_word set to false now also match inside words
diff --git a/lib/pleroma/filter.ex b/lib/pleroma/filter.ex
index 3ab44efb0..fe3fb1c27 100644
--- a/lib/pleroma/filter.ex
+++ b/lib/pleroma/filter.ex
@@ -212,7 +212,7 @@ defmodule Pleroma.Filter do
phrases =
filters
- |> Enum.map(&phrase_regex(&1.phrase, boundary))
+ |> Enum.map(&phrase_regex(&1, boundary))
|> Enum.join("|")
case format do
@@ -223,9 +223,12 @@ defmodule Pleroma.Filter do
def compose_regex(_, _), do: nil
- # Phrases are literal text. Like Mastodon, only add a word boundary
- # where the phrase starts or ends with a word character.
- defp phrase_regex(phrase, boundary) do
+ # Phrases are literal text. Like Mastodon, whole-word filters only get a
+ # word boundary where the phrase starts or ends with a word character.
+ defp phrase_regex(%__MODULE__{phrase: phrase, whole_word: false}, _boundary),
+ do: Regex.escape(phrase)
+
+ defp phrase_regex(%__MODULE__{phrase: phrase}, boundary) do
prefix = if String.match?(phrase, ~r/^\w/u), do: boundary, else: ""
suffix = if String.match?(phrase, ~r/\w$/u), do: boundary, else: ""
diff --git a/test/pleroma/filter_test.exs b/test/pleroma/filter_test.exs
index 5018b30bc..2fc72d9f5 100644
--- a/test/pleroma/filter_test.exs
+++ b/test/pleroma/filter_test.exs
@@ -233,6 +233,17 @@ defmodule Pleroma.FilterTest do
end
end
+ test "matches inside words when whole_word is false" do
+ filters = [%Filter{phrase: "cat", whole_word: false}, %Filter{phrase: "dog"}]
+ pg = Filter.compose_regex(filters)
+ re = Filter.compose_regex(filters, :re)
+
+ for {text, expected} <- [{"a catalog", true}, {"a hotdog", false}, {"a dog", true}] do
+ assert pg_match?(pg, text) == expected
+ assert Regex.match?(re, text) == expected
+ end
+ end
+
test "keeps word boundaries for every phrase in Elixir" do
regex = Filter.compose_regex(filters(["cat", "dog"]), :re)
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Oct 29, 5:39 AM (9 h, 22 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1797601
Default Alt Text
(2 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline