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