remove controller reindex from posts, tags, reports

This commit is contained in:
byte[] 2020-12-12 17:06:26 -05:00
parent d567d5ffc0
commit 75be0794c0
9 changed files with 58 additions and 27 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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