1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
13 alias Pleroma.Notification
14 alias Pleroma.Plugs.OAuthScopesPlug
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.WebFinger
19 plug(Pleroma.Web.FederatingPlug when action == :remote_subscribe)
23 %{scopes: ["follow", "write:follows"]}
24 when action == :follow_import
27 # Note: follower can submit the form (with password auth) not being signed in (having no token)
30 %{fallback: :proceed_unauthenticated, scopes: ["follow", "write:follows"]}
31 when action == :do_remote_follow
34 plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks_import)
38 %{scopes: ["write:accounts"]}
43 :update_notificaton_settings,
48 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :notifications_read)
50 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
51 with %User{} = user <- User.get_cached_by_nickname(nick),
52 avatar = User.avatar_url(user) do
54 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
57 render(conn, "subscribe.html", %{
60 error: "Could not find user"
65 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
66 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
67 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
69 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
72 render(conn, "subscribe.html", %{
75 error: "Something went wrong."
80 def notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
81 with {:ok, _} <- Notification.read_one(user, notification_id) do
82 json(conn, %{status: "success"})
86 |> put_resp_content_type("application/json")
87 |> send_resp(403, Jason.encode!(%{"error" => message}))
91 # Deprecated in favor of `/nodeinfo`
92 # https://git.pleroma.social/pleroma/pleroma/-/merge_requests/2327
93 # https://git.pleroma.social/pleroma/pleroma-fe/-/merge_requests/1084
94 def config(conn, _params) do
97 textlimit: to_string(Config.get([:instance, :limit])),
98 vapidPublicKey: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
103 def frontend_configurations(conn, _params) do
105 Pleroma.Config.get(:frontend_configurations, %{})
111 def emoji(conn, _params) do
113 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
114 Map.put(acc, code, %{image_url: file, tags: tags})
120 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
121 with {:ok, _} <- User.update_notification_settings(user, params) do
122 json(conn, %{status: "success"})
126 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
127 follow_import(conn, %{"list" => File.read!(listfile.path)})
130 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
131 with lines <- String.split(list, "\n"),
132 followed_identifiers <-
133 Enum.map(lines, fn line ->
134 String.split(line, ",") |> List.first()
136 |> List.delete("Account address") do
137 User.follow_import(follower, followed_identifiers)
138 json(conn, "job started")
142 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
143 blocks_import(conn, %{"list" => File.read!(listfile.path)})
146 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
147 with blocked_identifiers <- String.split(list) do
148 User.blocks_import(blocker, blocked_identifiers)
149 json(conn, "job started")
153 def change_password(%{assigns: %{user: user}} = conn, params) do
154 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
157 User.reset_password(user, %{
158 password: params["new_password"],
159 password_confirmation: params["new_password_confirmation"]
161 json(conn, %{status: "success"})
163 {:error, changeset} ->
164 {_, {error, _}} = Enum.at(changeset.errors, 0)
165 json(conn, %{error: "New password #{error}."})
168 json(conn, %{error: "Unable to change password."})
172 json(conn, %{error: msg})
176 def change_email(%{assigns: %{user: user}} = conn, params) do
177 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
179 with {:ok, _user} <- User.change_email(user, params["email"]) do
180 json(conn, %{status: "success"})
182 {:error, changeset} ->
183 {_, {error, _}} = Enum.at(changeset.errors, 0)
184 json(conn, %{error: "Email #{error}."})
187 json(conn, %{error: "Unable to change email."})
191 json(conn, %{error: msg})
195 def delete_account(%{assigns: %{user: user}} = conn, params) do
196 password = params["password"] || ""
198 case CommonAPI.Utils.confirm_current_password(user, password) do
201 json(conn, %{status: "success"})
204 json(conn, %{error: msg})
208 def disable_account(%{assigns: %{user: user}} = conn, params) do
209 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
211 User.deactivate_async(user)
212 json(conn, %{status: "success"})
215 json(conn, %{error: msg})
219 def captcha(conn, _params) do
220 json(conn, Pleroma.Captcha.new())
223 def healthcheck(conn, _params) do
224 with true <- Config.get([:instance, :healthcheck]),
225 %{healthy: true} = info <- Healthcheck.system_info() do
228 %{healthy: false} = info ->
229 service_unavailable(conn, info)
232 service_unavailable(conn, %{})
236 defp service_unavailable(conn, info) do
238 |> put_status(:service_unavailable)