diff --git a/lib/philomena/mod_notes.ex b/lib/philomena/mod_notes.ex index 4c75e3bc..450216b0 100644 --- a/lib/philomena/mod_notes.ex +++ b/lib/philomena/mod_notes.ex @@ -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 world"} + {%ModNote{body: "hello *world*"}, "hello world"} ] ## 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 """ diff --git a/lib/philomena_web/controllers/admin/report_controller.ex b/lib/philomena_web/controllers/admin/report_controller.ex index e6fc6a97..3dc773ea 100644 --- a/lib/philomena_web/controllers/admin/report_controller.ex +++ b/lib/philomena_web/controllers/admin/report_controller.ex @@ -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 -> diff --git a/lib/philomena_web/controllers/dnp_entry_controller.ex b/lib/philomena_web/controllers/dnp_entry_controller.ex index d2e675b2..a71407ac 100644 --- a/lib/philomena_web/controllers/dnp_entry_controller.ex +++ b/lib/philomena_web/controllers/dnp_entry_controller.ex @@ -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 -> diff --git a/lib/philomena_web/controllers/profile/detail_controller.ex b/lib/philomena_web/controllers/profile/detail_controller.ex index 0461c31e..c891b6ca 100644 --- a/lib/philomena_web/controllers/profile/detail_controller.ex +++ b/lib/philomena_web/controllers/profile/detail_controller.ex @@ -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 diff --git a/lib/philomena_web/controllers/profile_controller.ex b/lib/philomena_web/controllers/profile_controller.ex index 75f476d4..5cda70a3 100644 --- a/lib/philomena_web/controllers/profile_controller.ex +++ b/lib/philomena_web/controllers/profile_controller.ex @@ -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 -> diff --git a/lib/philomena_web/templates/admin/mod_note/_table.html.slime b/lib/philomena_web/templates/admin/mod_note/_table.html.slime index 45207800..b872ace6 100644 --- a/lib/philomena_web/templates/admin/mod_note/_table.html.slime +++ b/lib/philomena_web/templates/admin/mod_note/_table.html.slime @@ -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) diff --git a/lib/philomena_web/templates/profile/show.html.slime b/lib/philomena_web/templates/profile/show.html.slime index 6039a48d..b8644222 100644 --- a/lib/philomena_web/templates/profile/show.html.slime +++ b/lib/philomena_web/templates/profile/show.html.slime @@ -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)