diff --git a/lib/chiya/channels.ex b/lib/chiya/channels.ex index 788c42d..63259eb 100644 --- a/lib/chiya/channels.ex +++ b/lib/chiya/channels.ex @@ -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. diff --git a/lib/chiya_web.ex b/lib/chiya_web.ex index 35f3ab7..7f807b4 100644 --- a/lib/chiya_web.ex +++ b/lib/chiya_web.ex @@ -45,6 +45,7 @@ defmodule ChiyaWeb do import Plug.Conn import ChiyaWeb.Gettext import Phoenix.LiveView.Controller + import ChiyaWeb.Error unquote(verified_routes()) end diff --git a/lib/chiya_web/controllers/page_controller.ex b/lib/chiya_web/controllers/page_controller.ex index 3000dd3..3b00020 100644 --- a/lib/chiya_web/controllers/page_controller.ex +++ b/lib/chiya_web/controllers/page_controller.ex @@ -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