<commit_before>defmodule Ello.V2.EditorialController do
  use Ello.V2.Web, :controller
  alias Ello.Core.Discovery

  @doc """
  GET /v2/editorials
  """
  def index(conn, _params) do
    editorials = editorials(conn)

    conn
    |> track_post_view(Enum.map(editorials, &(&1.post)), stream_kind: "editorials")
    |> add_pagination_headers("/editorials", next_page_params(editorials, conn))
    |> last_page_header(length(editorials))
    |> api_render_if_stale(data: editorials)
  end

  defp editorials(conn) do
    Discovery.editorials(standard_params(conn, %{preview: preview?(conn)}))
  end

  defp next_page_params(editorials, conn) do
    next = %{per_page: per_page(conn)}
    if preview?(conn) do
      Map.merge(next, %{
        before: List.last(editorials).preview_position,
        preview: true,
      })
    else
      Map.merge(next, %{
        before: List.last(editorials).published_position,
      })
    end
  end

  defp last_page_header(conn, editorials_count) do
    per_page = per_page(conn)
    if editorials_count < per_page do
      put_resp_header(conn, "x-total-pages-remaining", "0")
    else
      put_resp_header(conn, "x-total-pages-remaining", "1")
    end
  end

  defp per_page(%{ params: %{ "per_page" => per_page }}) when is_integer(per_page), do: per_page
  defp per_page(%{ params: %{ "per_page" => per_page }}), do: String.to_integer(per_page)
  defp per_page(%{ params: %{ "per_page" => per_page }}), do: per_page
  defp per_page(_), do: 25

  defp preview?(%{assigns: %{current_user: %{is_staff: true}}, params: %{"preview" => _}}), do: true
  defp preview?(_), do: false
end<commit_msg>whitespace<commit_after>defmodule Ello.V2.EditorialController do
  use Ello.V2.Web, :controller
  alias Ello.Core.Discovery

  @doc """
  GET /v2/editorials
  """
  def index(conn, _params) do
    editorials = editorials(conn)

    conn
    |> track_post_view(Enum.map(editorials, &(&1.post)), stream_kind: "editorials")
    |> add_pagination_headers("/editorials", next_page_params(editorials, conn))
    |> last_page_header(length(editorials))
    |> api_render_if_stale(data: editorials)
  end

  defp editorials(conn) do
    Discovery.editorials(standard_params(conn, %{preview: preview?(conn)}))
  end

  defp next_page_params(editorials, conn) do
    next = %{per_page: per_page(conn)}
    if preview?(conn) do
      Map.merge(next, %{
        before: List.last(editorials).preview_position,
        preview: true,
      })
    else
      Map.merge(next, %{
        before: List.last(editorials).published_position,
      })
    end
  end

  defp last_page_header(conn, editorials_count) do
    per_page = per_page(conn)
    if editorials_count < per_page do
      put_resp_header(conn, "x-total-pages-remaining", "0")
    else
      put_resp_header(conn, "x-total-pages-remaining", "1")
    end
  end

  defp per_page(%{params: %{"per_page" => per_page}}) when is_integer(per_page), do: per_page
  defp per_page(%{params: %{"per_page" => per_page}}), do: String.to_integer(per_page)
  defp per_page(%{params: %{"per_page" => per_page}}), do: per_page
  defp per_page(_), do: 25

  defp preview?(%{assigns: %{current_user: %{is_staff: true}}, params: %{"preview" => _}}), do: true
  defp preview?(_), do: false
end
{'fraction_non_alphanumeric': 0.14452644526445266, 'fraction_numerical': 0.004920049200492005, 'mean_word_length': 3.974006116207951, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6811284', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1151, 'n_words': 230}