Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Dec 28, 2024
2 parents 8563700 + c328cb9 commit 7b8d428
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 44 deletions.
2 changes: 1 addition & 1 deletion docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM elixir:1.17.2-alpine
FROM elixir:1.18.0-alpine

ADD https://api.github.com/repos/philomena-dev/FFmpeg/git/refs/heads/release/6.1 /tmp/ffmpeg_version.json
RUN (echo "https://github.com/philomena-dev/prebuilt-ffmpeg/raw/master"; cat /etc/apk/repositories) > /tmp/repositories \
Expand Down
71 changes: 28 additions & 43 deletions lib/philomena/images/dnp_validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,62 @@ defmodule Philomena.Images.DnpValidator do
import Ecto.Query
alias Philomena.Repo
alias Philomena.Tags.Tag
alias Philomena.ArtistLinks.ArtistLink
alias Philomena.DnpEntries.DnpEntry

def validate_dnp(changeset, uploader) do
tags =
changeset
|> get_field(:tags)
|> extract_tags()
|> Enum.map(& &1.name)

edit_present? = MapSet.member?(tags, "edit")
edit_present? = "edit" in tags

tags_with_dnp =
Tag
|> where([t], t.name in ^extract_artists(tags))
|> preload(dnp_entries: :requesting_user)
|> from(as: :tag)
|> where([t], t.name in ^tags)
|> where(exists(where(DnpEntry, [d], d.tag_id == parent_as(:tag).id)))
|> preload(dnp_entries: [tag: :verified_links])
|> Repo.all()
|> Enum.filter(&(length(&1.dnp_entries) > 0))

changeset
|> validate_artist_only(tags_with_dnp, uploader)
|> validate_no_edits(tags_with_dnp, uploader, edit_present?)
end

defp validate_artist_only(changeset, tags_with_dnp, uploader) do
Enum.reduce(tags_with_dnp, changeset, fn tag, changeset ->
case Enum.any?(
tag.dnp_entries,
&(&1.dnp_type == "Artist Upload Only" and not valid_user?(&1, uploader))
) do
true ->
add_error(changeset, :image, "DNP (Artist upload only)")

false ->
changeset
end
end)
validate_tags_with_dnp(changeset, tags_with_dnp, uploader, "Artist Upload Only")
end

defp validate_no_edits(changeset, _tags_with_dnp, _uploader, false), do: changeset
defp validate_no_edits(changeset, tags_with_dnp, uploader, edit_present?) do
if edit_present? do
validate_tags_with_dnp(changeset, tags_with_dnp, uploader, "No Edits")
else
changeset
end
end

defp validate_no_edits(changeset, tags_with_dnp, uploader, true) do
defp validate_tags_with_dnp(changeset, tags_with_dnp, uploader, dnp_type) do
Enum.reduce(tags_with_dnp, changeset, fn tag, changeset ->
case Enum.any?(
tag.dnp_entries,
&(&1.dnp_type == "No Edits" and not valid_user?(&1, uploader))
) do
tag.dnp_entries
|> Enum.any?(&(&1.dnp_type == dnp_type and not uploader_permitted?(&1, uploader)))
|> case do
true ->
add_error(changeset, :image, "DNP (No edits)")
add_error(changeset, :image, "DNP (#{dnp_type})")

false ->
_ ->
changeset
end
end)
end

defp valid_user?(_dnp_entry, nil), do: false

defp valid_user?(dnp_entry, user) do
ArtistLink
|> where(tag_id: ^dnp_entry.tag_id)
|> where(aasm_state: "verified")
|> where(user_id: ^user.id)
|> Repo.exists?()
end

defp extract_tags(tags) do
tags
|> Enum.map(& &1.name)
|> MapSet.new()
end
defp uploader_permitted?(dnp_entry, uploader) do
case uploader do
%{id: uploader_id} ->
Enum.any?(dnp_entry.tag.verified_links, &(&1.user_id == uploader_id))

defp extract_artists(tags) do
Enum.filter(tags, &String.starts_with?(&1, "artist:"))
_ ->
false
end
end
end
1 change: 1 addition & 0 deletions lib/philomena/tags/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ defmodule Philomena.Tags.Tag do
join_through: "tags_implied_tags",
join_keys: [implied_tag_id: :id, tag_id: :id]

has_many :verified_links, ArtistLink, where: [aasm_state: "verified"]
has_many :public_links, ArtistLink, where: [public: true, aasm_state: "verified"]
has_many :hidden_links, ArtistLink, where: [public: false, aasm_state: "verified"]
has_many :dnp_entries, DnpEntry, where: [aasm_state: "listed"]
Expand Down

0 comments on commit 7b8d428

Please sign in to comment.