strip blacklist tags on post upload

Normally it would error out and instead it will just strip the tags out without informing the user.
This commit is contained in:
Chaska 2024-04-27 13:30:32 -05:00
parent dc6cef6a3d
commit 3266ac5d2b
4 changed files with 2435 additions and 24 deletions

File diff suppressed because it is too large Load diff

View file

@ -99,7 +99,7 @@ defmodule Philomena.Images.Image do
field :uploaded_image, :string, virtual: true
field :removed_image, :string, virtual: true
field :tag_input, :string, virtual: true
timestamps(inserted_at: :created_at, type: :utc_datetime)
end

View file

@ -3,7 +3,10 @@ defmodule Philomena.Images.TagValidator do
import Ecto.Changeset
def validate_tags(changeset) do
tags = changeset |> get_field(:tags)
blacklist = Config.get(:tag)["blacklist"]
tags = changeset |> get_field(:tags) |> Enum.reject(fn x ->
x.name in blacklist
end)
validate_tag_input(changeset, tags)
end
@ -14,7 +17,7 @@ defmodule Philomena.Images.TagValidator do
changeset
|> validate_number_of_tags(tag_set, 3)
|> validate_bad_words(tag_set)
|> strip_bad_words(tags)
|> validate_has_rating(rating_set)
|> validate_safe(rating_set)
|> validate_sexual_exclusion(rating_set)
@ -45,21 +48,14 @@ defmodule Philomena.Images.TagValidator do
end
end
def validate_bad_words(changeset, tag_set) do
bad_words = MapSet.new(Config.get(:tag)["blacklist"])
intersection = MapSet.intersection(tag_set, bad_words)
def strip_bad_words(changeset, tags) do
tag_input = tags
|> Enum.reduce([], fn x, acc -> [x.name | acc] end)
|> Enum.join(", ")
cond do
MapSet.size(intersection) > 0 ->
Enum.reduce(
intersection,
changeset,
&add_error(&2, :tag_input, "contains forbidden tag `#{&1}'")
)
true ->
changeset
end
changeset
|> put_change(:tag_input, tag_input)
|> put_change(:tags, tags)
end
defp validate_has_rating(changeset, %{safe: s, sexual: x, horror: h, gross: g}) do

View file

@ -13,10 +13,6 @@
= link "Usage", to: Routes.tag_detail_path(@conn, :index, @tag), class: "detail-link"
= if manages_dnp?(@conn) do
= link "Create new DNP entry", to: Routes.dnp_entry_path(@conn, :new, tag_id: @tag.id), class: "detail-link"
= if manages_tags?(@conn) do
= link "Edit spoiler image ", to: Routes.tag_image_path(@conn, :edit, @tag)
= if can?(@conn, :alias, @tag) do
= link "Edit aliases", to: Routes.tag_alias_path(@conn, :edit, @tag)
br
@ -122,4 +118,4 @@
| (
= link "more info", to: Routes.dnp_entry_path(@conn, :show, entry)
| )
| )