Merge branch 'bugfix/following-fix-status-code' 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(%{params: %{"id" => id}} = conn, _) do
86 case Pleroma.User.get_cached_by_id(id) do
87 %Pleroma.User{} = account -> assign(conn, :account, account)
88 nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
89 end
90 end
91
92 def try_render(conn, target, params) when is_binary(target) do
93 case render(conn, target, params) do
94 nil -> render_error(conn, :not_implemented, "Can't display this activity")
95 res -> res
96 end
97 end
98
99 def try_render(conn, _, _) do
100 render_error(conn, :not_implemented, "Can't display this activity")
101 end
102
103 @spec put_if_exist(map(), atom() | String.t(), any) :: map()
104 def put_if_exist(map, _key, nil), do: map
105
106 def put_if_exist(map, key, value), do: Map.put(map, key, value)
107
108 @doc "Whether to skip rendering `[:account][:pleroma][:relationship]`for statuses/notifications"
109 def skip_relationships?(params) do
110 if Config.get([:extensions, :output_relationships_in_statuses_by_default]) do
111 false
112 else
113 # BREAKING: older PleromaFE versions do not send this param but _do_ expect relationships.
114 not truthy_param?(params["with_relationships"])
115 end
116 end
117 end