From 75be0794c02e7789cdace17f50f8a98a32ce04a2 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 12 Dec 2020 17:06:26 -0500 Subject: [PATCH] remove controller reindex from posts, tags, reports --- lib/philomena/posts.ex | 39 ++++++++++++++----- lib/philomena/reports.ex | 18 +++++---- lib/philomena/tags.ex | 9 +++++ lib/philomena/topics.ex | 9 +++++ .../controllers/report_controller.ex | 2 - .../controllers/tag_controller.ex | 2 - .../topic/post/delete_controller.ex | 2 - .../controllers/topic/post_controller.ex | 3 -- .../controllers/topic_controller.ex | 1 - 9 files changed, 58 insertions(+), 27 deletions(-) diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index 4406d9e9..c8484de9 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -92,6 +92,15 @@ defmodule Philomena.Posts do Topics.create_subscription(topic, attributes[:user]) end) |> Repo.transaction() + |> case do + {:ok, %{post: post}} = result -> + reindex_post(post) + + result + + error -> + error + end end def notify_post(post) do @@ -152,6 +161,15 @@ defmodule Philomena.Posts do }) end) |> Repo.transaction() + |> case do + {:ok, %{post: post}} = result -> + reindex_post(post) + + result + + error -> + error + end end @doc """ @@ -199,21 +217,14 @@ defmodule Philomena.Posts do post |> Post.unhide_changeset() |> Repo.update() - |> case do - {:ok, post} -> - reindex_post(post) - - {:ok, post} - - error -> - error - end + |> reindex_after_update() end def destroy_post(%Post{} = post) do post |> Post.destroy_changeset() |> Repo.update() + |> reindex_after_update() end @doc """ @@ -235,6 +246,16 @@ defmodule Philomena.Posts do Elasticsearch.update_by_query(Post, data.query, data.set_replacements, data.replacements) end + defp reindex_after_update({:ok, post}) do + reindex_post(post) + + {:ok, post} + end + + defp reindex_after_update(result) do + result + end + def reindex_post(%Post{} = post) do Exq.enqueue(Exq, "indexing", IndexWorker, ["Posts", "id", [post.id]]) diff --git a/lib/philomena/reports.ex b/lib/philomena/reports.ex index 3df39ce0..904222ca 100644 --- a/lib/philomena/reports.ex +++ b/lib/philomena/reports.ex @@ -57,7 +57,7 @@ defmodule Philomena.Reports do %Report{reportable_id: reportable_id, reportable_type: reportable_type} |> Report.creation_changeset(attrs, attribution) |> Repo.insert() - |> maybe_reindex_report() + |> reindex_after_update() end @doc """ @@ -76,7 +76,7 @@ defmodule Philomena.Reports do report |> Report.changeset(attrs) |> Repo.update() - |> maybe_reindex_report() + |> reindex_after_update() end @doc """ @@ -112,21 +112,21 @@ defmodule Philomena.Reports do report |> Report.claim_changeset(user) |> Repo.update() - |> maybe_reindex_report() + |> reindex_after_update() end def unclaim_report(%Report{} = report) do report |> Report.unclaim_changeset() |> Repo.update() - |> maybe_reindex_report() + |> reindex_after_update() end def close_report(%Report{} = report, user) do report |> Report.close_changeset(user) |> Repo.update() - |> maybe_reindex_report() + |> reindex_after_update() end def user_name_reindex(old_name, new_name) do @@ -135,13 +135,15 @@ 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 + defp reindex_after_update({:ok, report}) do reindex_report(report) - result + {:ok, report} end - defp maybe_reindex_report(result), do: result + defp reindex_after_update(result) do + result + end def reindex_reports(report_ids) do Exq.enqueue(Exq, "indexing", IndexWorker, ["Reports", "id", report_ids]) diff --git a/lib/philomena/tags.ex b/lib/philomena/tags.ex index b5e95dcd..43a9d424 100644 --- a/lib/philomena/tags.ex +++ b/lib/philomena/tags.ex @@ -121,6 +121,15 @@ defmodule Philomena.Tags do tag |> Tag.changeset(attrs, implied_tags) |> Repo.update() + |> case do + {:ok, tag} -> + reindex_tag(tag) + + {:ok, tag} + + error -> + error + end end def update_tag_image(%Tag{} = tag, attrs) do diff --git a/lib/philomena/topics.ex b/lib/philomena/topics.ex index 5d645d4e..fae15f4a 100644 --- a/lib/philomena/topics.ex +++ b/lib/philomena/topics.ex @@ -73,6 +73,15 @@ defmodule Philomena.Topics do create_subscription(topic, attribution[:user]) end) |> Repo.transaction() + |> case do + {:ok, %{topic: topic}} = result -> + Posts.reindex_post(hd(topic.posts)) + + result + + error -> + error + end end def notify_topic(topic, post) do diff --git a/lib/philomena_web/controllers/report_controller.ex b/lib/philomena_web/controllers/report_controller.ex index 0252582e..7f1ac907 100644 --- a/lib/philomena_web/controllers/report_controller.ex +++ b/lib/philomena_web/controllers/report_controller.ex @@ -48,8 +48,6 @@ defmodule PhilomenaWeb.ReportController do _falsy -> case Reports.create_report(reportable.id, reportable_type, attribution, report_params) do {:ok, report} -> - Reports.reindex_report(report) - conn |> put_flash( :info, diff --git a/lib/philomena_web/controllers/tag_controller.ex b/lib/philomena_web/controllers/tag_controller.ex index 66f08e95..7f55bcf7 100644 --- a/lib/philomena_web/controllers/tag_controller.ex +++ b/lib/philomena_web/controllers/tag_controller.ex @@ -95,8 +95,6 @@ defmodule PhilomenaWeb.TagController do def update(conn, %{"tag" => tag_params}) do case Tags.update_tag(conn.assigns.tag, tag_params) do {:ok, tag} -> - Tags.reindex_tag(tag) - conn |> put_flash(:info, "Tag successfully updated.") |> redirect(to: Routes.tag_path(conn, :show, tag)) diff --git a/lib/philomena_web/controllers/topic/post/delete_controller.ex b/lib/philomena_web/controllers/topic/post/delete_controller.ex index 726a4a32..2bcc009b 100644 --- a/lib/philomena_web/controllers/topic/post/delete_controller.ex +++ b/lib/philomena_web/controllers/topic/post/delete_controller.ex @@ -17,8 +17,6 @@ defmodule PhilomenaWeb.Topic.Post.DeleteController do case Posts.destroy_post(post) do {:ok, post} -> - Posts.reindex_post(post) - conn |> put_flash(:info, "Post successfully destroyed!") |> redirect( diff --git a/lib/philomena_web/controllers/topic/post_controller.ex b/lib/philomena_web/controllers/topic/post_controller.ex index 1606b61c..a847ba71 100644 --- a/lib/philomena_web/controllers/topic/post_controller.ex +++ b/lib/philomena_web/controllers/topic/post_controller.ex @@ -36,7 +36,6 @@ defmodule PhilomenaWeb.Topic.PostController do case Posts.create_post(topic, attributes, post_params) do {:ok, %{post: post}} -> Posts.notify_post(post) - Posts.reindex_post(post) UserStatistics.inc_stat(conn.assigns.current_user, :forum_posts) if forum.access_level == "normal" do @@ -77,8 +76,6 @@ defmodule PhilomenaWeb.Topic.PostController do case Posts.update_post(post, user, post_params) do {:ok, _post} -> - Posts.reindex_post(post) - conn |> put_flash(:info, "Post successfully edited.") |> redirect( diff --git a/lib/philomena_web/controllers/topic_controller.ex b/lib/philomena_web/controllers/topic_controller.ex index 4b411061..e9eeea3d 100644 --- a/lib/philomena_web/controllers/topic_controller.ex +++ b/lib/philomena_web/controllers/topic_controller.ex @@ -112,7 +112,6 @@ defmodule PhilomenaWeb.TopicController do case Topics.create_topic(forum, attributes, topic_params) do {:ok, %{topic: topic}} -> post = hd(topic.posts) - Posts.reindex_post(post) Topics.notify_topic(topic, post) if forum.access_level == "normal" do