disallow tag self-aliasing

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

View file

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