1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ControllerHelper do
6 use Pleroma.Web, :controller
8 alias Pleroma.Pagination
9 alias Pleroma.Web.Utils.Params
11 def json_response(conn, status, _) when status in [204, :no_content] do
13 |> put_resp_header("content-type", "application/json")
14 |> send_resp(status, "")
17 def json_response(conn, status, json) do
23 @spec fetch_integer_param(map(), String.t() | atom(), integer() | nil) :: integer() | nil
24 def fetch_integer_param(params, name, default \\ nil) do
26 |> Map.get(name, default)
27 |> param_to_integer(default)
30 defp param_to_integer(val, _) when is_integer(val), do: val
32 defp param_to_integer(val, default) when is_binary(val) do
33 case Integer.parse(val) do
39 defp param_to_integer(_, default), do: default
41 def add_link_headers(conn, entries, extra_params \\ %{})
43 def add_link_headers(%{assigns: %{skip_link_headers: true}} = conn, _entries, _extra_params),
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\"")
56 # TODO: Only fetch the params from open_api_spex when everything is converted
57 @id_keys Pagination.page_keys() -- ["limit", "order"]
58 defp build_pagination_fields(conn, min_id, max_id, extra_params) do
60 if Map.has_key?(conn.private, :open_api_spex) do
61 get_in(conn, [Access.key(:private), Access.key(:open_api_spex), Access.key(:params)])
65 |> Map.drop(Map.keys(conn.path_params) |> Enum.map(&String.to_existing_atom/1))
66 |> Map.merge(extra_params)
70 "next" => current_url(conn, Map.put(params, :max_id, max_id)),
71 "prev" => current_url(conn, Map.put(params, :min_id, min_id)),
72 "id" => current_url(conn)
76 def get_pagination_fields(conn, entries, extra_params \\ %{}) do
77 case List.last(entries) do
78 %{pagination_id: max_id} when not is_nil(max_id) ->
79 %{pagination_id: min_id} = List.first(entries)
81 build_pagination_fields(conn, min_id, max_id, extra_params)
84 %{id: min_id} = List.first(entries)
86 build_pagination_fields(conn, min_id, max_id, extra_params)
93 def assign_account_by_id(%{private: %{open_api_spex: %{params: %{id: id}}}} = conn, _) do
94 case Pleroma.User.get_cached_by_id(id) do
95 %Pleroma.User{} = account -> assign(conn, :account, account)
96 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
100 def try_render(conn, target, params) when is_binary(target) do
101 render(conn, target, params)
104 def try_render(conn, _, _) do
105 render_error(conn, :not_implemented, "Can't display this activity")
109 Returns true if request specifies to include embedded relationships in account objects.
110 May only be used in selected account-related endpoints; has no effect for status- or
111 notification-related endpoints.
113 # Intended for PleromaFE: https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838
114 def embed_relationships?(params) do
115 # To do once OpenAPI transition mess is over: just `truthy_param?(params[:with_relationships])`
117 |> Map.get(:with_relationships, params["with_relationships"])
118 |> Params.truthy_param?()