Merge branch 'develop' into preload-data
[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 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, json) do
22 conn
23 |> put_status(status)
24 |> json(json)
25 end
26
27 @spec fetch_integer_param(map(), String.t(), integer() | nil) :: integer() | nil
28 def fetch_integer_param(params, name, default \\ nil) do
29 params
30 |> Map.get(name, default)
31 |> param_to_integer(default)
32 end
33
34 defp param_to_integer(val, _) when is_integer(val), do: val
35
36 defp param_to_integer(val, default) when is_binary(val) do
37 case Integer.parse(val) do
38 {res, _} -> res
39 _ -> default
40 end
41 end
42
43 defp param_to_integer(_, default), do: default
44
45 def add_link_headers(conn, activities, extra_params \\ %{})
46
47 def add_link_headers(%{assigns: %{skip_link_headers: true}} = conn, _activities, _extra_params),
48 do: conn
49
50 def add_link_headers(conn, activities, extra_params) do
51 case get_pagination_fields(conn, activities, extra_params) do
52 %{"next" => next_url, "prev" => prev_url} ->
53 put_resp_header(conn, "link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
54
55 _ ->
56 conn
57 end
58 end
59
60 def get_pagination_fields(conn, activities, extra_params \\ %{}) do
61 case List.last(activities) do
62 %{id: max_id} ->
63 params =
64 conn.params
65 |> Map.drop(Map.keys(conn.path_params))
66 |> Map.merge(extra_params)
67 |> Map.drop(Pagination.page_keys() -- ["limit", "order"])
68
69 min_id =
70 activities
71 |> List.first()
72 |> Map.get(:id)
73
74 fields = %{
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 }
78
79 # Generating an `id` without already present pagination keys would
80 # need a query-restriction with an `q.id >= ^id` or `q.id <= ^id`
81 # instead of the `q.id > ^min_id` and `q.id < ^max_id`.
82 # This is because we only have ids present inside of the page, while
83 # `min_id`, `since_id` and `max_id` requires to know one outside of it.
84 if Map.take(conn.params, Pagination.page_keys() -- ["limit", "order"]) != [] do
85 Map.put(fields, "id", current_url(conn, conn.params))
86 else
87 fields
88 end
89
90 _ ->
91 %{}
92 end
93 end
94
95 def assign_account_by_id(conn, _) do
96 case Pleroma.User.get_cached_by_id(conn.params.id) do
97 %Pleroma.User{} = account -> assign(conn, :account, account)
98 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
99 end
100 end
101
102 def try_render(conn, target, params) when is_binary(target) do
103 case render(conn, target, params) do
104 nil -> render_error(conn, :not_implemented, "Can't display this activity")
105 res -> res
106 end
107 end
108
109 def try_render(conn, _, _) do
110 render_error(conn, :not_implemented, "Can't display this activity")
111 end
112
113 @doc """
114 Returns true if request specifies to include embedded relationships in account objects.
115 May only be used in selected account-related endpoints; has no effect for status- or
116 notification-related endpoints.
117 """
118 # Intended for PleromaFE: https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838
119 def embed_relationships?(params) do
120 # To do once OpenAPI transition mess is over: just `truthy_param?(params[:with_relationships])`
121 params
122 |> Map.get(:with_relationships, params["with_relationships"])
123 |> truthy_param?()
124 end
125 end