Add `account_activation_required` to /api/v1/instance
[akkoma] / lib / pleroma / web / controller_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 Mastodon API, per https://api.rubyonrails.org/classes/ActiveModel/Type/Boolean.html
9 @falsy_param_values [false, 0, "0", "f", "F", "false", "False", "FALSE", "off", "OFF"]
10
11 def explicitly_falsy_param?(value), do: value in @falsy_param_values
12
13 # Note: `nil` and `""` are considered falsy values in Pleroma
14 def falsy_param?(value),
15 do: explicitly_falsy_param?(value) or value in [nil, ""]
16
17 def truthy_param?(value), do: not falsy_param?(value)
18
19 def json_response(conn, status, json) do
20 conn
21 |> put_status(status)
22 |> json(json)
23 end
24
25 @spec fetch_integer_param(map(), String.t(), integer() | nil) :: integer() | nil
26 def fetch_integer_param(params, name, default \\ nil) do
27 params
28 |> Map.get(name, default)
29 |> param_to_integer(default)
30 end
31
32 defp param_to_integer(val, _) when is_integer(val), do: val
33
34 defp param_to_integer(val, default) when is_binary(val) do
35 case Integer.parse(val) do
36 {res, _} -> res
37 _ -> default
38 end
39 end
40
41 defp param_to_integer(_, default), do: default
42
43 def add_link_headers(conn, activities, extra_params \\ %{})
44
45 def add_link_headers(%{assigns: %{skip_link_headers: true}} = conn, _activities, _extra_params),
46 do: conn
47
48 def add_link_headers(conn, activities, extra_params) do
49 case List.last(activities) do
50 %{id: max_id} ->
51 params =
52 conn.params
53 |> Map.drop(Map.keys(conn.path_params))
54 |> Map.drop(["since_id", "max_id", "min_id"])
55 |> Map.merge(extra_params)
56
57 limit =
58 params
59 |> Map.get("limit", "20")
60 |> String.to_integer()
61
62 min_id =
63 if length(activities) <= limit do
64 activities
65 |> List.first()
66 |> Map.get(:id)
67 else
68 activities
69 |> Enum.at(limit * -1)
70 |> Map.get(:id)
71 end
72
73 next_url = current_url(conn, Map.merge(params, %{max_id: max_id}))
74 prev_url = current_url(conn, Map.merge(params, %{min_id: min_id}))
75
76 put_resp_header(conn, "link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
77
78 _ ->
79 conn
80 end
81 end
82
83 def assign_account_by_id(conn, _) do
84 # TODO: use `conn.params[:id]` only after moving to OpenAPI
85 case Pleroma.User.get_cached_by_id(conn.params[:id] || conn.params["id"]) do
86 %Pleroma.User{} = account -> assign(conn, :account, account)
87 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
88 end
89 end
90
91 def try_render(conn, target, params) when is_binary(target) do
92 case render(conn, target, params) do
93 nil -> render_error(conn, :not_implemented, "Can't display this activity")
94 res -> res
95 end
96 end
97
98 def try_render(conn, _, _) do
99 render_error(conn, :not_implemented, "Can't display this activity")
100 end
101
102 @spec put_if_exist(map(), atom() | String.t(), any) :: map()
103 def put_if_exist(map, _key, nil), do: map
104
105 def put_if_exist(map, key, value), do: Map.put(map, key, value)
106
107 @doc """
108 Returns true if request specifies to include embedded relationships in account objects.
109 May only be used in selected account-related endpoints; has no effect for status- or
110 notification-related endpoints.
111 """
112 # Intended for PleromaFE: https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838
113 def embed_relationships?(params) do
114 # To do once OpenAPI transition mess is over: just `truthy_param?(params[:with_relationships])`
115 params
116 |> Map.get(:with_relationships, params["with_relationships"])
117 |> truthy_param?()
118 end
119 end