1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
12 alias Pleroma.Healthcheck
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 alias Pleroma.Web.WebFinger
18 plug(Pleroma.Web.ApiSpec.CastAndValidate when action != :remote_subscribe)
19 plug(Pleroma.Web.Plugs.FederatingPlug when action == :remote_subscribe)
23 %{scopes: ["write:accounts"]}
28 :update_notificaton_settings,
33 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TwitterUtilOperation
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
39 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
42 render(conn, "subscribe.html", %{
45 error: "Could not find user"
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
54 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
57 render(conn, "subscribe.html", %{
60 error: "Something went wrong."
65 def frontend_configurations(conn, _params) do
66 render(conn, "frontend_configurations.json")
69 def emoji(conn, _params) do
71 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
72 Map.put(acc, code, %{image_url: file, tags: tags})
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"})
84 def change_password(%{assigns: %{user: user}} = conn, %{
86 new_password: new_password,
87 new_password_confirmation: new_password_confirmation
89 case CommonAPI.Utils.confirm_current_password(user, password) do
92 User.reset_password(user, %{
93 password: new_password,
94 password_confirmation: new_password_confirmation
96 json(conn, %{status: "success"})
98 {:error, changeset} ->
99 {_, {error, _}} = Enum.at(changeset.errors, 0)
100 json(conn, %{error: "New password #{error}."})
103 json(conn, %{error: "Unable to change password."})
107 json(conn, %{error: msg})
111 def change_email(%{assigns: %{user: user}} = conn, %{password: password, email: email}) do
112 case CommonAPI.Utils.confirm_current_password(user, password) do
114 with {:ok, _user} <- User.change_email(user, email) do
115 json(conn, %{status: "success"})
117 {:error, changeset} ->
118 {_, {error, _}} = Enum.at(changeset.errors, 0)
119 json(conn, %{error: "Email #{error}."})
122 json(conn, %{error: "Unable to change email."})
126 json(conn, %{error: msg})
130 def delete_account(%{assigns: %{user: user}} = conn, params) do
131 password = params[:password] || ""
133 case CommonAPI.Utils.confirm_current_password(user, password) do
136 json(conn, %{status: "success"})
139 json(conn, %{error: msg})
143 def disable_account(%{assigns: %{user: user}} = conn, params) do
144 case CommonAPI.Utils.confirm_current_password(user, params[:password]) do
146 User.set_activation_async(user, false)
147 json(conn, %{status: "success"})
150 json(conn, %{error: msg})
154 def captcha(conn, _params) do
155 json(conn, Pleroma.Captcha.new())
158 def healthcheck(conn, _params) do
159 with true <- Config.get([:instance, :healthcheck]),
160 %{healthy: true} = info <- Healthcheck.system_info() do
163 %{healthy: false} = info ->
164 service_unavailable(conn, info)
167 service_unavailable(conn, %{})
171 defp service_unavailable(conn, info) do
173 |> put_status(:service_unavailable)