philomena/lib/philomena_web/views/layout_view.ex

139 lines
4.4 KiB
Elixir
Raw Normal View History

2019-08-15 02:32:32 +02:00
defmodule PhilomenaWeb.LayoutView do
use PhilomenaWeb, :view
2019-08-16 02:28:12 +02:00
2019-12-04 02:56:39 +01:00
alias PhilomenaWeb.ImageView
alias Philomena.Config
2019-12-21 20:51:43 +01:00
alias Plug.Conn
2019-12-04 02:56:39 +01:00
2019-11-17 23:51:14 +01:00
def layout_class(conn) do
conn.assigns[:layout_class] || "layout--narrow"
end
def container_class(%{use_centered_layout: false}), do: nil
2023-11-23 17:07:49 +01:00
def container_class(_user), do: "centered-layout"
2019-11-30 23:40:53 +01:00
2019-08-16 02:28:12 +02:00
def render_time(conn) do
(Time.diff(Time.utc_now(), conn.assigns[:start_time], :microsecond) / 1000.0)
|> Float.round(3)
|> Float.to_string()
end
2019-08-18 02:43:44 +02:00
def cdn_host do
Application.get_env(:philomena, :cdn_host)
end
2019-12-20 02:22:39 +01:00
defp ignored_tag_list(nil), do: []
defp ignored_tag_list([]), do: []
defp ignored_tag_list([{tag, _body, _dnp_entries}]), do: [tag.id]
defp ignored_tag_list(tags), do: Enum.map(tags, & &1.id)
2019-10-11 03:47:13 +02:00
def clientside_data(conn) do
2019-12-21 20:51:43 +01:00
conn = Conn.fetch_cookies(conn)
2019-10-11 03:47:13 +02:00
extra = Map.get(conn.assigns, :clientside_data, [])
interactions = Map.get(conn.assigns, :interactions, [])
user = conn.assigns.current_user
filter = conn.assigns.current_filter
data = [
filter_id: filter.id,
hidden_tag_list: Jason.encode!(filter.hidden_tag_ids),
hidden_filter: Philomena.Search.String.normalize(filter.hidden_complex_str || ""),
2019-10-11 03:47:13 +02:00
spoilered_tag_list: Jason.encode!(filter.spoilered_tag_ids),
spoilered_filter: Philomena.Search.String.normalize(filter.spoilered_complex_str || ""),
2019-10-11 03:47:13 +02:00
user_id: if(user, do: user.id, else: nil),
user_name: if(user, do: user.name, else: nil),
user_slug: if(user, do: user.slug, else: nil),
user_is_signed_in: if(user, do: "true", else: "false"),
user_can_edit_filter: if(user, do: filter.user_id == user.id, else: "false") |> to_string(),
2019-10-11 03:47:13 +02:00
spoiler_type: if(user, do: user.spoiler_type, else: "static"),
watched_tag_list: Jason.encode!(if(user, do: user.watched_tag_ids, else: [])),
fancy_tag_edit: if(user, do: user.fancy_tag_field_on_edit, else: "true") |> to_string(),
fancy_tag_upload: if(user, do: user.fancy_tag_field_on_upload, else: "true") |> to_string(),
2019-10-11 03:47:13 +02:00
interactions: Jason.encode!(interactions),
2019-12-20 23:35:46 +01:00
ignored_tag_list: Jason.encode!(ignored_tag_list(conn.assigns[:tags])),
hide_staff_tools: conn.cookies["hide_staff_tools"] |> to_string()
2019-10-11 03:47:13 +02:00
]
data = Keyword.merge(data, extra)
2020-03-31 02:12:25 +02:00
content_tag(:div, "", class: "js-datastore", data: data)
2019-10-11 03:47:13 +02:00
end
2019-11-16 01:59:28 +01:00
2019-11-29 18:03:39 +01:00
def footer_data do
2019-12-25 17:56:19 +01:00
Config.get(:footer)
2019-11-29 18:03:39 +01:00
end
2023-11-23 17:07:49 +01:00
# def stylesheet_path(conn, %{theme: "dark"}),
# do: Routes.static_path(conn, "/css/dark.css")
2019-11-16 01:59:28 +01:00
2023-11-23 17:07:49 +01:00
# def stylesheet_path(conn, %{theme: "red"}),
# do: Routes.static_path(conn, "/css/red.css")
2019-11-16 01:59:28 +01:00
def stylesheet_path(conn, _user),
2023-11-23 17:07:49 +01:00
do: Routes.static_path(conn, "/css/dark-blue.css")
2019-12-04 02:56:39 +01:00
def dark_stylesheet_path(conn),
2023-11-23 17:07:49 +01:00
do: Routes.static_path(conn, "/css/dark-blue.css")
2019-12-16 03:35:07 +01:00
def theme_name(%{theme: theme}), do: theme
def theme_name(_user), do: "default"
2019-12-04 02:56:39 +01:00
def artist_tags(tags),
2020-01-11 05:20:19 +01:00
do: Enum.filter(tags, &(&1.namespace == "artist"))
2019-12-04 22:47:46 +01:00
def opengraph?(conn),
2020-01-11 05:20:19 +01:00
do:
!is_nil(conn.assigns[:image]) and conn.assigns.image.__meta__.state == :loaded and
is_list(conn.assigns.image.tags)
2019-12-14 21:15:35 +01:00
def hides_images?(conn),
do: can?(conn, :hide, %Philomena.Images.Image{})
2019-12-14 21:15:35 +01:00
def manages_site_notices?(conn),
do: can?(conn, :index, Philomena.SiteNotices.SiteNotice)
def manages_tags?(conn),
2019-12-20 01:00:09 +01:00
do: can?(conn, :edit, %Philomena.Tags.Tag{})
2019-12-14 21:15:35 +01:00
def manages_users?(conn),
do: can?(conn, :index, Philomena.Users.User)
def manages_forums?(conn),
2019-12-20 01:00:09 +01:00
do: can?(conn, :edit, Philomena.Forums.Forum)
2019-12-14 21:15:35 +01:00
def manages_ads?(conn),
2019-12-20 01:00:09 +01:00
do: can?(conn, :index, Philomena.Adverts.Advert)
2019-12-14 21:15:35 +01:00
def manages_badges?(conn),
do: can?(conn, :index, Philomena.Badges.Badge)
def manages_static_pages?(conn),
2019-12-20 01:00:09 +01:00
do: can?(conn, :edit, %Philomena.StaticPages.StaticPage{})
2019-12-14 21:15:35 +01:00
def manages_mod_notes?(conn),
2019-12-20 01:00:09 +01:00
do: can?(conn, :index, Philomena.ModNotes.ModNote)
2019-12-14 21:15:35 +01:00
def manages_bans?(conn),
2019-12-20 01:00:09 +01:00
do: can?(conn, :create, Philomena.Bans.User)
2019-12-20 15:19:43 +01:00
2021-11-07 19:51:55 +01:00
def can_see_moderation_log?(conn),
do: can?(conn, :index, Philomena.ModerationLogs.ModerationLog)
2019-12-20 15:19:43 +01:00
def viewport_meta_tag(conn) do
ua = get_user_agent(conn)
case String.contains?(ua, ["Mobile", "webOS"]) do
2020-01-11 05:20:19 +01:00
true -> tag(:meta, name: "viewport", content: "width=device-width, initial-scale=1")
2019-12-20 15:19:43 +01:00
_false -> tag(:meta, name: "viewport", content: "width=1024, initial-scale=1")
end
end
defp get_user_agent(conn) do
case Plug.Conn.get_req_header(conn, "user-agent") do
[ua] -> ua
2020-01-11 05:20:19 +01:00
_ -> ""
2019-12-20 15:19:43 +01:00
end
end
2019-08-15 02:32:32 +02:00
end