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.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 """

View file

@ -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

View file

@ -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 ->

View file

@ -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.")

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
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")

View file

@ -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, _} ->

View file

@ -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))