mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-02 15:48:00 +01:00
autoclose reports when reportable item is deleted
This commit is contained in:
parent
2f3efe701e
commit
10707cd92d
7 changed files with 60 additions and 19 deletions
|
@ -7,6 +7,7 @@ defmodule Philomena.Comments do
|
||||||
alias Ecto.Multi
|
alias Ecto.Multi
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
|
|
||||||
|
alias Philomena.Reports.Report
|
||||||
alias Philomena.Comments.Comment
|
alias Philomena.Comments.Comment
|
||||||
alias Philomena.Images.Image
|
alias Philomena.Images.Image
|
||||||
alias Philomena.Images
|
alias Philomena.Images
|
||||||
|
@ -135,9 +136,18 @@ defmodule Philomena.Comments do
|
||||||
end
|
end
|
||||||
|
|
||||||
def hide_comment(%Comment{} = comment, attrs, user) do
|
def hide_comment(%Comment{} = comment, attrs, user) do
|
||||||
comment
|
reports =
|
||||||
|> Comment.hide_changeset(attrs, user)
|
Report
|
||||||
|> Repo.update()
|
|> 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
|
end
|
||||||
|
|
||||||
def unhide_comment(%Comment{} = comment) do
|
def unhide_comment(%Comment{} = comment) do
|
||||||
|
|
|
@ -19,6 +19,7 @@ defmodule Philomena.Images do
|
||||||
alias Philomena.Tags.Tag
|
alias Philomena.Tags.Tag
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.Interactions
|
alias Philomena.Interactions
|
||||||
|
alias Philomena.Reports.Report
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single image.
|
Gets a single image.
|
||||||
|
@ -298,13 +299,13 @@ defmodule Philomena.Images do
|
||||||
|
|
||||||
def hide_image(%Image{} = image, user, attrs) do
|
def hide_image(%Image{} = image, user, attrs) do
|
||||||
Image.hide_changeset(image, attrs, user)
|
Image.hide_changeset(image, attrs, user)
|
||||||
|> internal_hide_image()
|
|> internal_hide_image(image)
|
||||||
end
|
end
|
||||||
|
|
||||||
def merge_image(%Image{} = image, duplicate_of_image) do
|
def merge_image(%Image{} = image, duplicate_of_image) do
|
||||||
result =
|
result =
|
||||||
Image.merge_changeset(image, duplicate_of_image)
|
Image.merge_changeset(image, duplicate_of_image)
|
||||||
|> internal_hide_image()
|
|> internal_hide_image(image)
|
||||||
|
|
||||||
case result do
|
case result do
|
||||||
{:ok, _changes} ->
|
{:ok, _changes} ->
|
||||||
|
@ -316,9 +317,16 @@ defmodule Philomena.Images do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp internal_hide_image(changeset) do
|
defp internal_hide_image(changeset, image) do
|
||||||
Multi.new
|
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(:image, changeset)
|
||||||
|
|> Multi.update_all(:reports, reports, [])
|
||||||
|> Multi.run(:tags, fn repo, %{image: image} ->
|
|> Multi.run(:tags, fn repo, %{image: image} ->
|
||||||
image = Repo.preload(image, :tags, force: true)
|
image = Repo.preload(image, :tags, force: true)
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ defmodule Philomena.Posts do
|
||||||
alias Philomena.Forums.Forum
|
alias Philomena.Forums.Forum
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.Versions
|
alias Philomena.Versions
|
||||||
|
alias Philomena.Reports.Report
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single post.
|
Gets a single post.
|
||||||
|
@ -161,9 +162,18 @@ defmodule Philomena.Posts do
|
||||||
end
|
end
|
||||||
|
|
||||||
def hide_post(%Post{} = post, attrs, user) do
|
def hide_post(%Post{} = post, attrs, user) do
|
||||||
post
|
reports =
|
||||||
|> Post.hide_changeset(attrs, user)
|
Report
|
||||||
|> Repo.update()
|
|> 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
|
end
|
||||||
|
|
||||||
def unhide_post(%Post{} = post) do
|
def unhide_post(%Post{} = post) do
|
||||||
|
|
|
@ -121,17 +121,22 @@ defmodule Philomena.Reports do
|
||||||
|> Repo.update()
|
|> Repo.update()
|
||||||
end
|
end
|
||||||
|
|
||||||
def reindex_report(%Report{} = report) do
|
def reindex_reports(report_ids) do
|
||||||
spawn fn ->
|
spawn fn ->
|
||||||
Report
|
Report
|
||||||
|> where(id: ^report.id)
|
|> where([r], r.id in ^report_ids)
|
||||||
|> preload([:user, :admin])
|
|> preload([:user, :admin])
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
|
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
|
||||||
|> hd()
|
|> Enum.map(&Report.index_document/1)
|
||||||
|> Report.index_document()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
report_ids
|
||||||
|
end
|
||||||
|
|
||||||
|
def reindex_report(%Report{} = report) do
|
||||||
|
reindex_reports([report.id])
|
||||||
|
|
||||||
report
|
report
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Image.Comment.HideController do
|
||||||
|
|
||||||
alias Philomena.Comments.Comment
|
alias Philomena.Comments.Comment
|
||||||
alias Philomena.Comments
|
alias Philomena.Comments
|
||||||
|
alias Philomena.Reports
|
||||||
|
|
||||||
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
||||||
plug :load_and_authorize_resource, model: Comment, id_name: "comment_id", persisted: true
|
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
|
user = conn.assigns.current_user
|
||||||
|
|
||||||
case Comments.hide_comment(comment, comment_params, user) do
|
case Comments.hide_comment(comment, comment_params, user) do
|
||||||
{:ok, comment} ->
|
{:ok, %{comment: comment, reports: {_count, reports}}} ->
|
||||||
Comments.reindex_comment(comment)
|
Comments.reindex_comment(comment)
|
||||||
|
Reports.reindex_reports(reports)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Comment successfully hidden!")
|
|> put_flash(:info, "Comment successfully hidden!")
|
||||||
|> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}")
|
|> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}")
|
||||||
{:error, _changeset} ->
|
|
||||||
|
_error ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to hide comment!")
|
|> put_flash(:error, "Unable to hide comment!")
|
||||||
|> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}")
|
|> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}")
|
||||||
|
@ -35,6 +38,7 @@ defmodule PhilomenaWeb.Image.Comment.HideController do
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Comment successfully unhidden!")
|
|> put_flash(:info, "Comment successfully unhidden!")
|
||||||
|> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}")
|
|> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}")
|
||||||
|
|
||||||
{:error, _changeset} ->
|
{:error, _changeset} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Unable to unhide comment!")
|
|> put_flash(:error, "Unable to unhide comment!")
|
||||||
|
|
|
@ -7,6 +7,7 @@ defmodule PhilomenaWeb.Image.DeleteController do
|
||||||
alias Philomena.Images.Image
|
alias Philomena.Images.Image
|
||||||
alias Philomena.Images
|
alias Philomena.Images
|
||||||
alias Philomena.Tags
|
alias Philomena.Tags
|
||||||
|
alias Philomena.Reports
|
||||||
|
|
||||||
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
||||||
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
|
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
|
user = conn.assigns.current_user
|
||||||
|
|
||||||
case Images.hide_image(image, user, image_params) do
|
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)
|
Images.reindex_image(image)
|
||||||
|
Reports.reindex_reports(reports)
|
||||||
Tags.reindex_tags(tags)
|
Tags.reindex_tags(tags)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|
|
|
@ -2,6 +2,7 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
||||||
use PhilomenaWeb, :controller
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
alias Philomena.Posts.Post
|
alias Philomena.Posts.Post
|
||||||
|
alias Philomena.Reports
|
||||||
alias Philomena.Posts
|
alias Philomena.Posts
|
||||||
|
|
||||||
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
||||||
|
@ -12,8 +13,9 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
|
||||||
user = conn.assigns.current_user
|
user = conn.assigns.current_user
|
||||||
|
|
||||||
case Posts.hide_post(post, post_params, user) do
|
case Posts.hide_post(post, post_params, user) do
|
||||||
{:ok, post} ->
|
{:ok, %{post: post, reports: {_count, reports}}} ->
|
||||||
Posts.reindex_post(post)
|
Posts.reindex_post(post)
|
||||||
|
Reports.reindex_reports(reports)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Post successfully hidden!")
|
|> put_flash(:info, "Post successfully hidden!")
|
||||||
|
|
Loading…
Reference in a new issue