read site notices

This commit is contained in:
byte[] 2019-11-18 12:32:23 -05:00
parent bd0711730e
commit 2f4ba6d96d
5 changed files with 43 additions and 3 deletions

View file

@ -17,8 +17,13 @@ defmodule Philomena.SiteNotices do
[%SiteNotice{}, ...]
"""
def list_site_notices do
Repo.all(SiteNotice)
def active_site_notices do
now = DateTime.utc_now()
SiteNotice
|> where([n], n.start_date < ^now and n.finish_date > ^now)
|> order_by(desc: :start_date)
|> Repo.all()
end
@doc """

View file

@ -15,7 +15,7 @@ defmodule Philomena.SiteNotices.SiteNotice do
field :start_date, :naive_datetime
field :finish_date, :naive_datetime
timestamps(inserted_at: :created_At)
timestamps(inserted_at: :created_at)
end
@doc false

View file

@ -0,0 +1,25 @@
defmodule PhilomenaWeb.SiteNoticePlug do
@moduledoc """
This plug stores the current site-wide notices.
## Example
plug PhilomenaWeb.SiteNoticePlug
"""
alias Plug.Conn
alias Philomena.SiteNotices
@doc false
@spec init(any()) :: any()
def init(opts), do: opts
@doc false
@spec call(Plug.Conn.t(), any()) :: Plug.Conn.t()
def call(conn, _opts) do
notices = SiteNotices.active_site_notices()
conn
|> Conn.assign(:site_notices, notices)
end
end

View file

@ -15,6 +15,7 @@ defmodule PhilomenaWeb.Router do
plug PhilomenaWeb.EnsureUserEnabledPlug
plug PhilomenaWeb.CurrentBanPlug
plug PhilomenaWeb.NotificationCountPlug
plug PhilomenaWeb.SiteNoticePlug
plug PhilomenaWeb.FilterSelectPlug
end

View file

@ -1,3 +1,12 @@
= for notice <- @conn.assigns.site_notices do
.flash.flash--site-notice
strong>
= notice.title
=> notice.text
= if notice.link not in [nil, ""] do
a href=notice.link
= notice.link_text
noscript.flash.flash--warning
strong You don't appear to have Javascript enabled
' If you're using an add-on like NoScript, please allow