Add filtering by item content

This commit is contained in:
Shadowfacts 2021-08-28 12:17:16 -04:00
parent 71a23faa90
commit a85dca5b3d
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 28 additions and 5 deletions

View File

@ -45,18 +45,24 @@ defmodule Frenzy.Pipeline.FilterEngine do
def validate_rules(_rules), do: {:error, "rules must be a list"} def validate_rules(_rules), do: {:error, "rules must be a list"}
@rule_modes ~W[contains_string contains_string_case_sensitive matches_regex]
@rule_properties ~W[url title author content]
def validate_rule(rule) do def validate_rule(rule) do
cond do cond do
not is_map(rule) -> not is_map(rule) ->
{:error, "rule must be a map"} {:error, "rule must be a map"}
not (Map.has_key?(rule, "mode") and is_binary(rule["mode"]) and not (Map.has_key?(rule, "mode") and is_binary(rule["mode"]) and
rule["mode"] in ["contains_string", "matches_regex"]) -> rule["mode"] in @rule_modes) ->
{:error, "mode property must be a string, either 'contains_string' or 'matches_regex'"} rule_modes_text = Enum.map_join(@rule_modes, ", ", &"'#{&1}'")
{:error, "mode property must be a string, one of #{rule_modes_text}"}
not (Map.has_key?(rule, "property") and is_binary(rule["property"]) and not (Map.has_key?(rule, "property") and is_binary(rule["property"]) and
rule["property"] in ["url", "title", "author"]) -> rule["property"] in @rule_properties) ->
{:error, "property property must be a string, either 'url', 'title', or 'author'"} rule_props_text = Enum.map_join(@rule_properties, ", ", &"'#{&1}'")
{:error, "property property must be a string, one of #{rule_props_text}"}
not (Map.has_key?(rule, "param") and is_binary(rule["param"])) -> not (Map.has_key?(rule, "param") and is_binary(rule["param"])) ->
{:error, "param property must be a string"} {:error, "param property must be a string"}
@ -105,6 +111,10 @@ defmodule Frenzy.Pipeline.FilterEngine do
end end
defp matches(value, "contains_string", param) do defp matches(value, "contains_string", param) do
String.contains?(String.downcase(value), String.downcase(param))
end
defp matches(value, "contains_string_case_sensitive", param) do
String.contains?(value, param) String.contains?(value, param)
end end
@ -116,5 +126,16 @@ defmodule Frenzy.Pipeline.FilterEngine do
defp get_property(item_params, "url"), do: item_params.url defp get_property(item_params, "url"), do: item_params.url
defp get_property(item_params, "title"), do: item_params.title defp get_property(item_params, "title"), do: item_params.title
defp get_property(item_params, "author"), do: item_params.author defp get_property(item_params, "author"), do: item_params.author
defp get_property(%{content: content, content_type: type}, "content")
when type in ["text/plain", "text/gemini"],
do: content
defp get_property(%{content: content, content_type: "text/html"}, "content") do
content
|> Floki.parse()
|> Floki.text()
end
defp get_property(_item_params, _property), do: {:error, "invalid property"} defp get_property(_item_params, _property), do: {:error, "invalid property"}
end end

View File

@ -3,13 +3,15 @@ defmodule FrenzyWeb.FilterRuleLive do
@modes [ @modes [
{"Contains Substring", "contains_string"}, {"Contains Substring", "contains_string"},
{"Contains Substring (case sensitive)", "contains_string_case_sensitive"},
{"Matches Regex", "matches_regex"} {"Matches Regex", "matches_regex"}
] ]
@properties [ @properties [
{"Title", "title"}, {"Title", "title"},
{"URL", "url"}, {"URL", "url"},
{"Author", "author"} {"Author", "author"},
{"Content", "content"}
] ]
@impl true @impl true