Simplify mod note lookup in controllers

This commit is contained in:
Liam 2024-07-15 21:19:32 -04:00
parent 93a6d8d117
commit f3805d55fe
7 changed files with 45 additions and 65 deletions

View file

@ -10,8 +10,30 @@ defmodule Philomena.ModNotes do
alias Philomena.Polymorphic
@doc """
Returns a `m:Scrivener.Page` of 2-tuples of messages and rendered output
for the query string current pagination.
Returns a list of 2-tuples of mod notes and rendered output for the notable type and id.
See `list_mod_notes/3` for more information about collection rendering.
## Examples
iex> list_all_mod_notes_by_type_and_id("User", "1", & &1.body)
[
{%ModNote{body: "hello *world*"}, "hello *world*"}
]
"""
def list_all_mod_notes_by_type_and_id(notable_type, notable_id, collection_renderer) do
ModNote
|> where(notable_type: ^notable_type, notable_id: ^notable_id)
|> preload(:moderator)
|> order_by(desc: :id)
|> Repo.all()
|> preload_and_render(collection_renderer)
end
@doc """
Returns a `m:Scrivener.Page` of 2-tuples of mod notes and rendered output
for the query string and current pagination.
All mod notes containing the substring `query_string` are matched and returned
case-insensitively.
@ -31,13 +53,13 @@ defmodule Philomena.ModNotes do
end
@doc """
Returns a `m:Scrivener.Page` of 2-tuples of messages and rendered output
Returns a `m:Scrivener.Page` of 2-tuples of mod notes and rendered output
for the current pagination.
When coerced to a list and rendered as Markdown, the result may look like:
[
{%ModNote{body: "hello *world*"}, "hello <strong>world</strong>"}
{%ModNote{body: "hello *world*"}, "hello <em>world</em>"}
]
## Examples
@ -53,10 +75,14 @@ defmodule Philomena.ModNotes do
|> order_by(desc: :id)
|> Repo.paginate(pagination)
put_in(mod_notes.entries, preload_and_render(mod_notes, collection_renderer))
end
defp preload_and_render(mod_notes, collection_renderer) do
bodies = collection_renderer.(mod_notes)
preloaded = Polymorphic.load_polymorphic(mod_notes, notable: [notable_id: :notable_type])
put_in(mod_notes.entries, Enum.zip(bodies, preloaded))
Enum.zip(preloaded, bodies)
end
@doc """

View file

@ -6,7 +6,7 @@ defmodule PhilomenaWeb.Admin.ReportController do
alias Philomena.Reports.Report
alias Philomena.Reports.Query
alias Philomena.Polymorphic
alias Philomena.ModNotes.ModNote
alias Philomena.ModNotes
alias Philomena.Repo
import Ecto.Query
@ -128,19 +128,8 @@ defmodule PhilomenaWeb.Admin.ReportController 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
|> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes)
renderer = &MarkdownRenderer.render_collection(&1, conn)
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("Report", report.id, renderer)
assign(conn, :mod_notes, mod_notes)
_false ->

View file

@ -5,8 +5,7 @@ defmodule PhilomenaWeb.DnpEntryController do
alias PhilomenaWeb.MarkdownRenderer
alias Philomena.DnpEntries
alias Philomena.Tags.Tag
alias Philomena.ModNotes.ModNote
alias Philomena.Polymorphic
alias Philomena.ModNotes
alias Philomena.Repo
import Ecto.Query
@ -154,19 +153,8 @@ defmodule PhilomenaWeb.DnpEntryController 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
|> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes)
renderer = &MarkdownRenderer.render_collection(&1, conn)
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("DnpEntry", dnp_entry.id, renderer)
assign(conn, :mod_notes, mod_notes)
_false ->

View file

@ -2,9 +2,8 @@ defmodule PhilomenaWeb.Profile.DetailController do
use PhilomenaWeb, :controller
alias Philomena.UserNameChanges.UserNameChange
alias Philomena.ModNotes.ModNote
alias Philomena.ModNotes
alias PhilomenaWeb.MarkdownRenderer
alias Philomena.Polymorphic
alias Philomena.Users.User
alias Philomena.Repo
import Ecto.Query
@ -20,18 +19,8 @@ defmodule PhilomenaWeb.Profile.DetailController do
def index(conn, _params) do
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
|> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes)
renderer = &MarkdownRenderer.render_collection(&1, conn)
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("User", 1, renderer)
name_changes =
UserNameChange

View file

@ -14,8 +14,7 @@ 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.ModNotes
alias Philomena.Images.Image
alias Philomena.Repo
import Ecto.Query
@ -275,21 +274,10 @@ defmodule PhilomenaWeb.ProfileController do
defp set_mod_notes(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do
true ->
renderer = &MarkdownRenderer.render_collection(&1, conn)
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
|> MarkdownRenderer.render_collection(conn)
|> Enum.zip(mod_notes)
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("User", user.id, renderer)
assign(conn, :mod_notes, mod_notes)
_false ->

View file

@ -7,7 +7,7 @@ table.table
td Moderator
td Actions
tbody
= for {body, note} <- @mod_notes do
= for {note, body} <- @mod_notes do
tr
td
= link_to_noted_thing(note.notable)

View file

@ -144,7 +144,7 @@
th Note
th Created
tbody
= for {body, mod_note} <- @mod_notes do
= for {mod_note, body} <- @mod_notes do
tr
td = body
td = pretty_time(mod_note.created_at)