philomena/lib/philomena_web/controllers/setting_controller.ex

89 lines
2.7 KiB
Elixir
Raw Permalink Normal View History

2019-11-30 23:40:53 +01:00
defmodule PhilomenaWeb.SettingController do
use PhilomenaWeb, :controller
alias Philomena.Users
alias Philomena.Users.User
alias Philomena.Schema.TagList
alias Plug.Conn
def edit(conn, _params) do
changeset =
(conn.assigns.current_user || %User{})
2024-07-03 21:32:27 +02:00
|> assign_theme()
2019-11-30 23:40:53 +01:00
|> TagList.assign_tag_list(:watched_tag_ids, :watched_tag_list)
|> Users.change_user()
2019-12-16 20:24:38 +01:00
render(conn, "edit.html", title: "Editing Settings", changeset: changeset)
2019-11-30 23:40:53 +01:00
end
def update(conn, %{"user" => user_params}) do
user = conn.assigns.current_user
conn
|> update_local_settings(user_params)
|> maybe_update_user(user, user_params)
|> case do
{:ok, conn} ->
conn
|> put_flash(:info, "Settings updated successfully.")
2024-06-06 22:28:35 +02:00
|> redirect(to: ~p"/settings/edit")
2019-11-30 23:40:53 +01:00
{:error, changeset} ->
conn
|> put_flash(:error, "Your settings could not be saved!")
|> render("edit.html", changeset: changeset)
end
end
defp update_local_settings(conn, user_params) do
conn
|> set_cookie(user_params, "hidpi", "hidpi")
|> set_cookie(user_params, "webm", "webm")
|> set_cookie(user_params, "serve_webm", "serve_webm")
|> set_cookie(user_params, "unmute_videos", "unmute_videos")
2019-11-30 23:40:53 +01:00
|> set_cookie(user_params, "chan_nsfw", "chan_nsfw")
2019-12-20 23:35:46 +01:00
|> set_cookie(user_params, "hide_staff_tools", "hide_staff_tools")
|> set_cookie(user_params, "hide_uploader", "hide_uploader")
2020-12-16 15:35:58 +01:00
|> set_cookie(user_params, "hide_score", "hide_score")
|> set_cookie(user_params, "unfilter_tag_suggestions", "unfilter_tag_suggestions")
|> set_cookie(user_params, "enable_search_ac", "enable_search_ac")
2019-11-30 23:40:53 +01:00
end
defp set_cookie(conn, params, param_name, cookie_name) do
# JS wants access; max-age is set to 25 years from now
2020-01-11 05:20:19 +01:00
Conn.put_resp_cookie(conn, cookie_name, to_string(params[param_name] == "true"),
max_age: 788_923_800,
http_only: false,
extra: "SameSite=Lax"
2020-01-11 05:20:19 +01:00
)
2019-11-30 23:40:53 +01:00
end
2024-07-03 21:32:27 +02:00
defp assign_theme(%{theme: theme} = user) do
[theme_name, theme_color] = String.split(theme, "-")
2024-05-07 19:33:56 +02:00
2024-07-03 21:32:27 +02:00
user
|> Map.put(:theme_name, theme_name)
|> Map.put(:theme_color, theme_color)
end
defp assign_theme(_), do: assign_theme(%{theme: "dark-blue"})
defp determine_theme(%{"theme_name" => name, "theme_color" => color} = attrs)
when name != nil and color != nil,
do: Map.put(attrs, "theme", "#{name}-#{color}")
2024-05-07 19:33:56 +02:00
2024-07-03 21:32:27 +02:00
defp determine_theme(attrs), do: Map.put(attrs, "theme", "dark-blue")
2024-05-07 19:33:56 +02:00
2019-11-30 23:40:53 +01:00
defp maybe_update_user(conn, nil, _user_params), do: {:ok, conn}
2020-01-11 05:20:19 +01:00
2019-11-30 23:40:53 +01:00
defp maybe_update_user(conn, user, user_params) do
2024-05-07 19:33:56 +02:00
case Users.update_settings(user, determine_theme(user_params)) do
2019-11-30 23:40:53 +01:00
{:ok, _user} ->
{:ok, conn}
error ->
error
end
end
2019-12-16 20:24:38 +01:00
end