mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
Extract query for closing reports
This commit is contained in:
parent
844e0a3535
commit
3f09125425
5 changed files with 46 additions and 42 deletions
|
@ -8,7 +8,6 @@ defmodule Philomena.Comments do
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
|
|
||||||
alias PhilomenaQuery.Search
|
alias PhilomenaQuery.Search
|
||||||
alias Philomena.Reports.Report
|
|
||||||
alias Philomena.UserStatistics
|
alias Philomena.UserStatistics
|
||||||
alias Philomena.Comments.Comment
|
alias Philomena.Comments.Comment
|
||||||
alias Philomena.Comments.SearchIndex, as: CommentIndex
|
alias Philomena.Comments.SearchIndex, as: CommentIndex
|
||||||
|
@ -145,17 +144,12 @@ defmodule Philomena.Comments do
|
||||||
end
|
end
|
||||||
|
|
||||||
def hide_comment(%Comment{} = comment, attrs, user) do
|
def hide_comment(%Comment{} = comment, attrs, user) do
|
||||||
reports =
|
report_query = Reports.close_report_query("Comment", comment.id, user)
|
||||||
Report
|
|
||||||
|> where(reportable_type: "Comment", reportable_id: ^comment.id)
|
|
||||||
|> select([r], r.id)
|
|
||||||
|> update(set: [open: false, state: "closed", admin_id: ^user.id])
|
|
||||||
|
|
||||||
comment = Comment.hide_changeset(comment, attrs, user)
|
comment = Comment.hide_changeset(comment, attrs, user)
|
||||||
|
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.update(:comment, comment)
|
|> Multi.update(:comment, comment)
|
||||||
|> Multi.update_all(:reports, reports, [])
|
|> Multi.update_all(:reports, report_query, [])
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{comment: comment, reports: {_count, reports}}} ->
|
{:ok, %{comment: comment, reports: {_count, reports}}} ->
|
||||||
|
@ -191,17 +185,12 @@ defmodule Philomena.Comments do
|
||||||
end
|
end
|
||||||
|
|
||||||
def approve_comment(%Comment{} = comment, user) do
|
def approve_comment(%Comment{} = comment, user) do
|
||||||
reports =
|
report_query = Reports.close_report_query("Comment", comment.id, user)
|
||||||
Report
|
|
||||||
|> where(reportable_type: "Comment", reportable_id: ^comment.id)
|
|
||||||
|> select([r], r.id)
|
|
||||||
|> update(set: [open: false, state: "closed", admin_id: ^user.id])
|
|
||||||
|
|
||||||
comment = Comment.approve_changeset(comment)
|
comment = Comment.approve_changeset(comment)
|
||||||
|
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.update(:comment, comment)
|
|> Multi.update(:comment, comment)
|
||||||
|> Multi.update_all(:reports, reports, [])
|
|> Multi.update_all(:reports, report_query, [])
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{comment: comment, reports: {_count, reports}}} ->
|
{:ok, %{comment: comment, reports: {_count, reports}}} ->
|
||||||
|
|
|
@ -7,7 +7,6 @@ defmodule Philomena.Conversations do
|
||||||
alias Ecto.Multi
|
alias Ecto.Multi
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
alias Philomena.Reports
|
alias Philomena.Reports
|
||||||
alias Philomena.Reports.Report
|
|
||||||
alias Philomena.Conversations.Conversation
|
alias Philomena.Conversations.Conversation
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -209,11 +208,7 @@ defmodule Philomena.Conversations do
|
||||||
end
|
end
|
||||||
|
|
||||||
def approve_conversation_message(message, user) do
|
def approve_conversation_message(message, user) do
|
||||||
reports_query =
|
reports_query = Reports.close_report_query("Conversation", message.conversation_id, user)
|
||||||
Report
|
|
||||||
|> where(reportable_type: "Conversation", reportable_id: ^message.conversation_id)
|
|
||||||
|> select([r], r.id)
|
|
||||||
|> update(set: [open: false, state: "closed", admin_id: ^user.id])
|
|
||||||
|
|
||||||
message_query =
|
message_query =
|
||||||
message
|
message
|
||||||
|
|
|
@ -31,7 +31,6 @@ defmodule Philomena.Images do
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.Interactions
|
alias Philomena.Interactions
|
||||||
alias Philomena.Reports
|
alias Philomena.Reports
|
||||||
alias Philomena.Reports.Report
|
|
||||||
alias Philomena.Comments
|
alias Philomena.Comments
|
||||||
alias Philomena.Galleries.Gallery
|
alias Philomena.Galleries.Gallery
|
||||||
alias Philomena.Galleries.Interaction
|
alias Philomena.Galleries.Interaction
|
||||||
|
@ -578,11 +577,7 @@ defmodule Philomena.Images do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp hide_image_multi(changeset, image, user, multi) do
|
defp hide_image_multi(changeset, image, user, multi) do
|
||||||
reports =
|
report_query = Reports.close_report_query("Image", image.id, user)
|
||||||
Report
|
|
||||||
|> where(reportable_type: "Image", reportable_id: ^image.id)
|
|
||||||
|> select([r], r.id)
|
|
||||||
|> update(set: [open: false, state: "closed", admin_id: ^user.id])
|
|
||||||
|
|
||||||
galleries =
|
galleries =
|
||||||
Gallery
|
Gallery
|
||||||
|
@ -593,7 +588,7 @@ defmodule Philomena.Images do
|
||||||
|
|
||||||
multi
|
multi
|
||||||
|> Multi.update(:image, changeset)
|
|> Multi.update(:image, changeset)
|
||||||
|> Multi.update_all(:reports, reports, [])
|
|> Multi.update_all(:reports, report_query, [])
|
||||||
|> Multi.update_all(:galleries, galleries, [])
|
|> Multi.update_all(:galleries, galleries, [])
|
||||||
|> Multi.delete_all(:gallery_interactions, gallery_interactions, [])
|
|> Multi.delete_all(:gallery_interactions, gallery_interactions, [])
|
||||||
|> Multi.run(:tags, fn repo, %{image: image} ->
|
|> Multi.run(:tags, fn repo, %{image: image} ->
|
||||||
|
|
|
@ -19,7 +19,6 @@ defmodule Philomena.Posts do
|
||||||
alias Philomena.NotificationWorker
|
alias Philomena.NotificationWorker
|
||||||
alias Philomena.Versions
|
alias Philomena.Versions
|
||||||
alias Philomena.Reports
|
alias Philomena.Reports
|
||||||
alias Philomena.Reports.Report
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single post.
|
Gets a single post.
|
||||||
|
@ -204,11 +203,7 @@ defmodule Philomena.Posts do
|
||||||
end
|
end
|
||||||
|
|
||||||
def hide_post(%Post{} = post, attrs, user) do
|
def hide_post(%Post{} = post, attrs, user) do
|
||||||
reports =
|
report_query = Reports.close_report_query("Post", post.id, user)
|
||||||
Report
|
|
||||||
|> where(reportable_type: "Post", reportable_id: ^post.id)
|
|
||||||
|> select([r], r.id)
|
|
||||||
|> update(set: [open: false, state: "closed", admin_id: ^user.id])
|
|
||||||
|
|
||||||
topics =
|
topics =
|
||||||
Topic
|
Topic
|
||||||
|
@ -224,7 +219,7 @@ defmodule Philomena.Posts do
|
||||||
|
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.update(:post, post)
|
|> Multi.update(:post, post)
|
||||||
|> Multi.update_all(:reports, reports, [])
|
|> Multi.update_all(:reports, report_query, [])
|
||||||
|> Multi.update_all(:topics, topics, [])
|
|> Multi.update_all(:topics, topics, [])
|
||||||
|> Multi.update_all(:forums, forums, [])
|
|> Multi.update_all(:forums, forums, [])
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|
@ -255,17 +250,12 @@ defmodule Philomena.Posts do
|
||||||
end
|
end
|
||||||
|
|
||||||
def approve_post(%Post{} = post, user) do
|
def approve_post(%Post{} = post, user) do
|
||||||
reports =
|
report_query = Reports.close_report_query("Post", post.id, user)
|
||||||
Report
|
|
||||||
|> where(reportable_type: "Post", reportable_id: ^post.id)
|
|
||||||
|> select([r], r.id)
|
|
||||||
|> update(set: [open: false, state: "closed", admin_id: ^user.id])
|
|
||||||
|
|
||||||
post = Post.approve_changeset(post)
|
post = Post.approve_changeset(post)
|
||||||
|
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.update(:post, post)
|
|> Multi.update(:post, post)
|
||||||
|> Multi.update_all(:reports, reports, [])
|
|> Multi.update_all(:reports, report_query, [])
|
||||||
|> Repo.transaction()
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{post: post, reports: {_count, reports}}} ->
|
{:ok, %{post: post, reports: {_count, reports}}} ->
|
||||||
|
|
|
@ -60,6 +60,41 @@ defmodule Philomena.Reports do
|
||||||
|> reindex_after_update()
|
|> reindex_after_update()
|
||||||
end
|
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
|
def create_system_report(reportable_id, reportable_type, category, reason) do
|
||||||
attrs = %{
|
attrs = %{
|
||||||
reason: reason,
|
reason: reason,
|
||||||
|
|
Loading…
Reference in a new issue