filter out private / unpublished notes for page controller
This commit is contained in:
parent
4f3c4f7d25
commit
6b687a8cca
3 changed files with 17 additions and 3 deletions
|
@ -6,8 +6,10 @@ defmodule Chiya.Channels do
|
|||
import Ecto.Query, warn: false
|
||||
alias Chiya.Repo
|
||||
alias Chiya.Channels.Channel
|
||||
alias Chiya.Notes.Note
|
||||
|
||||
@preloads [:notes]
|
||||
@public_preloads [notes: (from n in Note, where: not is_nil(n.published_at))]
|
||||
|
||||
@doc """
|
||||
Returns the list of channels.
|
||||
|
@ -23,6 +25,7 @@ defmodule Chiya.Channels do
|
|||
end
|
||||
|
||||
def preload_channel(channel), do: Repo.preload(channel, @preloads)
|
||||
def preload_channel_public(channel), do: Repo.preload(channel, @public_preloads)
|
||||
|
||||
@doc """
|
||||
Gets a single channel.
|
||||
|
@ -48,7 +51,8 @@ defmodule Chiya.Channels do
|
|||
@doc """
|
||||
Gets a single channel by its slug with all associated entities preloaded.
|
||||
"""
|
||||
def get_channel_by_slug_preloaded!(slug), do: Repo.get_by!(Channel, slug: slug) |> preload_channel()
|
||||
def get_channel_by_slug!(slug), do: Repo.get_by!(Channel, slug: slug)
|
||||
|
||||
@doc """
|
||||
Creates a channel.
|
||||
|
||||
|
|
|
@ -45,6 +45,7 @@ defmodule ChiyaWeb do
|
|||
import Plug.Conn
|
||||
import ChiyaWeb.Gettext
|
||||
import Phoenix.LiveView.Controller
|
||||
import ChiyaWeb.Error
|
||||
|
||||
unquote(verified_routes())
|
||||
end
|
||||
|
|
|
@ -9,6 +9,7 @@ defmodule ChiyaWeb.PageController do
|
|||
channel =
|
||||
if settings.home_channel_id != nil do
|
||||
Chiya.Channels.get_channel_preloaded!(settings.home_channel_id)
|
||||
|> Chiya.Channels.preload_channel_public()
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
@ -17,12 +18,20 @@ defmodule ChiyaWeb.PageController do
|
|||
end
|
||||
|
||||
def channel(conn, %{"slug" => channel_slug}) do
|
||||
channel = Chiya.Channels.get_channel_by_slug_preloaded!(channel_slug)
|
||||
channel =
|
||||
Chiya.Channels.get_channel_by_slug!(channel_slug)
|
||||
|> Chiya.Channels.preload_channel_public()
|
||||
|
||||
render(conn, :channel, layout: {ChiyaWeb.Layouts, "public.html"}, channel: channel)
|
||||
end
|
||||
|
||||
def note(conn, %{"slug" => note_slug}) do
|
||||
note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug)
|
||||
render(conn, :note, layout: {ChiyaWeb.Layouts, "public.html"}, note: note)
|
||||
|
||||
if is_nil(note.published_at) do
|
||||
render_error(conn, :not_found)
|
||||
else
|
||||
render(conn, :note, layout: {ChiyaWeb.Layouts, "public.html"}, note: note)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue