Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.0KB

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