Merge branch 'features/openrc-console' into 'develop'
[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.Config
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
13 def explicitly_falsy_param?(value), do: value in @falsy_param_values
14
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, ""]
18
19 def truthy_param?(value), do: not falsy_param?(value)
20
21 def json_response(conn, status, json) do
22 conn
23 |> put_status(status)
24 |> json(json)
25 end
26
27 @spec fetch_integer_param(map(), String.t(), integer() | nil) :: integer() | nil
28 def fetch_integer_param(params, name, default \\ nil) do
29 params
30 |> Map.get(name, default)
31 |> param_to_integer(default)
32 end
33
34 defp param_to_integer(val, _) when is_integer(val), do: val
35
36 defp param_to_integer(val, default) when is_binary(val) do
37 case Integer.parse(val) do
38 {res, _} -> res
39 _ -> default
40 end
41 end
42
43 defp param_to_integer(_, default), do: default
44
45 def add_link_headers(conn, activities, extra_params \\ %{})
46
47 def add_link_headers(%{assigns: %{skip_link_headers: true}} = conn, _activities, _extra_params),
48 do: conn
49
50 def add_link_headers(conn, activities, extra_params) do
51 case List.last(activities) do
52 %{id: max_id} ->
53 params =
54 conn.params
55 |> Map.drop(Map.keys(conn.path_params))
56 |> Map.drop(["since_id", "max_id", "min_id"])
57 |> Map.merge(extra_params)
58
59 limit =
60 params
61 |> Map.get("limit", "20")
62 |> String.to_integer()
63
64 min_id =
65 if length(activities) <= limit do
66 activities
67 |> List.first()
68 |> Map.get(:id)
69 else
70 activities
71 |> Enum.at(limit * -1)
72 |> Map.get(:id)
73 end
74
75 next_url = current_url(conn, Map.merge(params, %{max_id: max_id}))
76 prev_url = current_url(conn, Map.merge(params, %{min_id: min_id}))
77
78 put_resp_header(conn, "link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
79
80 _ ->
81 conn
82 end
83 end
84
85 def assign_account_by_id(conn, _) do
86 # TODO: use `conn.params[:id]` only after moving to OpenAPI
87 case Pleroma.User.get_cached_by_id(conn.params[:id] || conn.params["id"]) do
88 %Pleroma.User{} = account -> assign(conn, :account, account)
89 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
90 end
91 end
92
93 def try_render(conn, target, params) when is_binary(target) do
94 case render(conn, target, params) do
95 nil -> render_error(conn, :not_implemented, "Can't display this activity")
96 res -> res
97 end
98 end
99
100 def try_render(conn, _, _) do
101 render_error(conn, :not_implemented, "Can't display this activity")
102 end
103
104 @spec put_if_exist(map(), atom() | String.t(), any) :: map()
105 def put_if_exist(map, _key, nil), do: map
106
107 def put_if_exist(map, key, value), do: Map.put(map, key, value)
108
109 @doc "Whether to skip rendering `[:account][:pleroma][:relationship]`for statuses/notifications"
110 def skip_relationships?(params) do
111 if Config.get([:extensions, :output_relationships_in_statuses_by_default]) do
112 false
113 else
114 # BREAKING: older PleromaFE versions do not send this param but _do_ expect relationships.
115 not truthy_param?(params["with_relationships"])
116 end
117 end
118 end