From 0fa10103f86504dece7ade46f00fa150838a203b Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Tue, 17 Dec 2019 12:29:18 -0500 Subject: [PATCH] display mod notes on profiles and such --- .../controllers/admin/report_controller.ex | 27 +++++++++++++++++ .../controllers/dnp_entry_controller.ex | 28 ++++++++++++++++++ .../controllers/profile_controller.ex | 29 +++++++++++++++++++ .../templates/admin/report/show.html.slime | 6 +++- .../templates/dnp_entry/show.html.slime | 4 +++ .../templates/profile/show.html.slime | 14 +++++++++ lib/philomena_web/views/profile_view.ex | 3 ++ 7 files changed, 110 insertions(+), 1 deletion(-) diff --git a/lib/philomena_web/controllers/admin/report_controller.ex b/lib/philomena_web/controllers/admin/report_controller.ex index 1dac7b70..4cb7f4c6 100644 --- a/lib/philomena_web/controllers/admin/report_controller.ex +++ b/lib/philomena_web/controllers/admin/report_controller.ex @@ -5,11 +5,13 @@ defmodule PhilomenaWeb.Admin.ReportController do alias Philomena.Reports.Report alias Philomena.Reports.Query alias Philomena.Polymorphic + alias Philomena.ModNotes.ModNote alias Philomena.Repo import Ecto.Query plug :verify_authorized plug :load_and_authorize_resource, model: Report, only: [:show], preload: [:admin, user: [:linked_tags, awards: :badge]] + plug :set_mod_notes when action in [:show] def index(conn, %{"rq" => query_string}) do {:ok, query} = Query.compile(query_string) @@ -88,4 +90,29 @@ defmodule PhilomenaWeb.Admin.ReportController do false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) end end + + defp set_mod_notes(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do + true -> + report = conn.assigns.report + + mod_notes = + ModNote + |> where(notable_type: "Report", notable_id: ^report.id) + |> order_by(desc: :id) + |> preload(:moderator) + |> Repo.all() + |> Polymorphic.load_polymorphic(notable: [notable_id: :notable_type]) + + mod_notes = + mod_notes + |> Renderer.render_collection(conn) + |> Enum.zip(mod_notes) + + assign(conn, :mod_notes, mod_notes) + + _false -> + conn + end + end end diff --git a/lib/philomena_web/controllers/dnp_entry_controller.ex b/lib/philomena_web/controllers/dnp_entry_controller.ex index 5f4eab5e..37140ab5 100644 --- a/lib/philomena_web/controllers/dnp_entry_controller.ex +++ b/lib/philomena_web/controllers/dnp_entry_controller.ex @@ -5,11 +5,14 @@ defmodule PhilomenaWeb.DnpEntryController do alias Philomena.Textile.Renderer alias Philomena.DnpEntries alias Philomena.Tags.Tag + alias Philomena.ModNotes.ModNote + alias Philomena.Polymorphic alias Philomena.Repo import Ecto.Query plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create] plug :load_and_authorize_resource, model: DnpEntry, only: [:show, :edit, :update], preload: [:tag] + plug :set_mod_notes when action in [:show] def index(%{assigns: %{current_user: user}} = conn, %{"mine" => _mine}) when not is_nil(user) do DnpEntry @@ -104,4 +107,29 @@ defmodule PhilomenaWeb.DnpEntryController do |> Repo.preload(:linked_tags) |> Map.get(:linked_tags) end + + 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 + + mod_notes = + ModNote + |> where(notable_type: "DnpEntry", notable_id: ^dnp_entry.id) + |> order_by(desc: :id) + |> preload(:moderator) + |> Repo.all() + |> Polymorphic.load_polymorphic(notable: [notable_id: :notable_type]) + + mod_notes = + mod_notes + |> Renderer.render_collection(conn) + |> Enum.zip(mod_notes) + + assign(conn, :mod_notes, mod_notes) + + _false -> + conn + end + end end diff --git a/lib/philomena_web/controllers/profile_controller.ex b/lib/philomena_web/controllers/profile_controller.ex index dc8d1eda..9411ad6f 100644 --- a/lib/philomena_web/controllers/profile_controller.ex +++ b/lib/philomena_web/controllers/profile_controller.ex @@ -13,6 +13,8 @@ defmodule PhilomenaWeb.ProfileController do alias Philomena.Tags.Tag alias Philomena.UserIps.UserIp alias Philomena.UserFingerprints.UserFingerprint + alias Philomena.ModNotes.ModNote + alias Philomena.Polymorphic alias Philomena.Repo import Ecto.Query @@ -20,6 +22,7 @@ defmodule PhilomenaWeb.ProfileController do awards: :badge, public_links: :tag, verified_links: :tag, commission: [sheet_image: :tags, items: [example_image: :tags]] ] plug :set_admin_metadata + plug :set_mod_notes def show(conn, _params) do current_user = conn.assigns.current_user @@ -213,6 +216,7 @@ defmodule PhilomenaWeb.ProfileController do |> limit(1) |> Repo.one() + conn |> assign(:filter, filter) |> assign(:last_ip, last_ip) @@ -222,4 +226,29 @@ defmodule PhilomenaWeb.ProfileController do conn end end + + defp set_mod_notes(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do + true -> + user = conn.assigns.user + + mod_notes = + ModNote + |> where(notable_type: "User", notable_id: ^user.id) + |> order_by(desc: :id) + |> preload(:moderator) + |> Repo.all() + |> Polymorphic.load_polymorphic(notable: [notable_id: :notable_type]) + + mod_notes = + mod_notes + |> Renderer.render_collection(conn) + |> Enum.zip(mod_notes) + + assign(conn, :mod_notes, mod_notes) + + _false -> + conn + end + end end diff --git a/lib/philomena_web/templates/admin/report/show.html.slime b/lib/philomena_web/templates/admin/report/show.html.slime index 03a00fb3..52962f74 100644 --- a/lib/philomena_web/templates/admin/report/show.html.slime +++ b/lib/philomena_web/templates/admin/report/show.html.slime @@ -28,6 +28,10 @@ article.block.communication code = @report.user_agent += if assigns[:mod_notes] do + h4 Mod Notes + = render PhilomenaWeb.Admin.ModNoteView, "_table.html", mod_notes: @mod_notes, conn: @conn + p = if @report.user do => link "Send PM", to: Routes.conversation_path(@conn, :new, recipient: @report.user.name), class: "button button--link" @@ -40,4 +44,4 @@ p - else => link "Claim", to: "#", class: "button", data: [method: "post"] -= link "Back", to: Routes.admin_report_path(@conn, :index), class: "button button-link" \ No newline at end of file += link "Back", to: Routes.admin_report_path(@conn, :index), class: "button button-link" diff --git a/lib/philomena_web/templates/dnp_entry/show.html.slime b/lib/philomena_web/templates/dnp_entry/show.html.slime index 58257293..a6a64b80 100644 --- a/lib/philomena_web/templates/dnp_entry/show.html.slime +++ b/lib/philomena_web/templates/dnp_entry/show.html.slime @@ -49,3 +49,7 @@ h2 td Status: td = String.capitalize(@dnp_entry.aasm_state) + += if assigns[:mod_notes] do + h4 Mod Notes + = render PhilomenaWeb.Admin.ModNoteView, "_table.html", mod_notes: @mod_notes, conn: @conn diff --git a/lib/philomena_web/templates/profile/show.html.slime b/lib/philomena_web/templates/profile/show.html.slime index ad8c0b36..6921ce5c 100644 --- a/lib/philomena_web/templates/profile/show.html.slime +++ b/lib/philomena_web/templates/profile/show.html.slime @@ -99,6 +99,20 @@ = render PhilomenaWeb.ProfileView, "_about_me.html", user: @user, about_me: @about_me, conn: @conn + = if can_read_mod_notes?(@conn) do + .block + a.block__header--single-item href=Routes.profile_detail_path(@conn, :index, @user) Mod Notes + table.table + thead + tr + th Note + th Created + tbody + = for {body, mod_note} <- @mod_notes do + tr + td == body + td = pretty_time(mod_note.created_at) + .column-layout__main = render PhilomenaWeb.ProfileView, "_statistics.html", user: @user, statistics: @statistics, conn: @conn = render PhilomenaWeb.ProfileView, "_recent_images.html", title: "Recent Artwork", images: @recent_artwork, view_all_path: Routes.search_path(@conn, :index, q: tag_disjunction(@tags)), conn: @conn diff --git a/lib/philomena_web/views/profile_view.ex b/lib/philomena_web/views/profile_view.ex index 028818d6..93fa0520 100644 --- a/lib/philomena_web/views/profile_view.ex +++ b/lib/philomena_web/views/profile_view.ex @@ -59,6 +59,9 @@ defmodule PhilomenaWeb.ProfileView do def can_index_user?(conn), do: can?(conn, :index, Philomena.Users.User) + def can_read_mod_notes?(conn), + do: can?(conn, :index, Philomena.ModNotes.ModNote) + def enabled_text(true), do: "Enabled" def enabled_text(_else), do: "Disabled"