1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UtilController do
6 use Pleroma.Web, :controller
10 alias Pleroma.Activity
13 alias Pleroma.Healthcheck
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.Plugs.OAuthScopesPlug
18 alias Pleroma.Web.WebFinger
21 Pleroma.Web.ApiSpec.CastAndValidate
22 when action != :remote_subscribe and action != :show_subscribe_form
26 Pleroma.Web.Plugs.FederatingPlug
27 when action == :remote_subscribe
28 when action == :show_subscribe_form
33 %{scopes: ["write:accounts"]}
38 :update_notificaton_settings,
48 %{scopes: ["read:accounts"]}
54 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TwitterUtilOperation
56 def show_subscribe_form(conn, %{"nickname" => nick}) do
57 with %User{} = user <- User.get_cached_by_nickname(nick),
58 avatar = User.avatar_url(user) do
60 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
63 render(conn, "subscribe.html", %{
67 Pleroma.Web.Gettext.dpgettext(
69 "remote follow error message - user not found",
76 def show_subscribe_form(conn, %{"status_id" => id}) do
77 with %Activity{} = activity <- Activity.get_by_id(id),
78 {:ok, ap_id} <- get_ap_id(activity),
79 %User{} = user <- User.get_cached_by_ap_id(activity.actor),
80 avatar = User.avatar_url(user) do
82 |> render("status_interact.html", %{
85 nickname: user.nickname,
91 render(conn, "status_interact.html", %{
95 Pleroma.Web.Gettext.dpgettext(
97 "status interact error message - status not found",
98 "Could not find status"
104 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
105 show_subscribe_form(conn, %{"nickname" => nick})
108 def remote_subscribe(conn, %{"status_id" => id, "profile" => _}) do
109 show_subscribe_form(conn, %{"status_id" => id})
112 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
113 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
114 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
116 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
119 render(conn, "subscribe.html", %{
123 Pleroma.Web.Gettext.dpgettext(
125 "remote follow error message - unknown error",
126 "Something went wrong."
132 def remote_subscribe(conn, %{"status" => %{"status_id" => id, "profile" => profile}}) do
133 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
134 %Activity{} = activity <- Activity.get_by_id(id),
135 {:ok, ap_id} <- get_ap_id(activity) do
137 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
140 render(conn, "status_interact.html", %{
144 Pleroma.Web.Gettext.dpgettext(
146 "status interact error message - unknown error",
147 "Something went wrong."
153 def remote_interaction(%{body_params: %{ap_id: ap_id, profile: profile}} = conn, _params) do
154 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile) do
156 |> json(%{url: String.replace(template, "{uri}", ap_id)})
158 _e -> json(conn, %{error: "Couldn't find user"})
162 defp get_ap_id(activity) do
163 object = Pleroma.Object.normalize(activity, fetch: false)
166 %{data: %{"id" => ap_id}} -> {:ok, ap_id}
167 _ -> {:no_ap_id, nil}
171 def frontend_configurations(conn, _params) do
172 render(conn, "frontend_configurations.json")
175 def emoji(conn, _params) do
177 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
178 Map.put(acc, code, %{image_url: file, tags: tags})
184 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
185 with {:ok, _} <- User.update_notification_settings(user, params) do
186 json(conn, %{status: "success"})
190 def change_password(%{assigns: %{user: user}, body_params: body_params} = conn, %{}) do
191 case CommonAPI.Utils.confirm_current_password(user, body_params.password) do
194 User.reset_password(user, %{
195 password: body_params.new_password,
196 password_confirmation: body_params.new_password_confirmation
198 json(conn, %{status: "success"})
200 {:error, changeset} ->
201 {_, {error, _}} = Enum.at(changeset.errors, 0)
202 json(conn, %{error: "New password #{error}."})
205 json(conn, %{error: "Unable to change password."})
209 json(conn, %{error: msg})
213 def change_email(%{assigns: %{user: user}, body_params: body_params} = conn, %{}) do
214 case CommonAPI.Utils.confirm_current_password(user, body_params.password) do
216 with {:ok, _user} <- User.change_email(user, body_params.email) do
217 json(conn, %{status: "success"})
219 {:error, changeset} ->
220 {_, {error, _}} = Enum.at(changeset.errors, 0)
221 json(conn, %{error: "Email #{error}."})
224 json(conn, %{error: "Unable to change email."})
228 json(conn, %{error: msg})
232 def delete_account(%{assigns: %{user: user}, body_params: body_params} = conn, params) do
233 # This endpoint can accept a query param or JSON body for backwards-compatibility.
234 # Submitting a JSON body is recommended, so passwords don't end up in server logs.
235 password = body_params[:password] || params[:password] || ""
237 case CommonAPI.Utils.confirm_current_password(user, password) do
240 json(conn, %{status: "success"})
243 json(conn, %{error: msg})
247 def disable_account(%{assigns: %{user: user}} = conn, params) do
248 case CommonAPI.Utils.confirm_current_password(user, params[:password]) do
250 User.set_activation_async(user, false)
251 json(conn, %{status: "success"})
254 json(conn, %{error: msg})
258 def move_account(%{assigns: %{user: user}, body_params: body_params} = conn, %{}) do
259 case CommonAPI.Utils.confirm_current_password(user, body_params.password) do
261 with {:ok, target_user} <- find_or_fetch_user_by_nickname(body_params.target_account),
262 {:ok, _user} <- ActivityPub.move(user, target_user) do
263 json(conn, %{status: "success"})
268 |> json(%{error: "Target account not found."})
271 json(conn, %{error: error})
275 json(conn, %{error: msg})
279 def add_alias(%{assigns: %{user: user}, body_params: body_params} = conn, _) do
280 with {:ok, alias_user} <- find_user_by_nickname(body_params.alias),
281 {:ok, _user} <- user |> User.add_alias(alias_user) do
282 json(conn, %{status: "success"})
287 |> json(%{error: "Target account does not exist."})
290 json(conn, %{error: error})
294 def delete_alias(%{assigns: %{user: user}, body_params: body_params} = conn, _) do
295 with {:ok, alias_user} <- find_user_by_nickname(body_params.alias),
296 {:ok, _user} <- user |> User.delete_alias(alias_user) do
297 json(conn, %{status: "success"})
299 {:error, :no_such_alias} ->
302 |> json(%{error: "Account has no such alias."})
305 json(conn, %{error: error})
309 def list_aliases(%{assigns: %{user: user}} = conn, %{}) do
312 |> User.alias_users()
313 |> Enum.map(&User.full_nickname/1)
315 json(conn, %{aliases: alias_nicks})
318 defp find_user_by_nickname(nickname) do
319 user = User.get_cached_by_nickname(nickname)
328 defp find_or_fetch_user_by_nickname(nickname) do
329 user = User.get_by_nickname(nickname)
331 if user != nil and user.local do
334 with {:ok, user} <- User.fetch_by_nickname(nickname) do
343 def captcha(conn, _params) do
344 json(conn, Pleroma.Captcha.new())
347 def healthcheck(conn, _params) do
348 with true <- Config.get([:instance, :healthcheck]),
349 %{healthy: true} = info <- Healthcheck.system_info() do
352 %{healthy: false} = info ->
353 service_unavailable(conn, info)
356 service_unavailable(conn, %{})
360 defp service_unavailable(conn, info) do
362 |> put_status(:service_unavailable)