Fix code smell

This commit is contained in:
Alex
2023-09-09 17:56:06 +02:00
parent f2106bbcb0
commit e4cd8379c1
8 changed files with 109 additions and 35 deletions

View File

@@ -1,3 +1,6 @@
@moduledoc """
Plug for user session token.
"""
defmodule Claper.Accounts.UserToken do
use Ecto.Schema
import Ecto.Query

View File

@@ -1,3 +1,6 @@
@moduledoc """
Plug for user authentication.
"""
defmodule ClaperWeb.UserAuth do
import Plug.Conn
import Phoenix.Controller

View File

@@ -124,20 +124,20 @@ defmodule ClaperWeb.EventLive.EventFormComponent do
[ext | _] = MIME.extensions(MIME.from_path(dest))
if !Map.has_key?(socket.assigns.event.presentation_file, :id) do
if Map.has_key?(socket.assigns.event.presentation_file, :id) do
after_save.(
socket,
Map.put(event_params, "presentation_file", %{
"status" => "progress",
"presentation_state" => %{}
}),
event_params,
hash,
ext
)
else
after_save.(
socket,
event_params,
Map.put(event_params, "presentation_file", %{
"status" => "progress",
"presentation_state" => %{}
}),
hash,
ext
)
@@ -190,17 +190,7 @@ defmodule ClaperWeb.EventLive.EventFormComponent do
event_params
) do
{:ok, _event} ->
if !is_nil(hash) && !is_nil(ext) do
Task.Supervisor.async_nolink(Claper.TaskSupervisor, fn ->
Claper.Tasks.Converter.convert(
socket.assigns.current_user.id,
"original.#{ext}",
hash,
ext,
socket.assigns.event.presentation_file.id
)
end)
end
handle_file_conversion(socket, hash, ext)
{:noreply,
socket
@@ -212,6 +202,22 @@ defmodule ClaperWeb.EventLive.EventFormComponent do
end
end
defp handle_file_conversion(socket, hash, ext) do
if is_nil(hash) || is_nil(ext) do
:ok
else
Task.Supervisor.async_nolink(Claper.TaskSupervisor, fn ->
Claper.Tasks.Converter.convert(
socket.assigns.current_user.id,
"original.#{ext}",
hash,
ext,
socket.assigns.event.presentation_file.id
)
end)
end
end
defp get_max_file_size() do
Application.get_env(:claper, :max_file_size)
end

View File

@@ -89,7 +89,7 @@ defmodule ClaperWeb.EventLive.Show do
|> check_leader(event)
|> leader_list(event)
{:ok, socket |> assign(:empty_room, Enum.count(socket.assigns.posts) == 0),
{:ok, socket |> assign(:empty_room, Enum.empty?(socket.assigns.posts)),
temporary_assigns: [posts: []]}
end
@@ -114,7 +114,9 @@ defmodule ClaperWeb.EventLive.Show do
defp check_leader(socket, _event), do: socket |> assign(:is_leader, false)
defp starting_soon_assigns(socket, event) do
if not Claper.Events.Event.started?(event) do
if Claper.Events.Event.started?(event) do
socket |> assign(:started, true)
else
:timer.send_interval(1000, self(), :tick)
diff =
@@ -130,13 +132,11 @@ defmodule ClaperWeb.EventLive.Show do
|> assign(:diff, diff)
|> assign(:started, false)
end
else
socket |> assign(:started, true)
end
end
defp seconds_to_d_h_m_s(seconds) do
{div(seconds, 86400), rem(seconds, 86400) |> div(3600), rem(seconds, 3600) |> div(60),
{div(seconds, 86_400), rem(seconds, 86_400) |> div(3600), rem(seconds, 3600) |> div(60),
rem(seconds, 3600) |> rem(60)}
end
@@ -374,7 +374,6 @@ defmodule ClaperWeb.EventLive.Show do
{:noreply, socket |> assign(:nickname, post_params["name"])}
false ->
IO.inspect(changeset)
{:noreply, assign(socket, post_changeset: %{changeset | action: :insert})}
end
end

View File

@@ -107,8 +107,7 @@ defmodule ClaperWeb.FormLive.FormComponent do
defp maybe_enable(form_params, socket) do
has_current_form =
socket.assigns.forms
|> Enum.filter(fn f -> f.position == socket.assigns.position && f.enabled == true end)
|> Enum.count() > 0
|> Enum.count(fn f -> f.position == socket.assigns.position && f.enabled == true end) > 0
form_params |> Map.put("enabled", !has_current_form)
end

View File

@@ -107,8 +107,7 @@ defmodule ClaperWeb.PollLive.FormComponent do
defp maybe_enable(poll_params, socket) do
has_current_poll =
socket.assigns.polls
|> Enum.filter(fn p -> p.position == socket.assigns.position && p.enabled == true end)
|> Enum.count() > 0
|> Enum.count(fn p -> p.position == socket.assigns.position && p.enabled == true end) > 0
poll_params |> Map.put("enabled", !has_current_poll)
end

View File

@@ -1,3 +1,63 @@
@moduledoc """
Plug to set the locale based on the Accept-Language header.
## Usage
Add the plug to your pipeline in `router.ex`:
pipeline :browser do
...
plug ClaperWeb.Plugs.Locale
end
## Configuration
The plug will use the `:default_locale` configuration value as the default
locale. If the `:default_locale` is not set, it will default to `:en`.
## Accept-Language header
The plug will parse the `Accept-Language` header and set the locale to the
first language in the list that is known to the application. If no language
is known, the locale will not be changed.
The `Accept-Language` header is a comma-separated list of language tags with
optional quality values. The quality value is a number between 0 and 1,
where 1 is the highest quality. The quality value is optional and defaults
to 1.
Examples:
Accept-Language: en-US,en;q=0.8,da;q=0.6
The above example will set the locale to `:en` if it is known to the
application. If `:en` is not known, it will set the locale to `:da` if it is
known to the application. If neither `:en` nor `:da` is known, the locale
will not be changed.
Accept-Language: en-US,en;q=0.8
The above example will set the locale to `:en` if it is known to the
application. If `:en` is not known, the locale will not be changed.
Accept-Language: en-US
The above example will set the locale to `:en` if it is known to the
application. If `:en` is not known, the locale will not be changed.
## Known locales
The plug will only set the locale if it is known to the application. The
known locales are determined by the `:gettext` configuration. The
`:gettext` configuration is set in `config/config.exs`:
config :claper, ClaperWeb.Gettext,
default_locale: "en",
default_domain: "claper",
available_locales: ~w(en fr)
The `:available_locales` option is
"""
defmodule ClaperWeb.Plugs.Locale do
import Plug.Conn
@@ -51,14 +111,16 @@ defmodule ClaperWeb.Plugs.Locale do
end
defp ensure_language_fallbacks(tags) do
Enum.flat_map(tags, fn tag ->
case String.split(tag, "-") do
[language, _country_variant] ->
if Enum.member?(tags, language), do: [tag], else: [tag, language]
Enum.flat_map(tags, &fallback_tags/1)
end
[_language] ->
[tag]
end
end)
defp fallback_tags(tag) do
case String.split(tag, "-") do
[language, _country_variant] ->
if Enum.member?(tags, language), do: [tag], else: [tag, language]
[_language] ->
[tag]
end
end
end

View File

@@ -1,3 +1,6 @@
@moduledoc """
Input component for forms
"""
defmodule ClaperWeb.Component.Input do
use ClaperWeb, :view_component