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
17 alias Pleroma.Web.CommonAPI
18 alias Pleroma.Web.WebFinger
22 %{scopes: ["follow", "write:follows"]}
23 when action == :follow_import
26 # Note: follower can submit the form (with password auth) not being signed in (having no token)
29 %{fallback: :proceed_unauthenticated, scopes: ["follow", "write:follows"]}
30 when action == :do_remote_follow
33 plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks_import)
37 %{scopes: ["write:accounts"]}
42 :update_notificaton_settings,
47 plug(OAuthScopesPlug, %{scopes: ["write:notifications"]} when action == :notifications_read)
49 plug(Pleroma.Plugs.SetFormatPlug when action in [:config, :version])
51 def help_test(conn, _params) do
55 def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
56 with %User{} = user <- User.get_cached_by_nickname(nick),
57 avatar = User.avatar_url(user) do
59 |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
62 render(conn, "subscribe.html", %{
65 error: "Could not find user"
70 def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
71 with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
72 %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
74 |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
77 render(conn, "subscribe.html", %{
80 error: "Something went wrong."
85 def notifications_read(%{assigns: %{user: user}} = conn, %{"id" => notification_id}) do
86 with {:ok, _} <- Notification.read_one(user, notification_id) do
87 json(conn, %{status: "success"})
91 |> put_resp_content_type("application/json")
92 |> send_resp(403, Jason.encode!(%{"error" => message}))
96 def config(%{assigns: %{format: "xml"}} = conn, _params) do
97 instance = Pleroma.Config.get(:instance)
102 <name>#{Keyword.get(instance, :name)}</name>
103 <site>#{Web.base_url()}</site>
104 <textlimit>#{Keyword.get(instance, :limit)}</textlimit>
105 <closed>#{!Keyword.get(instance, :registrations_open)}</closed>
111 |> put_resp_content_type("application/xml")
112 |> send_resp(200, response)
115 def config(conn, _params) do
116 instance = Pleroma.Config.get(:instance)
118 vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
121 uploadlimit: to_string(Keyword.get(instance, :upload_limit)),
122 avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)),
123 backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)),
124 bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit))
128 name: Keyword.get(instance, :name),
129 description: Keyword.get(instance, :description),
130 server: Web.base_url(),
131 textlimit: to_string(Keyword.get(instance, :limit)),
132 uploadlimit: uploadlimit,
133 closed: bool_to_val(Keyword.get(instance, :registrations_open), "0", "1"),
134 private: bool_to_val(Keyword.get(instance, :public, true), "0", "1"),
135 vapidPublicKey: vapid_public_key,
136 accountActivationRequired:
137 bool_to_val(Keyword.get(instance, :account_activation_required, false)),
138 invitesEnabled: bool_to_val(Keyword.get(instance, :invites_enabled, false)),
139 safeDMMentionsEnabled: bool_to_val(Pleroma.Config.get([:instance, :safe_dm_mentions]))
142 managed_config = Keyword.get(instance, :managed_config)
146 pleroma_fe = Pleroma.Config.get([:frontend_configurations, :pleroma_fe])
147 Map.put(data, "pleromafe", pleroma_fe)
152 json(conn, %{site: data})
155 defp bool_to_val(true), do: "1"
156 defp bool_to_val(_), do: "0"
157 defp bool_to_val(true, val, _), do: val
158 defp bool_to_val(_, _, val), do: val
160 def frontend_configurations(conn, _params) do
162 Pleroma.Config.get(:frontend_configurations, %{})
168 def version(%{assigns: %{format: "xml"}} = conn, _params) do
169 version = Pleroma.Application.named_version()
172 |> put_resp_content_type("application/xml")
173 |> send_resp(200, "<version>#{version}</version>")
176 def version(conn, _params) do
177 json(conn, Pleroma.Application.named_version())
180 def emoji(conn, _params) do
182 Enum.reduce(Emoji.get_all(), %{}, fn {code, %Emoji{file: file, tags: tags}}, acc ->
183 Map.put(acc, code, %{image_url: file, tags: tags})
189 def update_notificaton_settings(%{assigns: %{user: user}} = conn, params) do
190 with {:ok, _} <- User.update_notification_settings(user, params) do
191 json(conn, %{status: "success"})
195 def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
196 follow_import(conn, %{"list" => File.read!(listfile.path)})
199 def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
200 with lines <- String.split(list, "\n"),
201 followed_identifiers <-
202 Enum.map(lines, fn line ->
203 String.split(line, ",") |> List.first()
205 |> List.delete("Account address") do
206 User.follow_import(follower, followed_identifiers)
207 json(conn, "job started")
211 def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
212 blocks_import(conn, %{"list" => File.read!(listfile.path)})
215 def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
216 with blocked_identifiers <- String.split(list) do
217 User.blocks_import(blocker, blocked_identifiers)
218 json(conn, "job started")
222 def change_password(%{assigns: %{user: user}} = conn, params) do
223 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
226 User.reset_password(user, %{
227 password: params["new_password"],
228 password_confirmation: params["new_password_confirmation"]
230 json(conn, %{status: "success"})
232 {:error, changeset} ->
233 {_, {error, _}} = Enum.at(changeset.errors, 0)
234 json(conn, %{error: "New password #{error}."})
237 json(conn, %{error: "Unable to change password."})
241 json(conn, %{error: msg})
245 def change_email(%{assigns: %{user: user}} = conn, params) do
246 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
248 with {:ok, _user} <- User.change_email(user, params["email"]) do
249 json(conn, %{status: "success"})
251 {:error, changeset} ->
252 {_, {error, _}} = Enum.at(changeset.errors, 0)
253 json(conn, %{error: "Email #{error}."})
256 json(conn, %{error: "Unable to change email."})
260 json(conn, %{error: msg})
264 def delete_account(%{assigns: %{user: user}} = conn, params) do
265 password = params["password"] || ""
267 case CommonAPI.Utils.confirm_current_password(user, password) do
270 json(conn, %{status: "success"})
273 json(conn, %{error: msg})
277 def disable_account(%{assigns: %{user: user}} = conn, params) do
278 case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
280 User.deactivate_async(user)
281 json(conn, %{status: "success"})
284 json(conn, %{error: msg})
288 def captcha(conn, _params) do
289 json(conn, Pleroma.Captcha.new())
292 def healthcheck(conn, _params) do
293 with true <- Config.get([:instance, :healthcheck]),
294 %{healthy: true} = info <- Healthcheck.system_info() do
297 %{healthy: false} = info ->
298 service_unavailable(conn, info)
301 service_unavailable(conn, %{})
305 defp service_unavailable(conn, info) do
307 |> put_status(:service_unavailable)