chiya/lib/chiya_web/controllers/page_controller.ex

119 lines
2.8 KiB
Elixir
Raw Normal View History

2023-03-05 16:07:40 +01:00
defmodule ChiyaWeb.PageController do
use ChiyaWeb, :controller
2023-09-09 10:24:22 +02:00
alias Chiya.Channels
2023-03-05 16:07:40 +01:00
2023-07-03 21:08:34 +02:00
plug :put_layout, html: {ChiyaWeb.Layouts, :public}
2023-09-11 23:19:44 +02:00
plug :put_assigns
defp put_assigns(conn, opts) do
conn
|> assign(:page_header, true)
end
2023-07-03 21:08:34 +02:00
2023-09-09 16:08:30 +02:00
def home(conn, params) do
settings = conn.assigns.settings
2023-09-09 16:08:30 +02:00
{channel, notes, meta} =
2023-03-31 16:58:10 +02:00
case settings.home_channel_id do
2023-09-09 16:08:30 +02:00
nil ->
nil
id ->
channel = Channels.get_channel!(id)
{:ok, {notes, meta}} = Chiya.Notes.list_home_notes(channel, params)
{channel, notes, meta}
2023-03-31 16:58:10 +02:00
end
2023-03-31 16:58:10 +02:00
render(conn, :home,
channel: channel,
2023-09-09 16:08:30 +02:00
notes: notes,
meta: meta,
2023-09-11 23:19:44 +02:00
page_title: "Home",
page_header: false
2023-03-31 16:58:10 +02:00
)
2023-03-05 16:07:40 +01:00
end
2023-03-09 14:43:31 +01:00
def channel(conn, %{"slug" => channel_slug}) do
channel =
2023-09-09 10:24:22 +02:00
Channels.get_channel_by_slug!(channel_slug)
|> Channels.preload_channel_public()
2023-03-31 16:58:10 +02:00
render(conn, :channel,
channel: channel,
2023-09-11 23:19:44 +02:00
page_title: channel.name,
content: channel.content
2023-03-31 16:58:10 +02:00
)
2023-03-09 14:43:31 +01:00
end
2023-04-10 19:18:27 +02:00
def tag(conn, %{"slug" => tag_slug}) do
tag = Chiya.Tags.get_tag_by_slug!(tag_slug)
render(conn, :tag,
tag: tag,
2023-09-11 23:19:44 +02:00
page_title: "Tagged with #{tag.name}"
2023-04-10 19:18:27 +02:00
)
end
def note(conn, %{"slug" => note_slug}) do
note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug)
2023-04-30 13:35:22 +02:00
changeset = Chiya.Notes.change_note_comment(%Chiya.Notes.NoteComment{}, %{note_id: note.id})
2023-09-09 10:24:22 +02:00
if note.published_at || conn.assigns.current_user do
2023-03-31 16:58:10 +02:00
render(conn, :note,
note: note,
2023-09-11 23:19:44 +02:00
changeset: changeset,
2023-04-30 13:35:22 +02:00
page_title: note.name,
2023-09-11 23:19:44 +02:00
page_header: false
2023-03-31 16:58:10 +02:00
)
2023-09-09 10:24:22 +02:00
else
render_error(conn, :not_found)
end
end
2023-06-02 07:31:47 +02:00
def about(conn, _params) do
2023-07-03 20:38:35 +02:00
note = Chiya.Notes.get_note_by_slug_preloaded("about")
user = Chiya.Accounts.get_user!(1)
2023-09-09 10:24:22 +02:00
if note && user do
render(conn, :about,
note: note,
user: user,
2023-09-11 23:19:44 +02:00
page_title: user.name
2023-09-09 10:24:22 +02:00
)
else
render_error(conn, :not_found)
end
2023-06-02 07:31:47 +02:00
end
def wiki(conn, _params) do
2023-09-09 10:24:22 +02:00
if id = conn.assigns.settings.wiki_channel_id do
channel = Chiya.Channels.get_channel!(id)
notes = Chiya.Notes.list_notes_by_channel_updated(channel, 999)
render(conn, :wiki,
channel: channel,
notes: notes,
2023-09-11 23:19:44 +02:00
page_title: channel.name,
content: channel.content
2023-09-09 10:24:22 +02:00
)
else
render_error(conn, :not_found)
2023-09-09 16:08:30 +02:00
end
end
def bookmarks(conn, _params) do
2023-09-09 10:24:22 +02:00
if id = conn.assigns.settings.bookmark_channel_id do
channel = Chiya.Channels.get_channel!(id)
notes = Chiya.Notes.list_notes_by_channel_published(channel, 999)
render(conn, :bookmarks,
channel: channel,
notes: notes,
2023-09-11 23:19:44 +02:00
page_title: "#{Enum.count(notes)} Bookmarks"
2023-09-09 10:24:22 +02:00
)
else
render_error(conn, :not_found)
2023-09-09 16:08:30 +02:00
end
2023-08-01 07:32:06 +02:00
end
2023-03-05 16:07:40 +01:00
end