diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index 5c1258cd..5d8d2399 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -15,6 +15,7 @@ defmodule Philomena.Comments do alias Philomena.Images alias Philomena.Notifications alias Philomena.Versions + alias Philomena.Reports @doc """ Gets a single comment. @@ -141,7 +142,7 @@ defmodule Philomena.Comments do Report |> where(reportable_type: "Comment", reportable_id: ^comment.id) |> select([r], r.id) - |> update(set: [open: false, state: "closed"]) + |> update(set: [open: false, state: "closed", admin_id: ^user.id]) comment = Comment.hide_changeset(comment, attrs, user) @@ -149,12 +150,31 @@ defmodule Philomena.Comments do |> Multi.update(:comment, comment) |> Multi.update_all(:reports, reports, []) |> Repo.transaction() + |> case do + {:ok, %{comment: comment, reports: {_count, reports}}} -> + Reports.reindex_reports(reports) + reindex_comment(comment) + + {:ok, comment} + + error -> + error + end end def unhide_comment(%Comment{} = comment) do comment |> Comment.unhide_changeset() |> Repo.update() + |> case do + {:ok, comment} -> + reindex_comment(comment) + + {:ok, comment} + + error -> + error + end end def destroy_comment(%Comment{} = comment) do diff --git a/lib/philomena/duplicate_reports.ex b/lib/philomena/duplicate_reports.ex index 754f015c..e36b1cc5 100644 --- a/lib/philomena/duplicate_reports.ex +++ b/lib/philomena/duplicate_reports.ex @@ -96,7 +96,7 @@ defmodule Philomena.DuplicateReports do multi |> Multi.update(:duplicate_report, changeset) |> Multi.update_all(:other_reports, other_duplicate_reports, []) - |> Images.merge_image(duplicate_report.image, duplicate_report.duplicate_of_image) + |> Images.merge_image(duplicate_report.image, duplicate_report.duplicate_of_image, user) end def accept_reverse_duplicate_report(%DuplicateReport{} = duplicate_report, user) do diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index 183ff619..1df4d680 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -390,18 +390,18 @@ defmodule Philomena.Images do image |> Image.hide_changeset(attrs, user) - |> hide_image_multi(image, Ecto.Multi.new()) + |> hide_image_multi(image, user, Multi.new()) |> Multi.update_all(:duplicate_reports, duplicate_reports, []) |> Repo.transaction() |> process_after_hide() end - def merge_image(multi \\ nil, %Image{} = image, duplicate_of_image) do + def merge_image(multi \\ nil, %Image{} = image, duplicate_of_image, user) do multi = multi || Multi.new() image |> Image.merge_changeset(duplicate_of_image) - |> hide_image_multi(image, multi) + |> hide_image_multi(image, user, multi) |> Multi.run(:first_seen_at, fn _, %{} -> update_first_seen_at( duplicate_of_image, @@ -435,12 +435,12 @@ defmodule Philomena.Images do end end - defp hide_image_multi(changeset, image, multi) do + defp hide_image_multi(changeset, image, user, multi) do reports = Report |> where(reportable_type: "Image", reportable_id: ^image.id) |> select([r], r.id) - |> update(set: [open: false, state: "closed"]) + |> update(set: [open: false, state: "closed", admin_id: ^user.id]) multi |> Multi.update(:image, changeset) diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index 13c8678c..9a0ebb34 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -15,6 +15,7 @@ defmodule Philomena.Posts do alias Philomena.Forums.Forum alias Philomena.Notifications alias Philomena.Versions + alias Philomena.Reports alias Philomena.Reports.Report @doc """ @@ -170,7 +171,7 @@ defmodule Philomena.Posts do Report |> where(reportable_type: "Post", reportable_id: ^post.id) |> select([r], r.id) - |> update(set: [open: false, state: "closed"]) + |> update(set: [open: false, state: "closed", admin_id: ^user.id]) post = Post.hide_changeset(post, attrs, user) @@ -178,12 +179,29 @@ defmodule Philomena.Posts do |> Multi.update(:post, post) |> Multi.update_all(:reports, reports, []) |> Repo.transaction() + |> case do + {:ok, %{post: post, reports: {_count, reports}}} -> + Reports.reindex_reports(reports) + reindex_post(post) + + {:ok, post} + + error -> + error + end end def unhide_post(%Post{} = post) do post |> Post.unhide_changeset() |> Repo.update() + |> case do + {:ok, post} -> + reindex_post(post) + + error -> + error + end end def destroy_post(%Post{} = post) do diff --git a/lib/philomena_web/controllers/image/comment/hide_controller.ex b/lib/philomena_web/controllers/image/comment/hide_controller.ex index 5f2aba09..0bf32be0 100644 --- a/lib/philomena_web/controllers/image/comment/hide_controller.ex +++ b/lib/philomena_web/controllers/image/comment/hide_controller.ex @@ -3,7 +3,6 @@ defmodule PhilomenaWeb.Image.Comment.HideController do alias Philomena.Comments.Comment alias Philomena.Comments - alias Philomena.Reports plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide plug :load_and_authorize_resource, model: Comment, id_name: "comment_id", persisted: true @@ -13,10 +12,7 @@ defmodule PhilomenaWeb.Image.Comment.HideController do user = conn.assigns.current_user case Comments.hide_comment(comment, comment_params, user) do - {:ok, %{comment: comment, reports: {_count, reports}}} -> - Comments.reindex_comment(comment) - Reports.reindex_reports(reports) - + {:ok, comment} -> conn |> put_flash(:info, "Comment successfully hidden!") |> redirect( @@ -37,8 +33,6 @@ defmodule PhilomenaWeb.Image.Comment.HideController do case Comments.unhide_comment(comment) do {:ok, comment} -> - Comments.reindex_comment(comment) - conn |> put_flash(:info, "Comment successfully unhidden!") |> redirect( diff --git a/lib/philomena_web/controllers/topic/post/hide_controller.ex b/lib/philomena_web/controllers/topic/post/hide_controller.ex index 3779d358..68129468 100644 --- a/lib/philomena_web/controllers/topic/post/hide_controller.ex +++ b/lib/philomena_web/controllers/topic/post/hide_controller.ex @@ -2,7 +2,6 @@ defmodule PhilomenaWeb.Topic.Post.HideController do use PhilomenaWeb, :controller alias Philomena.Posts.Post - alias Philomena.Reports alias Philomena.Posts plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide @@ -18,12 +17,9 @@ defmodule PhilomenaWeb.Topic.Post.HideController do user = conn.assigns.current_user case Posts.hide_post(post, post_params, user) do - {:ok, %{post: post, reports: {_count, reports}}} -> - Posts.reindex_post(post) - Reports.reindex_reports(reports) - + {:ok, post} -> conn - |> put_flash(:info, "Post successfully hidden!") + |> put_flash(:info, "Post successfully hidden.") |> redirect( to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <> @@ -46,10 +42,8 @@ defmodule PhilomenaWeb.Topic.Post.HideController do case Posts.unhide_post(post) do {:ok, post} -> - Posts.reindex_post(post) - conn - |> put_flash(:info, "Post successfully unhidden!") + |> put_flash(:info, "Post successfully unhidden.") |> redirect( to: Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>