From a2b3817d5aaebda1a9e04f4661d7af148034ce47 Mon Sep 17 00:00:00 2001 From: Luna D Date: Tue, 10 Dec 2019 15:29:48 -0500 Subject: [PATCH] comment hiding --- lib/philomena/comments.ex | 12 +++++ lib/philomena/comments/comment.ex | 14 ++++++ .../image/comment/delete_controller.ex | 12 +++++ .../image/comment/hide_controller.ex | 44 +++++++++++++++++++ .../controllers/image_controller.ex | 2 +- lib/philomena_web/router.ex | 5 ++- .../templates/comment/_comment.html.slime | 31 +++++++++++-- 7 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 lib/philomena_web/controllers/image/comment/delete_controller.ex create mode 100644 lib/philomena_web/controllers/image/comment/hide_controller.ex diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index ae55ae10..861eb531 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -134,6 +134,18 @@ defmodule Philomena.Comments do Repo.delete(comment) end + def hide_comment(%Comment{} = comment, attrs, user) do + comment + |> Comment.hide_changeset(attrs, user) + |> Repo.update() + end + + def unhide_comment(%Comment{} = comment) do + comment + |> Comment.unhide_changeset() + |> Repo.update() + end + @doc """ Returns an `%Ecto.Changeset{}` for tracking comment changes. diff --git a/lib/philomena/comments/comment.ex b/lib/philomena/comments/comment.ex index 987ee79f..1ce42353 100644 --- a/lib/philomena/comments/comment.ex +++ b/lib/philomena/comments/comment.ex @@ -50,6 +50,20 @@ defmodule Philomena.Comments.Comment do |> validate_length(:edit_reason, max: 70, count: :bytes) end + def hide_changeset(comment, attrs, user) do + comment + |> cast(attrs, [:deletion_reason]) + |> put_change(:hidden_from_users, true) + |> put_change(:deleted_by_id, user.id) + |> validate_required([:deletion_reason]) + end + + def unhide_changeset(comment) do + change(comment) + |> put_change(:hidden_from_users, false) + |> put_change(:deletion_reason, "") + end + defp put_name_at_post_time(changeset, nil), do: changeset defp put_name_at_post_time(changeset, user), do: change(changeset, name_at_post_time: user.name) end diff --git a/lib/philomena_web/controllers/image/comment/delete_controller.ex b/lib/philomena_web/controllers/image/comment/delete_controller.ex new file mode 100644 index 00000000..ff91f070 --- /dev/null +++ b/lib/philomena_web/controllers/image/comment/delete_controller.ex @@ -0,0 +1,12 @@ +defmodule PhilomenaWeb.Image.Comment.DeleteController do + use PhilomenaWeb, :controller + + alias Philomena.Comments.Comment + alias Philomena.Comments + + plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide + plug :load_and_authorize_resource, model: Comment, id_name: "comment_id", persisted: true + + def delete(conn, _params) do + end +end diff --git a/lib/philomena_web/controllers/image/comment/hide_controller.ex b/lib/philomena_web/controllers/image/comment/hide_controller.ex new file mode 100644 index 00000000..d1aecb24 --- /dev/null +++ b/lib/philomena_web/controllers/image/comment/hide_controller.ex @@ -0,0 +1,44 @@ +defmodule PhilomenaWeb.Image.Comment.HideController do + use PhilomenaWeb, :controller + + alias Philomena.Comments.Comment + alias Philomena.Comments + + plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide + plug :load_and_authorize_resource, model: Comment, id_name: "comment_id", persisted: true + + def create(conn, %{"comment" => comment_params}) do + comment = conn.assigns.comment + user = conn.assigns.current_user + + case Comments.hide_comment(comment, comment_params, user) do + {:ok, comment} -> + Comments.reindex_comment(comment) + + conn + |> put_flash(:info, "Comment successfully hidden!") + |> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}") + {:error, _changeset} -> + conn + |> put_flash(:error, "Unable to hide comment!") + |> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}") + end + end + + def delete(conn, _params) do + comment = conn.assigns.comment + + case Comments.unhide_comment(comment) do + {:ok, comment} -> + Comments.reindex_comment(comment) + + conn + |> put_flash(:info, "Comment successfully unhidden!") + |> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}") + {:error, _changeset} -> + conn + |> put_flash(:error, "Unable to unhide comment!") + |> redirect(to: Routes.image_path(conn, :show, comment.image_id) <> "#comment_#{comment.id}") + end + end +end diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 646b4818..0032afc2 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -41,7 +41,7 @@ defmodule PhilomenaWeb.ImageController do comments = Comment |> where(image_id: ^image.id) - |> preload([:image, user: [awards: :badge]]) + |> preload([:image, :deleted_by, user: [awards: :badge]]) |> order_by(desc: :created_at) |> limit(25) |> Repo.paginate(conn.assigns.comment_scrivener) diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index f355b67a..af886282 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -115,7 +115,10 @@ defmodule PhilomenaWeb.Router do resources "/hide", Image.HideController, only: [:create, :delete], singleton: true resources "/subscription", Image.SubscriptionController, only: [:create, :delete], singleton: true resources "/read", Image.ReadController, only: [:create], singleton: true - resources "/comments", Image.CommentController, only: [:edit, :update] + resources "/comments", Image.CommentController, only: [:edit, :update] do + resources "/hide", Image.Comment.HideController, only: [:create, :delete], singleton: true + resources "/delete", Image.Comment.DeleteController, only: [:delete], singleton: true + end resources "/delete", Image.DeleteController, only: [:create, :delete], singleton: true end diff --git a/lib/philomena_web/templates/comment/_comment.html.slime b/lib/philomena_web/templates/comment/_comment.html.slime index 38ae6ee8..4906a300 100644 --- a/lib/philomena_web/templates/comment/_comment.html.slime +++ b/lib/philomena_web/templates/comment/_comment.html.slime @@ -10,7 +10,14 @@ article.block.communication id="comment_#{@comment.id}" = if @comment.hidden_from_users do strong.comment_deleted ' Deletion reason: - => @comment.deletion_reason + =<> @comment.deletion_reason + = if can?(@conn, :delete, @comment) do + | ( + = @comment.deleted_by.name + | ) + = if can?(@conn, :delete, @comment) do + br + ==<> @body - else ==<> @body .block__content.communication__options @@ -19,14 +26,30 @@ article.block.communication id="comment_#{@comment.id}" = if can?(@conn, :hide, @comment) do .js-staff-action / todo: make delete button work - a.communication__interaction.togglable-delete-form-link href="#" - i.fa.fa-times - =<> "Delete" + = case @comment.destroyed_content do + - true -> + - _false -> + = case @comment.hidden_from_users do + - true -> + = link(to: Routes.image_comment_hide_path(@conn, :delete, @comment.image_id, @comment), data: [confirm: "Are you sure?"], method: :delete, class: "communication__interaction") do + i.fas.fa-check> + ' Restore + = if can?(@conn, :delete, @comment) do + = link(to: Routes.image_comment_delete_path(@conn, :delete, @comment.image_id, @comment), method: :delete, data: [confirm: "Are you sure?"], class: "communication__interaction") do + i.fas.fa-times> + ' Delete Contents + - _false -> + a.communication__interaction.togglable-delete-form-link href="#" data-click-toggle="#inline-del-form-comment-#{@comment.id}" + i.fas.fa-times> + ' Delete = if can?(@conn, :manage, @comment) do .communication__info =<> link_to_ip(@conn, @comment.ip) .communication__info =<> link_to_fingerprint(@conn, @comment.fingerprint) + = form_for :comment, Routes.image_comment_hide_path(@conn, :create, @comment.image_id, @comment), [class: "togglable-delete-form hidden flex", id: "inline-del-form-comment-#{@comment.id}"], fn f -> + = text_input f, :deletion_reason, class: "input input--wide", placeholder: "Deletion Reason", id: "inline-del-reason-comment-#{@comment.id}", required: true + = submit "Delete", class: "button" /- if can?(:hide, Comment) / .js-staff-action / - if !comment.hidden_from_users && !comment.destroyed_content