chiya/lib/chiya_web/controllers/page_controller.ex

123 lines
2.9 KiB
Elixir
Raw Normal View History

2023-03-05 16:07:40 +01:00
defmodule ChiyaWeb.PageController do
use ChiyaWeb, :controller
2023-07-03 21:08:34 +02:00
plug :put_layout, html: {ChiyaWeb.Layouts, :public}
2023-03-05 16:07:40 +01:00
def home(conn, _params) do
settings = conn.assigns.settings
2023-03-31 16:58:10 +02:00
channel =
case settings.home_channel_id do
nil -> nil
id -> Chiya.Channels.get_channel!(id) |> Chiya.Channels.preload_channel_public()
end
2023-03-31 16:58:10 +02:00
render(conn, :home,
channel: channel,
page_title: "Home"
)
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 =
Chiya.Channels.get_channel_by_slug!(channel_slug)
|> Chiya.Channels.preload_channel_public()
2023-03-31 16:58:10 +02:00
render(conn, :channel,
channel: channel,
page_title: channel.name
)
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,
page_title: tag.name
)
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-04-30 11:56:45 +02:00
if is_nil(note.published_at) and is_nil(conn.assigns.current_user) do
render_error(conn, :not_found)
2023-03-31 16:58:10 +02:00
else
render(conn, :note,
note: note,
2023-04-30 13:35:22 +02:00
page_title: note.name,
changeset: changeset
2023-03-31 16:58:10 +02:00
)
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)
render(conn, :about,
note: note,
user: user,
page_title: "About"
)
2023-06-02 07:31:47 +02:00
end
def wiki(conn, _params) do
[channel, notes_updated, notes_published] =
case conn.assigns.settings.wiki_channel_id do
2023-07-09 19:58:31 +02:00
nil ->
[nil, nil, nil]
id ->
channel = Chiya.Channels.get_channel!(id)
2023-07-09 19:58:31 +02:00
updated = Chiya.Notes.list_notes_by_channel_updated(channel, 5)
published = Chiya.Notes.list_notes_by_channel_published(channel, 5)
[channel, updated, published]
end
render(conn, :wiki,
channel: channel,
notes_updated: notes_updated,
notes_published: notes_published,
page_title: "Wiki"
)
end
def bookmarks(conn, _params) do
2023-08-01 07:32:06 +02:00
[channel, notes, tags] =
case conn.assigns.settings.bookmark_channel_id do
nil ->
[nil, nil]
id ->
channel = Chiya.Channels.get_channel!(id)
notes = Chiya.Notes.list_notes_by_channel_published(channel, 999)
2023-08-01 07:32:06 +02:00
tags = group_tags(notes)
[channel, notes, tags]
end
render(conn, :bookmarks,
channel: channel,
notes: notes,
2023-08-01 07:32:06 +02:00
tags: tags,
page_title: "Bookmarks"
)
end
2023-08-01 07:32:06 +02:00
defp group_tags(notes) do
Enum.reduce(notes, [], fn n, acc ->
acc ++ n.tags
end)
|> Enum.uniq_by(fn t -> t.id end)
|> Enum.sort_by(fn t -> t.slug end, :asc)
|> Enum.group_by(
fn n -> String.first(n.name) end,
fn n -> n end
)
|> IO.inspect()
end
2023-03-05 16:07:40 +01:00
end