philomena/lib/philomena_web/plugs/admin_counters_plug.ex

52 lines
1.5 KiB
Elixir
Raw Permalink Normal View History

2019-12-05 00:15:54 +01:00
defmodule PhilomenaWeb.AdminCountersPlug do
@moduledoc """
This plug stores the counts used by the admin bar.
## Example
plug PhilomenaWeb.AdminCountersPlug
"""
alias Philomena.DuplicateReports
alias Philomena.Reports
alias Philomena.ArtistLinks
2019-12-05 00:15:54 +01:00
alias Philomena.DnpEntries
2022-03-24 17:31:57 +01:00
alias Philomena.Images
2019-12-05 00:15:54 +01:00
import Plug.Conn, only: [assign: 3]
@doc false
@spec init(any()) :: any()
def init(opts), do: opts
@doc false
@spec call(Plug.Conn.t()) :: Plug.Conn.t()
def call(conn), do: call(conn, nil)
@doc false
@spec call(Plug.Conn.t(), any()) :: Plug.Conn.t()
def call(conn, _opts) do
user = conn.assigns.current_user
2019-12-05 20:31:29 +01:00
maybe_assign_admin_metrics(conn, user, staff?(user))
2019-12-05 00:15:54 +01:00
end
2019-12-05 20:31:29 +01:00
defp maybe_assign_admin_metrics(conn, _user, false), do: conn
2020-01-11 05:20:19 +01:00
2019-12-05 20:31:29 +01:00
defp maybe_assign_admin_metrics(conn, user, true) do
pending_approvals = Images.count_pending_approvals(user)
2019-12-05 20:31:29 +01:00
duplicate_reports = DuplicateReports.count_duplicate_reports(user)
reports = Reports.count_reports(user)
artist_links = ArtistLinks.count_artist_links(user)
2019-12-05 20:31:29 +01:00
dnps = DnpEntries.count_dnp_entries(user)
2019-12-05 00:15:54 +01:00
conn
2022-03-24 17:31:57 +01:00
|> assign(:pending_approval_count, pending_approvals)
2019-12-05 00:15:54 +01:00
|> assign(:duplicate_report_count, duplicate_reports)
|> assign(:report_count, reports)
|> assign(:artist_link_count, artist_links)
2019-12-05 00:15:54 +01:00
|> assign(:dnp_entry_count, dnps)
end
defp staff?(%{role: role}) when role in ["assistant", "moderator", "admin"], do: true
defp staff?(_user), do: false
end