Merge branch 'release/2.0.6' into 'stable'
[akkoma] / lib / pleroma / web / controller_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ControllerHelper do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Pagination
9
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"]
12 def truthy_param?(blank_value) when blank_value in [nil, ""], do: nil
13 def truthy_param?(value), do: value not in @falsy_param_values
14
15 def json_response(conn, status, json) do
16 conn
17 |> put_status(status)
18 |> json(json)
19 end
20
21 @spec fetch_integer_param(map(), String.t(), integer() | nil) :: integer() | nil
22 def fetch_integer_param(params, name, default \\ nil) do
23 params
24 |> Map.get(name, default)
25 |> param_to_integer(default)
26 end
27
28 defp param_to_integer(val, _) when is_integer(val), do: val
29
30 defp param_to_integer(val, default) when is_binary(val) do
31 case Integer.parse(val) do
32 {res, _} -> res
33 _ -> default
34 end
35 end
36
37 defp param_to_integer(_, default), do: default
38
39 def add_link_headers(conn, activities, extra_params \\ %{})
40
41 def add_link_headers(%{assigns: %{skip_link_headers: true}} = conn, _activities, _extra_params),
42 do: conn
43
44 def add_link_headers(conn, activities, extra_params) do
45 case get_pagination_fields(conn, activities, extra_params) do
46 %{"next" => next_url, "prev" => prev_url} ->
47 put_resp_header(conn, "link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
48
49 _ ->
50 conn
51 end
52 end
53
54 def get_pagination_fields(conn, activities, extra_params \\ %{}) do
55 case List.last(activities) do
56 %{id: max_id} ->
57 params =
58 conn.params
59 |> Map.drop(Map.keys(conn.path_params))
60 |> Map.merge(extra_params)
61 |> Map.drop(Pagination.page_keys() -- ["limit", "order"])
62
63 min_id =
64 activities
65 |> List.first()
66 |> Map.get(:id)
67
68 fields = %{
69 "next" => current_url(conn, Map.put(params, :max_id, max_id)),
70 "prev" => current_url(conn, Map.put(params, :min_id, min_id))
71 }
72
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, Pagination.page_keys() -- ["limit", "order"]) != [] do
79 Map.put(fields, "id", current_url(conn, conn.params))
80 else
81 fields
82 end
83
84 _ ->
85 %{}
86 end
87 end
88
89 def assign_account_by_id(%{params: %{"id" => id}} = conn, _) do
90 case Pleroma.User.get_cached_by_id(id) do
91 %Pleroma.User{} = account -> assign(conn, :account, account)
92 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
93 end
94 end
95
96 def try_render(conn, target, params) when is_binary(target) do
97 case render(conn, target, params) do
98 nil -> render_error(conn, :not_implemented, "Can't display this activity")
99 res -> res
100 end
101 end
102
103 def try_render(conn, _, _) do
104 render_error(conn, :not_implemented, "Can't display this activity")
105 end
106
107 @spec put_in_if_exist(map(), atom() | String.t(), any) :: map()
108 def put_in_if_exist(map, _key, nil), do: map
109 def put_in_if_exist(map, key, value), do: put_in(map, key, value)
110 end