fix(filters): treat phrases as literal text in the regex
Phrases went into the Postgres and Elixir regexes as they were, so a
phrase like "(" broke the timeline and notification queries. The :re
form also put the word boundaries only around the whole alternation.
Escape each phrase and, as Mastodon does, add a word boundary only where
the phrase starts or ends with a word character.
Reported in Akkoma (d6209837b).
This commit is contained in:
parent
90eb5e4015
commit
f975db8719
3 changed files with 66 additions and 6 deletions
|
|
@ -208,19 +208,27 @@ defmodule Pleroma.Filter do
|
|||
end
|
||||
|
||||
def compose_regex([_ | _] = filters, format) do
|
||||
boundary = if format == :postgres, do: "\\y", else: "\\b"
|
||||
|
||||
phrases =
|
||||
filters
|
||||
|> Enum.map(& &1.phrase)
|
||||
|> Enum.map(&phrase_regex(&1.phrase, boundary))
|
||||
|> Enum.join("|")
|
||||
|
||||
case format do
|
||||
:postgres ->
|
||||
"\\y(#{phrases})\\y"
|
||||
|
||||
:re ->
|
||||
~r/\b#{phrases}\b/i
|
||||
:postgres -> "(#{phrases})"
|
||||
:re -> Regex.compile!("(?:#{phrases})", "iu")
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
prefix = if String.match?(phrase, ~r/^\w/u), do: boundary, else: ""
|
||||
suffix = if String.match?(phrase, ~r/\w$/u), do: boundary, else: ""
|
||||
|
||||
prefix <> Regex.escape(phrase) <> suffix
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue