mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-03-28 06:17:46 +01:00
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:
parent
dc6cef6a3d
commit
3266ac5d2b
4 changed files with 2435 additions and 24 deletions
2423
config/tag.json
2423
config/tag.json
File diff suppressed because it is too large
Load diff
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
| )
|
||||
| )
|
Loading…
Add table
Reference in a new issue