don't allow duplicate reports to have identical source and target

This commit is contained in:
byte[] 2019-12-30 08:42:00 -05:00
parent bf5158b60e
commit aacd3e31b7
2 changed files with 12 additions and 1 deletions

View file

@ -42,7 +42,7 @@ defmodule Philomena.Bans.Subnet do
changeset
|> get_field(:specification)
|> case do
%Postgrex.INET{address: {h1, h2, h3, h4, h5, h6, h7, h8}, netmask: 128} ->
%Postgrex.INET{address: {h1, h2, h3, h4, _h5, _h6, _h7, _h8}, netmask: 128} ->
%Postgrex.INET{address: {h1, h2, h3, h4, 0, 0, 0, 0}, netmask: 64}
val ->

View file

@ -30,6 +30,7 @@ defmodule Philomena.DuplicateReports.DuplicateReport do
|> cast(attrs, [:reason])
|> put_assoc(:user, attribution[:user])
|> validate_length(:reason, max: 250, count: :bytes)
|> validate_source_is_not_target()
end
def accept_changeset(duplicate_report, user) do
@ -55,4 +56,14 @@ defmodule Philomena.DuplicateReports.DuplicateReport do
|> put_change(:modifier_id, user.id)
|> put_change(:state, "rejected")
end
defp validate_source_is_not_target(changeset) do
source_id = get_field(changeset, :image_id)
target_id = get_field(changeset, :duplicate_of_image_id)
case source_id == target_id do
true -> add_error(changeset, :image_id, "must be different from the target")
false -> changeset
end
end
end