disallow tag self-aliasing

This commit is contained in:
Wren 2020-06-06 12:13:31 -04:00
parent 7f55c63303
commit 485d8be3b3

View file

@ -179,56 +179,60 @@ defmodule Philomena.Tags do
def alias_tag(%Tag{} = tag, attrs) do def alias_tag(%Tag{} = tag, attrs) do
target_tag = Repo.get_by!(Tag, name: attrs["target_tag"]) target_tag = Repo.get_by!(Tag, name: attrs["target_tag"])
filters_hidden = if(tag.id == target_tag.id) do
where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.hidden_tag_ids, ^tag.id)) tag
else
filters_hidden =
where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.hidden_tag_ids, ^tag.id))
filters_spoilered = filters_spoilered =
where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.spoilered_tag_ids, ^tag.id)) where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.spoilered_tag_ids, ^tag.id))
users_watching = users_watching =
where(User, [u], fragment("? @> ARRAY[?]::integer[]", u.watched_tag_ids, ^tag.id)) where(User, [u], fragment("? @> ARRAY[?]::integer[]", u.watched_tag_ids, ^tag.id))
array_replace(filters_hidden, :hidden_tag_ids, tag.id, target_tag.id) array_replace(filters_hidden, :hidden_tag_ids, tag.id, target_tag.id)
array_replace(filters_spoilered, :spoilered_tag_ids, tag.id, target_tag.id) array_replace(filters_spoilered, :spoilered_tag_ids, tag.id, target_tag.id)
array_replace(users_watching, :watched_tag_ids, tag.id, target_tag.id) array_replace(users_watching, :watched_tag_ids, tag.id, target_tag.id)
# Manual insert all because ecto won't do it for us # Manual insert all because ecto won't do it for us
Repo.query!( Repo.query!(
"INSERT INTO image_taggings (image_id, tag_id) " <> "INSERT INTO image_taggings (image_id, tag_id) " <>
"SELECT i.id, #{target_tag.id} FROM images i " <> "SELECT i.id, #{target_tag.id} FROM images i " <>
"INNER JOIN image_taggings it on it.image_id = i.id " <> "INNER JOIN image_taggings it on it.image_id = i.id " <>
"WHERE it.tag_id = #{tag.id} " <> "WHERE it.tag_id = #{tag.id} " <>
"ON CONFLICT DO NOTHING" "ON CONFLICT DO NOTHING"
) )
# Delete taggings on the source tag # Delete taggings on the source tag
Tagging Tagging
|> where(tag_id: ^tag.id) |> where(tag_id: ^tag.id)
|> Repo.delete_all() |> Repo.delete_all()
# Update other assocations # Update other assocations
UserLink UserLink
|> where(tag_id: ^tag.id) |> where(tag_id: ^tag.id)
|> Repo.update_all(set: [tag_id: target_tag.id]) |> Repo.update_all(set: [tag_id: target_tag.id])
DnpEntry DnpEntry
|> where(tag_id: ^tag.id) |> where(tag_id: ^tag.id)
|> Repo.update_all(set: [tag_id: target_tag.id]) |> Repo.update_all(set: [tag_id: target_tag.id])
Channel Channel
|> where(associated_artist_tag_id: ^tag.id) |> where(associated_artist_tag_id: ^tag.id)
|> Repo.update_all(set: [associated_artist_tag_id: target_tag.id]) |> Repo.update_all(set: [associated_artist_tag_id: target_tag.id])
# Update counter # Update counter
Tag Tag
|> where(id: ^tag.id) |> where(id: ^tag.id)
|> Repo.update_all( |> Repo.update_all(
set: [images_count: 0, aliased_tag_id: target_tag.id, updated_at: DateTime.utc_now()] set: [images_count: 0, aliased_tag_id: target_tag.id, updated_at: DateTime.utc_now()]
) )
# Finally, reindex # Finally, reindex
reindex_tag_images(target_tag) reindex_tag_images(target_tag)
reindex_tags([tag, target_tag]) reindex_tags([tag, target_tag])
end
end end
def reindex_tag_images(%Tag{} = tag) do def reindex_tag_images(%Tag{} = tag) do