comment hiding

This commit is contained in:
Luna D 2019-12-10 15:29:48 -05:00
parent b6d4c38979
commit a2b3817d5a
No known key found for this signature in database
GPG key ID: D0F46C94720BAA4B
7 changed files with 114 additions and 6 deletions

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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