philomena/lib/philomena_web/controllers/setting_controller.ex

72 lines
2.2 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{})
|> 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.")
|> 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
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
case Users.update_settings(user, user_params) do
{:ok, _user} ->
{:ok, conn}
error ->
error
end
end
2019-12-16 20:24:38 +01:00
end