display mod notes on profiles and such

This commit is contained in:
byte[] 2019-12-17 12:29:18 -05:00
parent 3aa730a325
commit 0fa10103f8
7 changed files with 110 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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"
= link "Back", to: Routes.admin_report_path(@conn, :index), class: "button button-link"

View file

@ -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

View file

@ -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

View file

@ -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"