From 70b077e4e7b2bea1801640c82735fb167a9da90a Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 16 Dec 2019 13:49:53 -0500 Subject: [PATCH] add reindex and delete buttons --- lib/philomena/tags.ex | 37 +++++++++++------- .../controllers/tag/reindex_controller.ex | 19 +++++++++ .../controllers/tag_controller.ex | 2 +- lib/philomena_web/router.ex | 1 + .../templates/tag/alias/edit.html.slime | 2 +- .../templates/tag/edit.html.slime | 39 ++++++++----------- 6 files changed, 61 insertions(+), 39 deletions(-) create mode 100644 lib/philomena_web/controllers/tag/reindex_controller.ex diff --git a/lib/philomena/tags.ex b/lib/philomena/tags.ex index 3e7051e8..2220f716 100644 --- a/lib/philomena/tags.ex +++ b/lib/philomena/tags.ex @@ -163,7 +163,9 @@ defmodule Philomena.Tags do |> select([i, _t], i.id) |> Repo.all() - {:ok, _tag} = Repo.delete(tag) + {:ok, tag} = Repo.delete(tag) + + Tag.delete_document(tag.id) Image |> where([i], i.id in ^image_ids) @@ -205,25 +207,32 @@ defmodule Philomena.Tags do |> where(tag_id: ^tag.id) |> Repo.update_all(set: [tag_id: target_tag.id]) - # Update counters - new_count = - Image - |> join(:inner, [i], _ in assoc(i, :tags)) - |> where([i, t], i.hidden_from_users == false and t.id == ^target_tag.id) - |> Repo.aggregate(:count, :id) - - Tag - |> where(id: ^target_tag.id) - |> Repo.update_all(set: [images_count: new_count]) - + # Update counter Tag |> where(id: ^tag.id) |> Repo.update_all(set: [images_count: 0, aliased_tag_id: target_tag.id, updated_at: DateTime.utc_now()]) - # Finally, update images + # Finally, reindex + reindex_tag_images(target_tag) + reindex_tags([tag, target_tag]) + end + + def reindex_tag_images(%Tag{} = tag) do + # First recount the tag + image_count = + Image + |> join(:inner, [i], _ in assoc(i, :tags)) + |> where([i, t], i.hidden_from_users == false and t.id == ^tag.id) + |> Repo.aggregate(:count, :id) + + Tag + |> where(id: ^tag.id) + |> Repo.update_all(set: [images_count: image_count]) + + # Then reindex Image |> join(:inner, [i], _ in assoc(i, :tags)) - |> where([_i, t], t.id == ^target_tag.id) + |> where([_i, t], t.id == ^tag.id) |> preload(^Images.indexing_preloads()) |> Image.reindex() end diff --git a/lib/philomena_web/controllers/tag/reindex_controller.ex b/lib/philomena_web/controllers/tag/reindex_controller.ex new file mode 100644 index 00000000..fe0b96ff --- /dev/null +++ b/lib/philomena_web/controllers/tag/reindex_controller.ex @@ -0,0 +1,19 @@ +defmodule PhilomenaWeb.Tag.ReindexController do + use PhilomenaWeb, :controller + + alias Philomena.Tags.Tag + alias Philomena.Tags + + plug PhilomenaWeb.CanaryMapPlug, create: :alias + plug :load_and_authorize_resource, model: Tag, id_name: "tag_id", id_field: "slug", preload: [:implied_tags, :aliased_tag], persisted: true + + def create(conn, _params) do + spawn fn -> + Tags.reindex_tag_images(conn.assigns.tag) + end + + conn + |> put_flash(:info, "Tag reindex started.") + |> redirect(to: Routes.tag_path(conn, :edit, conn.assigns.tag)) + end +end diff --git a/lib/philomena_web/controllers/tag_controller.ex b/lib/philomena_web/controllers/tag_controller.ex index ed01bfb8..8b6bf541 100644 --- a/lib/philomena_web/controllers/tag_controller.ex +++ b/lib/philomena_web/controllers/tag_controller.ex @@ -9,7 +9,7 @@ defmodule PhilomenaWeb.TagController do plug PhilomenaWeb.RecodeParameterPlug, [name: "id"] when action in [:show] plug PhilomenaWeb.CanaryMapPlug, update: :edit plug :load_and_authorize_resource, model: Tag, id_field: "slug", only: [:show, :edit, :update, :delete], preload: [:aliases, :aliased_tag, :implied_tags, :implied_by_tags, :dnp_entries, public_links: :user] - plug :redirect_alias when action in [:show, :edit, :update, :delete] + plug :redirect_alias when action in [:show] def index(conn, params) do query_string = params["tq"] || "*" diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index cae9fd09..219eec95 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -232,6 +232,7 @@ defmodule PhilomenaWeb.Router do resources "/tags", TagController, only: [:edit, :update, :delete] do resources "/image", Tag.ImageController, only: [:edit, :update, :delete], singleton: true resources "/alias", Tag.AliasController, only: [:edit, :update, :delete], singleton: true + resources "/reindex", Tag.ReindexController, only: [:create], singleton: true end resources "/pages", PageController, only: [:index, :new, :create, :edit, :update] diff --git a/lib/philomena_web/templates/tag/alias/edit.html.slime b/lib/philomena_web/templates/tag/alias/edit.html.slime index 249c69e6..463c6e64 100644 --- a/lib/philomena_web/templates/tag/alias/edit.html.slime +++ b/lib/philomena_web/templates/tag/alias/edit.html.slime @@ -15,7 +15,7 @@ h1 => submit "Alias tag", class: "button" br -= button_to "Remove tag alias", Routes.tag_alias_path(@conn, :delete, @tag), method: "delete", class: "button", data: [confirm: "Are you really, really sure?"] += button_to "Remove tag alias", Routes.tag_alias_path(@conn, :delete, @tag), method: "delete", class: "button", data: [confirm: "Are you really, really sure?", disable_with: raw("Saving…")] br = link "Back", to: Routes.tag_path(@conn, :show, @tag) diff --git a/lib/philomena_web/templates/tag/edit.html.slime b/lib/philomena_web/templates/tag/edit.html.slime index 5bd3d36f..dfa0cbac 100644 --- a/lib/philomena_web/templates/tag/edit.html.slime +++ b/lib/philomena_web/templates/tag/edit.html.slime @@ -36,26 +36,19 @@ p = link "Edit aliases", to: Routes.tag_alias_path(@conn, :edit, @tag) br = submit "Save Tag", class: "button button--state-primary" - / not ready yet - br - br - input.toggle-box#tag-management checked="false" type="checkbox" - label for="tag-management" Tag Processing - .toggle-box-container - .toggle-box-container__content - = link_to "Rebuild index", admin_tag_reindex_path(@tag), class: "button", data: { confirm: t("are_you_sure") }, method: :post - p Use this if the tag displays the wrong number of images or returns the wrong search results. - = link_to "Recreate slug", admin_tag_slug_path(@tag), class: "button", method: :post - p - | Use this for old tags with invalid slugs ( - code - ' /mlp/ → - = Tag.generate_slug "/mlp/" - | ) - = link_to "Destroy tag", admin_tag_path(@tag), class: "button button--state-danger", data: { confirm: t("are_you_sure") }, method: :delete - p - strong Irreversible. Use with extreme caution! - ul - li Intended use is removing garbage tags. - li Will remove tag changes on the tag, but not on images or profiles. - li Will fail if the tag is the target of an alias, is implied by other tags, or is a rating tag. +br +br +input.toggle-box#tag-management checked="false" type="checkbox" +label for="tag-management" Tag Processing +.toggle-box-container + .toggle-box-container__content + = button_to "Rebuild index", Routes.tag_reindex_path(@conn, :create, @tag), method: "post", class: "button", data: [confirm: "Are you really, really sure?", disable_with: raw("Reindexing…")] + p Use this if the tag displays the wrong number of images or returns the wrong search results. + + = button_to "Destroy tag", Routes.tag_path(@conn, :delete, @tag), method: "delete", class: "button button--state-danger", data: [confirm: "Are you really, really sure?", disable_with: raw("Deleting…")] + p + strong Irreversible. Use with extreme caution! + ul + li Intended use is removing garbage tags. + li Will remove tag changes on the tag, but not on images or profiles. + li Will unset the alias if this tag is an alias target, and will automatically remove any implications that exist to it.