~mlb/linkhut

a4eaf84456024643b2fe2be82ebf7d9c7fc968bc — Matías Larre Borges a month ago 6ba2092
Update initialization of Gettext
M lib/linkhut_web.ex => lib/linkhut_web.ex +5 -5
@@ 25,8 25,9 @@ defmodule LinkhutWeb do
        namespace: LinkhutWeb,
        formats: [:html, :json, :xml]

      use Gettext, backend: LinkhutWeb.Gettext

      import Plug.Conn
      import LinkhutWeb.Gettext
      alias LinkhutWeb.Router.Helpers, as: Routes

      unquote(verified_routes())


@@ 51,7 52,7 @@ defmodule LinkhutWeb do
      import Phoenix.HTML
      # Core UI components and translation
      # import LinkhutWeb.CoreComponents
      import LinkhutWeb.Gettext
      use Gettext, backend: LinkhutWeb.Gettext

      # Shortcut for generating JS commands
      alias Phoenix.LiveView.JS


@@ 64,8 65,7 @@ defmodule LinkhutWeb do
  def xml do
    quote do
      use Phoenix.Component

      import LinkhutWeb.Gettext
      use Gettext, backend: LinkhutWeb.Gettext

      # Routes generation with the ~p sigil
      unquote(verified_routes())


@@ 85,9 85,9 @@ defmodule LinkhutWeb do
      # Use all HTML functionality (forms, tags, etc)
      use Phoenix.HTML
      use PhoenixHtmlSanitizer, :basic_html
      use Gettext, backend: LinkhutWeb.Gettext

      import LinkhutWeb.FormHelpers
      import LinkhutWeb.Gettext
      import LinkhutWeb.Helpers
      alias LinkhutWeb.Router.Helpers, as: Routes


M lib/linkhut_web/controllers/link_xml.ex => lib/linkhut_web/controllers/link_xml.ex +10 -11
@@ 4,7 4,6 @@ defmodule LinkhutWeb.LinkXML do
  import LinkhutWeb.Controllers.Utils

  alias Atomex.{Entry, Feed}
  alias LinkhutWeb.Gettext

  @doc """
  Renders a feed of links


@@ 29,10 28,10 @@ defmodule LinkhutWeb.LinkXML do
    |> Enum.into(%{})
    |> case do
      %{view: :unread} ->
        Gettext.gettext("Your Unread Bookmarks")
        gettext("Your Unread Bookmarks")

      %{user: user, url: url, tags: tags} ->
        Gettext.gettext(
        gettext(
          "Bookmarks for url: %{url} by linkhut user: %{user} tagged with: %{tags}",
          url: url,
          tags: Enum.join(tags, ","),


@@ 40,37 39,37 @@ defmodule LinkhutWeb.LinkXML do
        )

      %{user: user, url: url} ->
        Gettext.gettext("Bookmarks for url: %{url} by linkhut user: %{user}",
        gettext("Bookmarks for url: %{url} by linkhut user: %{user}",
          url: url,
          user: user
        )

      %{url: url, tags: tags} ->
        Gettext.gettext("Bookmarks for url: %{url} tagged with: %{tags}",
        gettext("Bookmarks for url: %{url} tagged with: %{tags}",
          url: url,
          tags: Enum.join(tags, ",")
        )

      %{url: url} ->
        Gettext.gettext("Bookmarks for url: %{url}", url: url)
        gettext("Bookmarks for url: %{url}", url: url)

      %{user: user, tags: tags} ->
        Gettext.gettext("Bookmarks by linkhut user: %{user} tagged with: %{tags}",
        gettext("Bookmarks by linkhut user: %{user} tagged with: %{tags}",
          tags: Enum.join(tags, ","),
          user: user
        )

      %{user: user} ->
        Gettext.gettext("Bookmarks by linkhut user: %{user}", user: user)
        gettext("Bookmarks by linkhut user: %{user}", user: user)

      %{tags: tags} ->
        Gettext.gettext("Bookmarks tagged with: %{tags}", tags: Enum.join(tags, ","))
        gettext("Bookmarks tagged with: %{tags}", tags: Enum.join(tags, ","))

      %{params: %{"v" => "popular"}} ->
        Gettext.gettext("Popular bookmarks")
        gettext("Popular bookmarks")

      _ ->
        Gettext.gettext("Recent bookmarks")
        gettext("Recent bookmarks")
    end
  end


M lib/linkhut_web/gettext.ex => lib/linkhut_web/gettext.ex +2 -2
@@ 5,7 5,7 @@ defmodule LinkhutWeb.Gettext do
  By using [Gettext](https://hexdocs.pm/gettext),
  your module gains a set of macros for translations, for example:

      import LinkhutWeb.Gettext
      use Gettext, backend: LinkhutWeb.Gettext

      # Simple translation
      gettext("Here is the string to translate")


@@ 20,5 20,5 @@ defmodule LinkhutWeb.Gettext do

  See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
  """
  use Gettext, otp_app: :linkhut
  use Gettext.Backend, otp_app: :linkhut
end

A lib/linkhut_web/plugs/ensure_recent_auth.ex => lib/linkhut_web/plugs/ensure_recent_auth.ex +73 -0
@@ 0,0 1,73 @@
defmodule LinkhutWeb.Plugs.EnsureAuth do
  @behaviour Plug

  @moduledoc """
  Plug ensuring the requester is logged in.
  """

  import Phoenix.Controller, only: [put_flash: 3, redirect: 2]
  import Plug.Conn
  alias Linkhut.Accounts
  alias LinkhutWeb.Router.Helpers, as: RouteHelpers

  @doc false
  @impl true
  def init([]), do: false

  @doc false
  @impl true
  def call(conn, _) do
    if user = get_user(conn) do
      assign(conn, :current_user, user)
    else
      auth_error!(conn)
    end
  end

  def get_user(conn) do
    case conn.assigns[:current_user] do
      nil ->
        fetch_user(conn)

      user ->
        user
    end
  end

  defp fetch_user(conn) do
    if user_id = get_session(conn, :user_id) do
      Accounts.get_user(user_id)
    else
      nil
    end
  end

  defp auth_error!(conn) do
    conn
    |> store_path_and_querystring_in_session()
    |> put_flash(:error, "Login required")
    |> redirect(to: RouteHelpers.session_path(conn, :new))
    |> halt()
  end

  defp store_path_and_querystring_in_session(conn) do
    # Get HTTP method and url from conn
    method = conn.method

    path =
      if conn.query_string != "" do
        conn.request_path <> "?" <> conn.query_string
      else
        conn.request_path
      end

    # If conditions apply store path in session, else return conn unmodified
    case method do
      "GET" ->
        put_session(conn, :login_redirect_path, path)

      _ ->
        conn
    end
  end
end

M lib/linkhut_web/views/helpers.ex => lib/linkhut_web/views/helpers.ex +3 -3
@@ 1,7 1,7 @@
defmodule LinkhutWeb.Helpers do
  @moduledoc false

  require LinkhutWeb.Gettext
  use Gettext, backend: LinkhutWeb.Gettext
  alias Timex

  @doc """


@@ 17,10 17,10 @@ defmodule LinkhutWeb.Helpers do
        Timex.format!(time1, "{relative}", :relative)

      diff < 1 ->
        LinkhutWeb.Gettext.gettext("Today")
        gettext("Today")

      diff < 2 ->
        LinkhutWeb.Gettext.gettext("Yesterday")
        gettext("Yesterday")

      diff < 10 ->
        Timex.format!(time1, "{relative}", :relative)