filter out private / unpublished notes for page controller

This commit is contained in:
Inhji 2023-03-14 23:48:58 +01:00
parent 4f3c4f7d25
commit 6b687a8cca
3 changed files with 17 additions and 3 deletions

View file

@ -6,8 +6,10 @@ defmodule Chiya.Channels do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Chiya.Repo alias Chiya.Repo
alias Chiya.Channels.Channel alias Chiya.Channels.Channel
alias Chiya.Notes.Note
@preloads [:notes] @preloads [:notes]
@public_preloads [notes: (from n in Note, where: not is_nil(n.published_at))]
@doc """ @doc """
Returns the list of channels. Returns the list of channels.
@ -23,6 +25,7 @@ defmodule Chiya.Channels do
end end
def preload_channel(channel), do: Repo.preload(channel, @preloads) def preload_channel(channel), do: Repo.preload(channel, @preloads)
def preload_channel_public(channel), do: Repo.preload(channel, @public_preloads)
@doc """ @doc """
Gets a single channel. Gets a single channel.
@ -48,7 +51,8 @@ defmodule Chiya.Channels do
@doc """ @doc """
Gets a single channel by its slug with all associated entities preloaded. 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 """ @doc """
Creates a channel. Creates a channel.

View file

@ -45,6 +45,7 @@ defmodule ChiyaWeb do
import Plug.Conn import Plug.Conn
import ChiyaWeb.Gettext import ChiyaWeb.Gettext
import Phoenix.LiveView.Controller import Phoenix.LiveView.Controller
import ChiyaWeb.Error
unquote(verified_routes()) unquote(verified_routes())
end end

View file

@ -9,6 +9,7 @@ defmodule ChiyaWeb.PageController do
channel = channel =
if settings.home_channel_id != nil do if settings.home_channel_id != nil do
Chiya.Channels.get_channel_preloaded!(settings.home_channel_id) Chiya.Channels.get_channel_preloaded!(settings.home_channel_id)
|> Chiya.Channels.preload_channel_public()
else else
nil nil
end end
@ -17,12 +18,20 @@ defmodule ChiyaWeb.PageController do
end end
def channel(conn, %{"slug" => channel_slug}) do 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) render(conn, :channel, layout: {ChiyaWeb.Layouts, "public.html"}, channel: channel)
end end
def note(conn, %{"slug" => note_slug}) do def note(conn, %{"slug" => note_slug}) do
note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug) 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
end end