autoclose reports when reportable item is deleted

This commit is contained in:
byte[] 2019-12-17 11:45:22 -05:00
parent 2f3efe701e
commit 10707cd92d
7 changed files with 60 additions and 19 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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