1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ControllerHelper do
6 use Pleroma.Web, :controller
8 # As in MastoAPI, per https://api.rubyonrails.org/classes/ActiveModel/Type/Boolean.html
9 @falsy_param_values [false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"]
10 def truthy_param?(blank_value) when blank_value in [nil, ""], do: nil
11 def truthy_param?(value), do: value not in @falsy_param_values
13 def json_response(conn, status, json) do
19 @spec fetch_integer_param(map(), String.t(), integer() | nil) :: integer() | nil
20 def fetch_integer_param(params, name, default \\ nil) do
22 |> Map.get(name, default)
23 |> param_to_integer(default)
26 defp param_to_integer(val, _) when is_integer(val), do: val
28 defp param_to_integer(val, default) when is_binary(val) do
29 case Integer.parse(val) do
35 defp param_to_integer(_, default), do: default