propagate hiding user on reports

This commit is contained in:
byte[] 2020-09-07 14:50:34 -04:00
parent a2e524b535
commit 90830d361b
6 changed files with 50 additions and 24 deletions

View file

@ -15,6 +15,7 @@ defmodule Philomena.Comments do
alias Philomena.Images alias Philomena.Images
alias Philomena.Notifications alias Philomena.Notifications
alias Philomena.Versions alias Philomena.Versions
alias Philomena.Reports
@doc """ @doc """
Gets a single comment. Gets a single comment.
@ -141,7 +142,7 @@ defmodule Philomena.Comments do
Report Report
|> where(reportable_type: "Comment", reportable_id: ^comment.id) |> where(reportable_type: "Comment", reportable_id: ^comment.id)
|> select([r], r.id) |> select([r], r.id)
|> update(set: [open: false, state: "closed"]) |> update(set: [open: false, state: "closed", admin_id: ^user.id])
comment = Comment.hide_changeset(comment, attrs, user) comment = Comment.hide_changeset(comment, attrs, user)
@ -149,12 +150,31 @@ defmodule Philomena.Comments do
|> Multi.update(:comment, comment) |> Multi.update(:comment, comment)
|> Multi.update_all(:reports, reports, []) |> Multi.update_all(:reports, reports, [])
|> Repo.transaction() |> Repo.transaction()
|> case do
{:ok, %{comment: comment, reports: {_count, reports}}} ->
Reports.reindex_reports(reports)
reindex_comment(comment)
{:ok, comment}
error ->
error
end
end end
def unhide_comment(%Comment{} = comment) do def unhide_comment(%Comment{} = comment) do
comment comment
|> Comment.unhide_changeset() |> Comment.unhide_changeset()
|> Repo.update() |> Repo.update()
|> case do
{:ok, comment} ->
reindex_comment(comment)
{:ok, comment}
error ->
error
end
end end
def destroy_comment(%Comment{} = comment) do def destroy_comment(%Comment{} = comment) do

View file

@ -96,7 +96,7 @@ defmodule Philomena.DuplicateReports do
multi multi
|> Multi.update(:duplicate_report, changeset) |> Multi.update(:duplicate_report, changeset)
|> Multi.update_all(:other_reports, other_duplicate_reports, []) |> Multi.update_all(:other_reports, other_duplicate_reports, [])
|> Images.merge_image(duplicate_report.image, duplicate_report.duplicate_of_image) |> Images.merge_image(duplicate_report.image, duplicate_report.duplicate_of_image, user)
end end
def accept_reverse_duplicate_report(%DuplicateReport{} = duplicate_report, user) do def accept_reverse_duplicate_report(%DuplicateReport{} = duplicate_report, user) do

View file

