topic hiding / restoring

This commit is contained in:
Luna D 2019-12-14 13:03:11 -05:00
parent 37be32f2b2
commit 782baba324
No known key found for this signature in database
GPG key ID: D0F46C94720BAA4B
6 changed files with 186 additions and 88 deletions

View file

@ -235,6 +235,16 @@ defmodule Philomena.Topics do
|> Repo.isolated_transaction(:serializable) |> Repo.isolated_transaction(:serializable)
end end
def hide_topic(topic, deletion_reason, user) do
Topic.hide_changeset(topic, deletion_reason, user)
|> Repo.update()
end
def unhide_topic(topic) do
Topic.unhide_changeset(topic)
|> Repo.update()
end
def clear_notification(nil, _user), do: nil def clear_notification(nil, _user), do: nil
def clear_notification(_topic, nil), do: nil def clear_notification(_topic, nil), do: nil
def clear_notification(topic, user) do def clear_notification(topic, user) do

View file

@ -96,6 +96,21 @@ defmodule Philomena.Topics.Topic do
|> put_change(:forum_id, new_forum_id) |> put_change(:forum_id, new_forum_id)
end end
def hide_changeset(topic, deletion_reason, user) do
change(topic)
|> put_change(:hidden_from_users, true)
|> put_change(:deleted_by_id, user.id)
|> put_change(:deletion_reason, deletion_reason)
|> validate_required([:deletion_reason])
end
def unhide_changeset(topic) do
change(topic)
|> put_change(:hidden_from_users, false)
|> put_change(:deleted_by_id, nil)
|> put_change(:deletion_reason, "")
end
def put_slug(changeset) do def put_slug(changeset) do
slug = slug =
changeset changeset

View file

