topic stick, lock, total shitcode

This commit is contained in:
Luna D 2019-12-13 18:31:42 -05:00
parent 6337b23dbe
commit 375a5c6de1
No known key found for this signature in database
GPG key ID: D0F46C94720BAA4B
8 changed files with 210 additions and 3 deletions

View file

@ -190,6 +190,26 @@ defmodule Philomena.Topics do
|> Repo.delete()
end
def stick_topic(topic) do
Topic.stick_changeset(topic)
|> Repo.update()
end
def unstick_topic(topic) do
Topic.unstick_changeset(topic)
|> Repo.update()
end
def lock_topic(%Topic{} = topic, attrs, user) do
Topic.lock_changeset(topic, attrs, user)
|> Repo.update()
end
def unlock_topic(%Topic{} = topic) do
Topic.unlock_changeset(topic)
|> Repo.update()
end
def clear_notification(_topic, nil), do: nil
def clear_notification(topic, user) do
Notifications.delete_unread_notification("Topic", topic.id, user)

View file

@ -25,7 +25,7 @@ defmodule Philomena.Topics.Topic do
field :view_count, :integer, default: 0
field :sticky, :boolean, default: false
field :last_replied_to_at, :naive_datetime
field :locked_at, :naive_datetime
field :locked_at, :utc_datetime
field :deletion_reason, :string
field :lock_reason, :string
field :slug, :string
@ -64,6 +64,33 @@ defmodule Philomena.Topics.Topic do
|> unique_constraint(:slug, name: :index_topics_on_forum_id_and_slug)
end
def stick_changeset(topic) do
change(topic)
|> put_change(:sticky, true)
end
def unstick_changeset(topic) do
change(topic)
|> put_change(:sticky, false)
end
def lock_changeset(topic, attrs, user) do
now = DateTime.utc_now() |> DateTime.truncate(:second)
change(topic)
|> cast(attrs, [:lock_reason])
|> put_change(:locked_at, now)
|> put_change(:locked_by_id, user.id)
|> validate_required([:lock_reason])
end
def unlock_changeset(topic) do
change(topic)
|> put_change(:locked_at, nil)
|> put_change(:locked_by_id, nil)
|> put_change(:lock_reason, "")
end
def put_slug(changeset) do
slug =
changeset

View file

@ -0,0 +1,52 @@
defmodule PhilomenaWeb.Topic.LockController 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: :lock, delete: :unlock
plug :authorize_resource, model: Topic, id_name: "topic_id", persisted: true
def create(conn, %{"topic" => topic_params}) do
topic = conn.assigns.topic
user = conn.assigns.current_user
case Topics.lock_topic(topic, topic_params, user) do
{:ok, topic} ->
conn
|> put_flash(:info, "Topic successfully locked!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to lock the topic!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
end
end
def delete(conn, _opts) do
topic = conn.assigns.topic
case Topics.unlock_topic(topic) do
{:ok, topic} ->
conn
|> put_flash(:info, "Topic successfully unlocked!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to unlock the topic!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
end
end
defp load_topic(conn, _opts) do
topic = Topic
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|> Repo.one()
Plug.Conn.assign(conn, :topic, topic)
end
end

View file

@ -0,0 +1,26 @@
defmodule PhilomenaWeb.Topic.MoveController 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: :stick, delete: :stick
plug :authorize_resource, model: Topic, id_name: "topic_id", persisted: true
# intentionally blank
# todo: moving
def create(conn, _opts) do
end
defp load_topic(conn, _opts) do
topic = Topic
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|> Repo.one()
Plug.Conn.assign(conn, :topic, topic)
end
end

View file

@ -83,4 +83,4 @@ defmodule PhilomenaWeb.Topic.PostController do
Plug.Conn.assign(conn, :topic, topic)
end
end
end
end

View file

@ -0,0 +1,51 @@
defmodule PhilomenaWeb.Topic.StickController 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: :stick, delete: :stick
plug :authorize_resource, model: Topic, id_name: "topic_id", persisted: true
def create(conn, _opts) do
topic = conn.assigns.topic
case Topics.stick_topic(topic) do
{:ok, topic} ->
conn
|> put_flash(:info, "Topic successfully stickied!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to stick the topic!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
end
end
def delete(conn, _opts) do
topic = conn.assigns.topic
case Topics.unstick_topic(topic) do
{:ok, topic} ->
conn
|> put_flash(:info, "Topic successfully unstickied!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
{:error, _changeset} ->
conn
|> put_flash(:error, "Unable to unstick the topic!")
|> redirect(to: Routes.forum_topic_path(conn, :show, topic.forum_id, topic.id))
end
end
defp load_topic(conn, _opts) do
topic = Topic
|> where(forum_id: ^conn.params["forum_id"], slug: ^conn.params["topic_id"])
|> Repo.one()
Plug.Conn.assign(conn, :topic, topic)
end
end

View file

@ -126,6 +126,9 @@ defmodule PhilomenaWeb.Router do
resources "/topics", TopicController, only: [:new, :create] do
resources "/subscription", Topic.SubscriptionController, only: [:create, :delete], singleton: true
resources "/read", Topic.ReadController, only: [:create], singleton: true
resources "/move", Topic.MoveController, only: [:create], singleton: true
resources "/stick", Topic.StickController, only: [:create, :delete], singleton: true
resources "/lock", Topic.LockController, only: [:create, :delete], singleton: true
resources "/posts", Topic.PostController, only: [:edit, :update] do
resources "/hide", Topic.Post.HideController, only: [:create, :delete], singleton: true
resources "/delete", Topic.Post.DeleteController, only: [:create], singleton: true

View file

@ -74,7 +74,35 @@ h1 = @topic.title
p For various reasons, we'd like to ask you to start a new topic.
- true ->
= if can?(@conn, :assist, Topic) do
.block__content
input.toggle-box id="administrator_tools" type="checkbox" checked=false
label for="administrator_tools" Manage Topic
.toggle-box-container
.toggle-box-container__content
p
= cond do
- can?(@conn, :lock, @topic) and is_nil(@topic.locked_at) ->
= form_for :topic, Routes.forum_topic_lock_path(@conn, :create, @topic.forum_id, @topic), [method: :post, class: "hform"], fn f ->
= text_input f, :lock_reason, class: "input hform__text", placeholder: "Lock reason"
= submit class: "hform__button button" do
i.fas.fa-lock>
' Lock
- can?(@conn, :unlock, @topic) and not is_nil(@topic.locked_at) ->
= link(to: Routes.forum_topic_lock_path(@conn, :delete, @topic.forum_id, @topic), method: :delete, class: "button") do
i.fas.fa-unlock>
' Unlock
- true ->
= cond do
- can?(@conn, :stick, @topic) and @topic.sticky ->
= link(to: Routes.forum_topic_stick_path(@conn, :delete, @topic.forum_id, @topic), method: :delete, class: "button") do
i.fas.fa-thumbtack>
' Unstick
- can?(@conn, :stick, @topic) and not @topic.sticky ->
= link(to: Routes.forum_topic_stick_path(@conn, :create, @topic.forum_id, @topic), method: :post, class: "button") do
i.fas.fa-thumbtack>
' Stick
- true ->
/ Mod tools
/- if can? :assist, Topic
/ .block__content