From 7b49f8d978ef48ff8dda6bb6126858a70e3d0076 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 30 Apr 2020 16:34:35 -0400 Subject: [PATCH] fixes #96: add tag blacklist --- config/tag.json | 20 ++++++++++++++++++++ lib/philomena/images/tag_validator.ex | 15 +++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 config/tag.json diff --git a/config/tag.json b/config/tag.json new file mode 100644 index 00000000..7294ede5 --- /dev/null +++ b/config/tag.json @@ -0,0 +1,20 @@ +{ + "blacklist": [ + "tagme", + "tag me", + "not tagged", + "no tag", + "notag", + "notags", + "upvotes galore", + "downvotes galore", + "wall of faves", + "drama in the comments", + "drama in comments", + "tag needed", + "paywall", + "cringeworthy", + "solo oc", + "tag your shit" + ] +} diff --git a/lib/philomena/images/tag_validator.ex b/lib/philomena/images/tag_validator.ex index a1d588fe..96a303e9 100644 --- a/lib/philomena/images/tag_validator.ex +++ b/lib/philomena/images/tag_validator.ex @@ -1,4 +1,5 @@ defmodule Philomena.Images.TagValidator do + alias Philomena.Servers.Config import Ecto.Changeset @safe_rating MapSet.new(["safe"]) @@ -19,6 +20,7 @@ defmodule Philomena.Images.TagValidator do changeset |> validate_number_of_tags(tag_set, 3) + |> validate_bad_words(tag_set) |> validate_has_rating(rating_set) |> validate_safe(rating_set) |> validate_sexual_exclusion(rating_set) @@ -50,6 +52,19 @@ 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) + + cond do + MapSet.size(intersection) > 0 -> + Enum.reduce(intersection, changeset, &add_error(&2, :tag_input, "contains forbidden tag `#{&1}'")) + + true -> + changeset + end + end + defp validate_has_rating(changeset, %{safe: s, sexual: x, horror: h, gross: g}) when s == @empty and x == @empty and h == @empty and g == @empty do changeset