diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index f0ac4dc0..90569d6e 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -8,7 +8,6 @@ defmodule Philomena.Comments do alias Philomena.Repo alias PhilomenaQuery.Search - alias Philomena.Reports.Report alias Philomena.UserStatistics alias Philomena.Comments.Comment alias Philomena.Comments.SearchIndex, as: CommentIndex @@ -145,17 +144,12 @@ defmodule Philomena.Comments do end def hide_comment(%Comment{} = comment, attrs, user) do - reports = - Report - |> where(reportable_type: "Comment", reportable_id: ^comment.id) - |> select([r], r.id) - |> update(set: [open: false, state: "closed", admin_id: ^user.id]) - + report_query = Reports.close_report_query("Comment", comment.id, user) comment = Comment.hide_changeset(comment, attrs, user) Multi.new() |> Multi.update(:comment, comment) - |> Multi.update_all(:reports, reports, []) + |> Multi.update_all(:reports, report_query, []) |> Repo.transaction() |> case do {:ok, %{comment: comment, reports: {_count, reports}}} -> @@ -191,17 +185,12 @@ defmodule Philomena.Comments do end def approve_comment(%Comment{} = comment, user) do - reports = - Report - |> where(reportable_type: "Comment", reportable_id: ^comment.id) - |> select([r], r.id) - |> update(set: [open: false, state: "closed", admin_id: ^user.id]) - + report_query = Reports.close_report_query("Comment", comment.id, user) comment = Comment.approve_changeset(comment) Multi.new() |> Multi.update(:comment, comment) - |> Multi.update_all(:reports, reports, []) + |> Multi.update_all(:reports, report_query, []) |> Repo.transaction() |> case do {:ok, %{comment: comment, reports: {_count, reports}}} -> diff --git a/lib/philomena/conversations.ex b/lib/philomena/conversations.ex index 597b64f7..e9ed7500 100644 --- a/lib/philomena/conversations.ex +++ b/lib/philomena/conversations.ex @@ -7,7 +7,6 @@ defmodule Philomena.Conversations do alias Ecto.Multi alias Philomena.Repo alias Philomena.Reports - alias Philomena.Reports.Report alias Philomena.Conversations.Conversation @doc """ @@ -209,11 +208,7 @@ defmodule Philomena.Conversations do end def approve_conversation_message(message, user) do - reports_query = - Report - |> where(reportable_type: "Conversation", reportable_id: ^message.conversation_id) - |> select([r], r.id) - |> update(set: [open: false, state: "closed", admin_id: ^user.id]) + reports_query = Reports.close_report_query("Conversation", message.conversation_id, user) message_query = message diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index db95b09a..5fea2e35 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -31,7 +31,6 @@ defmodule Philomena.Images do alias Philomena.Notifications alias Philomena.Interactions alias Philomena.Reports - alias Philomena.Reports.Report alias Philomena.Comments alias Philomena.Galleries.Gallery alias Philomena.Galleries.Interaction @@ -578,11 +577,7 @@ defmodule Philomena.Images do end 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", admin_id: ^user.id]) + report_query = Reports.close_report_query("Image", image.id, user) galleries = Gallery @@ -593,7 +588,7 @@ defmodule Philomena.Images do multi |> Multi.update(:image, changeset) - |> Multi.update_all(:reports, reports, []) + |> Multi.update_all(:reports, report_query, []) |> Multi.update_all(:galleries, galleries, []) |> Multi.delete_all(:gallery_interactions, gallery_interactions, []) |> Multi.run(:tags, fn repo, %{image: image} -> diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index fa46f406..fc339c97 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -19,7 +19,6 @@ defmodule Philomena.Posts do alias Philomena.NotificationWorker alias Philomena.Versions alias Philomena.Reports - alias Philomena.Reports.Report @doc """ Gets a single post. @@ -204,11 +203,7 @@ defmodule Philomena.Posts do end def hide_post(%Post{} = post, attrs, user) do - reports = - Report - |> where(reportable_type: "Post", reportable_id: ^post.id) - |> select([r], r.id) - |> update(set: [open: false, state: "closed", admin_id: ^user.id]) + report_query = Reports.close_report_query("Post", post.id, user) topics = Topic @@ -224,7 +219,7 @@ defmodule Philomena.Posts do Multi.new() |> Multi.update(:post, post) - |> Multi.update_all(:reports, reports, []) + |> Multi.update_all(:reports, report_query, []) |> Multi.update_all(:topics, topics, []) |> Multi.update_all(:forums, forums, []) |> Repo.transaction() @@ -255,17 +250,12 @@ defmodule Philomena.Posts do end def approve_post(%Post{} = post, user) do - reports = - Report - |> where(reportable_type: "Post", reportable_id: ^post.id) - |> select([r], r.id) - |> update(set: [open: false, state: "closed", admin_id: ^user.id]) - + report_query = Reports.close_report_query("Post", post.id, user) post = Post.approve_changeset(post) Multi.new() |> Multi.update(:post, post) - |> Multi.update_all(:reports, reports, []) + |> Multi.update_all(:reports, report_query, []) |> Repo.transaction() |> case do {:ok, %{post: post, reports: {_count, reports}}} -> diff --git a/lib/philomena/reports.ex b/lib/philomena/reports.ex index 1639929d..4f10391d 100644 --- a/lib/philomena/reports.ex +++ b/lib/philomena/reports.ex @@ -60,6 +60,41 @@ defmodule Philomena.Reports do |> reindex_after_update() end + @doc """ + Returns an `m:Ecto.Query` which updates all reports for the given `reportable_type` + and `reportable_id` to close them. + + Because this is only a query due to the limitations of `m:Ecto.Multi`, this must be + coupled with an associated call to `reindex_reports/1` to operate correctly, e.g.: + + report_query = Reports.close_system_report_query("Image", image.id, user) + + Multi.new() + |> Multi.update_all(:reports, report_query, []) + |> Repo.transaction() + |> case do + {:ok, %{reports: {_count, reports}} = result} -> + Reports.reindex_reports(reports) + + {:ok, result} + + error -> + error + end + + ## Examples + + iex> close_system_report_query("Image", 1, %User{}) + #Ecto.Query<...> + + """ + def close_report_query(reportable_type, reportable_id, closing_user) do + from r in Report, + where: r.reportable_type == ^reportable_type and r.reportable_id == ^reportable_id, + select: r.id, + update: [set: [open: false, state: "closed", admin_id: ^closing_user.id]] + end + def create_system_report(reportable_id, reportable_type, category, reason) do attrs = %{ reason: reason,