Merge branch 'develop' into feature/addressable-lists
[akkoma] / lib / pleroma / web / controller_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ControllerHelper do
6 use Pleroma.Web, :controller
7
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
12
13 def json_response(conn, status, json) do
14 conn
15 |> put_status(status)
16 |> json(json)
17 end
18
19 @spec fetch_integer_param(map(), String.t(), integer() | nil) :: integer() | nil
20 def fetch_integer_param(params, name, default \\ nil) do
21 params
22 |> Map.get(name, default)
23 |> param_to_integer(default)
24 end
25
26 defp param_to_integer(val, _) when is_integer(val), do: val
27
28 defp param_to_integer(val, default) when is_binary(val) do
29 case Integer.parse(val) do
30 {res, _} -> res
31 _ -> default
32 end
33 end
34
35 defp param_to_integer(_, default), do: default
36 end