legendary-doc-site/apps/content/lib/content_web/controllers/posts_controller.ex

124 lines
3.3 KiB
Elixir
Raw Normal View History

defmodule Legendary.Content.PostsController do
use Legendary.Content, :controller
alias Legendary.Content.{Options, Posts}
plug :put_layout, false when action in [:preview]
2020-08-13 20:21:43 +00:00
def index(conn, %{"category" => _} = params) do
conn |> index_posts(params)
end
def index(conn, params) do
show_on_front = Options.get_value("show_on_front") || "page"
case show_on_front do
"posts" ->
conn |> index_posts(params)
"page" ->
conn |> index_page(params)
end
end
def index_posts(conn, params) do
page = params |> Map.get("page", "1")
params = params |> Map.merge(%{"page" => page})
category = params |> Map.get("category")
posts = Posts.list_posts(params)
thumbs = posts |> Posts.thumbs_for_posts()
last_page = Posts.last_page(params)
conn
|> render(
"index.html",
[
posts: posts,
page: String.to_integer(page),
last_page: last_page,
thumbs: thumbs,
category: category,
]
)
end
def index_page(conn, _params) do
page_id = Options.get_value("page_on_front") || "index"
show(conn, %{"id" => page_id})
end
def preview(conn, %{"post" => post_params}) do
post = Posts.preview_post(post_params)
conn
|> render("show.html", post: post, page: 1, thumbs: [])
end
def show(conn, %{"id" => "blog", "page" => page_string}) do
conn |> index_posts(%{"id" => "blog", "page" => page_string})
end
2021-03-25 22:42:55 +00:00
def show(conn, %{"id" => id, "page" => page_string}) when is_binary(id) or is_integer(id) do
post = Posts.get_post(id)
if is_nil(post) do
try_static_post(conn, id)
else
conn |> show_one(post, page_string)
end
end
2021-03-25 22:42:55 +00:00
def show(conn, %{"id" => id, "page" => page_string}) when is_list(id) do
show(conn, %{"id" => Enum.join(id, "/"), "page" => page_string})
end
def show(conn, %{"id" => id}), do: show(conn, %{"id" => id, "page" => "1"})
defp try_static_post(conn, id) do
path = "static_pages/#{id}.html"
try do
2020-08-28 21:49:56 +00:00
render(conn, path)
rescue
e in Phoenix.Template.UndefinedError ->
case e do
%{template: ^path} ->
router =
case conn do
%{private: %{phoenix_router: router}} -> router
_ -> Legendary.Content.Router
end
# The static page we're looking for is missing, so this is just a 404
# credo:disable-for-next-line
raise Phoenix.Router.NoRouteError.exception(conn: conn, router: router)
_ ->
# We aren't missing the static page, we're missing a partial. This is probably
# a developer error, so bubble it up
reraise e, System.stacktrace
end
end
end
def show_one(conn, post, page_string) do
{front_page_id, _} = Options.get_value_as_int("page_on_front")
template =
if post.id == front_page_id do
"front.html"
else
"show.html"
end
page = String.to_integer(page_string)
thumbs = [post] |> Posts.thumbs_for_posts()
2020-07-28 15:54:24 +00:00
case post.type do
"attachment" ->
2020-07-28 15:54:24 +00:00
{:ok, decoded} = post.content |> Base.decode64
conn
2020-07-28 15:54:24 +00:00
|> put_resp_content_type(post.mime_type, "binary")
|> send_resp(conn.status || 200, decoded)
_ ->
render(conn, template, post: post, page: page, thumbs: thumbs)
end
end
end