From 10707cd92d5852190d6c897b337aff2fc33f3c4e Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Tue, 17 Dec 2019 11:45:22 -0500 Subject: [PATCH] autoclose reports when reportable item is deleted --- lib/philomena/comments.ex | 16 +++++++++++++--- lib/philomena/images.ex | 16 ++++++++++++---- lib/philomena/posts.ex | 16 +++++++++++++--- lib/philomena/reports.ex | 13 +++++++++---- .../controllers/image/comment/hide_controller.ex | 8 ++++++-- .../controllers/image/delete_controller.ex | 6 ++++-- .../controllers/topic/post/hide_controller.ex | 4 +++- 7 files changed, 60 insertions(+), 19 deletions(-) diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index dc8171d9..9ed501a7 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -7,6 +7,7 @@ defmodule Philomena.Comments do alias Ecto.Multi alias Philomena.Repo + alias Philomena.Reports.Report alias Philomena.Comments.Comment alias Philomena.Images.Image alias Philomena.Images @@ -135,9 +136,18 @@ defmodule Philomena.Comments do end def hide_comment(%Comment{} = comment, attrs, user) do - comment - |> Comment.hide_changeset(attrs, user) - |> Repo.update() + reports = + Report + |> where(reportable_type: "Comment", reportable_id: ^comment.id) + |> select([r], r.id) + |> update(set: [open: false, state: "closed"]) + + comment = Comment.hide_changeset(comment, attrs, user) + + Multi.new() + |> Multi.update(:comment, comment) + |> Multi.update_all(:reports, reports, []) + |> Repo.transaction() end def unhide_comment(%Comment{} = comment) do diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index c0fb09fc..d5e48665 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -19,6 +19,7 @@ defmodule Philomena.Images do alias Philomena.Tags.Tag alias Philomena.Notifications alias Philomena.Interactions + alias Philomena.Reports.Report @doc """ Gets a single image. @@ -298,13 +299,13 @@ defmodule Philomena.Images do def hide_image(%Image{} = image, user, attrs) do Image.hide_changeset(image, attrs, user) - |> internal_hide_image() + |> internal_hide_image(image) end def merge_image(%Image{} = image, duplicate_of_image) do result = Image.merge_changeset(image, duplicate_of_image) - |> internal_hide_image() + |> internal_hide_image(image) case result do {:ok, _changes} -> @@ -316,9 +317,16 @@ defmodule Philomena.Images do end end - defp internal_hide_image(changeset) do - Multi.new + defp internal_hide_image(changeset, image) do + reports = + Report + |> where(reportable_type: "Image", reportable_id: ^image.id) + |> select([r], r.id) + |> update(set: [open: false, state: "closed"]) + + Multi.new() |> Multi.update(:image, changeset) + |> Multi.update_all(:reports, reports, []) |> Multi.run(:tags, fn repo, %{image: image} -> image = Repo.preload(image, :tags, force: true) diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index 35345ce7..8ae46a20 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -13,6 +13,7 @@ defmodule Philomena.Posts do alias Philomena.Forums.Forum alias Philomena.Notifications alias Philomena.Versions + alias Philomena.Reports.Report @doc """ Gets a single post. @@ -161,9 +162,18 @@ defmodule Philomena.Posts do end def hide_post(%Post{} = post, attrs, user) do - post - |> Post.hide_changeset(attrs, user) - |> Repo.update() + reports = + Report + |> where(reportable_type: "Post", reportable_id: ^post.id) + |> select([r], r.id) + |> update(set: [open: false, state: "closed"]) + + post = Post.hide_changeset(post, attrs, user) + + Multi.new() + |> Multi.update(:post, post) + |> Multi.update_all(:reports, reports, []) + |> Repo.transaction() end def unhide_post(%Post{} = post) do diff --git a/lib/philomena/reports.ex b/lib/philomena/reports.ex index 5775c64b..88fb913a 100644 --- a/lib/philomena/reports.ex +++ b/lib/philomena/reports.ex @@ -121,17 +121,22 @@ defmodule Philomena.Reports do |> Repo.update() end - def reindex_report(%Report{} = report) do + def reindex_reports(report_ids) do spawn fn -> Report - |> where(id: ^report.id) + |> where([r], r.id in ^report_ids) |> preload([:user, :admin]) |> Repo.all() |> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type]) - |> hd() - |> Report.index_document() + |> Enum.map(&Report.index_document/1) end + report_ids + end + + def reindex_report(%Report{} = report) do + reindex_reports([report.id]) + report end diff --git a/lib/philomena_web/controllers/image/comment/hide_controller.ex b/lib/philomena_web/controllers/image/comment/hide_controller.ex index d1aecb24..20d83650 100644 --- a/lib/philomena_web/controllers/image/comment/hide_controller.ex +++ b/lib/philomena_web/controllers/image/comment/hide_controller.ex @@ -3,6 +3,7 @@ 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 @@ -12,13 +13,15 @@ defmodule PhilomenaWeb.Image.Comment.HideController do user = conn.assigns.current_user case Comments.hide_comment(comment, comment_params, user) do - {:ok, comment} -> + {:ok, %{comment: comment, reports: {_count, reports}}} -> Comments.reindex_comment(comment) + Reports.reindex_reports(reports) conn |> put_flash(:info, "Comment successfully hidden!") |> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}") - {:error, _changeset} -> + + _error -> conn |> put_flash(:error, "Unable to hide comment!") |> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}") @@ -35,6 +38,7 @@ defmodule PhilomenaWeb.Image.Comment.HideController do conn |> put_flash(:info, "Comment successfully unhidden!") |> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}") + {:error, _changeset} -> conn |> put_flash(:error, "Unable to unhide comment!") diff --git a/lib/philomena_web/controllers/image/delete_controller.ex b/lib/philomena_web/controllers/image/delete_controller.ex index 2a60f4a6..1531cc51 100644 --- a/lib/philomena_web/controllers/image/delete_controller.ex +++ b/lib/philomena_web/controllers/image/delete_controller.ex @@ -7,6 +7,7 @@ defmodule PhilomenaWeb.Image.DeleteController do alias Philomena.Images.Image alias Philomena.Images alias Philomena.Tags + alias Philomena.Reports plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true @@ -16,8 +17,9 @@ defmodule PhilomenaWeb.Image.DeleteController do user = conn.assigns.current_user case Images.hide_image(image, user, image_params) do - {:ok, %{image: image, tags: tags}} -> + {:ok, %{image: image, tags: tags, reports: {_count, reports}}} -> Images.reindex_image(image) + Reports.reindex_reports(reports) Tags.reindex_tags(tags) conn @@ -40,4 +42,4 @@ defmodule PhilomenaWeb.Image.DeleteController do |> put_flash(:info, "Image successfully unhidden.") |> redirect(to: Routes.image_path(conn, :show, image)) end -end \ No newline at end of file +end diff --git a/lib/philomena_web/controllers/topic/post/hide_controller.ex b/lib/philomena_web/controllers/topic/post/hide_controller.ex index 9725090f..3dc2c844 100644 --- a/lib/philomena_web/controllers/topic/post/hide_controller.ex +++ b/lib/philomena_web/controllers/topic/post/hide_controller.ex @@ -2,6 +2,7 @@ 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 @@ -12,8 +13,9 @@ defmodule PhilomenaWeb.Topic.Post.HideController do user = conn.assigns.current_user case Posts.hide_post(post, post_params, user) do - {:ok, post} -> + {:ok, %{post: post, reports: {_count, reports}}} -> Posts.reindex_post(post) + Reports.reindex_reports(reports) conn |> put_flash(:info, "Post successfully hidden!")