1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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"]
13 def explicitly_falsy_param?(value), do: value in @falsy_param_values
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, ""]
19 def truthy_param?(value), do: not falsy_param?(value)
21 def json_response(conn, status, json) do
27 @spec fetch_integer_param(map(), String.t(), integer() | nil) :: integer() | nil
28 def fetch_integer_param(params, name, default \\ nil) do
30 |> Map.get(name, default)
31 |> param_to_integer(default)
34 defp param_to_integer(val, _) when is_integer(val), do: val
36 defp param_to_integer(val, default) when is_binary(val) do
37 case Integer.parse(val) do
43 defp param_to_integer(_, default), do: default
45 def add_link_headers(conn, activities, extra_params \\ %{})
47 def add_link_headers(%{assigns: %{skip_link_headers: true}} = conn, _activities, _extra_params),
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\"")
60 @id_keys Pagination.page_keys() -- ["limit", "order"]
61 defp build_pagination_fields(conn, min_id, max_id, extra_params) do
64 |> Map.drop(Map.keys(conn.path_params))
65 |> Map.merge(extra_params)
69 "next" => current_url(conn, Map.put(params, :max_id, max_id)),
70 "prev" => current_url(conn, Map.put(params, :min_id, min_id))
73 # Generating an `id` without already present pagination keys would
74 # need a query-restriction with an `q.id >= ^id` or `q.id <= ^id`
75 # instead of the `q.id > ^min_id` and `q.id < ^max_id`.
76 # This is because we only have ids present inside of the page, while
77 # `min_id`, `since_id` and `max_id` requires to know one outside of it.
78 if Map.take(conn.params, @id_keys) != %{} do
79 Map.put(fields, "id", current_url(conn))
85 def get_pagination_fields(conn, activities, extra_params \\ %{}) do
86 case List.last(activities) do
87 %{pagination_id: max_id} when not is_nil(max_id) ->
88 %{pagination_id: min_id} =
92 build_pagination_fields(conn, min_id, max_id, extra_params)
99 build_pagination_fields(conn, min_id, max_id, extra_params)
106 def assign_account_by_id(conn, _) do
107 case Pleroma.User.get_cached_by_id(conn.params.id) do
108 %Pleroma.User{} = account -> assign(conn, :account, account)
109 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
113 def try_render(conn, target, params) when is_binary(target) do
114 case render(conn, target, params) do
115 nil -> render_error(conn, :not_implemented, "Can't display this activity")
120 def try_render(conn, _, _) do
121 render_error(conn, :not_implemented, "Can't display this activity")
125 Returns true if request specifies to include embedded relationships in account objects.
126 May only be used in selected account-related endpoints; has no effect for status- or
127 notification-related endpoints.
129 # Intended for PleromaFE: https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838
130 def embed_relationships?(params) do
131 # To do once OpenAPI transition mess is over: just `truthy_param?(params[:with_relationships])`
133 |> Map.get(:with_relationships, params["with_relationships"])