mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
Merge pull request #335 from philomena-dev/report-close-query
Extract query for closing reports
This commit is contained in:
commit
0601625f27
5 changed files with 46 additions and 42 deletions
|
@ -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}}} ->
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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} ->
|
||||
|
|
|
@ -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}}} ->
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue