From 84d784d33a1b29401bbe6e1efd4d313d9fbb5a99 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 7 May 2020 23:02:48 -0400 Subject: [PATCH] move reindex logic to context for reports, image creation --- lib/philomena/images.ex | 26 +++++++++++++++++++ lib/philomena/reports.ex | 12 +++++++++ .../controllers/admin/batch/tag_controller.ex | 4 --- .../admin/report/claim_controller.ex | 5 +--- .../admin/report/close_controller.ex | 3 +-- .../controllers/api/json/image_controller.ex | 12 --------- .../controllers/image_controller.ex | 12 --------- 7 files changed, 40 insertions(+), 34 deletions(-) diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index e7e69fe3..cc36b17d 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -19,6 +19,7 @@ defmodule Philomena.Images do alias Philomena.SourceChanges.SourceChange alias Philomena.TagChanges.TagChange alias Philomena.Tags + alias Philomena.UserStatistics alias Philomena.Tags.Tag alias Philomena.Notifications alias Philomena.Interactions @@ -94,6 +95,21 @@ defmodule Philomena.Images do {:ok, nil} end) |> Repo.isolated_transaction(:serializable) + |> case do + {:ok, %{image: image}} = result -> + spawn(fn -> + repair_image(image) + end) + + reindex_image(image) + Tags.reindex_tags(image.added_tags) + UserStatistics.inc_stat(attribution[:user], :uploads) + + result + + result -> + result + end end def feature_image(featurer, %Image{} = image) do @@ -492,6 +508,16 @@ defmodule Philomena.Images do Repo.update_all(where(Tag, [t], t.id in ^added_tags), inc: [images_count: added_count]) Repo.update_all(where(Tag, [t], t.id in ^removed_tags), inc: [images_count: -removed_count]) end) + |> case do + {:ok, _} = result -> + reindex_images(image_ids) + Tags.reindex_tags(added_tags ++ removed_tags) + + result + + result -> + result + end end @doc """ diff --git a/lib/philomena/reports.ex b/lib/philomena/reports.ex index 9fd504aa..1a4ba9b2 100644 --- a/lib/philomena/reports.ex +++ b/lib/philomena/reports.ex @@ -56,6 +56,7 @@ defmodule Philomena.Reports do %Report{reportable_id: reportable_id, reportable_type: reportable_type} |> Report.creation_changeset(attrs, attribution) |> Repo.insert() + |> maybe_reindex_report() end @doc """ @@ -74,6 +75,7 @@ defmodule Philomena.Reports do report |> Report.changeset(attrs) |> Repo.update() + |> maybe_reindex_report() end @doc """ @@ -109,18 +111,21 @@ defmodule Philomena.Reports do report |> Report.claim_changeset(user) |> Repo.update() + |> maybe_reindex_report() end def unclaim_report(%Report{} = report) do report |> Report.unclaim_changeset() |> Repo.update() + |> maybe_reindex_report() end def close_report(%Report{} = report, user) do report |> Report.close_changeset(user) |> Repo.update() + |> maybe_reindex_report() end def user_name_reindex(old_name, new_name) do @@ -129,6 +134,13 @@ defmodule Philomena.Reports do Elasticsearch.update_by_query(Report, data.query, data.set_replacements, data.replacements) end + defp maybe_reindex_report({:ok, report} = result) do + reindex_report(report) + + result + end + defp maybe_reindex_report(result), do: result + def reindex_reports(report_ids) do spawn(fn -> Report diff --git a/lib/philomena_web/controllers/admin/batch/tag_controller.ex b/lib/philomena_web/controllers/admin/batch/tag_controller.ex index 4e8563a9..7287bed6 100644 --- a/lib/philomena_web/controllers/admin/batch/tag_controller.ex +++ b/lib/philomena_web/controllers/admin/batch/tag_controller.ex @@ -2,7 +2,6 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do use PhilomenaWeb, :controller alias Philomena.Tags.Tag - alias Philomena.Tags alias Philomena.Images alias Philomena.Repo import Ecto.Query @@ -47,9 +46,6 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do case Images.batch_update(image_ids, added_tags, removed_tags, attributes) do {:ok, _} -> - Images.reindex_images(image_ids) - Tags.reindex_tags(added_tags ++ removed_tags) - json(conn, %{succeeded: image_ids, failed: []}) _error -> diff --git a/lib/philomena_web/controllers/admin/report/claim_controller.ex b/lib/philomena_web/controllers/admin/report/claim_controller.ex index c31e8dad..93880a22 100644 --- a/lib/philomena_web/controllers/admin/report/claim_controller.ex +++ b/lib/philomena_web/controllers/admin/report/claim_controller.ex @@ -9,9 +9,7 @@ defmodule PhilomenaWeb.Admin.Report.ClaimController do def create(conn, _params) do case Reports.claim_report(conn.assigns.report, conn.assigns.current_user) do - {:ok, report} -> - Reports.reindex_report(report) - + {:ok, _report} -> conn |> put_flash(:info, "Successfully marked report as in progress") |> redirect(to: Routes.admin_report_path(conn, :index)) @@ -25,7 +23,6 @@ defmodule PhilomenaWeb.Admin.Report.ClaimController do def delete(conn, _params) do {:ok, report} = Reports.unclaim_report(conn.assigns.report) - Reports.reindex_report(report) conn |> put_flash(:info, "Successfully released report.") diff --git a/lib/philomena_web/controllers/admin/report/close_controller.ex b/lib/philomena_web/controllers/admin/report/close_controller.ex index 2ce98b1d..dedb4d2d 100644 --- a/lib/philomena_web/controllers/admin/report/close_controller.ex +++ b/lib/philomena_web/controllers/admin/report/close_controller.ex @@ -8,8 +8,7 @@ defmodule PhilomenaWeb.Admin.Report.CloseController do plug :load_and_authorize_resource, model: Report, id_name: "report_id", persisted: true def create(conn, _params) do - {:ok, report} = Reports.close_report(conn.assigns.report, conn.assigns.current_user) - Reports.reindex_report(report) + {:ok, _report} = Reports.close_report(conn.assigns.report, conn.assigns.current_user) conn |> put_flash(:info, "Successfully closed report") diff --git a/lib/philomena_web/controllers/api/json/image_controller.ex b/lib/philomena_web/controllers/api/json/image_controller.ex index fa908f9d..0ddc1f51 100644 --- a/lib/philomena_web/controllers/api/json/image_controller.ex +++ b/lib/philomena_web/controllers/api/json/image_controller.ex @@ -5,8 +5,6 @@ defmodule PhilomenaWeb.Api.Json.ImageController do alias Philomena.Images alias Philomena.Interactions alias Philomena.Repo - alias Philomena.Tags - alias Philomena.UserStatistics import Ecto.Query plug :set_scraper_cache @@ -39,20 +37,10 @@ defmodule PhilomenaWeb.Api.Json.ImageController do end def create(conn, %{"image" => image_params}) do - user = conn.assigns.current_user attributes = conn.assigns.attributes case Images.create_image(attributes, image_params) do {:ok, %{image: image}} -> - spawn(fn -> - Images.repair_image(image) - end) - - # ImageProcessor.cast(image.id) - Images.reindex_image(image) - Tags.reindex_tags(image.added_tags) - UserStatistics.inc_stat(user, :uploads) - render(conn, "show.json", image: image, interactions: []) {:error, :image, changeset, _} -> diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 8df0c085..1e363e97 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -13,11 +13,8 @@ defmodule PhilomenaWeb.ImageController do Galleries.Gallery } - # alias Philomena.Servers.ImageProcessor - alias Philomena.UserStatistics alias Philomena.Interactions alias Philomena.Comments - alias Philomena.Tags alias Philomena.Repo import Ecto.Query @@ -111,15 +108,6 @@ defmodule PhilomenaWeb.ImageController do case Images.create_image(attributes, image_params) do {:ok, %{image: image}} -> - spawn(fn -> - Images.repair_image(image) - end) - - # ImageProcessor.cast(image.id) - Images.reindex_image(image) - Tags.reindex_tags(image.added_tags) - UserStatistics.inc_stat(conn.assigns.current_user, :uploads) - conn |> put_flash(:info, "Image created successfully.") |> redirect(to: Routes.image_path(conn, :show, image))