mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-02-18 19:34:23 +01:00
Simplify mod note lookup in controllers
This commit is contained in:
parent
93a6d8d117
commit
f3805d55fe
7 changed files with 45 additions and 65 deletions
|
@ -10,8 +10,30 @@ defmodule Philomena.ModNotes do
|
||||||
alias Philomena.Polymorphic
|
alias Philomena.Polymorphic
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns a `m:Scrivener.Page` of 2-tuples of messages and rendered output
|
Returns a list of 2-tuples of mod notes and rendered output for the notable type and id.
|
||||||
for the query string current pagination.
|
|
||||||
|
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
|
All mod notes containing the substring `query_string` are matched and returned
|
||||||
case-insensitively.
|
case-insensitively.
|
||||||
|
@ -31,13 +53,13 @@ defmodule Philomena.ModNotes do
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@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.
|
for the current pagination.
|
||||||
|
|
||||||
When coerced to a list and rendered as Markdown, the result may look like:
|
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
|
## Examples
|
||||||
|
@ -53,10 +75,14 @@ defmodule Philomena.ModNotes do
|
||||||
|> order_by(desc: :id)
|
|> order_by(desc: :id)
|
||||||
|> Repo.paginate(pagination)
|
|> 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)
|
bodies = collection_renderer.(mod_notes)
|
||||||
preloaded = Polymorphic.load_polymorphic(mod_notes, notable: [notable_id: :notable_type])
|
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
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -6,7 +6,7 @@ defmodule PhilomenaWeb.Admin.ReportController do
|
||||||
alias Philomena.Reports.Report
|
alias Philomena.Reports.Report
|
||||||
alias Philomena.Reports.Query
|
alias Philomena.Reports.Query
|
||||||
alias Philomena.Polymorphic
|
alias Philomena.Polymorphic
|
||||||
alias Philomena.ModNotes.ModNote
|
alias Philomena.ModNotes
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
|
@ -128,19 +128,8 @@ defmodule PhilomenaWeb.Admin.ReportController do
|
||||||
true ->
|
true ->
|
||||||
report = conn.assigns.report
|
report = conn.assigns.report
|
||||||
|
|
||||||
mod_notes =
|
renderer = &MarkdownRenderer.render_collection(&1, conn)
|
||||||
ModNote
|
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("Report", report.id, renderer)
|
||||||
|> 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)
|
|
||||||
|
|
||||||
assign(conn, :mod_notes, mod_notes)
|
assign(conn, :mod_notes, mod_notes)
|
||||||
|
|
||||||
_false ->
|
_false ->
|
||||||
|
|
|
@ -5,8 +5,7 @@ defmodule PhilomenaWeb.DnpEntryController do
|
||||||
alias PhilomenaWeb.MarkdownRenderer
|
alias PhilomenaWeb.MarkdownRenderer
|
||||||
alias Philomena.DnpEntries
|
alias Philomena.DnpEntries
|
||||||
alias Philomena.Tags.Tag
|
alias Philomena.Tags.Tag
|
||||||
alias Philomena.ModNotes.ModNote
|
alias Philomena.ModNotes
|
||||||
alias Philomena.Polymorphic
|
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
|
@ -154,19 +153,8 @@ defmodule PhilomenaWeb.DnpEntryController do
|
||||||
true ->
|
true ->
|
||||||
dnp_entry = conn.assigns.dnp_entry
|
dnp_entry = conn.assigns.dnp_entry
|
||||||
|
|
||||||
mod_notes =
|
renderer = &MarkdownRenderer.render_collection(&1, conn)
|
||||||
ModNote
|
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("DnpEntry", dnp_entry.id, renderer)
|
||||||
|> 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)
|
|
||||||
|
|
||||||
assign(conn, :mod_notes, mod_notes)
|
assign(conn, :mod_notes, mod_notes)
|
||||||
|
|
||||||
_false ->
|
_false ->
|
||||||
|
|
|
@ -2,9 +2,8 @@ defmodule PhilomenaWeb.Profile.DetailController do
|
||||||
use PhilomenaWeb, :controller
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
alias Philomena.UserNameChanges.UserNameChange
|
alias Philomena.UserNameChanges.UserNameChange
|
||||||
alias Philomena.ModNotes.ModNote
|
alias Philomena.ModNotes
|
||||||
alias PhilomenaWeb.MarkdownRenderer
|
alias PhilomenaWeb.MarkdownRenderer
|
||||||
alias Philomena.Polymorphic
|
|
||||||
alias Philomena.Users.User
|
alias Philomena.Users.User
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
@ -20,18 +19,8 @@ defmodule PhilomenaWeb.Profile.DetailController do
|
||||||
def index(conn, _params) do
|
def index(conn, _params) do
|
||||||
user = conn.assigns.user
|
user = conn.assigns.user
|
||||||
|
|
||||||
mod_notes =
|
renderer = &MarkdownRenderer.render_collection(&1, conn)
|
||||||
ModNote
|
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("User", 1, renderer)
|
||||||
|> 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)
|
|
||||||
|
|
||||||
name_changes =
|
name_changes =
|
||||||
UserNameChange
|
UserNameChange
|
||||||
|
|
|
@ -14,8 +14,7 @@ defmodule PhilomenaWeb.ProfileController do
|
||||||
alias Philomena.Tags.Tag
|
alias Philomena.Tags.Tag
|
||||||
alias Philomena.UserIps.UserIp
|
alias Philomena.UserIps.UserIp
|
||||||
alias Philomena.UserFingerprints.UserFingerprint
|
alias Philomena.UserFingerprints.UserFingerprint
|
||||||
alias Philomena.ModNotes.ModNote
|
alias Philomena.ModNotes
|
||||||
alias Philomena.Polymorphic
|
|
||||||
alias Philomena.Images.Image
|
alias Philomena.Images.Image
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
@ -275,21 +274,10 @@ defmodule PhilomenaWeb.ProfileController do
|
||||||
defp set_mod_notes(conn, _opts) do
|
defp set_mod_notes(conn, _opts) do
|
||||||
case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do
|
case Canada.Can.can?(conn.assigns.current_user, :index, ModNote) do
|
||||||
true ->
|
true ->
|
||||||
|
renderer = &MarkdownRenderer.render_collection(&1, conn)
|
||||||
user = conn.assigns.user
|
user = conn.assigns.user
|
||||||
|
|
||||||
mod_notes =
|
mod_notes = ModNotes.list_all_mod_notes_by_type_and_id("User", user.id, renderer)
|
||||||
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)
|
|
||||||
|
|
||||||
assign(conn, :mod_notes, mod_notes)
|
assign(conn, :mod_notes, mod_notes)
|
||||||
|
|
||||||
_false ->
|
_false ->
|
||||||
|
|
|
@ -7,7 +7,7 @@ table.table
|
||||||
td Moderator
|
td Moderator
|
||||||
td Actions
|
td Actions
|
||||||
tbody
|
tbody
|
||||||
= for {body, note} <- @mod_notes do
|
= for {note, body} <- @mod_notes do
|
||||||
tr
|
tr
|
||||||
td
|
td
|
||||||
= link_to_noted_thing(note.notable)
|
= link_to_noted_thing(note.notable)
|
||||||
|
|
|
@ -144,7 +144,7 @@
|
||||||
th Note
|
th Note
|
||||||
th Created
|
th Created
|
||||||
tbody
|
tbody
|
||||||
= for {body, mod_note} <- @mod_notes do
|
= for {mod_note, body} <- @mod_notes do
|
||||||
tr
|
tr
|
||||||
td = body
|
td = body
|
||||||
td = pretty_time(mod_note.created_at)
|
td = pretty_time(mod_note.created_at)
|
||||||
|
|
Loading…
Reference in a new issue