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.

119 lines
3.6KB

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