Merge branch 'tests/openapi-everywhere' into 'develop'
[akkoma] / lib / pleroma / web / twitter_api / controllers / util_controller.ex
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
5 defmodule Pleroma.Web.TwitterAPI.UtilController do
6 use Pleroma.Web, :controller
7
8 require Logger
9
10 alias Pleroma.Config
11 alias Pleroma.Emoji
12 alias Pleroma.Healthcheck
13 alias Pleroma.User
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 alias Pleroma.Web.WebFinger
17
18 plug(Pleroma.Web.ApiSpec.CastAndValidate when action != :remote_subscribe)
19 plug(Pleroma.Web.Plugs.FederatingPlug when action == :remote_subscribe)
20
21 plug(
22 OAuthScopesPlug,
23 %{scopes: ["write:accounts"]}
24 when action in [
25 :change_email,
26 :change_password,
27 :delete_account,
28 :update_notificaton_settings,
29 :disable_account
30 ]
31 )
32
33 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TwitterUtilOperation
34
35 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
36 with %User{} = user <- User.get_cached_by_nickname(nick),
37 avatar = User.avatar_url(user) do
38 conn
39 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
40 else
41 _e ->
42 render(conn, "subscribe.html", %{
43 nickname: nick,
44 avatar: nil,
45 error: "Could not find user"
46 })
47 end
48 end
49
50 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
51 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
52 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
53 conn
54 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
55 else
56 _e ->
57 render(conn, "subscribe.html", %{
58 nickname: nick,
59 avatar: nil,
60 error: "Something went wrong."
61 })
62 end
63 end
64
65 def frontend_configurations(conn, _params) do
66 render(conn, "frontend_configurations.json")
67 end
68
69 def emoji(conn, _params) do
70 emoji =
71 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
72 Map.put(acc, code, %{image_url: file, tags: tags})
73 end)
74
75 json(conn, emoji)
76 end
77
78 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
79 with {:ok, _} <- User.update_notification_settings(user, params) do
80 json(conn, %{status: "success"})
81 end
82 end
83
84 def change_password(%{assigns: %{user: user}} = conn, %{
85 password: password,
86 new_password: new_password,
87 new_password_confirmation: new_password_confirmation
88 }) do
89 case CommonAPI.Utils.confirm_current_password(user, password) do
90 {:ok, user} ->
91 with {:ok, _user} <-
92 User.reset_password(user, %{
93 password: new_password,
94 password_confirmation: new_password_confirmation
95 }) do
96 json(conn, %{status: "success"})
97 else
98 {:error, changeset} ->
99 {_, {error, _}} = Enum.at(changeset.errors, 0)
100 json(conn, %{error: "New password #{error}."})
101
102 _ ->
103 json(conn, %{error: "Unable to change password."})
104 end
105
106 {:error, msg} ->
107 json(conn, %{error: msg})
108 end
109 end
110
111 def change_email(%{assigns: %{user: user}} = conn, %{password: password, email: email}) do
112 case CommonAPI.Utils.confirm_current_password(user, password) do
113 {:ok, user} ->
114 with {:ok, _user} <- User.change_email(user, email) do
115 json(conn, %{status: "success"})
116 else
117 {:error, changeset} ->
118 {_, {error, _}} = Enum.at(changeset.errors, 0)
119 json(conn, %{error: "Email #{error}."})
120
121 _ ->
122 json(conn, %{error: "Unable to change email."})
123 end
124
125 {:error, msg} ->
126 json(conn, %{error: msg})
127 end
128 end
129
130 def delete_account(%{assigns: %{user: user}} = conn, params) do
131 password = params[:password] || ""
132
133 case CommonAPI.Utils.confirm_current_password(user, password) do
134 {:ok, user} ->
135 User.delete(user)
136 json(conn, %{status: "success"})
137
138 {:error, msg} ->
139 json(conn, %{error: msg})
140 end
141 end
142
143 def disable_account(%{assigns: %{user: user}} = conn, params) do
144 case CommonAPI.Utils.confirm_current_password(user, params[:password]) do
145 {:ok, user} ->
146 User.set_activation_async(user, false)
147 json(conn, %{status: "success"})
148
149 {:error, msg} ->
150 json(conn, %{error: msg})
151 end
152 end
153
154 def captcha(conn, _params) do
155 json(conn, Pleroma.Captcha.new())
156 end
157
158 def healthcheck(conn, _params) do
159 with true <- Config.get([:instance, :healthcheck]),
160 %{healthy: true} = info <- Healthcheck.system_info() do
161 json(conn, info)
162 else
163 %{healthy: false} = info ->
164 service_unavailable(conn, info)
165
166 _ ->
167 service_unavailable(conn, %{})
168 end
169 end
170
171 defp service_unavailable(conn, info) do
172 conn
173 |> put_status(:service_unavailable)
174 |> json(info)
175 end
176 end