@ -0,0 +1,54 @@
defmodule PhilomenaWeb.Topic.HideController do
import Plug.Conn
use PhilomenaWeb, :controller
alias Philomena.Topics.Topic
alias Philomena.Topics
alias Philomena.Repo
import Ecto.Query
plug :load_topic
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
plug :authorize_resource, model: Topic, id_name: "topic_id", persisted: true
def create(conn, %{"topic" => topic_params}) do
topic = conn.assigns.topic
deletion_reason = topic_params["deletion_reason"]
user = conn.assigns.current_user
case Topics.hide_topic(topic, deletion_reason, user) do
{:ok, topic} ->
conn
|> put_flash(:info, "Topic successfully hidden!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to hide the topic!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
end
end
def delete(conn, _opts) do
topic = conn.assigns.topic
case Topics.unhide_topic(topic) do
{:ok, topic} ->
conn
|> put_flash(:info, "Topic successfully restored!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to restore the topic!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum, topic))
end
end
defp load_topic(conn, _opts) do
topic = Topic
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|> preload([:forum])
|> Repo.one()
Plug.Conn.assign(conn, :topic, topic)
end
end

View file

@ -19,8 +19,8 @@ defmodule PhilomenaWeb.TopicController do
forum = conn.assigns.forum forum = conn.assigns.forum
topic = topic =
Topic Topic
|> where(forum_id: ^forum.id, slug: ^slug, hidden_from_users: false) |> where(forum_id: ^forum.id, slug: ^slug)
|> preload([:user, poll: :options]) |> preload([:deleted_by, :user, poll: :options])
|> Repo.one() |> Repo.one()
user = conn.assigns.current_user user = conn.assigns.current_user

View file

@ -129,6 +129,7 @@ defmodule PhilomenaWeb.Router do
resources "/move", Topic.MoveController, only: [:create], singleton: true resources "/move", Topic.MoveController, only: [:create], singleton: true
resources "/stick", Topic.StickController, only: [:create, :delete], singleton: true resources "/stick", Topic.StickController, only: [:create, :delete], singleton: true
resources "/lock", Topic.LockController, only: [:create, :delete], singleton: true resources "/lock", Topic.LockController, only: [:create, :delete], singleton: true
resources "/hide", Topic.HideController, only: [:create, :delete], singleton: true
resources "/posts", Topic.PostController, only: [:edit, :update] do resources "/posts", Topic.PostController, only: [:edit, :update] do
resources "/hide", Topic.Post.HideController, only: [:create, :delete], singleton: true resources "/hide", Topic.Post.HideController, only: [:create, :delete], singleton: true
resources "/delete", Topic.Post.DeleteController, only: [:create], singleton: true resources "/delete", Topic.Post.DeleteController, only: [:create], singleton: true

View file

@ -3,6 +3,23 @@ elixir:
pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @posts, route: route, last: true pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @posts, route: route, last: true
h1 = @topic.title h1 = @topic.title
= if @topic.hidden_from_users do
.block.block--fixed.block--danger
h4 This topic has been deleted by a moderator.
p
strong> Reason:
em
= @topic.deletion_reason
= if can?(@conn, :unhide, @topic) do
p
strong> Deleted by:
em
strong
= link(@topic.deleted_by.name, to: Routes.profile_path(@conn, :show, @topic.deleted_by))
p
= link(to: Routes.forum_topic_hide_path(@conn, :delete, @forum.id, @topic), method: :delete, class: "button") do
i.fas.fa-check>
' Restore
/ Header section / Header section
.block .block
.block__header .block__header
@ -11,60 +28,66 @@ h1 = @topic.title
=> link(@forum.name, to: Routes.forum_path(@conn, :show, @forum)) => link(@forum.name, to: Routes.forum_path(@conn, :show, @forum))
' » ' »
=> link(@topic.title, to: Routes.forum_topic_path(@conn, :show, @forum, @topic)) => link(@topic.title, to: Routes.forum_topic_path(@conn, :show, @forum, @topic))
a href=Routes.post_path(@conn, :index, pq: "topic_id:#{@topic.id}") = if not @topic.hidden_from_users or can?(@conn, :hide, @topic) do
i.fa.fa-fw.fa-search> a href=Routes.post_path(@conn, :index, pq: "topic_id:#{@topic.id}")
' Search Posts i.fa.fa-fw.fa-search>
.flex.flex--wrap.block__header.block__header--light ' Search Posts
.flex--fixed .flex.flex--wrap.block__header.block__header--light
= pagination .flex--fixed
.flex--fixed.block__header__item = pagination
' Started by .flex--fixed.block__header__item
=> render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @topic, conn: @conn ' Started by
.flex--fixed.block__header__item => render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @topic, conn: @conn
' Posted .flex--fixed.block__header__item
=< pretty_time(@topic.created_at) ' Posted
.flex--fixed.block__header__item =< pretty_time(@topic.created_at)
=> @topic.post_count - 1 .flex--fixed.block__header__item
' replies => @topic.post_count - 1
= render PhilomenaWeb.Topic.SubscriptionView, "_subscription.html", forum: @forum, topic: @topic, watching: @watching, conn: @conn ' replies
= if not @topic.hidden_from_users do
= render PhilomenaWeb.Topic.SubscriptionView, "_subscription.html", forum: @forum, topic: @topic, watching: @watching, conn: @conn
/ Display the poll, if any = if not @topic.hidden_from_users or can?(@conn, :hide, @topic) do
= if @topic.poll do / Display the poll, if any
= render PhilomenaWeb.Topic.PollView, "_display.html", poll: @topic.poll, conn: @conn = if @topic.poll do
= render PhilomenaWeb.Topic.PollView, "_display.html", poll: @topic.poll, conn: @conn
/ The actual posts / The actual posts
.posts-area .posts-area
.post-list .post-list
= for {post, body} <- @posts, (!post.destroyed_content or can?(@conn, :hide, post)) do = for {post, body} <- @posts, (!post.destroyed_content or can?(@conn, :hide, post)) do
= render PhilomenaWeb.PostView, "_post.html", conn: @conn, post: post, body: body = render PhilomenaWeb.PostView, "_post.html", conn: @conn, post: post, body: body
= if @conn.assigns.advert do = if @conn.assigns.advert do
= render PhilomenaWeb.AdvertView, "_box.html", advert: @conn.assigns.advert, conn: @conn = render PhilomenaWeb.AdvertView, "_box.html", advert: @conn.assigns.advert, conn: @conn
/ Post editability data for JS / Post editability data for JS
/.js-editable-posts data-editable=editable_communications(@posts).to_json /.js-editable-posts data-editable=editable_communications(@posts).to_json
/ Footer section / Footer section
.block .block
.block__header.block__header--light .block__header.block__header--light
= pagination = pagination
= if @topic.locked_at do = if @topic.locked_at do
.block.block--fixed.block--warning .block.block--fixed.block--warning
h4 This topic has been locked to new posts from non-moderators. h4 This topic has been locked to new posts from non-moderators.
p p
' Locked ' Locked
=> pretty_time(@topic.locked_at) => pretty_time(@topic.locked_at)
p
' Lock reason:
em = @topic.lock_reason
p
' Lock reason:
em = @topic.lock_reason
/ Post form / Post form
= cond do = cond do
- @conn.assigns.current_ban -> - @conn.assigns.current_ban ->
= render PhilomenaWeb.BanView, "_ban_reason.html", conn: @conn = render PhilomenaWeb.BanView, "_ban_reason.html", conn: @conn
- @topic.hidden_from_users ->
.block.block--fixed.block--danger
h4 Cannot reply to a deleted topic.
- @topic.post_count < 200_000 and can?(@conn, :create_post, @topic) -> - @topic.post_count < 200_000 and can?(@conn, :create_post, @topic) ->
= render PhilomenaWeb.Topic.PostView, "_form.html", conn: @conn, forum: @forum, topic: @topic, changeset: @changeset = render PhilomenaWeb.Topic.PostView, "_form.html", conn: @conn, forum: @forum, topic: @topic, changeset: @changeset
@ -81,47 +104,42 @@ h1 = @topic.title
.toggle-box-container .toggle-box-container
.toggle-box-container__content .toggle-box-container__content
p p
= cond do = if not @topic.hidden_from_users do
- can?(@conn, :lock, @topic) and is_nil(@topic.locked_at) -> = cond do
= form_for :topic, Routes.forum_topic_lock_path(@conn, :create, @topic.forum_id, @topic), [method: :post, class: "hform"], fn f -> - can?(@conn, :lock, @topic) and is_nil(@topic.locked_at) ->
= text_input f, :lock_reason, class: "input hform__text", placeholder: "Lock reason" = form_for :topic, Routes.forum_topic_lock_path(@conn, :create, @forum.id, @topic), [method: :post, class: "hform"], fn f ->
= submit class: "hform__button button" do = text_input f, :lock_reason, class: "input hform__text", placeholder: "Lock reason", required: true
i.fas.fa-lock> = submit class: "hform__button button" do
' Lock i.fas.fa-lock>
- can?(@conn, :unlock, @topic) and not is_nil(@topic.locked_at) -> ' Lock
= link(to: Routes.forum_topic_lock_path(@conn, :delete, @topic.forum_id, @topic), method: :delete, class: "button") do - can?(@conn, :unlock, @topic) and not is_nil(@topic.locked_at) ->
i.fas.fa-unlock> = link(to: Routes.forum_topic_lock_path(@conn, :delete, @forum.id, @topic), method: :delete, class: "button") do
' Unlock i.fas.fa-unlock>
- true -> ' Unlock
= cond do - true ->
- can?(@conn, :stick, @topic) and @topic.sticky -> = cond do
= link(to: Routes.forum_topic_stick_path(@conn, :delete, @topic.forum_id, @topic), method: :delete, class: "button") do - can?(@conn, :stick, @topic) and @topic.sticky ->
i.fas.fa-thumbtack> = link(to: Routes.forum_topic_stick_path(@conn, :delete, @forum.id, @topic), method: :delete, class: "button") do
' Unstick i.fas.fa-thumbtack>
- can?(@conn, :stick, @topic) and not @topic.sticky -> ' Unstick
= link(to: Routes.forum_topic_stick_path(@conn, :create, @topic.forum_id, @topic), method: :post, class: "button") do - can?(@conn, :stick, @topic) and not @topic.sticky ->
i.fas.fa-thumbtack> = link(to: Routes.forum_topic_stick_path(@conn, :create, @forum.id, @topic), method: :post, class: "button") do
' Stick i.fas.fa-thumbtack>
- true -> ' Stick
= if can?(@conn, :move, @topic) do - true ->
= form_for :topic, Routes.forum_topic_move_path(@conn, :create, @topic.forum_id, @topic), [method: :post, class: "hform"], fn f -> = if can?(@conn, :move, @topic) do
.field = form_for :topic, Routes.forum_topic_move_path(@conn, :create, @forum.id, @topic), [method: :post, class: "hform"], fn f ->
= select f, :target_forum_id, @conn.assigns.forums |> Enum.map(fn forum -> {forum.name, forum.id} end), class: "input hform__text" .field
= submit class: "hform__button button" do = select f, :target_forum_id, @conn.assigns.forums |> Enum.map(fn forum -> {forum.name, forum.id} end), class: "input hform__text"
i.fas.fa-truck> = submit class: "hform__button button" do
' Move i.fas.fa-truck>
/ Mod tools ' Move
/- if can? :assist, Topic = if can?(@conn, :hide, @topic) do
/ .block__content = form_for :topic, Routes.forum_topic_hide_path(@conn, :create, @forum.id, @topic), [method: :post, class: "hform"], fn f ->
/ input.toggle-box id="administrator_tools" type="checkbox" checked=false .field
/ label for="administrator_tools" Topic Admin Tools = text_input f, :deletion_reason, class: "input hform__text", placeholder: "Deletion reason", required: true
/ .toggle-box-container = submit class: "hform__button button" do
/ .toggle-box-container__content i.fas.fa-trash>
/ p ' Delete
/ - if can?(:destroy, @topic) && !@topic.hidden_from_users - else
/ = form_tag forum_topic_hide_path(@forum, @topic), method: :post, class: 'hform' do | Moderation tools unavailable for deleted topics.
/ .field
/ => text_field_tag :deletion_reason, '', placeholder: 'Deletion reason', required: true, class: 'input hform__text'
/ => button_tag class: 'hform__button button' do
/ i.fa.fa-trash>
/ | Delete