@ -390,18 +390,18 @@ defmodule Philomena.Images do
image image
|> Image.hide_changeset(attrs, user) |> Image.hide_changeset(attrs, user)
|> hide_image_multi(image, Ecto.Multi.new()) |> hide_image_multi(image, user, Multi.new())
|> Multi.update_all(:duplicate_reports, duplicate_reports, []) |> Multi.update_all(:duplicate_reports, duplicate_reports, [])
|> Repo.transaction() |> Repo.transaction()
|> process_after_hide() |> process_after_hide()
end end
def merge_image(multi \\ nil, %Image{} = image, duplicate_of_image) do def merge_image(multi \\ nil, %Image{} = image, duplicate_of_image, user) do
multi = multi || Multi.new() multi = multi || Multi.new()
image image
|> Image.merge_changeset(duplicate_of_image) |> Image.merge_changeset(duplicate_of_image)
|> hide_image_multi(image, multi) |> hide_image_multi(image, user, multi)
|> Multi.run(:first_seen_at, fn _, %{} -> |> Multi.run(:first_seen_at, fn _, %{} ->
update_first_seen_at( update_first_seen_at(
duplicate_of_image, duplicate_of_image,
@ -435,12 +435,12 @@ defmodule Philomena.Images do
end end
end end
defp hide_image_multi(changeset, image, multi) do defp hide_image_multi(changeset, image, user, multi) do
reports = reports =
Report Report
|> where(reportable_type: "Image", reportable_id: ^image.id) |> where(reportable_type: "Image", reportable_id: ^image.id)
|> select([r], r.id) |> select([r], r.id)
|> update(set: [open: false, state: "closed"]) |> update(set: [open: false, state: "closed", admin_id: ^user.id])
multi multi
|> Multi.update(:image, changeset) |> Multi.update(:image, changeset)

View file

@ -15,6 +15,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
alias Philomena.Reports.Report alias Philomena.Reports.Report
@doc """ @doc """
@ -170,7 +171,7 @@ defmodule Philomena.Posts do
Report Report
|> where(reportable_type: "Post", reportable_id: ^post.id) |> where(reportable_type: "Post", reportable_id: ^post.id)
|> select([r], r.id) |> select([r], r.id)
|> update(set: [open: false, state: "closed"]) |> update(set: [open: false, state: "closed", admin_id: ^user.id])
post = Post.hide_changeset(post, attrs, user) post = Post.hide_changeset(post, attrs, user)
@ -178,12 +179,29 @@ defmodule Philomena.Posts do
|> Multi.update(:post, post) |> Multi.update(:post, post)
|> Multi.update_all(:reports, reports, []) |> Multi.update_all(:reports, reports, [])
|> Repo.transaction() |> Repo.transaction()
|> case do
{:ok, %{post: post, reports: {_count, reports}}} ->
Reports.reindex_reports(reports)
reindex_post(post)
{:ok, post}
error ->
error
end
end end
def unhide_post(%Post{} = post) do def unhide_post(%Post{} = post) do
post post
|> Post.unhide_changeset() |> Post.unhide_changeset()
|> Repo.update() |> Repo.update()
|> case do
{:ok, post} ->
reindex_post(post)
error ->
error
end
end end
def destroy_post(%Post{} = post) do def destroy_post(%Post{} = post) do

View file

@ -3,7 +3,6 @@ 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
@ -13,10 +12,7 @@ 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: comment, reports: {_count, reports}}} -> {:ok, comment} ->
Comments.reindex_comment(comment)
Reports.reindex_reports(reports)
conn conn
|> put_flash(:info, "Comment successfully hidden!") |> put_flash(:info, "Comment successfully hidden!")
|> redirect( |> redirect(
@ -37,8 +33,6 @@ defmodule PhilomenaWeb.Image.Comment.HideController do
case Comments.unhide_comment(comment) do case Comments.unhide_comment(comment) do
{:ok, comment} -> {:ok, comment} ->
Comments.reindex_comment(comment)
conn conn
|> put_flash(:info, "Comment successfully unhidden!") |> put_flash(:info, "Comment successfully unhidden!")
|> redirect( |> redirect(

View file

@ -2,7 +2,6 @@ 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
@ -18,12 +17,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: post, reports: {_count, reports}}} -> {:ok, post} ->
Posts.reindex_post(post)
Reports.reindex_reports(reports)
conn conn
|> put_flash(:info, "Post successfully hidden!") |> put_flash(:info, "Post successfully hidden.")
|> redirect( |> redirect(
to: to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <> Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>
@ -46,10 +42,8 @@ defmodule PhilomenaWeb.Topic.Post.HideController do
case Posts.unhide_post(post) do case Posts.unhide_post(post) do
{:ok, post} -> {:ok, post} ->
Posts.reindex_post(post)
conn conn
|> put_flash(:info, "Post successfully unhidden!") |> put_flash(:info, "Post successfully unhidden.")
|> redirect( |> redirect(
to: to:
Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <> Routes.forum_topic_path(conn, :show, post.topic.forum, post.topic, post_id: post.id) <>