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