2019-11-29 22:47:41 +01:00
|
|
|
defmodule PhilomenaWeb.DnpEntryController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.DnpEntries.DnpEntry
|
2021-09-29 22:24:38 +02:00
|
|
|
alias PhilomenaWeb.MarkdownRenderer
|
2019-12-12 22:44:50 +01:00
|
|
|
alias Philomena.DnpEntries
|
2019-11-29 22:47:41 +01:00
|
|
|
alias Philomena.Tags.Tag
|
2024-07-29 17:27:04 +02:00
|
|
|
alias Philomena.ModNotes.ModNote
|
2024-07-16 03:19:32 +02:00
|
|
|
alias Philomena.ModNotes
|
2019-11-29 22:47:41 +01:00
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-12-12 22:44:50 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
|
2020-05-29 00:46:52 +02:00
|
|
|
plug :set_tags when action in [:new, :create, :edit, :update, :create]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: DnpEntry,
|
|
|
|
only: [:show, :edit, :update],
|
|
|
|
preload: [:tag]
|
|
|
|
|
2019-12-17 18:29:18 +01:00
|
|
|
plug :set_mod_notes when action in [:show]
|
2019-12-12 22:44:50 +01:00
|
|
|
|
|
|
|
def index(%{assigns: %{current_user: user}} = conn, %{"mine" => _mine}) when not is_nil(user) do
|
|
|
|
DnpEntry
|
|
|
|
|> where(requesting_user_id: ^user.id)
|
|
|
|
|> preload(:tag)
|
|
|
|
|> order_by(asc: :created_at)
|
|
|
|
|> load_entries(conn, true)
|
|
|
|
end
|
2019-11-29 22:47:41 +01:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
2019-12-12 22:44:50 +01:00
|
|
|
DnpEntry
|
|
|
|
|> where(aasm_state: "listed")
|
|
|
|
|> join(:inner, [d], t in Tag, on: d.tag_id == t.id)
|
|
|
|
|> preload(:tag)
|
|
|
|
|> order_by([_d, t], asc: t.name_in_namespace)
|
|
|
|
|> load_entries(conn, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp load_entries(dnp_entries, conn, status) do
|
|
|
|
dnp_entries = Repo.paginate(dnp_entries, conn.assigns.scrivener)
|
2019-12-31 05:03:44 +01:00
|
|
|
linked_tags = linked_tags(conn)
|
2019-11-29 22:47:41 +01:00
|
|
|
|
|
|
|
bodies =
|
|
|
|
dnp_entries
|
2021-09-29 22:05:16 +02:00
|
|
|
|> Enum.map(&%{body: &1.conditions || "-"})
|
2021-09-29 22:24:38 +02:00
|
|
|
|> MarkdownRenderer.render_collection(conn)
|
2019-11-29 22:47:41 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}
|
2019-11-29 22:47:41 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "index.html",
|
|
|
|
title: "Do-Not-Post List",
|
|
|
|
layout_class: "layout--medium",
|
|
|
|
dnp_entries: dnp_entries,
|
|
|
|
status_column: status,
|
|
|
|
linked_tags: linked_tags
|
|
|
|
)
|
2019-11-29 22:47:41 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, _params) do
|
|
|
|
dnp_entry = conn.assigns.dnp_entry
|
|
|
|
|
|
|
|
[conditions, reason, instructions] =
|
2021-09-29 22:24:38 +02:00
|
|
|
MarkdownRenderer.render_collection(
|
2019-12-01 18:11:00 +01:00
|
|
|
[
|
2021-09-29 22:05:16 +02:00
|
|
|
%{body: dnp_entry.conditions || "-"},
|
|
|
|
%{body: dnp_entry.reason || "-"},
|
|
|
|
%{body: dnp_entry.instructions || "-"}
|
2019-12-01 18:11:00 +01:00
|
|
|
],
|
|
|
|
conn
|
|
|
|
)
|
2019-11-29 22:47:41 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "show.html",
|
|
|
|
title: "Showing DNP Listing",
|
|
|
|
dnp_entry: dnp_entry,
|
|
|
|
conditions: conditions,
|
|
|
|
reason: reason,
|
|
|
|
instructions: instructions
|
|
|
|
)
|
2019-11-29 22:47:41 +01:00
|
|
|
end
|
2019-12-12 22:44:50 +01:00
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
changeset = DnpEntries.change_dnp_entry(%DnpEntry{})
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
render(conn, "new.html",
|
|
|
|
title: "New DNP Listing",
|
2020-05-29 00:46:52 +02:00
|
|
|
changeset: changeset
|
2020-01-11 05:20:19 +01:00
|
|
|
)
|
2019-12-12 22:44:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"dnp_entry" => dnp_entry_params}) do
|
2020-01-11 05:20:19 +01:00
|
|
|
case DnpEntries.create_dnp_entry(
|
|
|
|
conn.assigns.current_user,
|
2020-05-29 00:46:52 +02:00
|
|
|
conn.assigns.selectable_tags,
|
2020-01-11 05:20:19 +01:00
|
|
|
dnp_entry_params
|
|
|
|
) do
|
2019-12-12 22:44:50 +01:00
|
|
|
{:ok, dnp_entry} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Successfully submitted DNP request.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/dnp/#{dnp_entry}")
|
2019-12-12 22:44:50 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
2020-05-29 00:46:52 +02:00
|
|
|
render(conn, "new.html", changeset: changeset)
|
2019-12-12 22:44:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
changeset = DnpEntries.change_dnp_entry(conn.assigns.dnp_entry)
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
render(conn, "edit.html",
|
|
|
|
title: "Editing DNP Listing",
|
2020-05-29 00:46:52 +02:00
|
|
|
changeset: changeset
|
2020-01-11 05:20:19 +01:00
|
|
|
)
|
2019-12-12 22:44:50 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"dnp_entry" => dnp_entry_params}) do
|
2020-01-11 05:20:19 +01:00
|
|
|
case DnpEntries.update_dnp_entry(
|
|
|
|
conn.assigns.dnp_entry,
|
2020-05-29 00:46:52 +02:00
|
|
|
conn.assigns.selectable_tags,
|
2020-01-11 05:20:19 +01:00
|
|
|
dnp_entry_params
|
|
|
|
) do
|
2019-12-12 22:44:50 +01:00
|
|
|
{:ok, dnp_entry} ->
|
|
|
|
conn
|
2019-12-16 20:24:38 +01:00
|
|
|
|> put_flash(:info, "Successfully updated DNP request.")
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/dnp/#{dnp_entry}")
|
2019-12-12 22:44:50 +01:00
|
|
|
|
|
|
|
{:error, changeset} ->
|
2020-05-29 00:46:52 +02:00
|
|
|
render(conn, "edit.html", changeset: changeset)
|
2019-12-12 22:44:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp selectable_tags(conn) do
|
2020-04-30 06:38:47 +02:00
|
|
|
case present?(conn.params["tag_id"]) and
|
2020-01-11 05:20:19 +01:00
|
|
|
Canada.Can.can?(conn.assigns.current_user, :index, DnpEntry) do
|
2019-12-12 22:44:50 +01:00
|
|
|
true -> [Repo.get!(Tag, conn.params["tag_id"])]
|
|
|
|
false -> linked_tags(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-31 05:03:44 +01:00
|
|
|
defp linked_tags(%{assigns: %{current_user: user}}) when not is_nil(user) do
|
|
|
|
user
|
2019-12-12 22:44:50 +01:00
|
|
|
|> Repo.preload(:linked_tags)
|
|
|
|
|> Map.get(:linked_tags)
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-31 05:03:44 +01:00
|
|
|
defp linked_tags(_), do: []
|
2019-12-17 18:29:18 +01:00
|
|
|
|
2020-04-30 06:38:47 +02:00
|
|
|
defp present?(nil), do: false
|
|
|
|
defp present?(""), do: false
|
|
|
|
defp present?(_), do: true
|
|
|
|
|
2019-12-17 18:29:18 +01:00
|
|
|
defp set_mod_notes(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do
|
|
|
|
true ->
|
|
|
|
dnp_entry = conn.assigns.dnp_entry
|
|
|
|
|
2024-07-16 03:19:32 +02:00
|
|
|
renderer = &MarkdownRenderer.render_collection(&1, conn)
|
|
|
|
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("DnpEntry", dnp_entry.id, renderer)
|
2019-12-17 18:29:18 +01:00
|
|
|
assign(conn, :mod_notes, mod_notes)
|
|
|
|
|
|
|
|
_false ->
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
2020-05-29 00:46:52 +02:00
|
|
|
|
|
|
|
defp set_tags(conn, _opts) do
|
|
|
|
tags = selectable_tags(conn)
|
|
|
|
|
|
|
|
case tags do
|
|
|
|
[] ->
|
|
|
|
PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
assign(conn, :selectable_tags, tags)
|
|
|
|
end
|
|
|
|
end
|
2019-11-29 22:47:41 +01:00
|
|
|
end
|