move reindex logic to context for reports, image creation

This commit is contained in:
byte[] 2020-05-07 23:02:48 -04:00
parent a1b8ed9d33
commit 84d784d33a
7 changed files with 40 additions and 34 deletions

View file

@ -19,6 +19,7 @@ defmodule Philomena.Images do
alias Philomena.SourceChanges.SourceChange alias Philomena.SourceChanges.SourceChange
alias Philomena.TagChanges.TagChange alias Philomena.TagChanges.TagChange
alias Philomena.Tags alias Philomena.Tags
alias Philomena.UserStatistics
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
alias Philomena.Notifications alias Philomena.Notifications
alias Philomena.Interactions alias Philomena.Interactions
@ -94,6 +95,21 @@ defmodule Philomena.Images do
{:ok, nil} {:ok, nil}
end) end)
|> Repo.isolated_transaction(:serializable) |> 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 end
def feature_image(featurer, %Image{} = image) do 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 ^added_tags), inc: [images_count: added_count])
Repo.update_all(where(Tag, [t], t.id in ^removed_tags), inc: [images_count: -removed_count]) Repo.update_all(where(Tag, [t], t.id in ^removed_tags), inc: [images_count: -removed_count])
end) end)
|> case do
{:ok, _} = result ->
reindex_images(image_ids)
Tags.reindex_tags(added_tags ++ removed_tags)
result
result ->
result
end
end end
@doc """ @doc """

View file

@ -56,6 +56,7 @@ defmodule Philomena.Reports do
%Report{reportable_id: reportable_id, reportable_type: reportable_type} %Report{reportable_id: reportable_id, reportable_type: reportable_type}
|> Report.creation_changeset(attrs, attribution) |> Report.creation_changeset(attrs, attribution)
|> Repo.insert() |> Repo.insert()
|> maybe_reindex_report()
end end
@doc """ @doc """
@ -74,6 +75,7 @@ defmodule Philomena.Reports do
report report
|> Report.changeset(attrs) |> Report.changeset(attrs)
|> Repo.update() |> Repo.update()
|> maybe_reindex_report()
end end
@doc """ @doc """
@ -109,18 +111,21 @@ defmodule Philomena.Reports do
report report
|> Report.claim_changeset(user) |> Report.claim_changeset(user)
|> Repo.update() |> Repo.update()
|> maybe_reindex_report()
end end
def unclaim_report(%Report{} = report) do def unclaim_report(%Report{} = report) do
report report
|> Report.unclaim_changeset() |> Report.unclaim_changeset()
|> Repo.update() |> Repo.update()
|> maybe_reindex_report()
end end
def close_report(%Report{} = report, user) do def close_report(%Report{} = report, user) do
report report
|> Report.close_changeset(user) |> Report.close_changeset(user)
|> Repo.update() |> Repo.update()
|> maybe_reindex_report()
end end
def user_name_reindex(old_name, new_name) do 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) Elasticsearch.update_by_query(Report, data.query, data.set_replacements, data.replacements)
end 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 def reindex_reports(report_ids) do
spawn(fn -> spawn(fn ->
Report Report

View file

@ -2,7 +2,6 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias Philomena.Tags.Tag alias Philomena.Tags.Tag
alias Philomena.Tags
alias Philomena.Images alias Philomena.Images
alias Philomena.Repo alias Philomena.Repo
import Ecto.Query 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 case Images.batch_update(image_ids, added_tags, removed_tags, attributes) do
{:ok, _} -> {:ok, _} ->
Images.reindex_images(image_ids)
Tags.reindex_tags(added_tags ++ removed_tags)
json(conn, %{succeeded: image_ids, failed: []}) json(conn, %{succeeded: image_ids, failed: []})
_error -> _error ->

View file

@ -9,9 +9,7 @@ defmodule PhilomenaWeb.Admin.Report.ClaimController do
def create(conn, _params) do def create(conn, _params) do
case Reports.claim_report(conn.assigns.report, conn.assigns.current_user) do case Reports.claim_report(conn.assigns.report, conn.assigns.current_user) do
{:ok, report} -> {:ok, _report} ->
Reports.reindex_report(report)
conn conn
|> put_flash(:info, "Successfully marked report as in progress") |> put_flash(:info, "Successfully marked report as in progress")
|> redirect(to: Routes.admin_report_path(conn, :index)) |> redirect(to: Routes.admin_report_path(conn, :index))
@ -25,7 +23,6 @@ defmodule PhilomenaWeb.Admin.Report.ClaimController do
def delete(conn, _params) do def delete(conn, _params) do
{:ok, report} = Reports.unclaim_report(conn.assigns.report) {:ok, report} = Reports.unclaim_report(conn.assigns.report)
Reports.reindex_report(report)
conn conn
|> put_flash(:info, "Successfully released report.") |> put_flash(:info, "Successfully released report.")

View file

@ -8,8 +8,7 @@ defmodule PhilomenaWeb.Admin.Report.CloseController do
plug :load_and_authorize_resource, model: Report, id_name: "report_id", persisted: true plug :load_and_authorize_resource, model: Report, id_name: "report_id", persisted: true
def create(conn, _params) do def create(conn, _params) do
{:ok, report} = Reports.close_report(conn.assigns.report, conn.assigns.current_user) {:ok, _report} = Reports.close_report(conn.assigns.report, conn.assigns.current_user)
Reports.reindex_report(report)
conn conn
|> put_flash(:info, "Successfully closed report") |> put_flash(:info, "Successfully closed report")

View file

@ -5,8 +5,6 @@ defmodule PhilomenaWeb.Api.Json.ImageController do
alias Philomena.Images alias Philomena.Images
alias Philomena.Interactions alias Philomena.Interactions
alias Philomena.Repo alias Philomena.Repo
alias Philomena.Tags
alias Philomena.UserStatistics
import Ecto.Query import Ecto.Query
plug :set_scraper_cache plug :set_scraper_cache
@ -39,20 +37,10 @@ defmodule PhilomenaWeb.Api.Json.ImageController do
end end
def create(conn, %{"image" => image_params}) do def create(conn, %{"image" => image_params}) do
user = conn.assigns.current_user
attributes = conn.assigns.attributes attributes = conn.assigns.attributes
case Images.create_image(attributes, image_params) do case Images.create_image(attributes, image_params) do
{:ok, %{image: image}} -> {: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: []) render(conn, "show.json", image: image, interactions: [])
{:error, :image, changeset, _} -> {:error, :image, changeset, _} ->

View file

@ -13,11 +13,8 @@ defmodule PhilomenaWeb.ImageController do
Galleries.Gallery Galleries.Gallery
} }
# alias Philomena.Servers.ImageProcessor
alias Philomena.UserStatistics
alias Philomena.Interactions alias Philomena.Interactions
alias Philomena.Comments alias Philomena.Comments
alias Philomena.Tags
alias Philomena.Repo alias Philomena.Repo
import Ecto.Query import Ecto.Query
@ -111,15 +108,6 @@ defmodule PhilomenaWeb.ImageController do
case Images.create_image(attributes, image_params) do case Images.create_image(attributes, image_params) do
{:ok, %{image: image}} -> {: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 conn
|> put_flash(:info, "Image created successfully.") |> put_flash(:info, "Image created successfully.")
|> redirect(to: Routes.image_path(conn, :show, image)) |> redirect(to: Routes.image_path(conn, :show, image))