2019-12-14 00:31:42 +01:00
|
|
|
defmodule PhilomenaWeb.Topic.StickController do
|
|
|
|
import Plug.Conn
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-14 20:46:50 +01:00
|
|
|
alias Philomena.Forums.Forum
|
2019-12-14 00:31:42 +01:00
|
|
|
alias Philomena.Topics.Topic
|
|
|
|
alias Philomena.Topics
|
|
|
|
|
2020-02-21 08:24:45 +01:00
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show, delete: :show
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Forum,
|
|
|
|
id_name: "forum_id",
|
|
|
|
id_field: "short_name",
|
|
|
|
persisted: true
|
2019-12-14 20:46:50 +01:00
|
|
|
|
|
|
|
plug PhilomenaWeb.LoadTopicPlug
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :hide, delete: :hide
|
|
|
|
plug :authorize_resource, model: Topic, persisted: true
|
2019-12-14 00:31:42 +01:00
|
|
|
|
|
|
|
def create(conn, _opts) do
|
|
|
|
topic = conn.assigns.topic
|
|
|
|
|
|
|
|
case Topics.stick_topic(topic) do
|
|
|
|
{:ok, topic} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Topic successfully stickied!")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: topic)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-14 00:31:42 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to stick the topic!")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2019-12-14 00:31:42 +01:00
|
|
|
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!")
|
2021-11-08 14:45:19 +01:00
|
|
|
|> moderation_log(details: &log_details/3, data: topic)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2019-12-14 20:46:50 +01:00
|
|
|
|
2019-12-14 00:31:42 +01:00
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to unstick the topic!")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/forums/#{topic.forum}/topics/#{topic}")
|
2019-12-14 00:31:42 +01:00
|
|
|
end
|
|
|
|
end
|
2021-11-07 19:51:55 +01:00
|
|
|
|
2024-04-29 03:03:37 +02:00
|
|
|
defp log_details(_conn, action, topic) do
|
2021-11-07 19:51:55 +01:00
|
|
|
body =
|
|
|
|
case action do
|
|
|
|
:create -> "Stickied topic '#{topic.title}' in #{topic.forum.name}"
|
|
|
|
:delete -> "Unstickied topic '#{topic.title}' in #{topic.forum.name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
%{
|
|
|
|
body: body,
|
2024-04-29 02:55:27 +02:00
|
|
|
subject_path: ~p"/forums/#{topic.forum}/topics/#{topic}"
|
2021-11-07 19:51:55 +01:00
|
|
|
}
|
|
|
|
end
|
2019-12-14 00:31:42 +01:00
|
|
|
end
|