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(), 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 @id_keys Pagination.page_keys() -- ["limit", "order"]
57 defp build_pagination_fields(conn, min_id, max_id, extra_params) do
60 |> Map.drop(Map.keys(conn.path_params) |> Enum.map(&String.to_existing_atom/1))
61 |> Map.merge(extra_params)
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)
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)
76 build_pagination_fields(conn, min_id, max_id, extra_params)
79 %{id: min_id} = List.first(entries)
81 build_pagination_fields(conn, min_id, max_id, extra_params)
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()
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")
102 def try_render(conn, _, _) do
103 render_error(conn, :not_implemented, "Can't display this activity")
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.
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])`
115 |> Map.get(:with_relationships, params["with_relationships"])
116 |> Params.truthy_param?()